mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-08-26 18:42:49 +00:00
api
This commit is contained in:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user