Files
edr-platform/apps/edr-freight-api/src/modules/cargoes/cargoes.service.ts

104 lines
3.4 KiB
TypeScript

import { Injectable, NotFoundException, ConflictException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { CreateCargoDto } from './dto/create-cargo.dto';
import { UpdateCargoDto } from './dto/update-cargo.dto';
import { LoadCargoDto } from './dto/load-cargo.dto';
import { DeliverCargoDto } from './dto/deliver-cargo.dto';
import { Cargo } from './entities/cargoes.entity';
import { Container } from '../container-management/entities/container.entity';
@Injectable()
export class CargoesService {
constructor(
@InjectRepository(Cargo)
private readonly cargoRepo: Repository<Cargo>,
@InjectRepository(Container)
private readonly containerRepo: Repository<Container>,
) {}
async create(dto: CreateCargoDto): Promise<Cargo> {
const cargo = this.cargoRepo.create(dto);
return this.cargoRepo.save(cargo);
}
async findAll(): Promise<Cargo[]> {
return this.cargoRepo.find({ order: { cargoReference: 'ASC' } });
}
async findById(id: string): Promise<Cargo> {
const cargo = await this.cargoRepo.findOne({ where: { id } });
if (!cargo) throw new NotFoundException(`Cargo ${id} not found`);
return cargo;
}
async update(id: string, dto: UpdateCargoDto): Promise<Cargo> {
const cargo = await this.findById(id);
Object.assign(cargo, dto);
return this.cargoRepo.save(cargo);
}
async remove(id: string): Promise<void> {
const cargo = await this.findById(id);
await this.cargoRepo.remove(cargo);
}
async loadCargo(id: string, dto: LoadCargoDto): Promise<Cargo> {
const cargo = await this.cargoRepo.findOne({
where: { id },
relations: { container: true }, // ✅ fixed
});
if (!cargo) throw new NotFoundException('Cargo not found');
if (cargo.status !== 'PENDING') {
throw new ConflictException('Cargo already loaded or delivered');
}
cargo.status = 'LOADED';
cargo.loadedAt = new Date();
cargo.quantity = dto.quantity;
cargo.weight = dto.weight;
cargo.volume = dto.volume ?? null;
if (dto.description) cargo.description = dto.description;
if (cargo.container) {
cargo.container.status = 'LOADED';
await this.containerRepo.save(cargo.container);
}
return this.cargoRepo.save(cargo);
}
async unloadCargo(id: string): Promise<Cargo> {
const cargo = await this.findById(id);
if (cargo.status !== 'LOADED') {
throw new ConflictException('Cargo is not loaded');
}
cargo.status = 'UNLOADED';
cargo.unloadedAt = new Date();
return this.cargoRepo.save(cargo);
}
async deliverCargo(id: string, dto?: DeliverCargoDto): Promise<Cargo> {
const cargo = await this.cargoRepo.findOne({
where: { id },
relations: { container: true }, // ✅ fixed
});
if (!cargo) throw new NotFoundException('Cargo not found');
if (cargo.status !== 'LOADED') {
throw new ConflictException('Only loaded cargo can be delivered');
}
cargo.status = 'DELIVERED';
if (dto?.deliveryRemarks) cargo.description = dto.deliveryRemarks;
const remaining = await this.cargoRepo.count({
where: { containerId: cargo.containerId, status: 'LOADED' },
});
if (remaining === 0 && cargo.container) {
cargo.container.status = 'AVAILABLE';
await this.containerRepo.save(cargo.container);
}
return this.cargoRepo.save(cargo);
}
}