mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-08-28 09:00:57 +00:00
fix issue
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { DataSource, Repository } from 'typeorm';
|
||||
import { MaintenanceRepository } from './maintenance.repository';
|
||||
import { MaintenanceSchedule } from './entities/maintenance-schedule.entity';
|
||||
import { MaintenanceSchedule, MaintenanceStatus } from './entities/maintenance-schedule.entity';
|
||||
import { MaintenanceCost } from './entities/maintenance-cost.entity';
|
||||
import { Vehicle, VehicleAvailability, VehicleStatus } from '../vehicles/entities/vehicle.entity';
|
||||
import { CreateMaintenanceScheduleDto, CreateMaintenanceCostDto, UpdateMaintenanceScheduleDto } from './dto/create-maintenance.dto';
|
||||
|
||||
@Injectable()
|
||||
@@ -14,15 +15,41 @@ export class MaintenanceService {
|
||||
private readonly scheduleRepository: Repository<MaintenanceSchedule>,
|
||||
@InjectRepository(MaintenanceCost)
|
||||
private readonly costRepository: Repository<MaintenanceCost>,
|
||||
// Vehicle isn't registered in this module's TypeOrmModule.forFeature, so we
|
||||
// reach it through the global DataSource rather than @InjectRepository.
|
||||
private readonly dataSource: DataSource,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Reflect a maintenance schedule's lifecycle on the target vehicle. A vehicle
|
||||
* under maintenance is taken out of service (MAINTENANCE + BUSY); once the
|
||||
* maintenance completes or is cancelled it returns to service (ACTIVE + FREE).
|
||||
* Only the vehicle's status/availability columns are written here. The
|
||||
* assignment-side reject (first-mile/last-mile refusing MAINTENANCE vehicles)
|
||||
* lives in those excluded mile modules, not here.
|
||||
*/
|
||||
private async setVehicleMaintenanceState(
|
||||
vehicleId: string,
|
||||
underMaintenance: boolean,
|
||||
): Promise<void> {
|
||||
await this.dataSource.getRepository(Vehicle).update(vehicleId, {
|
||||
status: underMaintenance ? VehicleStatus.MAINTENANCE : VehicleStatus.ACTIVE,
|
||||
availability: underMaintenance
|
||||
? VehicleAvailability.BUSY
|
||||
: VehicleAvailability.FREE,
|
||||
});
|
||||
}
|
||||
|
||||
async scheduleMaintenanceAsync(dto: CreateMaintenanceScheduleDto): Promise<MaintenanceSchedule> {
|
||||
const schedule = this.scheduleRepository.create({
|
||||
...dto,
|
||||
scheduledDate: new Date(dto.scheduledDate),
|
||||
nextDueDate: dto.nextDueDate ? new Date(dto.nextDueDate) : undefined,
|
||||
});
|
||||
return this.scheduleRepository.save(schedule);
|
||||
const saved = await this.scheduleRepository.save(schedule);
|
||||
// Scheduling maintenance takes the vehicle out of the available pool.
|
||||
await this.setVehicleMaintenanceState(saved.vehicleId, true);
|
||||
return saved;
|
||||
}
|
||||
|
||||
async recordMaintenanceCost(dto: CreateMaintenanceCostDto): Promise<MaintenanceCost> {
|
||||
@@ -42,6 +69,21 @@ export class MaintenanceService {
|
||||
completedDate: dto.completedDate ? new Date(dto.completedDate) : undefined,
|
||||
});
|
||||
const updated = await this.scheduleRepository.findOneBy({ id });
|
||||
|
||||
// Keep the vehicle's status/availability in step with the schedule status.
|
||||
if (updated && dto.status) {
|
||||
if (
|
||||
dto.status === MaintenanceStatus.COMPLETED ||
|
||||
dto.status === MaintenanceStatus.CANCELLED
|
||||
) {
|
||||
// Maintenance finished/aborted → vehicle back in service.
|
||||
await this.setVehicleMaintenanceState(updated.vehicleId, false);
|
||||
} else if (dto.status === MaintenanceStatus.IN_PROGRESS) {
|
||||
// Maintenance started → keep the vehicle out of service.
|
||||
await this.setVehicleMaintenanceState(updated.vehicleId, true);
|
||||
}
|
||||
}
|
||||
|
||||
return updated!;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user