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); });