import dotenv from 'dotenv'; dotenv.config(); import prisma from './src/utils/db'; import bcrypt from 'bcrypt'; async function seed() { console.log('Starting database seeding...'); // 1. Seed Active Legal Documents let ndaDoc = await prisma.legalDocument.findFirst({ where: { type: 'NDA', version: '1.0' } }); if (!ndaDoc) { ndaDoc = await prisma.legalDocument.create({ data: { type: 'NDA', version: '1.0', content: 'This is the standard Non-Disclosure Agreement content. By signing this document, you agree to keep all technical and business materials confidential.', isActive: true, } }); console.log('Seeded NDA Document.'); } else { console.log('NDA Document already exists.'); } let msaDoc = await prisma.legalDocument.findFirst({ where: { type: 'MSA', version: '1.0' } }); if (!msaDoc) { msaDoc = await prisma.legalDocument.create({ data: { type: 'MSA', version: '1.0', content: 'This is the standard Master Services Agreement content. It defines the core relationship, deliverables, and service levels between Tech4Biz and the partner.', isActive: true, } }); console.log('Seeded MSA Document.'); } else { console.log('MSA Document already exists.'); } // 2. Hash Password const passwordHash = await bcrypt.hash('Password123', 10); // Create default Partner organization let partnerOrg = await prisma.organization.findFirst({ where: { name: 'PARTNER' } }); if (!partnerOrg) { partnerOrg = await prisma.organization.create({ data: { name: 'PARTNER' } }); console.log('Seeded PARTNER organization.'); } // 3. Seed Admin const adminEmail = 'admin@tech4biz.com'; let adminUser = await prisma.user.findUnique({ where: { email: adminEmail } }); if (!adminUser) { adminUser = await prisma.user.create({ data: { email: adminEmail, passwordHash, role: 'ADMIN', onboardingStatus: 'APPROVED', mfaEnabled: false, } }); console.log(`Seeded Admin User: ${adminEmail}`); } else { console.log(`Admin User already exists: ${adminEmail}`); } // 4. Seed Partner: Fully active & approved const activeEmail = 'active-partner@partner.com'; let activeUser = await prisma.user.findUnique({ where: { email: activeEmail } }); if (!activeUser) { activeUser = await prisma.user.create({ data: { email: activeEmail, passwordHash, role: 'PARTNER_USER', onboardingStatus: 'APPROVED', mfaEnabled: false, organizationId: partnerOrg.id } }); console.log(`Seeded Partner (Approved): ${activeEmail}`); // Seed LegalAcceptance for NDA await prisma.legalAcceptance.create({ data: { docId: ndaDoc.id, userId: activeUser.id, ipAddress: '127.0.0.1', signatureHash: 'sha256-dummyndaacceptactive987654321', acceptedAt: new Date() } }); // Seed LegalAcceptance for MSA await prisma.legalAcceptance.create({ data: { docId: msaDoc.id, userId: activeUser.id, ipAddress: '127.0.0.1', signatureHash: 'sha256-dummymsaacceptactive987654321', acceptedAt: new Date() } }); console.log('Seeded Legal Acceptances for NDA/MSA for active partner.'); } else { console.log(`Partner (Approved) already exists: ${activeEmail}`); } console.log('Seeding completed successfully.'); } seed() .catch((err) => { console.error('Seeding failed:', err); process.exit(1); }) .finally(() => prisma.$disconnect());