trains, wagons,containers and cargoes schema and API

This commit is contained in:
hagiye
2026-06-04 15:56:29 +03:00
parent 18015ceaff
commit 71ac89edc7
66 changed files with 2077 additions and 90 deletions

View File

@@ -0,0 +1,70 @@
import {
Body,
Controller,
Delete,
Get,
Param,
ParseUUIDPipe,
Patch,
Post,
} from '@nestjs/common';
import { ApiOperation, ApiTags } from '@nestjs/swagger';
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 { CargoesService } from './cargoes.service';
@ApiTags('cargoes')
@Controller('cargoes')
export class CargoesController {
constructor(private readonly cargoesService: CargoesService) {}
@Post()
@ApiOperation({ summary: 'Create a new cargo' })
create(@Body() dto: CreateCargoDto) {
return this.cargoesService.create(dto);
}
@Get()
@ApiOperation({ summary: 'List all cargoes' })
findAll() {
return this.cargoesService.findAll();
}
@Get(':id')
@ApiOperation({ summary: 'Get a cargo by ID' })
findOne(@Param('id', ParseUUIDPipe) id: string) {
return this.cargoesService.findById(id);
}
@Patch(':id')
@ApiOperation({ summary: 'Update a cargo' })
update(@Param('id', ParseUUIDPipe) id: string, @Body() dto: UpdateCargoDto) {
return this.cargoesService.update(id, dto);
}
@Delete(':id')
@ApiOperation({ summary: 'Delete a cargo' })
remove(@Param('id', ParseUUIDPipe) id: string) {
return this.cargoesService.remove(id);
}
@Post(':id/load')
@ApiOperation({ summary: 'Load cargo into a container' })
load(@Param('id', ParseUUIDPipe) id: string, @Body() dto: LoadCargoDto) {
return this.cargoesService.loadCargo(id, dto);
}
@Post(':id/unload')
@ApiOperation({ summary: 'Unload cargo from container' })
unload(@Param('id', ParseUUIDPipe) id: string) {
return this.cargoesService.unloadCargo(id);
}
@Post(':id/deliver')
@ApiOperation({ summary: 'Mark cargo as delivered' })
deliver(@Param('id', ParseUUIDPipe) id: string, @Body() dto?: DeliverCargoDto) {
return this.cargoesService.deliverCargo(id, dto);
}
}

View File

@@ -0,0 +1,14 @@
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Cargo } from './entities/cargoes.entity';
import { Container } from '../container-management/entities/container.entity';
import { CargoesController } from './cargoes.controller';
import { CargoesService } from './cargoes.service';
@Module({
imports: [TypeOrmModule.forFeature([Cargo, Container])],
controllers: [CargoesController],
providers: [CargoesService],
exports: [CargoesService],
})
export class CargoesModule {}

View File

@@ -0,0 +1,15 @@
import { BaseRepository } from '@edr/api-common';
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Cargo } from './entities/cargoes.entity';
@Injectable()
export class CargoesRepository extends BaseRepository<Cargo> {
constructor(
@InjectRepository(Cargo)
repository: Repository<Cargo>,
) {
super(repository);
}
}

View File

@@ -0,0 +1,104 @@
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);
}
}

View File

@@ -0,0 +1,45 @@
import { IsString, IsUUID, IsOptional, IsNumber, Min, IsIn, IsDateString } from 'class-validator';
export class CreateCargoDto {
@IsString()
cargoReference!: string;
@IsUUID()
shipmentId!: string;
@IsUUID()
containerId!: string;
@IsOptional()
@IsUUID()
cargoTypeId?: string;
@IsOptional()
@IsString()
description?: string;
@IsNumber()
@Min(0.001)
quantity!: number;
@IsNumber()
@Min(0)
weight!: number;
@IsOptional()
@IsNumber()
@Min(0)
volume?: number;
@IsOptional()
@IsIn(['PENDING', 'LOADED', 'IN_TRANSIT', 'DELIVERED', 'UNLOADED'])
status?: string;
@IsOptional()
@IsDateString()
loadedAt?: string;
@IsOptional()
@IsDateString()
unloadedAt?: string;
}

View File

@@ -0,0 +1,7 @@
import { IsOptional, IsString } from 'class-validator';
export class DeliverCargoDto {
@IsOptional()
@IsString()
deliveryRemarks?: string;
}

View File

@@ -0,0 +1,20 @@
import { IsNumber, Min, IsOptional, IsString } from 'class-validator';
export class LoadCargoDto {
@IsNumber()
@Min(0.001)
quantity!: number;
@IsNumber()
@Min(0)
weight!: number;
@IsOptional()
@IsNumber()
@Min(0)
volume?: number;
@IsOptional()
@IsString()
description?: string;
}

View File

@@ -0,0 +1,4 @@
import { PartialType } from '@nestjs/swagger';
import { CreateCargoDto } from './create-cargo.dto';
export class UpdateCargoDto extends PartialType(CreateCargoDto) {}

View File

@@ -0,0 +1,45 @@
// apps/edr-freight-api/src/modules/cargoes/entities/cargo.entity.ts
import { Entity, Column, ManyToOne, JoinColumn } from 'typeorm';
import { BaseEntity } from '@edr/api-common';
import { Container } from '../../container-management/entities/container.entity';
@Entity({ name: 'cargoes', schema: 'freight' })
export class Cargo extends BaseEntity {
@Column({ unique: true, name: 'cargo_reference' })
cargoReference!: string;
@Column({ name: 'shipment_id', type: 'uuid' })
shipmentId!: string;
@Column({ name: 'container_id', type: 'uuid' })
containerId!: string;
@Column({ name: 'cargo_type_id', type: 'uuid', nullable: true })
cargoTypeId!: string | null; // optional link to cargo_types table
@Column({ type: 'text', nullable: true })
description!: string | null;
@Column({ type: 'decimal', precision: 12, scale: 3 })
quantity!: number;
@Column({ type: 'decimal', precision: 10, scale: 2 })
weight!: number; // kg
@Column({ type: 'decimal', precision: 10, scale: 2, nullable: true })
volume!: number | null; // m³
@Column({ type: 'varchar', default: 'PENDING' })
status!: string; // PENDING, LOADED, IN_TRANSIT, DELIVERED, UNLOADED
@Column({ name: 'loaded_at', type: 'timestamp', nullable: true })
loadedAt!: Date | null;
@Column({ name: 'unloaded_at', type: 'timestamp', nullable: true })
unloadedAt!: Date | null;
// Relationship to Container
@ManyToOne(() => Container, (container) => container.cargoes, { onDelete: 'RESTRICT' })
@JoinColumn({ name: 'container_id' })
container!: Container;
}