mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-08-28 19:30:57 +00:00
fix issue
This commit is contained in:
@@ -1,8 +1,15 @@
|
||||
import { BadRequestException, Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { DataSource } from 'typeorm';
|
||||
import {
|
||||
BadRequestException,
|
||||
ConflictException,
|
||||
Injectable,
|
||||
NotFoundException,
|
||||
} from '@nestjs/common';
|
||||
import { TrainScheduleStatus } from '@edr/types';
|
||||
import { DataSource, In } from 'typeorm';
|
||||
|
||||
import { deriveTradeDirection } from '../../common/derive-trade-direction.util';
|
||||
import { Yard } from '../rule-engine/entities/yard.entity';
|
||||
import { TrainSchedule } from '../train-schedules/entities/train-schedule.entity';
|
||||
import { CreateRouteDto } from './dto/create-route.dto';
|
||||
import { FilterRoutesDto } from './dto/filter-routes.dto';
|
||||
import { UpdateRouteDto } from './dto/update-route.dto';
|
||||
@@ -112,6 +119,30 @@ export class RoutesService {
|
||||
? await this.validateMilestones(dto.milestones)
|
||||
: null;
|
||||
|
||||
// Milestones or endpoints are about to be rewritten — reject if any
|
||||
// non-terminal schedule still references this route, otherwise its stop list
|
||||
// and distances would silently shift under a live plan. Status-only /
|
||||
// label-only edits (no milestones supplied) are always allowed.
|
||||
if (milestoneInput) {
|
||||
const activeSchedules = await this.dataSource
|
||||
.getRepository(TrainSchedule)
|
||||
.count({
|
||||
where: {
|
||||
routeId: id,
|
||||
status: In([
|
||||
TrainScheduleStatus.Draft,
|
||||
TrainScheduleStatus.Scheduled,
|
||||
TrainScheduleStatus.Dispatched,
|
||||
]),
|
||||
},
|
||||
});
|
||||
if (activeSchedules > 0) {
|
||||
throw new ConflictException(
|
||||
'This route is used by active train schedules and its stops cannot be changed. Create a new route instead.',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
await this.dataSource.transaction(async (manager) => {
|
||||
await manager.getRepository(Route).update(id, {
|
||||
originYardId: milestoneInput?.originYardId ?? existing.originYardId,
|
||||
|
||||
Reference in New Issue
Block a user