import { Controller, Get, Post, Put, Delete, Body, Param, Query, UseGuards, Request, } from '@nestjs/common'; import { DeliveryService } from './delivery.service'; import { DeliveryStatus } from './entities/delivery.entity'; import { CreateDeliveryDto, CreateDeliveryZoneDto, UpdateDeliveryZoneDto, AssignDeliveryDto, UpdateDeliveryStatusDto, ProofOfDeliveryDto, CheckCoverageDto, CheckCoverageByAddressDto, } from './dto/delivery.dto'; import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard'; @Controller('delivery') @UseGuards(JwtAuthGuard) export class DeliveryController { constructor(private readonly deliveryService: DeliveryService) {} // ============================================ // Zone Endpoints // ============================================ @Post('zones') async createZone(@Request() req: any, @Body() dto: CreateDeliveryZoneDto) { return this.deliveryService.createZone(req.tenantId, dto); } @Get('zones') async getZones(@Request() req: any, @Query('active') active?: string) { const activeOnly = active === 'true'; return this.deliveryService.getZones(req.tenantId, activeOnly); } @Get('zones/:id') async getZone(@Request() req: any, @Param('id') id: string) { return this.deliveryService.getZoneById(req.tenantId, id); } @Put('zones/:id') async updateZone(@Request() req: any, @Param('id') id: string, @Body() dto: UpdateDeliveryZoneDto) { return this.deliveryService.updateZone(req.tenantId, id, dto); } @Delete('zones/:id') async deleteZone(@Request() req: any, @Param('id') id: string) { await this.deliveryService.deleteZone(req.tenantId, id); return { success: true }; } // ============================================ // Coverage Check Endpoints // ============================================ @Post('check-coverage') async checkCoverage(@Request() req: any, @Body() dto: CheckCoverageDto) { return this.deliveryService.checkCoverage(req.tenantId, dto); } @Post('check-coverage/address') async checkCoverageByAddress(@Request() req: any, @Body() dto: CheckCoverageByAddressDto) { return this.deliveryService.checkCoverageByAddress(req.tenantId, dto); } // ============================================ // Delivery Endpoints // ============================================ @Post() async createDelivery(@Request() req: any, @Body() dto: CreateDeliveryDto) { return this.deliveryService.createDelivery(req.tenantId, dto); } @Get() async getDeliveries( @Request() req: any, @Query('status') status?: DeliveryStatus, @Query('assignedTo') assignedTo?: string, ) { return this.deliveryService.getDeliveries(req.tenantId, { status, assignedTo }); } @Get('pending') async getPendingDeliveries(@Request() req: any) { return this.deliveryService.getPendingDeliveries(req.tenantId); } @Get('stats') async getStats(@Request() req: any, @Query('date') date?: string) { const dateObj = date ? new Date(date) : undefined; return this.deliveryService.getStats(req.tenantId, dateObj); } @Get('driver/:driverId') async getDriverDeliveries(@Request() req: any, @Param('driverId') driverId: string) { return this.deliveryService.getDriverDeliveries(req.tenantId, driverId); } @Get('order/:orderId') async getDeliveryByOrder(@Request() req: any, @Param('orderId') orderId: string) { return this.deliveryService.getDeliveryByOrderId(req.tenantId, orderId); } @Get(':id') async getDelivery(@Request() req: any, @Param('id') id: string) { return this.deliveryService.getDeliveryById(req.tenantId, id); } @Put(':id/assign') async assignDelivery(@Request() req: any, @Param('id') id: string, @Body() dto: AssignDeliveryDto) { return this.deliveryService.assignDelivery(req.tenantId, id, dto); } @Put(':id/status') async updateStatus(@Request() req: any, @Param('id') id: string, @Body() dto: UpdateDeliveryStatusDto) { return this.deliveryService.updateStatus(req.tenantId, id, dto); } @Post(':id/proof') async addProofOfDelivery(@Request() req: any, @Param('id') id: string, @Body() dto: ProofOfDeliveryDto) { return this.deliveryService.addProofOfDelivery(req.tenantId, id, dto); } }