This commit is contained in:
hagiye
2026-06-25 11:00:40 +03:00
parent 503878110f
commit f4a90755c2
42 changed files with 2100 additions and 98 deletions

View File

@@ -1,5 +1,5 @@
import { BadRequestException, Injectable, NotFoundException } from '@nestjs/common';
import { FindOptionsWhere } from 'typeorm';
import { DataSource, FindOptionsWhere } from 'typeorm';
import { BookingsRepository } from '../bookings/bookings.repository';
import { CreateLastMileDto } from './dto/create-last-mile.dto';
@@ -29,6 +29,7 @@ export class LastMileService {
constructor(
private readonly lastMileRepository: LastMileRepository,
private readonly bookingsRepository: BookingsRepository,
private readonly dataSource: DataSource,
) {}
async acceptBooking(bookingReference: string): Promise<LastMile> {
@@ -44,6 +45,41 @@ export class LastMileService {
);
}
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 this.create({
bookingId: booking.id,
advancedPayment: booking.totalAmount,