mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-09-08 19:28:17 +00:00
DJbouti port Export Unloading
This commit is contained in:
@@ -37,6 +37,36 @@ export interface ImportTrainItemRow {
|
||||
lastMileRequested: boolean;
|
||||
pickupOption: string;
|
||||
}
|
||||
|
||||
export interface ExportDjiboutiQueueFilter {
|
||||
scheduleId?: string;
|
||||
destination?: string;
|
||||
status?: string;
|
||||
dateFrom?: string;
|
||||
dateTo?: string;
|
||||
}
|
||||
|
||||
export interface ExportTrainRow extends ImportTrainRow {
|
||||
departureTime: string | null;
|
||||
}
|
||||
|
||||
export interface ExportTrainItemRow {
|
||||
bookingId: string;
|
||||
bookingReference: string | null;
|
||||
customerId: string | null;
|
||||
customerName: string | null;
|
||||
itemType: 'CONTAINER' | 'CARGO';
|
||||
itemId: string | null;
|
||||
inventoryId: string | null;
|
||||
containerNumber: string | null;
|
||||
cargoType: string | null;
|
||||
weight: number | null;
|
||||
origin: string | null;
|
||||
destination: string | null;
|
||||
trainSchedule: string | null;
|
||||
arrivalTime: string | null;
|
||||
currentStatus: string | null;
|
||||
}
|
||||
export interface WagonView {
|
||||
id: string;
|
||||
wagonNumber: string;
|
||||
@@ -67,6 +97,13 @@ export interface BookingScheduleView {
|
||||
export class SchedulingReadFacade {
|
||||
constructor(private readonly dataSource: DataSource) {}
|
||||
|
||||
private isDjiboutiPortDestination(value: string | null | undefined): boolean {
|
||||
const normalized = (value ?? '').toUpperCase();
|
||||
return ['DJIBOUTI', 'DORALEH', 'DMP', 'DCT', 'NAGAD'].some((token) =>
|
||||
normalized.includes(token),
|
||||
);
|
||||
}
|
||||
|
||||
/** Look up a single physical wagon. Returns null if it does not exist. */
|
||||
async findWagon(wagonId: string): Promise<WagonView | null> {
|
||||
const rows = await this.dataSource.query(
|
||||
@@ -220,4 +257,187 @@ export class SchedulingReadFacade {
|
||||
);
|
||||
return rows;
|
||||
}
|
||||
|
||||
/**
|
||||
* ARRIVED export train schedules at Djibouti-side destinations, with assigned item counts.
|
||||
* Read-only: this only selects from scheduling/booking/inventory tables.
|
||||
*/
|
||||
async exportDjiboutiArrivalQueue(
|
||||
filter: ExportDjiboutiQueueFilter = {},
|
||||
): Promise<ExportTrainRow[]> {
|
||||
const params: unknown[] = [];
|
||||
const where = [
|
||||
'ts.deleted_at IS NULL',
|
||||
"ts.status = ANY($1)",
|
||||
`EXISTS (
|
||||
SELECT 1
|
||||
FROM freight.train_schedule_bookings tsb_exists
|
||||
JOIN freight.bookings b_exists ON b_exists.id = tsb_exists.booking_id AND b_exists.deleted_at IS NULL
|
||||
LEFT JOIN freight.warehouse_inventory inv_exists ON inv_exists.booking_id = b_exists.id AND inv_exists.deleted_at IS NULL
|
||||
WHERE tsb_exists.train_schedule_id = ts.id
|
||||
AND tsb_exists.deleted_at IS NULL
|
||||
AND (inv_exists.status = ANY($2) OR b_exists.status = ANY($2))
|
||||
)`,
|
||||
];
|
||||
params.push(
|
||||
filter.status ? [filter.status] : ['ARRIVED', 'ARRIVED_AT_DJIBOUTI'],
|
||||
['DISPATCHED', 'IN_TRANSIT', 'ARRIVED_AT_DJIBOUTI', 'ARRIVED_AT_PORT', 'ARRIVED_AT_DESTINATION'],
|
||||
);
|
||||
|
||||
if (filter.scheduleId) {
|
||||
params.push(filter.scheduleId);
|
||||
where.push(`ts.id = $${params.length}`);
|
||||
}
|
||||
if (filter.destination) {
|
||||
params.push(`%${filter.destination}%`);
|
||||
where.push(`(dy.code ILIKE $${params.length} OR dy.name ILIKE $${params.length})`);
|
||||
}
|
||||
if (filter.dateFrom) {
|
||||
params.push(filter.dateFrom);
|
||||
where.push(`COALESCE(ts.actual_arrival_at, ts.scheduled_arrival_date) >= $${params.length}`);
|
||||
}
|
||||
if (filter.dateTo) {
|
||||
params.push(filter.dateTo);
|
||||
where.push(`COALESCE(ts.actual_arrival_at, ts.scheduled_arrival_date) <= $${params.length}`);
|
||||
}
|
||||
|
||||
const rows: Array<
|
||||
ExportTrainRow & {
|
||||
originCountry: string | null;
|
||||
destinationCountry: string | null;
|
||||
destinationName: string | null;
|
||||
}
|
||||
> = await this.dataSource.query(
|
||||
`SELECT ts.id AS "scheduleId",
|
||||
ts.train_number AS "trainNumber",
|
||||
oy.code AS "origin",
|
||||
dy.code AS "destination",
|
||||
dy.name AS "destinationName",
|
||||
oy.country AS "originCountry",
|
||||
dy.country AS "destinationCountry",
|
||||
ts.scheduled_departure_date AS "departureTime",
|
||||
COALESCE(ts.actual_arrival_at, ts.scheduled_arrival_date) AS "arrivalTime",
|
||||
ts.status,
|
||||
(SELECT count(*) FROM freight.train_schedule_bookings tsb
|
||||
WHERE tsb.train_schedule_id = ts.id AND tsb.deleted_at IS NULL) AS "totalBookings",
|
||||
(SELECT count(*) FROM freight.containers c
|
||||
JOIN freight.train_schedule_bookings tsbc ON tsbc.booking_id = c.booking_id AND tsbc.deleted_at IS NULL
|
||||
WHERE tsbc.train_schedule_id = ts.id AND c.deleted_at IS NULL) AS "totalContainers",
|
||||
(SELECT count(*) FROM freight.cargoes cg
|
||||
JOIN freight.train_schedule_bookings tsbg ON tsbg.booking_id = cg.booking_id AND tsbg.deleted_at IS NULL
|
||||
WHERE tsbg.train_schedule_id = ts.id AND cg.deleted_at IS NULL) AS "totalCargoes"
|
||||
FROM freight.train_schedules ts
|
||||
LEFT JOIN freight.yards oy ON oy.id = ts.origin_station_id
|
||||
LEFT JOIN freight.yards dy ON dy.id = ts.destination_station_id
|
||||
WHERE ${where.join(' AND ')}
|
||||
ORDER BY COALESCE(ts.actual_arrival_at, ts.scheduled_arrival_date) DESC NULLS LAST`,
|
||||
params,
|
||||
);
|
||||
|
||||
return rows
|
||||
.filter((r) => {
|
||||
const direction = deriveTradeDirection(
|
||||
{ country: r.originCountry },
|
||||
{ country: r.destinationCountry },
|
||||
);
|
||||
return (
|
||||
direction === 'EXPORT' &&
|
||||
this.isDjiboutiPortDestination(`${r.destination ?? ''} ${r.destinationName ?? ''}`)
|
||||
);
|
||||
})
|
||||
.map(({ originCountry: _oc, destinationCountry: _dc, destinationName: _dn, ...rest }) => ({
|
||||
...rest,
|
||||
totalBookings: Number(rest.totalBookings) || 0,
|
||||
totalContainers: Number(rest.totalContainers) || 0,
|
||||
totalCargoes: Number(rest.totalCargoes) || 0,
|
||||
route: rest.origin || rest.destination ? `${rest.origin ?? '?'} -> ${rest.destination ?? '?'}` : null,
|
||||
}));
|
||||
}
|
||||
|
||||
/** Assigned export booking items for an arrived Djibouti-side export train. Read-only. */
|
||||
async exportDjiboutiTrainDetail(scheduleId: string): Promise<ExportTrainItemRow[]> {
|
||||
const rows: ExportTrainItemRow[] = await this.dataSource.query(
|
||||
`WITH assigned AS (
|
||||
SELECT b.id AS booking_id,
|
||||
b.reference,
|
||||
b.company_id,
|
||||
company.name AS customer_name,
|
||||
COALESCE(cgt.cargo_type_name, b.cargo_free_text) AS cargo_type,
|
||||
b.cargo_total_weight_vgm AS booking_weight,
|
||||
oy.code AS origin,
|
||||
dy.code AS destination,
|
||||
ts.train_number,
|
||||
COALESCE(ts.actual_arrival_at, ts.scheduled_arrival_date) AS arrival_time,
|
||||
inv.id AS inventory_id,
|
||||
COALESCE(inv.status, b.status) AS current_status
|
||||
FROM freight.train_schedule_bookings tsb
|
||||
JOIN freight.train_schedules ts ON ts.id = tsb.train_schedule_id
|
||||
JOIN freight.bookings b ON b.id = tsb.booking_id AND b.deleted_at IS NULL
|
||||
LEFT JOIN freight.companies company ON company.id = b.company_id
|
||||
LEFT JOIN freight.cargo_types cgt ON cgt.id = b.cargo_type_id
|
||||
LEFT JOIN freight.yards oy ON oy.id = b.origin_yard_id
|
||||
LEFT JOIN freight.yards dy ON dy.id = b.destination_yard_id
|
||||
LEFT JOIN freight.warehouse_inventory inv ON inv.booking_id = b.id AND inv.deleted_at IS NULL
|
||||
WHERE tsb.train_schedule_id = $1 AND tsb.deleted_at IS NULL
|
||||
)
|
||||
SELECT a.booking_id AS "bookingId",
|
||||
a.reference AS "bookingReference",
|
||||
a.company_id AS "customerId",
|
||||
a.customer_name AS "customerName",
|
||||
'CONTAINER' AS "itemType",
|
||||
c.id AS "itemId",
|
||||
a.inventory_id AS "inventoryId",
|
||||
c.container_number AS "containerNumber",
|
||||
a.cargo_type AS "cargoType",
|
||||
a.booking_weight AS "weight",
|
||||
a.origin,
|
||||
a.destination,
|
||||
a.train_number AS "trainSchedule",
|
||||
a.arrival_time AS "arrivalTime",
|
||||
a.current_status AS "currentStatus"
|
||||
FROM assigned a
|
||||
JOIN freight.containers c ON c.booking_id = a.booking_id AND c.deleted_at IS NULL
|
||||
UNION ALL
|
||||
SELECT a.booking_id AS "bookingId",
|
||||
a.reference AS "bookingReference",
|
||||
a.company_id AS "customerId",
|
||||
a.customer_name AS "customerName",
|
||||
'CARGO' AS "itemType",
|
||||
cg.id AS "itemId",
|
||||
a.inventory_id AS "inventoryId",
|
||||
NULL AS "containerNumber",
|
||||
COALESCE(cgt.cargo_type_name, a.cargo_type) AS "cargoType",
|
||||
COALESCE(cg.weight, a.booking_weight) AS "weight",
|
||||
a.origin,
|
||||
a.destination,
|
||||
a.train_number AS "trainSchedule",
|
||||
a.arrival_time AS "arrivalTime",
|
||||
a.current_status AS "currentStatus"
|
||||
FROM assigned a
|
||||
JOIN freight.cargoes cg ON cg.booking_id = a.booking_id AND cg.deleted_at IS NULL
|
||||
LEFT JOIN freight.cargo_types cgt ON cgt.id = cg.cargo_type_id
|
||||
UNION ALL
|
||||
SELECT a.booking_id AS "bookingId",
|
||||
a.reference AS "bookingReference",
|
||||
a.company_id AS "customerId",
|
||||
a.customer_name AS "customerName",
|
||||
CASE WHEN a.cargo_type ILIKE '%container%' THEN 'CONTAINER' ELSE 'CARGO' END AS "itemType",
|
||||
a.inventory_id AS "itemId",
|
||||
a.inventory_id AS "inventoryId",
|
||||
NULL AS "containerNumber",
|
||||
a.cargo_type AS "cargoType",
|
||||
a.booking_weight AS "weight",
|
||||
a.origin,
|
||||
a.destination,
|
||||
a.train_number AS "trainSchedule",
|
||||
a.arrival_time AS "arrivalTime",
|
||||
a.current_status AS "currentStatus"
|
||||
FROM assigned a
|
||||
WHERE NOT EXISTS (SELECT 1 FROM freight.containers c WHERE c.booking_id = a.booking_id AND c.deleted_at IS NULL)
|
||||
AND NOT EXISTS (SELECT 1 FROM freight.cargoes cg WHERE cg.booking_id = a.booking_id AND cg.deleted_at IS NULL)
|
||||
ORDER BY "bookingReference" ASC NULLS LAST, "itemType" ASC`,
|
||||
[scheduleId],
|
||||
);
|
||||
return rows;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user