feat(import-operations): bulk Excel upload for yard-resident empty containers

Empties already sitting in an EDR yard but never entered in the system had
to be typed one at a time. Adds a bulk path: parse the sheet in the browser
(all-or-nothing, row-numbered errors), preview it, then POST one batch.

The server rejects the batch if any container already has a non-COMPLETED
return, so re-uploading the same sheet cannot duplicate boxes. No interchange
notification fires — these are historical rows, not a live handover.

Company is an Autocomplete over registered customers that also accepts a
typed name, since a backfilled box may belong to a company that is not a
customer yet. Exact name match sets customer_id; the name always lands in the
new empty_container_returns.company_name.

Also fixes the single Record Return modal, which collected Yard and Zone and
then dropped them before the API call, and did not invalidate the returns
list after a standalone return.
This commit is contained in:
Hagernesh
2026-08-28 11:31:50 +00:00
parent 132374ed03
commit 3f4fd8648a
16 changed files with 1075 additions and 44 deletions

View File

@@ -3617,7 +3617,83 @@ export class TrainSchedulingService {
return { wagons, unassignedBookings };
}
async intercityMarshallingDocument(scheduleId: string): Promise<{ filename: string; buffer: Buffer }> {
/**
* Every corridor stop where the consist actually changed for this schedule
* (coupled, uncoupled, or switched — any flavor), in the order the train
* reached them. Origin is never in this list — it's always its own doc (the
* plain import/export load list), so numbering here starts at 2. A stop with
* only a routine checkpoint and no consist change never gets a row, which is
* the point: "Marshalling 2, 3, 4…" tracks events, not raw stop count.
*/
async marshallingStops(
scheduleId: string,
): Promise<Array<{ stopIndex: number; yardId: string; yardLabel: string; firstOccurredAt: string }>> {
const rows = await this.dataSource.getRepository(ScheduleWagonAdjustmentLog).find({
where: { trainScheduleId: scheduleId },
order: { occurredAt: 'ASC' },
});
const firstSeenAt = new Map<string, Date>();
for (const row of rows) {
if (!row.yardId || firstSeenAt.has(row.yardId)) continue;
firstSeenAt.set(row.yardId, row.occurredAt);
}
const orderedYardIds = [...firstSeenAt.entries()]
.sort((a, b) => a[1].getTime() - b[1].getTime())
.map(([yardId]) => yardId);
const labels = await this.yardLabelsById(orderedYardIds);
return orderedYardIds.map((yardId, i) => ({
stopIndex: i + 2,
yardId,
yardLabel: labels.get(yardId) ?? yardId,
firstOccurredAt: firstSeenAt.get(yardId)!.toISOString(),
}));
}
/**
* The coupled/uncoupled/switched rows for one stop, in the locked table
* shape (wagon, event, containers). "EMPTY WAGON" replaces the container
* list rather than a blank cell — the column always exists so a loaded and
* an empty coupling read as the same table, not two different layouts.
* Cargo for ADD/REMOVE rows is read off the schedule's OWN slot allocations
* for that physical wagon: an ADD is a leg slot boarding already loaded (see
* stampSlotLegs) or an empty couple (plannedWagonCouples) with none; a
* REMOVE is a slot alighting with its cargo, or an empty trim. A SWITCH row
* carries the incoming wagon's id — the slot's cargo already rides it.
*/
private consistChangesAt(
schedule: TrainSchedule,
logRows: ScheduleWagonAdjustmentLog[],
): Array<{ wagonNumber: string; event: 'Coupled' | 'Uncoupled' | 'Switched'; containerNumbers: string }> {
const slotByPhysicalWagonId = new Map(
(schedule.trainSet?.wagons ?? [])
.filter((wagon) => wagon.physicalWagonId)
.map((wagon) => [wagon.physicalWagonId as string, wagon]),
);
return logRows.map((row) => {
const slot = slotByPhysicalWagonId.get(row.wagonId);
const containerNumbers = (slot?.allocations ?? [])
.flatMap((allocation) => allocation.containerItems ?? [])
.map((item) => item.containerNumber)
.filter(Boolean)
.join(', ');
return {
wagonNumber: row.wagonNumber,
event: row.action === 'ADD' ? 'Coupled' : row.action === 'REMOVE' ? 'Uncoupled' : 'Switched',
containerNumbers: containerNumbers || 'EMPTY WAGON',
};
});
}
/**
* The numbered marshalling document for one corridor stop (see
* marshallingStops — stopIndex 2+, origin is its own separate doc).
* ponytail: the wagon table always shows the CURRENT on-board state, not a
* point-in-time reconstruction of what stood on the train at that past
* stop — a full historical snapshot is a much bigger feature nobody has
* asked for. What's stop-specific is the consist-changes table below it,
* which IS scoped to that stop's own logged events.
*/
async marshallingDocumentAt(scheduleId: string, stopIndex: number): Promise<{ filename: string; buffer: Buffer }> {
const schedule = await this.trainSchedulesRepository.findByIdWithFullGraph(scheduleId);
if (!schedule) {
throw new NotFoundException(`Train schedule ${scheduleId} not found`);
@@ -3627,24 +3703,66 @@ export class TrainSchedulingService {
'Intercity marshalling document applies only to dispatched or arrived trains',
);
}
const stops = await this.marshallingStops(scheduleId);
const stop = stops.find((s) => s.stopIndex === stopIndex);
if (!stop) {
throw new NotFoundException(
`No marshalling document at stop ${stopIndex} for this schedule — nothing coupled/uncoupled there, or the stop doesn't exist`,
);
}
const { wagons, unassignedBookings } = this.intercityOnBoardView(schedule);
const logRows = await this.dataSource.getRepository(ScheduleWagonAdjustmentLog).find({
where: { trainScheduleId: scheduleId, yardId: stop.yardId },
order: { occurredAt: 'ASC' },
});
const html = this.buildExportLoadListHtml(schedule, {
title: `Intercity Marshalling Document / Load List (Marshalling ${stopIndex})`,
positionLabel: `At ${stop.yardLabel}`,
wagons,
unassignedBookings,
emptyContainers: await this.loadedEmptyContainers(scheduleId),
logoImageUrl: await this.logoSettings.getLogoImageUrl(),
consistChangesAtStop: this.consistChangesAt(schedule, logRows),
});
// Styled table-aware fallback (marshalling grid) — see importLoadListDocument.
const buffer = await this.pdfDocuments.renderTabularDocument(html, `Marshalling ${stopIndex}`);
const reference = schedule.trainNumber ?? schedule.id;
return {
filename: `marshalling-${stopIndex}-${this.safeDocumentName(reference)}.pdf`,
buffer,
};
}
/**
* Back-compat alias: the single "current" intercity doc (Marshalling 2) the
* old one-document-per-schedule UI calls. Resolves to the LATEST stop with
* a logged consist change; falls back to the current-position doc with no
* changes table when nothing has coupled/uncoupled yet (e.g. right after
* dispatch, before any mid-corridor stop).
*/
async intercityMarshallingDocument(scheduleId: string): Promise<{ filename: string; buffer: Buffer }> {
const stops = await this.marshallingStops(scheduleId);
const latest = stops[stops.length - 1];
if (latest) {
return this.marshallingDocumentAt(scheduleId, latest.stopIndex);
}
const schedule = await this.trainSchedulesRepository.findByIdWithFullGraph(scheduleId);
if (!schedule) {
throw new NotFoundException(`Train schedule ${scheduleId} not found`);
}
if (schedule.status !== 'DISPATCHED' && schedule.status !== 'ARRIVED') {
throw new BadRequestException(
'Intercity marshalling document applies only to dispatched or arrived trains',
);
}
const checkpoints = await this.trainCheckpointEventsRepository.findBySchedule(scheduleId);
const last = checkpoints[checkpoints.length - 1];
const positionLabel = last
? `After ${last.yard?.label ?? last.yard?.code ?? 'checkpoint'}`
: `At ${schedule.originStation?.label ?? schedule.originStation?.code ?? 'origin'} — no checkpoint recorded`;
const { wagons, unassignedBookings } = this.intercityOnBoardView(schedule);
// Couples/switches logged AT THIS STOP — what staff standing here actually
// just did to the consist. Bare trims (REMOVE, no replacement) are left
// out: nothing new to point staff at for those. Origin adjustments (a
// different yard) don't show up on this stop's document.
const consistChangesAtStop = last
? await this.dataSource.getRepository(ScheduleWagonAdjustmentLog).find({
where: { trainScheduleId: scheduleId, yardId: last.yardId, action: In(['ADD', 'SWITCH']) },
order: { occurredAt: 'DESC' },
})
: [];
const html = this.buildExportLoadListHtml(schedule, {
title: 'Intercity Marshalling Document / Load List (Marshalling 2)',
positionLabel,
@@ -3652,7 +3770,6 @@ export class TrainSchedulingService {
unassignedBookings,
emptyContainers: await this.loadedEmptyContainers(scheduleId),
logoImageUrl: await this.logoSettings.getLogoImageUrl(),
consistChangesAtStop,
});
// Styled table-aware fallback (marshalling grid) — see importLoadListDocument.
const buffer = await this.pdfDocuments.renderTabularDocument(html, 'Intercity marshalling / load list');
@@ -3722,10 +3839,14 @@ export class TrainSchedulingService {
// Slots that couple to the train downstream (slot id → board yard label).
// Their cargo renders as TO LOAD AT and stays out of the loaded tallies.
pendingBoardYardLabelBySlot?: Map<string, string>;
// Intercity (Marshalling 2) only: couples/switches logged at the stop
// this document is printed at (see ScheduleWagonAdjustmentLog). Origin
// import/export docs never pass this, so they render no such box.
consistChangesAtStop?: ScheduleWagonAdjustmentLog[];
// Numbered marshalling docs only (see marshallingDocumentAt /
// consistChangesAt) — couples/uncouples/switches logged at THIS stop.
// Origin import/export docs never pass this, so they render no such box.
consistChangesAtStop?: Array<{
wagonNumber: string;
event: 'Coupled' | 'Uncoupled' | 'Switched';
containerNumbers: string;
}>;
},
): string {
const esc = (value: unknown) =>
@@ -3890,6 +4011,7 @@ export class TrainSchedulingService {
.tile span { display: block; color: #64748b; font-size: 9px; text-transform: uppercase; letter-spacing: .05em; margin-bottom: 4px; }
.tile strong { font-size: 11px; }
${logoImageCss()}
h2 { margin: 16px 0 6px; font-size: 12px; color: #0f766e; text-transform: uppercase; letter-spacing: .05em; }
table { width: 100%; border-collapse: collapse; }
th { background: #f8fafc; color: #475569; text-align: left; }
th, td { border: 1px solid #cbd5e1; padding: 5px 6px; font-size: 9.5px; vertical-align: top; }
@@ -3938,19 +4060,27 @@ export class TrainSchedulingService {
${
opts?.consistChangesAtStop?.length
? `<div class="notice">
<b>Consist changed at this stop:</b>
${(() => {
const coupled = opts.consistChangesAtStop.filter((row) => row.action === 'ADD');
const switched = opts.consistChangesAtStop.filter((row) => row.action === 'SWITCH');
return [
coupled.length ? `Coupled: ${esc(coupled.map((row) => row.wagonNumber).join(', '))}` : '',
switched.length ? `Uncoupled — replaced: ${esc(switched.map((row) => row.wagonNumber).join(', '))}` : '',
]
.filter(Boolean)
.join(' &nbsp;|&nbsp; ');
})()}
</div>`
? `<h2>Consist Changed At This Stop</h2>
<table>
<thead>
<tr>
<th>Wagon No</th>
<th>Event</th>
<th>Container No</th>
</tr>
</thead>
<tbody>
${opts.consistChangesAtStop
.map(
(row) => `<tr>
<td>${esc(row.wagonNumber)}</td>
<td>${esc(row.event)}</td>
<td>${esc(row.containerNumbers)}</td>
</tr>`,
)
.join('')}
</tbody>
</table>`
: ''
}