This commit is contained in:
natib21
2026-07-03 21:45:54 +00:00
parent bc62ce3a8e
commit 9422e5588e
2 changed files with 158 additions and 49 deletions

View File

@@ -365,20 +365,28 @@ export class LastMileService {
}
/**
* Free every vehicle held by this record (direct assignment + container
* allocations), unless still in use by another active trip.
* Free every vehicle held by this record — junction assignments, the legacy
* direct vehicle, and container allocations unless still used by another
* active trip.
*/
private async releaseVehicles(record: LastMile): Promise<void> {
const recordAllocations = await this.dataSource.manager.find(
LastMileContainerAllocation,
{ where: { lastMileId: record.id } },
);
const vehicleIds = recordAllocations
.map((a) => a.vehicleId)
.filter((id): id is string => Boolean(id));
if (record.vehicleId) {
vehicleIds.push(record.vehicleId);
}
const [assignments, recordAllocations] = await Promise.all([
this.dataSource.manager.find(LastMileVehicleAssignment, {
where: { lastMileId: record.id },
}),
this.dataSource.manager.find(LastMileContainerAllocation, {
where: { lastMileId: record.id },
}),
]);
const vehicleIds = [
...new Set(
[
...assignments.map((a) => a.vehicleId),
...recordAllocations.map((a) => a.vehicleId),
record.vehicleId ?? null,
].filter((id): id is string => Boolean(id)),
),
];
await this.vehiclesService.releaseIfUnused(vehicleIds);
}