Update seed.ts

This commit is contained in:
Abubeker Yasin
2026-06-10 11:04:57 +03:00
parent ceb7f17e88
commit 310c0a0bc8

View File

@@ -1,4 +1,4 @@
import { PrismaClient } from '@prisma/client'; import { Prisma, PrismaClient } from '@prisma/client';
import * as bcrypt from 'bcrypt'; import * as bcrypt from 'bcrypt';
import { v4 as uuidv4 } from 'uuid'; import { v4 as uuidv4 } from 'uuid';
@@ -224,34 +224,36 @@ async function seedCoaches() {
create: coach, create: coach,
}); });
// Rebuild the coach's seats from scratch. A plain upsert keyed on
// coachId_seatNumber can't reconcile a changed layout (it's blind to the
// @@unique([coachId, row, col]) constraint), so stale row/col data collides.
await prisma.seat.deleteMany({ where: { coachId: c.id } });
const seats: Prisma.SeatCreateManyInput[] = [];
let seatIndex = 1; let seatIndex = 1;
for (let row = 1; row <= Math.ceil(coach.capacity / 2); row++) { for (let row = 1; row <= Math.ceil(coach.capacity / 2); row++) {
for (const col of ['A', 'B', 'C', 'D']) { for (const col of ['A', 'B', 'C', 'D']) {
if (seatIndex <= coach.capacity) { if (seatIndex > coach.capacity) break;
let bedPosition: string | null = null; let bedPosition: string | null = null;
if (c.coachTypeId === ecoBedCoachType!.id || c.coachTypeId === vipBedCoachType!.id) { if (c.coachTypeId === ecoBedCoachType!.id || c.coachTypeId === vipBedCoachType!.id) {
if (row % 3 === 1) bedPosition = 'upper'; if (row % 3 === 1) bedPosition = 'upper';
else if (row % 3 === 2) bedPosition = 'middle'; else if (row % 3 === 2) bedPosition = 'middle';
else bedPosition = 'lower'; else bedPosition = 'lower';
}
await prisma.seat.upsert({
where: { coachId_seatNumber: { coachId: c.id, seatNumber: seatIndex.toString() } },
update: { bedPosition },
create: {
coachId: c.id,
seatNumber: seatIndex.toString(),
row,
col,
isWindow: col === 'A' || col === 'D',
isAisle: col === 'B' || col === 'C',
bedPosition,
},
});
seatIndex++;
} }
seats.push({
coachId: c.id,
seatNumber: seatIndex.toString(),
row,
col,
isWindow: col === 'A' || col === 'D',
isAisle: col === 'B' || col === 'C',
bedPosition,
});
seatIndex++;
} }
} }
await prisma.seat.createMany({ data: seats });
totalSeats += coach.capacity; totalSeats += coach.capacity;
} }
console.log(`${coaches.length} coaches with ${totalSeats} seats created`); console.log(`${coaches.length} coaches with ${totalSeats} seats created`);