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 a00e54fff..ba06604ce 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 @@ -133,7 +133,7 @@ export class FirstMileService { async create(dto: CreateFirstMileDto): Promise { return this.firstMileRepository.create({ bookingId: dto.bookingId, - status: dto.status ?? 'PAYMENT_PENDING', + status: dto.status ?? 'READY_TO_TRANSIT', advancedPayment: dto.advancedPayment ?? 0, remainingPayment: dto.remainingPayment ?? 0, estimatedKm: dto.estimatedKm ?? null, 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 55d5d2965..22b96a344 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 @@ -1,4 +1,4 @@ -import { BadRequestException, Injectable, Logger, NotFoundException } from '@nestjs/common'; +import { Injectable, Logger, NotFoundException } from '@nestjs/common'; import { FindOptionsWhere } from 'typeorm'; import { BookingsRepository } from '../bookings/bookings.repository'; @@ -39,52 +39,15 @@ export class LastMileService { private readonly notificationsService: NotificationsService, ) {} - async acceptBooking(bookingReference: string): Promise { + async acceptBooking(bookingReference: string): Promise { const booking = await this.bookingsRepository.findByReference(bookingReference); if (!booking) { - throw new NotFoundException(`Booking ${bookingReference} not found`); + return null; } if (booking.paymentStatus !== 'PAID') { - throw new BadRequestException( - `Booking ${bookingReference} is not paid (payment status: ${booking.paymentStatus})`, - ); - } - - if ((booking.tradeDirection ?? '').toUpperCase() !== 'IMPORT') { - throw new BadRequestException(`Booking ${bookingReference} is not an import booking`); - } - - if (!booking.lastMileDeliveryAddress?.trim()) { - throw new BadRequestException(`Booking ${bookingReference} has no last-mile delivery address`); - } - - const [eligibleInventory] = await this.dataSource.query( - `SELECT inv.id - FROM freight.warehouse_inventory inv - WHERE inv.booking_id = $1 - AND inv.deleted_at IS NULL - AND inv.status = 'READY_FOR_PICKUP' - AND inv.inspection_status = 'PASSED' - LIMIT 1`, - [booking.id], - ); - if (!eligibleInventory) { - throw new BadRequestException( - `Booking ${bookingReference} is not eligible for last mile. Import inventory must pass inspection and be READY_FOR_PICKUP.`, - ); - } - - const [existing] = await this.dataSource.query( - `SELECT id - FROM freight.last_mile - WHERE booking_id = $1 AND deleted_at IS NULL - LIMIT 1`, - [booking.id], - ); - if (existing) { - return this.findById(existing.id); + return null; } return this.create({ @@ -93,17 +56,15 @@ export class LastMileService { }); } - async acceptBookingByReference(bookingReference: string): Promise { + async acceptBookingByReference(bookingReference: string): Promise { const booking = await this.bookingsRepository.findByReference(bookingReference); if (!booking) { - throw new NotFoundException(`Booking ${bookingReference} not found`); + return null; } if (booking.paymentStatus !== 'PAID') { - throw new BadRequestException( - `Booking ${bookingReference} is not paid (payment status: ${booking.paymentStatus})`, - ); + return null; } return this.create({ @@ -168,7 +129,7 @@ export class LastMileService { async create(dto: CreateLastMileDto): Promise { return this.lastMileRepository.create({ bookingId: dto.bookingId, - status: dto.status ?? 'PAYMENT_PENDING', + status: dto.status ?? 'READY_TO_TRANSIT', advancedPayment: dto.advancedPayment ?? 0, remainingPayment: dto.remainingPayment ?? 0, estimatedKm: dto.estimatedKm ?? null,