import { PrismaClient } from '../src/generated/prisma/client.js'; import { PrismaLibSql } from '@prisma/adapter-libsql'; import bcrypt from 'bcryptjs'; import { randomBytes } from 'crypto'; import path from 'path'; const dbPath = path.resolve(process.cwd(), 'prisma', 'lootah.db'); const adapter = new PrismaLibSql({ url: `file:${dbPath}` }); const prisma = new PrismaClient({ adapter } as ConstructorParameters[0]); async function main() { console.log('Seeding database...'); // Create default admin user const existingAdmin = await prisma.adminUser.findUnique({ where: { username: 'admin' }, }); if (!existingAdmin) { const passwordHash = await bcrypt.hash('admin123', 12); await prisma.adminUser.create({ data: { username: 'admin', passwordHash, }, }); console.log('✓ Created admin user (username: admin, password: admin123)'); console.log(' ⚠️ Change the password after first login!'); } else { console.log('✓ Admin user already exists, skipping.'); } // Generate and store JWT secret const existingSecret = await prisma.appSettings.findUnique({ where: { key: 'jwt_secret' }, }); if (!existingSecret) { const jwtSecret = randomBytes(64).toString('hex'); await prisma.appSettings.upsert({ where: { key: 'jwt_secret' }, update: { value: jwtSecret }, create: { key: 'jwt_secret', value: jwtSecret }, }); console.log('✓ Generated JWT secret and stored in database.'); } else { console.log('✓ JWT secret already exists, skipping.'); } // Seed default pricing items (idempotent — only if table is empty) const pricingCount = await prisma.pricingItem.count(); if (pricingCount === 0) { const defaultItems = [ { id: 'base', label: 'G1 Robot Base', price: 250000, sortOrder: 0 }, { id: 'emarati-kandura', label: 'Emarati Kandura', price: 15000, sortOrder: 1 }, { id: 'industrial-vest', label: 'Industrial Vest', price: 8500, sortOrder: 2 }, { id: 'business-suit', label: 'Business Suit', price: 12000, sortOrder: 3 }, { id: 'custom-color', label: 'Custom Color', price: 3500, sortOrder: 4 }, { id: 'robot-doctor', label: 'Robot Doctor', price: 5000, sortOrder: 5 }, { id: 'security-guard', label: 'Security Guard', price: 5000, sortOrder: 6 }, ]; for (const item of defaultItems) { await prisma.pricingItem.create({ data: item }); } console.log(`✓ Seeded ${defaultItems.length} default pricing items.`); } else { console.log(`✓ ${pricingCount} pricing items already exist, skipping.`); } console.log('Seeding complete!'); } main() .catch((e) => { console.error(e); process.exit(1); }) .finally(async () => { await prisma.$disconnect(); });