mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-09-09 01:18:18 +00:00
Truck assignment for Export before loading and import after arrival
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { BadRequestException, Injectable, Logger, NotFoundException } from '@nestjs/common';
|
||||
import { Cron, CronExpression } from '@nestjs/schedule';
|
||||
import { Between, DataSource, EntityManager, FindManyOptions, ILike, LessThanOrEqual, MoreThanOrEqual } from 'typeorm';
|
||||
|
||||
import { deriveTradeDirection } from '../../common/derive-trade-direction.util';
|
||||
@@ -396,6 +397,54 @@ export class WarehouseInventoryService {
|
||||
* but has no customer truck assigned yet, nudge the customer to assign one — with
|
||||
* a deep-link to the booking's truck-assignment card. Fire-and-forget.
|
||||
*/
|
||||
/**
|
||||
* Recurring nudge: keep reminding self-haul IMPORT customers to assign a
|
||||
* collection truck while their goods are still in the warehouse
|
||||
* (READY_FOR_PICKUP) and no truck has been assigned yet. Stops once a truck is
|
||||
* assigned (customer_truck_assigned_at set) or the goods leave (DELIVERED).
|
||||
*/
|
||||
@Cron(CronExpression.EVERY_30_MINUTES, { name: 'import-truck-assignment-reminder' })
|
||||
async remindImportTruckAssignment(): Promise<void> {
|
||||
try {
|
||||
const rows: Array<{
|
||||
bookingId: string;
|
||||
companyId: string | null;
|
||||
reference: string | null;
|
||||
}> = await this.dataSource.query(
|
||||
`SELECT DISTINCT b.id AS "bookingId",
|
||||
b.company_id AS "companyId",
|
||||
b.reference
|
||||
FROM freight.warehouse_inventory inv
|
||||
JOIN freight.bookings b ON b.id = inv.booking_id AND b.deleted_at IS NULL
|
||||
WHERE inv.deleted_at IS NULL
|
||||
AND inv.status = 'READY_FOR_PICKUP'
|
||||
AND b.trade_direction = 'IMPORT'
|
||||
AND b.customer_truck_assigned_at IS NULL
|
||||
AND COALESCE(NULLIF(TRIM(b.last_mile_delivery_address), ''), '') = ''`,
|
||||
);
|
||||
if (!rows.length) return;
|
||||
this.logger.log(
|
||||
`Import truck-assignment reminder: ${rows.length} booking(s) awaiting a collection truck`,
|
||||
);
|
||||
for (const row of rows) {
|
||||
await this.notifyTruckAssignmentNeeded(
|
||||
{
|
||||
companyId: row.companyId,
|
||||
reference: row.reference,
|
||||
hasFirstMile: false,
|
||||
hasLastMile: false,
|
||||
customerTruckAssignedAt: null,
|
||||
},
|
||||
row.bookingId,
|
||||
);
|
||||
}
|
||||
} catch (err) {
|
||||
this.logger.warn(
|
||||
`Import truck-assignment reminder tick failed: ${(err as Error).message}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private async notifyTruckAssignmentNeeded(booking: {
|
||||
companyId?: string | null;
|
||||
reference?: string | null;
|
||||
|
||||
Reference in New Issue
Block a user