mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-09-07 21:15:41 +00:00
Refactor code structure for improved readability and maintainability
This commit is contained in:
@@ -74,6 +74,25 @@ import { WagonType } from '../../wagon-types/entities/wagon-type.entity';
|
||||
import { WagonTypesRepository } from '../../wagon-types/wagon-types.repository';
|
||||
import { Wagon } from '../../wagons/entities/wagon.entity';
|
||||
import { WagonEventInput, WagonHistoryService } from '../../wagon-history/wagon-history.service';
|
||||
import { TabularExportService } from '../../exports/tabular-export.service';
|
||||
|
||||
/** One line of the schedule wagon-list export (raw SQL projection). */
|
||||
interface ScheduleWagonListRow {
|
||||
sequenceNo: number | null;
|
||||
wagonNumber: string | null;
|
||||
wagonType: string | null;
|
||||
containerNumber: string | null;
|
||||
containerSizeFt: number | null;
|
||||
loadType: string | null;
|
||||
status: string | null;
|
||||
bulkCargoDescription: string | null;
|
||||
/** numeric columns arrive as strings from pg. */
|
||||
vgmTons: string | null;
|
||||
originLabel: string | null;
|
||||
destinationLabel: string | null;
|
||||
bookingReference: string | null;
|
||||
customerName: string | null;
|
||||
}
|
||||
import { AdjustScheduleConsistDto } from '../dto/adjust-schedule-consist.dto';
|
||||
import { AssignBookingsDto } from '../dto/assign-bookings.dto';
|
||||
import { CreateContainerTrainScheduleDto } from '../dto/create-container-train-schedule.dto';
|
||||
@@ -427,6 +446,9 @@ export class TrainSchedulingService {
|
||||
// Per-wagon history ledger (global module). @Optional keeps the positional
|
||||
// spec constructors working; production always has it.
|
||||
@Optional() private readonly wagonHistory?: WagonHistoryService,
|
||||
// Trailing + @Optional so the positional constructors in the existing specs
|
||||
// keep working; production always resolves it from ExportsModule.
|
||||
@Optional() private readonly tabularExport?: TabularExportService,
|
||||
) {}
|
||||
|
||||
/** Physical wagons behind a set of booking allocations (via their slots), for cargo history rows. */
|
||||
@@ -3747,6 +3769,132 @@ export class TrainSchedulingService {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* The schedule detail page's wagon-list Excel export.
|
||||
*
|
||||
* One row per container (a wagon carrying two boxes yields two rows, repeating
|
||||
* the wagon number) so each container's own VGM is present and totals footable.
|
||||
* Bulk wagons, having no containers, yield a single row carrying the bulk
|
||||
* description and the allocated tonnage as the VGM figure.
|
||||
*
|
||||
* Only wagon slots that actually carry an allocation are listed — empty slots
|
||||
* on the consist are omitted.
|
||||
*/
|
||||
async scheduleWagonListWorkbook(
|
||||
scheduleId: string,
|
||||
): Promise<{ filename: string; buffer: Buffer }> {
|
||||
const schedule = await this.trainSchedulesRepository.findById(scheduleId);
|
||||
if (!schedule) {
|
||||
throw new NotFoundException(`Train schedule ${scheduleId} not found`);
|
||||
}
|
||||
if (!this.tabularExport) {
|
||||
throw new BadRequestException('Tabular export service is unavailable');
|
||||
}
|
||||
|
||||
// Row grain is the container item; the LEFT JOIN keeps bulk (and any
|
||||
// container-less) allocation as one row. `booking_container_units` is joined
|
||||
// on BOTH container number and its booking_container line — container
|
||||
// numbers repeat across bookings, so number alone would multiply rows.
|
||||
const rows: ScheduleWagonListRow[] = await this.dataSource.query(
|
||||
`SELECT tsw.sequence_no AS "sequenceNo",
|
||||
w.wagon_number AS "wagonNumber",
|
||||
COALESCE(wt.name, wt.code) AS "wagonType",
|
||||
ci.container_number AS "containerNumber",
|
||||
cit.size_ft AS "containerSizeFt",
|
||||
a.load_type AS "loadType",
|
||||
a.status AS "status",
|
||||
bl.cargo_description AS "bulkCargoDescription",
|
||||
COALESCE(
|
||||
ci.gross_weight_tons,
|
||||
bcu.vgm_tons,
|
||||
bc.vgm_per_unit_tons,
|
||||
a.allocated_weight_tons
|
||||
) AS "vgmTons",
|
||||
COALESCE(by_.label, so.label) AS "originLabel",
|
||||
COALESCE(ay.label, sd.label) AS "destinationLabel",
|
||||
b.reference AS "bookingReference",
|
||||
COALESCE(
|
||||
slc.name,
|
||||
CASE WHEN b.is_government THEN NULLIF(TRIM(b.government_institution), '') END,
|
||||
c.name
|
||||
) AS "customerName"
|
||||
FROM freight.train_schedules s
|
||||
JOIN freight.train_set_wagons tsw
|
||||
ON tsw.train_set_id = s.train_set_id AND tsw.deleted_at IS NULL
|
||||
JOIN freight.wagon_booking_allocations a
|
||||
ON a.train_set_wagon_id = tsw.id AND a.deleted_at IS NULL
|
||||
LEFT JOIN freight.wagons w ON w.id = tsw.physical_wagon_id
|
||||
LEFT JOIN freight.wagon_types wt ON wt.id = tsw.wagon_type_id
|
||||
LEFT JOIN freight.bookings b ON b.id = a.booking_id
|
||||
LEFT JOIN freight.companies c ON c.id = b.company_id
|
||||
LEFT JOIN freight.shipping_line_companies slc ON slc.id = b.shipping_line_company_id
|
||||
LEFT JOIN freight.wagon_allocation_container_items ci
|
||||
ON ci.wagon_booking_allocation_id = a.id AND ci.deleted_at IS NULL
|
||||
LEFT JOIN freight.container_types cit ON cit.id = ci.container_type_id
|
||||
LEFT JOIN freight.booking_container bc
|
||||
ON bc.id = ci.booking_container_id AND bc.deleted_at IS NULL
|
||||
LEFT JOIN freight.booking_container_units bcu
|
||||
ON bcu.container_number = ci.container_number
|
||||
AND bcu.booking_container_id = bc.id
|
||||
AND bcu.deleted_at IS NULL
|
||||
LEFT JOIN freight.wagon_allocation_bulk_loads bl
|
||||
ON bl.wagon_booking_allocation_id = a.id AND bl.deleted_at IS NULL
|
||||
LEFT JOIN freight.yards so ON so.id = s.origin_station_id
|
||||
LEFT JOIN freight.yards sd ON sd.id = s.destination_station_id
|
||||
LEFT JOIN freight.yards by_ ON by_.id = tsw.board_yard_id
|
||||
LEFT JOIN freight.yards ay ON ay.id = tsw.alight_yard_id
|
||||
WHERE s.id = $1 AND s.deleted_at IS NULL
|
||||
ORDER BY tsw.sequence_no, ci.position_on_wagon, ci.container_number`,
|
||||
[scheduleId],
|
||||
);
|
||||
|
||||
// "number" is the printed line number of the sheet, not the wagon sequence —
|
||||
// a two-container wagon occupies two lines, and the reader counts lines.
|
||||
const sheetRows = rows.map((row, index) => ({
|
||||
number: index + 1,
|
||||
wagonNumber: row.wagonNumber ?? '—',
|
||||
containerNumber:
|
||||
row.containerNumber ??
|
||||
(row.loadType === 'BULK' ? (row.bulkCargoDescription ?? 'Bulk') : '—'),
|
||||
vgmTons: row.vgmTons === null ? null : Number(row.vgmTons),
|
||||
originLabel: row.originLabel ?? '—',
|
||||
destinationLabel: row.destinationLabel ?? '—',
|
||||
customerName: row.customerName ?? '—',
|
||||
}));
|
||||
|
||||
const totalVgm = sheetRows.reduce((sum, r) => sum + (r.vgmTons ?? 0), 0);
|
||||
const reference = schedule.reference ?? schedule.trainNumber ?? schedule.id;
|
||||
|
||||
const buffer = await this.tabularExport.toXlsx({
|
||||
title: `Wagons ${reference}`.slice(0, 31),
|
||||
description: `Wagon list for train ${reference}`,
|
||||
label: 'train-schedule:wagon-list',
|
||||
kpis: [
|
||||
{ label: 'Lines', value: sheetRows.length },
|
||||
{
|
||||
label: 'Wagons',
|
||||
value: new Set(rows.map((r) => r.sequenceNo)).size,
|
||||
},
|
||||
{ label: 'Total VGM', value: Number(totalVgm.toFixed(3)), unit: 't' },
|
||||
],
|
||||
columns: [
|
||||
{ key: 'number', label: 'No.', type: 'number' },
|
||||
{ key: 'wagonNumber', label: 'Wagon', type: 'string' },
|
||||
{ key: 'containerNumber', label: 'Container number', type: 'string' },
|
||||
{ key: 'vgmTons', label: 'VGM', type: 'tons' },
|
||||
{ key: 'originLabel', label: 'Origin', type: 'string' },
|
||||
{ key: 'destinationLabel', label: 'Destination', type: 'string' },
|
||||
{ key: 'customerName', label: 'Customer', type: 'string' },
|
||||
],
|
||||
rows: sheetRows,
|
||||
});
|
||||
|
||||
return {
|
||||
filename: `wagon-list-${this.safeDocumentName(reference)}.xlsx`,
|
||||
buffer,
|
||||
};
|
||||
}
|
||||
|
||||
async exportLoadListDocument(scheduleId: string): Promise<{ filename: string; buffer: Buffer }> {
|
||||
const schedule = await this.trainSchedulesRepository.findByIdWithFullGraph(scheduleId);
|
||||
if (!schedule) {
|
||||
|
||||
Reference in New Issue
Block a user