Import operation gate pass

This commit is contained in:
hagiye
2026-06-27 21:22:18 +03:00
parent 19e395abf6
commit 2a038c59db
2 changed files with 127 additions and 0 deletions

View File

@@ -21,6 +21,7 @@
"seed:export-djibouti-interchange-demo": "ts-node -r tsconfig-paths/register src/scripts/seed-export-djibouti-interchange-demo.ts",
"seed:import-djibouti-demo": "ts-node -r tsconfig-paths/register src/scripts/seed-import-djibouti-demo.ts",
"seed:approved-first-lastmile-demo-bookings": "ts-node -r tsconfig-paths/register src/scripts/seed-approved-first-lastmile-demo-bookings.ts",
"seed:negad-indode-arrived-train": "ts-node -r tsconfig-paths/register src/scripts/seed-negad-indode-arrived-train.ts",
"seed:file-upload-settings": "ts-node -r tsconfig-paths/register src/scripts/seed-file-upload-settings.ts",
"seed:fleet-wagons": "bash ../../../docs/new/seeds/seed-fleet-wagons.sh",
"iam:typeorm:cli": "cross-env MIGRATIONS_DIR=node_modules/@tria-plc/iamapi-common/dist/db/migrations/*.{ts,js} ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js -d ./node_modules/@tria-plc/api-common/dist/modules/typeorm/typeorm.config.js",

View File

@@ -0,0 +1,126 @@
import 'reflect-metadata';
import { config } from 'dotenv';
import { resolve } from 'path';
config({ path: resolve(__dirname, '../../.env') });
import { AppDataSource } from '../data-source';
import { Locomotive } from '../modules/locomotives/entities/locomotive.entity';
import { Yard } from '../modules/rule-engine/entities/yard.entity';
import { TrainSchedule } from '../modules/train-schedules/entities/train-schedule.entity';
import { TrainSet } from '../modules/train-sets/entities/train-set.entity';
const TRAIN_NUMBER = 'NEGAD-INDODE-ARR-01';
function addHours(date: Date, hours: number): Date {
return new Date(date.getTime() + hours * 60 * 60 * 1000);
}
async function main() {
const dataSource = await AppDataSource.initialize();
try {
await dataSource.transaction(async (manager) => {
const yardRepo = manager.getRepository(Yard);
const locomotiveRepo = manager.getRepository(Locomotive);
const trainSetRepo = manager.getRepository(TrainSet);
const scheduleRepo = manager.getRepository(TrainSchedule);
const negad =
(await yardRepo.findOne({ where: { code: 'NEGAD' } })) ??
(await yardRepo.save(
yardRepo.create({
code: 'NEGAD',
label: 'Negad',
country: 'Djibouti',
isActive: true,
displayOrder: 5,
}),
));
const indode =
(await yardRepo.findOne({ where: { code: 'INDODE' } })) ??
(await yardRepo.save(
yardRepo.create({
code: 'INDODE',
label: 'Indode Dry Port',
country: 'Ethiopia',
isActive: true,
displayOrder: 6,
}),
));
const locomotive =
(await locomotiveRepo.findOne({ where: { code: 'NEGAD-INDODE-LOCO' } })) ??
(await locomotiveRepo.save(
locomotiveRepo.create({
code: 'NEGAD-INDODE-LOCO',
name: 'Negad to Indode Demo Locomotive',
locomotiveType: 'DIESEL',
maxPullWeightTons: 4200,
maxTrainLengthMeters: 760,
status: 'AVAILABLE',
currentYardId: indode.id,
}),
));
const now = new Date();
const departure = addHours(now, -12);
const arrival = now;
let schedule = await scheduleRepo.findOne({ where: { trainNumber: TRAIN_NUMBER } });
if (!schedule) {
const trainSet = await trainSetRepo.save(
trainSetRepo.create({
locomotiveId: locomotive.id,
totalWeightTons: 960,
totalLengthMeters: 420,
wagonCount: 18,
status: 'COMPLETED',
}),
);
schedule = scheduleRepo.create({
trainSetId: trainSet.id,
originStationId: negad.id,
destinationStationId: indode.id,
scheduledDepartureDate: departure,
scheduledArrivalDate: arrival,
actualDepartureAt: departure,
actualArrivalAt: arrival,
status: 'ARRIVED' as TrainSchedule['status'],
trainNumber: TRAIN_NUMBER,
direction: 'IMPORT',
maxWagons: 53,
bookingWindowStatus: 'CLOSED',
});
} else {
schedule.originStationId = negad.id;
schedule.destinationStationId = indode.id;
schedule.scheduledDepartureDate = departure;
schedule.scheduledArrivalDate = arrival;
schedule.actualDepartureAt = departure;
schedule.actualArrivalAt = arrival;
schedule.status = 'ARRIVED' as TrainSchedule['status'];
schedule.direction = 'IMPORT';
schedule.bookingWindowStatus = 'CLOSED';
if (schedule.trainSetId) {
await trainSetRepo.update(schedule.trainSetId, { status: 'COMPLETED' });
}
}
const saved = await scheduleRepo.save(schedule);
console.log(`Seeded ARRIVED train ${TRAIN_NUMBER}`);
console.log(`Schedule ID: ${saved.id}`);
console.log(`Route: ${negad.code} -> ${indode.code}`);
});
} finally {
await dataSource.destroy();
}
}
main().catch((err) => {
console.error('Negad to Indode arrived train seed failed:', err);
process.exit(1);
});