mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-09-02 12:43:43 +00:00
fix
This commit is contained in:
@@ -14,13 +14,17 @@ import { FleetManage, FleetView } from '../../common/booking-guards';
|
||||
import { VehiclesService } from './vehicles.service';
|
||||
import { CreateVehicleDto } from './dto/create-vehicle.dto';
|
||||
import { UpdateVehicleDto } from './dto/update-vehicle.dto';
|
||||
import { FleetHistoryService } from '../fleet-history/fleet-history.service';
|
||||
|
||||
@ApiTags('vehicles')
|
||||
@ApiBearerAuth()
|
||||
@Controller('vehicles')
|
||||
@FleetView()
|
||||
export class VehiclesController {
|
||||
constructor(private readonly vehiclesService: VehiclesService) {}
|
||||
constructor(
|
||||
private readonly vehiclesService: VehiclesService,
|
||||
private readonly fleetHistory: FleetHistoryService,
|
||||
) {}
|
||||
|
||||
@Post()
|
||||
@FleetManage()
|
||||
@@ -57,6 +61,12 @@ export class VehiclesController {
|
||||
return this.vehiclesService.findById(id);
|
||||
}
|
||||
|
||||
@Get(':id/history')
|
||||
@ApiOperation({ summary: 'Get vehicle assignment, status & mile history' })
|
||||
history(@Param('id', ParseUUIDPipe) id: string) {
|
||||
return this.fleetHistory.getVehicleHistory(id);
|
||||
}
|
||||
|
||||
@Patch(':id')
|
||||
@FleetManage()
|
||||
@ApiOperation({ summary: 'Update a vehicle' })
|
||||
|
||||
@@ -9,12 +9,15 @@ import { FirstMileContainerAllocation } from '../first-mile/entities/first-mile-
|
||||
import { LastMile, LastMileStatus } from '../last-mile/entities/last-mile.entity';
|
||||
import { LastMileContainerAllocation } from '../last-mile/entities/last-mile-container-allocation.entity';
|
||||
import { BookingContainerAllocation } from '../bookings/entities/booking-container-allocation.entity';
|
||||
import { FleetHistoryService } from '../fleet-history/fleet-history.service';
|
||||
import { FleetEventType } from '../fleet-history/entities/fleet-event.entity';
|
||||
|
||||
@Injectable()
|
||||
export class VehiclesService {
|
||||
constructor(
|
||||
@InjectRepository(Vehicle)
|
||||
private readonly vehicleRepo: Repository<Vehicle>,
|
||||
private readonly history: FleetHistoryService,
|
||||
) {}
|
||||
|
||||
async create(dto: CreateVehicleDto): Promise<Vehicle> {
|
||||
@@ -34,7 +37,24 @@ export class VehiclesService {
|
||||
registrationNumber,
|
||||
});
|
||||
|
||||
return this.vehicleRepo.save(vehicle);
|
||||
const saved = await this.vehicleRepo.save(vehicle);
|
||||
|
||||
await this.history.record({
|
||||
eventType: FleetEventType.VEHICLE_REGISTERED,
|
||||
vehicleId: saved.id,
|
||||
label: saved.plateNumber ?? saved.code ?? null,
|
||||
toValue: saved.availability ?? null,
|
||||
});
|
||||
if (saved.assignedDriverId) {
|
||||
await this.history.record({
|
||||
eventType: FleetEventType.DRIVER_ASSIGNED,
|
||||
vehicleId: saved.id,
|
||||
driverId: saved.assignedDriverId,
|
||||
label: saved.assignedDriverName ?? null,
|
||||
});
|
||||
}
|
||||
|
||||
return saved;
|
||||
}
|
||||
|
||||
async findAll(query: {
|
||||
@@ -97,12 +117,73 @@ export class VehiclesService {
|
||||
}
|
||||
}
|
||||
|
||||
const prev = {
|
||||
assignedDriverId: vehicle.assignedDriverId,
|
||||
assignedDriverName: vehicle.assignedDriverName,
|
||||
status: vehicle.status,
|
||||
availability: vehicle.availability,
|
||||
};
|
||||
|
||||
Object.assign(vehicle, dto);
|
||||
return this.vehicleRepo.save(vehicle);
|
||||
const saved = await this.vehicleRepo.save(vehicle);
|
||||
|
||||
// Driver (re)assignment — emit an unassign for the old driver and/or an
|
||||
// assign for the new one so both drivers' timelines and the vehicle's line up.
|
||||
if (
|
||||
dto.assignedDriverId !== undefined &&
|
||||
dto.assignedDriverId !== prev.assignedDriverId
|
||||
) {
|
||||
if (prev.assignedDriverId) {
|
||||
await this.history.record({
|
||||
eventType: FleetEventType.DRIVER_UNASSIGNED,
|
||||
vehicleId: id,
|
||||
driverId: prev.assignedDriverId,
|
||||
label: prev.assignedDriverName ?? null,
|
||||
});
|
||||
}
|
||||
if (saved.assignedDriverId) {
|
||||
await this.history.record({
|
||||
eventType: FleetEventType.DRIVER_ASSIGNED,
|
||||
vehicleId: id,
|
||||
driverId: saved.assignedDriverId,
|
||||
label: saved.assignedDriverName ?? null,
|
||||
});
|
||||
}
|
||||
}
|
||||
if (dto.status !== undefined && dto.status !== prev.status) {
|
||||
await this.history.record({
|
||||
eventType: FleetEventType.VEHICLE_STATUS_CHANGED,
|
||||
vehicleId: id,
|
||||
fromValue: prev.status ?? null,
|
||||
toValue: saved.status ?? null,
|
||||
});
|
||||
}
|
||||
if (dto.availability !== undefined && dto.availability !== prev.availability) {
|
||||
await this.history.record({
|
||||
eventType: FleetEventType.VEHICLE_AVAILABILITY_CHANGED,
|
||||
vehicleId: id,
|
||||
fromValue: prev.availability ?? null,
|
||||
toValue: saved.availability ?? null,
|
||||
});
|
||||
}
|
||||
|
||||
return saved;
|
||||
}
|
||||
|
||||
async setAvailability(id: string, availability: VehicleAvailability): Promise<void> {
|
||||
// Read the current value so the audit event records an accurate from→to and
|
||||
// we skip logging no-op writes (setAvailability is called in release loops).
|
||||
const vehicle = await this.vehicleRepo.findOne({ where: { id } });
|
||||
const previous = vehicle?.availability;
|
||||
await this.vehicleRepo.update(id, { availability });
|
||||
if (previous !== availability) {
|
||||
await this.history.record({
|
||||
eventType: FleetEventType.VEHICLE_AVAILABILITY_CHANGED,
|
||||
vehicleId: id,
|
||||
fromValue: previous ?? null,
|
||||
toValue: availability,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user