mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-08-29 14:08:11 +00:00
fix
This commit is contained in:
@@ -1,9 +1,13 @@
|
||||
import { Injectable, NotFoundException, ConflictException } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { Not, Repository } from 'typeorm';
|
||||
import { CreateVehicleDto } from './dto/create-vehicle.dto';
|
||||
import { UpdateVehicleDto } from './dto/update-vehicle.dto';
|
||||
import { Vehicle, VehicleStatus } from './entities/vehicle.entity';
|
||||
import { FirstMile, FirstMileStatus } from '../first-mile/entities/first-mile.entity';
|
||||
import { FirstMileContainerAllocation } from '../first-mile/entities/first-mile-container-allocation.entity';
|
||||
import { LastMile, LastMileStatus } from '../last-mile/entities/last-mile.entity';
|
||||
import { LastMileContainerAllocation } from '../last-mile/entities/last-mile-container-allocation.entity';
|
||||
|
||||
@Injectable()
|
||||
export class VehiclesService {
|
||||
@@ -91,6 +95,47 @@ export class VehiclesService {
|
||||
return this.vehicleRepo.save(vehicle);
|
||||
}
|
||||
|
||||
async setStatus(id: string, status: VehicleStatus): Promise<void> {
|
||||
await this.vehicleRepo.update(id, { status });
|
||||
}
|
||||
|
||||
/**
|
||||
* Set vehicles back to FREE, but only when no active (non-completed)
|
||||
* first/last-mile record or container allocation still references them.
|
||||
* First-mile trips ending in RECEIVED_TO_PORT and last-mile trips ending
|
||||
* in DELIVERED no longer hold the vehicle.
|
||||
*/
|
||||
async releaseIfUnused(vehicleIds: string[]): Promise<void> {
|
||||
const manager = this.vehicleRepo.manager;
|
||||
for (const vehicleId of [...new Set(vehicleIds)]) {
|
||||
const [fmRecords, lmRecords, fmAllocations, lmAllocations] = await Promise.all([
|
||||
manager.count(FirstMile, {
|
||||
where: { vehicleId, status: Not<FirstMileStatus>('RECEIVED_TO_PORT') },
|
||||
}),
|
||||
manager.count(LastMile, {
|
||||
where: { vehicleId, status: Not<LastMileStatus>('DELIVERED') },
|
||||
}),
|
||||
manager
|
||||
.createQueryBuilder(FirstMileContainerAllocation, 'alloc')
|
||||
.innerJoin(FirstMile, 'fm', 'fm.id = alloc.firstMileId')
|
||||
.where('alloc.vehicleId = :vehicleId', { vehicleId })
|
||||
.andWhere('fm.status != :done', { done: 'RECEIVED_TO_PORT' })
|
||||
.andWhere('fm.deletedAt IS NULL')
|
||||
.getCount(),
|
||||
manager
|
||||
.createQueryBuilder(LastMileContainerAllocation, 'alloc')
|
||||
.innerJoin(LastMile, 'lm', 'lm.id = alloc.lastMileId')
|
||||
.where('alloc.vehicleId = :vehicleId', { vehicleId })
|
||||
.andWhere('lm.status != :done', { done: 'DELIVERED' })
|
||||
.andWhere('lm.deletedAt IS NULL')
|
||||
.getCount(),
|
||||
]);
|
||||
if (fmRecords + lmRecords + fmAllocations + lmAllocations === 0) {
|
||||
await this.setStatus(vehicleId, VehicleStatus.FREE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async remove(id: string): Promise<void> {
|
||||
await this.findById(id);
|
||||
await this.vehicleRepo.softDelete(id);
|
||||
|
||||
Reference in New Issue
Block a user