From 26e2974d87443c09d1b8e7ea99b5e28f57e98546 Mon Sep 17 00:00:00 2001 From: yaschalew Date: Thu, 2 Jul 2026 18:02:23 +0300 Subject: [PATCH] fix --- .../modules/last-mile/last-mile.service.ts | 84 +------------------ 1 file changed, 4 insertions(+), 80 deletions(-) diff --git a/apps/edr-freight-api/src/modules/last-mile/last-mile.service.ts b/apps/edr-freight-api/src/modules/last-mile/last-mile.service.ts index dbdba14a5..69eec29ae 100644 --- a/apps/edr-freight-api/src/modules/last-mile/last-mile.service.ts +++ b/apps/edr-freight-api/src/modules/last-mile/last-mile.service.ts @@ -1,11 +1,10 @@ -import { BadRequestException, Injectable, Logger, NotFoundException } from '@nestjs/common'; -import { DataSource, FindOptionsWhere, In } from 'typeorm'; +import { Injectable, Logger, NotFoundException } from '@nestjs/common'; +import { DataSource, FindOptionsWhere } from 'typeorm'; import { BookingsRepository } from '../bookings/bookings.repository'; import { DriversService } from '../drivers/drivers.service'; import { SmsClientService } from '../notifications/sms-client.service'; import { VehiclesService } from '../vehicles/vehicles.service'; -import { VehicleAvailability } from '../vehicles/entities/vehicle.entity'; import { CreateLastMileDto } from './dto/create-last-mile.dto'; import { UpdateLastMileDto } from './dto/update-last-mile.dto'; import { LastMile, LastMileStatus } from './entities/last-mile.entity'; @@ -72,6 +71,7 @@ export class LastMileService { return null; } + return this.create({ bookingId: booking.id, advancedPayment: 0, @@ -132,17 +132,7 @@ export class LastMileService { } async create(dto: CreateLastMileDto): Promise { -<<<<<<< HEAD - const record = await this.lastMileRepository.create({ -======= - const [existing] = await this.lastMileRepository.findAll({ - where: { bookingId: dto.bookingId }, - take: 1, - }); - if (existing) return existing; - return this.lastMileRepository.create({ ->>>>>>> 9d14414bf10079b38a04a709aa918c7e470dce34 bookingId: dto.bookingId, status: dto.status ?? 'READY_TO_TRANSIT', advancedPayment: dto.advancedPayment ?? 0, @@ -152,12 +142,6 @@ export class LastMileService { vehicleId: dto.vehicleId ?? null, paid: (dto as any).paid ?? false, }); - - if (dto.vehicleId) { - await this.vehiclesService.setAvailability(dto.vehicleId, VehicleAvailability.BUSY); - } - - return record; } @OnEvent("lastmile.invoice.paid") @@ -175,13 +159,6 @@ export class LastMileService { async update(id: string, dto: UpdateLastMileDto): Promise { const existing = await this.findById(id); - if ( - existing.status === 'DELIVERED' && - (dto.vehicleId !== undefined || dto.exactKm !== undefined) - ) { - throw new BadRequestException('Delivered records cannot be reassigned or have distance changed'); - } - const dtoAny = dto as any; const updated = await this.lastMileRepository.update(id, { ...(dto.bookingId !== undefined ? { bookingId: dto.bookingId } : {}), @@ -198,46 +175,14 @@ export class LastMileService { throw new NotFoundException(`Last-mile record ${id} not found`); } - // Keep vehicle statuses in sync: new vehicle goes BUSY, replaced one goes back to FREE - if (dto.vehicleId !== undefined && dto.vehicleId !== existing.vehicleId) { - if (dto.vehicleId) { - await this.vehiclesService.setAvailability(dto.vehicleId, VehicleAvailability.BUSY); - } - if (existing.vehicleId) { - await this.vehiclesService.releaseIfUnused([existing.vehicleId]); - } - } - // Notify assigned driver on every explicit vehicle assignment or reassignment if (dto.vehicleId) { void this.notifyDriverAssignment(dto.vehicleId, existing); } - // Trip finished — release the vehicles it was holding - if (dto.status === 'DELIVERED' && existing.status !== 'DELIVERED') { - await this.releaseVehicles(updated); - } - return updated; } - /** - * Free every vehicle held by this record (direct assignment + container - * allocations), unless still in use by another active trip. - */ - private async releaseVehicles(record: LastMile): Promise { - 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); - } - await this.vehiclesService.releaseIfUnused(vehicleIds); - } - private async notifyDriverAssignment(vehicleId: string, record: LastMile): Promise { try { const vehicle = await this.vehiclesService.findById(vehicleId); @@ -278,10 +223,7 @@ export class LastMileService { } async remove(id: string): Promise { - const existing = await this.findById(id); - if (existing.status === 'DELIVERED') { - throw new BadRequestException('Delivered records cannot be deleted'); - } + await this.findById(id); await this.lastMileRepository.softDelete(id); } @@ -294,16 +236,6 @@ export class LastMileService { throw new NotFoundException(`Last-mile record ${lastMileId} not found`); } - const previousAllocations = await this.dataSource.manager.find(LastMileContainerAllocation, { - where: { - lastMileId, - containerId: In(allocations.map((a) => a.containerId)), - }, - }); - const previousVehicleIds = previousAllocations - .map((a) => a.vehicleId) - .filter((id): id is string => Boolean(id)); - await this.dataSource.transaction(async (manager) => { for (const allocation of allocations) { await manager.delete(LastMileContainerAllocation, { @@ -320,14 +252,6 @@ export class LastMileService { } }); - const vehicleIds = new Set(allocations.map((a) => a.vehicleId)); - await Promise.all( - [...vehicleIds].map((vehicleId) => this.vehiclesService.setAvailability(vehicleId, VehicleAvailability.BUSY)), - ); - await this.vehiclesService.releaseIfUnused( - previousVehicleIds.filter((id) => !vehicleIds.has(id)), - ); - return { success: true, allocated: allocations.length,