mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-08-29 04:50:54 +00:00
mile
This commit is contained in:
@@ -539,8 +539,46 @@ export class LastMileService {
|
||||
}
|
||||
|
||||
async remove(id: string): Promise<void> {
|
||||
await this.findById(id);
|
||||
const existing = await this.findById(id);
|
||||
|
||||
// Every vehicle this delivery holds — junction + legacy + container rows.
|
||||
const assignments = await this.dataSource.manager.find(LastMileVehicleAssignment, {
|
||||
where: { lastMileId: id },
|
||||
});
|
||||
const allocations = await this.dataSource.manager.find(LastMileContainerAllocation, {
|
||||
where: { lastMileId: id },
|
||||
});
|
||||
const vehicleIds = [
|
||||
...new Set(
|
||||
[
|
||||
...assignments.map((a) => a.vehicleId),
|
||||
...allocations.map((a) => a.vehicleId),
|
||||
existing.vehicleId ?? null,
|
||||
].filter((v): v is string => Boolean(v)),
|
||||
),
|
||||
];
|
||||
|
||||
await this.lastMileRepository.softDelete(id);
|
||||
if (assignments.length) {
|
||||
await this.dataSource.manager.softDelete(LastMileVehicleAssignment, { lastMileId: id });
|
||||
}
|
||||
|
||||
// Free every vehicle no longer held by another active trip (releaseIfUnused
|
||||
// ignores this now soft-deleted record) and audit the release.
|
||||
if (vehicleIds.length) {
|
||||
await this.vehiclesService.releaseIfUnused(vehicleIds);
|
||||
const bookingRef = await this.resolveBookingRef(existing);
|
||||
for (const vehicleId of vehicleIds) {
|
||||
const info = await this.vehicleInfo(vehicleId);
|
||||
await this.history.record({
|
||||
eventType: FleetEventType.MILE_VEHICLE_RELEASED,
|
||||
vehicleId,
|
||||
lastMileId: id,
|
||||
driverId: info.driverId,
|
||||
metadata: { mile: 'LAST', bookingRef, vehiclePlate: info.plate, driverName: info.driverName },
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async allocateContainers(
|
||||
|
||||
Reference in New Issue
Block a user