const { PrismaClient } = require('@prisma/client'); const bcrypt = require('bcrypt'); const { v4: uuidv4 } = require('uuid'); const prisma = new PrismaClient(); const args = process.argv.slice(2).filter((arg) => arg !== '--force'); const FORCE = process.argv.includes('--force'); const VENUE_NAME = process.env.SETUP_VENUE_NAME || args[0] || 'My Venue'; const ADMIN_EMAIL = process.env.SETUP_ADMIN_EMAIL || args[1]; const ADMIN_PASSWORD = process.env.SETUP_ADMIN_PASSWORD || args[2]; async function main() { console.log('=== KoolTap Production Setup ===\n'); const venues = await prisma.venue.count(); if (venues > 0 && !FORCE) { console.log(`The database already has ${venues} venue(s); nothing to do. Pass --force to wipe ALL data and start over.`); return; } if (!ADMIN_EMAIL || !ADMIN_PASSWORD) { throw new Error('Usage: node prisma/setup.js "" [--force]'); } if (ADMIN_PASSWORD.length < 10) { throw new Error('The admin password must be at least 10 characters'); } const venueId = uuidv4(); const hash = await bcrypt.hash(ADMIN_PASSWORD, 10); await prisma.$transaction(async (tx) => { await tx.auditLog.deleteMany(); await tx.tabReminder.deleteMany(); await tx.plannedTab.deleteMany(); await tx.remoteCommand.deleteMany(); await tx.openTabSnapshot.deleteMany(); await tx.tabOwnership.deleteMany(); await tx.refreshToken.deleteMany(); await tx.registrationCode.deleteMany(); await tx.device.deleteMany(); await tx.clientUser.deleteMany(); await tx.venue.deleteMany(); await tx.venue.create({ data: { id: venueId, name: VENUE_NAME } }); await tx.clientUser.create({ data: { id: uuidv4(), venueId, email: ADMIN_EMAIL, passwordHash: hash, name: 'Admin', role: 'admin', }, }); }); console.log('=== Setup complete ==='); console.log(` Venue: ${VENUE_NAME}`); console.log(` Venue ID: ${venueId}`); console.log(` Admin: ${ADMIN_EMAIL}`); console.log('\nLog in to the dashboard with the admin credentials you provided.'); } main() .catch((err) => { console.error('Setup failed:', err.message ?? err); process.exitCode = 1; }) .finally(() => prisma.$disconnect());