From 925754067b7f30b02e15cf9109fd5c3735423cc4 Mon Sep 17 00:00:00 2001 From: yaschalew Date: Thu, 25 Jun 2026 09:40:52 +0300 Subject: [PATCH] last mile --- .../first-mile/first-mile.controller.ts | 2 +- .../modules/first-mile/first-mile.service.ts | 20 ++++++++++++++++-- .../modules/last-mile/last-mile.controller.ts | 2 +- .../modules/last-mile/last-mile.service.ts | 21 ++++++++++++++++++- 4 files changed, 40 insertions(+), 5 deletions(-) diff --git a/apps/edr-freight-api/src/modules/first-mile/first-mile.controller.ts b/apps/edr-freight-api/src/modules/first-mile/first-mile.controller.ts index 161604e81..78c3d43ff 100644 --- a/apps/edr-freight-api/src/modules/first-mile/first-mile.controller.ts +++ b/apps/edr-freight-api/src/modules/first-mile/first-mile.controller.ts @@ -59,7 +59,7 @@ export class FirstMileController { @TrainSchedulingManage() @ApiOperation({ summary: 'Accept a paid booking and create a first-mile leg' }) acceptBooking(@Param('reference') reference: string) { - return this.firstMileService.acceptBooking(reference); + return this.firstMileService.acceptBookingByReference(reference); } @Post() diff --git a/apps/edr-freight-api/src/modules/first-mile/first-mile.service.ts b/apps/edr-freight-api/src/modules/first-mile/first-mile.service.ts index cb35f9a89..a00e54fff 100644 --- a/apps/edr-freight-api/src/modules/first-mile/first-mile.service.ts +++ b/apps/edr-freight-api/src/modules/first-mile/first-mile.service.ts @@ -44,8 +44,8 @@ export class FirstMileService { * paid before any first-mile work proceeds. Throws if the reference is * unknown or the booking has not reached PAID status. */ - async acceptBooking(bookingReference: string): Promise { - const booking = await this.bookingsRepository.findByReference(bookingReference); + async acceptBooking(bookingId: string): Promise { + const booking = await this.bookingsRepository.findById(bookingId); if (!booking) { return null; @@ -61,6 +61,22 @@ export class FirstMileService { }); } + async acceptBookingByReference(bookingReference: string): Promise { + const booking = await this.bookingsRepository.findByReference(bookingReference); + + if (!booking) { + return null; + } + + if (booking.paymentStatus !== 'PAID') { + return null; + } + + return this.create({ + bookingId: booking.id, + advancedPayment: 0, + }); + } async findAll(filter: FirstMileListFilter = {}): Promise<{ data: FirstMile[]; meta: { total: number; page: number; pageSize: number; totalPages: number }; diff --git a/apps/edr-freight-api/src/modules/last-mile/last-mile.controller.ts b/apps/edr-freight-api/src/modules/last-mile/last-mile.controller.ts index ea1e29a3d..e8abf52c6 100644 --- a/apps/edr-freight-api/src/modules/last-mile/last-mile.controller.ts +++ b/apps/edr-freight-api/src/modules/last-mile/last-mile.controller.ts @@ -59,7 +59,7 @@ export class LastMileController { @TrainSchedulingManage() @ApiOperation({ summary: 'Accept a paid booking and create a last-mile leg' }) acceptBooking(@Param('reference') reference: string) { - return this.lastMileService.acceptBooking(reference); + return this.lastMileService.acceptBookingByReference(reference); } @Post() diff --git a/apps/edr-freight-api/src/modules/last-mile/last-mile.service.ts b/apps/edr-freight-api/src/modules/last-mile/last-mile.service.ts index 1dc0ce48d..b764d3428 100644 --- a/apps/edr-freight-api/src/modules/last-mile/last-mile.service.ts +++ b/apps/edr-freight-api/src/modules/last-mile/last-mile.service.ts @@ -54,7 +54,26 @@ export class LastMileService { return this.create({ bookingId: booking.id, - advancedPayment: booking.totalAmount, + advancedPayment: 0, + }); + } + + async acceptBookingByReference(bookingReference: string): Promise { + const booking = await this.bookingsRepository.findByReference(bookingReference); + + if (!booking) { + throw new NotFoundException(`Booking ${bookingReference} not found`); + } + + if (booking.paymentStatus !== 'PAID') { + throw new BadRequestException( + `Booking ${bookingReference} is not paid (payment status: ${booking.paymentStatus})`, + ); + } + + return this.create({ + bookingId: booking.id, + advancedPayment: 0, }); }