feat: complete train scheduling integration

This commit is contained in:
hagiye
2026-06-06 05:52:47 +03:00
parent 29247d4140
commit 788de5381e
56 changed files with 1191 additions and 141 deletions

View File

@@ -3,12 +3,13 @@ import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Container } from './entities/container.entity';
import { Wagon } from '../wagons/entities/wagon.entity';
import { ContainerType } from '../rule-engine/entities/container-type.entity';
import { ContainersController } from './containers.controller';
import { ContainersService } from './containers.service';
@Module({
imports: [TypeOrmModule.forFeature([Container, Wagon])], // ✅ add Wagon
imports: [TypeOrmModule.forFeature([Container, Wagon, ContainerType])],
controllers: [ContainersController],
providers: [ContainersService],
})
export class ContainersModule {}
export class ContainersModule {}

View File

@@ -7,6 +7,7 @@ import { UpdateContainerDto } from './dto/update-container.dto';
import { AssignContainerToWagonDto } from './dto/assign-container-to-wagon.dto';
import { Container } from './entities/container.entity';
import { Wagon } from '../wagons/entities/wagon.entity';
import { ContainerType } from '../rule-engine/entities/container-type.entity';
@Injectable()
export class ContainersService {
@@ -15,9 +16,30 @@ export class ContainersService {
private readonly containerRepo: Repository<Container>,
@InjectRepository(Wagon)
private readonly wagonRepo: Repository<Wagon>, // ✅ use raw repository
@InjectRepository(ContainerType)
private readonly containerTypeRepo: Repository<ContainerType>,
) {}
async create(dto: CreateContainerDto): Promise<Container> {
const existing = await this.containerRepo.findOne({
where: { containerNumber: dto.containerNumber },
});
if (existing) {
throw new ConflictException(`Container number "${dto.containerNumber}" already exists`);
}
const containerType = await this.containerTypeRepo.findOne({
where: { id: dto.containerTypeId, isActive: true },
});
if (!containerType) {
throw new NotFoundException(`Container type ${dto.containerTypeId} not found`);
}
if (dto.wagonId) {
const wagon = await this.wagonRepo.findOne({ where: { id: dto.wagonId } });
if (!wagon) throw new NotFoundException(`Wagon ${dto.wagonId} not found`);
}
const container = this.containerRepo.create(dto);
if (dto.wagonId === undefined) container.wagonId = null;
if (dto.position === undefined) container.position = null;
@@ -59,6 +81,26 @@ export class ContainersService {
async update(id: string, dto: UpdateContainerDto): Promise<Container> {
const container = await this.findById(id);
if (dto.containerNumber && dto.containerNumber !== container.containerNumber) {
const existing = await this.containerRepo.findOne({
where: { containerNumber: dto.containerNumber },
});
if (existing) {
throw new ConflictException(`Container number "${dto.containerNumber}" already exists`);
}
}
if (dto.containerTypeId) {
const containerType = await this.containerTypeRepo.findOne({
where: { id: dto.containerTypeId, isActive: true },
});
if (!containerType) {
throw new NotFoundException(`Container type ${dto.containerTypeId} not found`);
}
}
if (dto.wagonId) {
const wagon = await this.wagonRepo.findOne({ where: { id: dto.wagonId } });
if (!wagon) throw new NotFoundException(`Wagon ${dto.wagonId} not found`);
}
Object.assign(container, dto);
return this.containerRepo.save(container);
}