mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-08-26 18:42:49 +00:00
feat(wagons): filter list by last-maintenance date range
Adds maintenanceFrom/maintenanceTo to ListWagonsQueryDto and applies them in WagonsService.buildListQuery as a correlated subquery against wagon_status_logs (last flip to MAINTENANCE), mirroring the existing createdFrom/createdTo range filter. Both ends inclusive, whole days.
This commit is contained in:
@@ -91,4 +91,18 @@ export class ListWagonsQueryDto {
|
||||
@IsOptional()
|
||||
@IsDateString()
|
||||
createdTo?: string;
|
||||
|
||||
@ApiPropertyOptional({
|
||||
description: 'Last maintenance flip on or after this day (YYYY-MM-DD)',
|
||||
})
|
||||
@IsOptional()
|
||||
@IsDateString()
|
||||
maintenanceFrom?: string;
|
||||
|
||||
@ApiPropertyOptional({
|
||||
description: 'Last maintenance flip on or before this day (YYYY-MM-DD)',
|
||||
})
|
||||
@IsOptional()
|
||||
@IsDateString()
|
||||
maintenanceTo?: string;
|
||||
}
|
||||
|
||||
@@ -90,6 +90,27 @@ export class WagonsService {
|
||||
});
|
||||
}
|
||||
|
||||
// Last-maintenance range, both ends inclusive. There's no column to
|
||||
// compare directly — "last maintenance" is the latest status-log flip to
|
||||
// MAINTENANCE (see attachStatusDates below), so this mirrors that same
|
||||
// MAX(...) FILTER(...) as a correlated subquery against the same table.
|
||||
if (query.maintenanceFrom) {
|
||||
qb.andWhere(
|
||||
`(SELECT MAX(l.created_at) FROM freight.wagon_status_logs l
|
||||
WHERE l.wagon_id = w.id AND l.to_status = '${WagonStatus.Maintenance}')
|
||||
>= CAST(:maintenanceFrom AS date)`,
|
||||
{ maintenanceFrom: query.maintenanceFrom },
|
||||
);
|
||||
}
|
||||
if (query.maintenanceTo) {
|
||||
qb.andWhere(
|
||||
`(SELECT MAX(l.created_at) FROM freight.wagon_status_logs l
|
||||
WHERE l.wagon_id = w.id AND l.to_status = '${WagonStatus.Maintenance}')
|
||||
< CAST(:maintenanceTo AS date) + INTERVAL '1 day'`,
|
||||
{ maintenanceTo: query.maintenanceTo },
|
||||
);
|
||||
}
|
||||
|
||||
// Search matches the wagon number or either run number.
|
||||
if (search) {
|
||||
qb.andWhere(
|
||||
|
||||
Reference in New Issue
Block a user