import 'dotenv/config'; import { PrismaPg } from '@prisma/adapter-pg'; import { PrismaClient } from '../lib/generated/prisma/client'; const connectionString = process.env.DATABASE_URL; if (!connectionString) { throw new Error('DATABASE_URL is not set'); } const prisma = new PrismaClient({ adapter: new PrismaPg({ connectionString }) }); /** * Placeholder reference week, to be corrected from /admin/horaires on first use. * Wednesday carries a lunch break on purpose: it exercises the multi-slot path * of the resolver from the very first run, rather than leaving it untested * against real data until someone happens to need it. * * dayOfWeek follows JS getDay(): 0 = Sunday .. 6 = Saturday. */ const WEEK: { dayOfWeek: number; isClosed: boolean; slots: { open: string; close: string }[] }[] = [ { dayOfWeek: 0, isClosed: true, slots: [] }, { dayOfWeek: 1, isClosed: true, slots: [] }, { dayOfWeek: 2, isClosed: false, slots: [{ open: '10:00', close: '18:30' }] }, { dayOfWeek: 3, isClosed: false, slots: [ { open: '10:00', close: '13:00' }, { open: '14:00', close: '18:30' }, ], }, { dayOfWeek: 4, isClosed: false, slots: [{ open: '10:00', close: '18:30' }] }, { dayOfWeek: 5, isClosed: false, slots: [{ open: '10:00', close: '18:30' }] }, { dayOfWeek: 6, isClosed: false, slots: [{ open: '10:00', close: '18:30' }] }, ]; async function main(): Promise { // Every write below is an upsert: running the seed twice must leave the // database exactly as running it once did. const settings = await prisma.settings.upsert({ where: { id: 'singleton' }, update: {}, create: { id: 'singleton' }, }); for (const day of WEEK) { await prisma.weeklySchedule.upsert({ where: { dayOfWeek: day.dayOfWeek }, update: {}, create: { dayOfWeek: day.dayOfWeek, isClosed: day.isClosed, slots: day.slots }, }); } await prisma.syncState.upsert({ where: { key: 'holidays' }, update: {}, create: { key: 'holidays' }, }); const openDays = WEEK.filter((day) => !day.isClosed).length; console.log(`Seeded settings for "${settings.shopName}" and ${openDays} open days.`); console.log('Adjust the reference week from /admin/horaires.'); } main() .catch((error: unknown) => { console.error(error); process.exitCode = 1; }) .finally(() => { void prisma.$disconnect(); });