From 310c0a0bc81d6ee7fdf9a8d09603d4485c5d0ac1 Mon Sep 17 00:00:00 2001 From: Abubeker Yasin Date: Wed, 10 Jun 2026 11:04:57 +0300 Subject: [PATCH] Update seed.ts --- apps/edr-passenger-api/prisma/seed.ts | 48 ++++++++++++++------------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/apps/edr-passenger-api/prisma/seed.ts b/apps/edr-passenger-api/prisma/seed.ts index 9baaf6433..3a9402126 100644 --- a/apps/edr-passenger-api/prisma/seed.ts +++ b/apps/edr-passenger-api/prisma/seed.ts @@ -1,4 +1,4 @@ -import { PrismaClient } from '@prisma/client'; +import { Prisma, PrismaClient } from '@prisma/client'; import * as bcrypt from 'bcrypt'; import { v4 as uuidv4 } from 'uuid'; @@ -224,34 +224,36 @@ async function seedCoaches() { 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; for (let row = 1; row <= Math.ceil(coach.capacity / 2); row++) { for (const col of ['A', 'B', 'C', 'D']) { - if (seatIndex <= coach.capacity) { - let bedPosition: string | null = null; - if (c.coachTypeId === ecoBedCoachType!.id || c.coachTypeId === vipBedCoachType!.id) { - if (row % 3 === 1) bedPosition = 'upper'; - else if (row % 3 === 2) bedPosition = 'middle'; - 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++; + if (seatIndex > coach.capacity) break; + let bedPosition: string | null = null; + if (c.coachTypeId === ecoBedCoachType!.id || c.coachTypeId === vipBedCoachType!.id) { + if (row % 3 === 1) bedPosition = 'upper'; + else if (row % 3 === 2) bedPosition = 'middle'; + else bedPosition = 'lower'; } + + 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; } console.log(` ✅ ${coaches.length} coaches with ${totalSeats} seats created`);