import { PrismaClient, RoomType, SectionType } from '@prisma/client'; import * as bcrypt from 'bcrypt'; const prisma = new PrismaClient(); async function main() { console.log('Seeding database...'); // Seed Roles const rolesData = [ { name: 'Facility Owner', permissions: { admin: true }, isSystem: true }, { name: 'Manager', permissions: { users: { manage: true }, tasks: { manage: true }, inventory: { manage: true } }, isSystem: true }, { name: 'Grower', permissions: { tasks: { view: true, complete: true }, inventory: { view: true } }, isSystem: true }, { name: 'Worker', permissions: { tasks: { view: true, complete: true } }, isSystem: true } ]; for (const r of rolesData) { const existing = await prisma.role.findUnique({ where: { name: r.name } }); if (!existing) { await prisma.role.create({ data: r }); console.log(`Created Role: ${r.name}`); } } const ownerRole = await prisma.role.findUnique({ where: { name: 'Facility Owner' } }); // Create Owner const ownerEmail = 'admin@runfoo.run'; const existingOwner = await prisma.user.findUnique({ where: { email: ownerEmail } }); if (!existingOwner) { await prisma.user.create({ data: { email: ownerEmail, passwordHash: await bcrypt.hash('password123', 10), name: 'Travis', role: 'OWNER', // Enum fallback roleId: ownerRole?.id, rate: 100.00 } }); console.log('Created Owner: Travis (admin@runfoo.run)'); } else { // Always ensure password is properly hashed and role is set await prisma.user.update({ where: { email: ownerEmail }, data: { passwordHash: await bcrypt.hash('password123', 10), roleId: ownerRole?.id || existingOwner.roleId, name: 'Travis' } }); console.log('Updated Owner password hash'); } // Create Default Supplies const supplies = [ { name: 'Nitrile Gloves (L)', category: 'PPE', quantity: 5, minThreshold: 10, unit: 'box', location: 'Ante Room', vendor: 'Uline', productUrl: 'https://uline.com' }, { name: 'Hypochlorous Acid', category: 'CLEANING', quantity: 12, minThreshold: 5, unit: 'gallon', location: 'Janitor Clonet', vendor: 'Athena' }, { name: 'Rockwool Cubes 4"', category: 'OTHER', quantity: 450, minThreshold: 200, unit: 'cube', location: 'Veg Storage', vendor: 'GrowGen' }, { name: 'Trimmers (Fiskars)', category: 'MAINTENANCE', quantity: 15, minThreshold: 15, unit: 'pair', location: 'Trim Jail', vendor: 'Amazon' } ]; for (const s of supplies) { const existing = await prisma.supplyItem.findFirst({ where: { name: s.name } }); if (!existing) { await prisma.supplyItem.create({ data: { ...s, category: s.category as any // Type assertion for Enum } }); console.log(`Created Supply: ${s.name}`); } } // ============================================ // FACILITY 3D LAYOUT (New) // ============================================ // 1. Property let property = await prisma.facilityProperty.findFirst(); if (!property) { property = await prisma.facilityProperty.create({ data: { name: 'Wolfpack Facility', address: '123 Grow St', licenseNum: 'CML-123456' } }); console.log('Created Facility Property'); } // 2. Building let building = await prisma.facilityBuilding.findFirst({ where: { propertyId: property.id } }); if (!building) { building = await prisma.facilityBuilding.create({ data: { propertyId: property.id, name: 'Main Building', code: 'MAIN', type: 'CULTIVATION' } }); console.log('Created Facility Building'); } // 3. Floor let floor = await prisma.facilityFloor.findFirst({ where: { buildingId: building.id } }); if (!floor) { floor = await prisma.facilityFloor.create({ data: { buildingId: building.id, name: 'Ground Floor', number: 1, width: 100, // 100x100 grid height: 100 } }); console.log('Created Facility Floor'); } // 4. Rooms (Spatial) const spatialRooms = [ { name: 'Veg Room', code: 'VEG', type: RoomType.VEG, posX: 5, posY: 5, width: 30, height: 40, color: '#4ade80', sections: [ { name: 'Rack A', code: 'A', rows: 4, columns: 8, spacing: 2, posX: 2, posY: 2 }, { name: 'Rack B', code: 'B', rows: 4, columns: 8, spacing: 2, posX: 15, posY: 2 } ] }, { name: 'Flower Room', code: 'FLO', type: RoomType.FLOWER, posX: 40, posY: 5, width: 50, height: 60, color: '#a855f7', sections: [ { name: 'Bench 1', code: 'B1', rows: 10, columns: 4, spacing: 2, posX: 5, posY: 5 }, { name: 'Bench 2', code: 'B2', rows: 10, columns: 4, spacing: 2, posX: 20, posY: 5 }, { name: 'Bench 3', code: 'B3', rows: 10, columns: 4, spacing: 2, posX: 35, posY: 5 } ] }, { name: 'Dry Room', code: 'DRY', type: RoomType.DRY, posX: 5, posY: 50, width: 25, height: 25, color: '#f59e0b', sections: [ { name: 'Hangers', code: 'H1', rows: 2, columns: 10, spacing: 1, posX: 2, posY: 2 } ] } ]; for (const r of spatialRooms) { let room = await prisma.facilityRoom.findFirst({ where: { floorId: floor.id, code: r.code } }); if (!room) { room = await prisma.facilityRoom.create({ data: { floorId: floor.id, name: r.name, code: r.code, type: r.type, posX: r.posX, posY: r.posY, width: r.width, height: r.height, color: r.color } }); console.log(`Created Spatial Room: ${r.name}`); } else { await prisma.facilityRoom.update({ where: { id: room.id }, data: { posX: r.posX, posY: r.posY, width: r.width, height: r.height, color: r.color } }); console.log(`Updated Spatial Room Coords: ${r.name}`); } // Sections for (const s of r.sections) { let section = await prisma.facilitySection.findFirst({ where: { roomId: room.id, code: s.code } }); if (!section) { // Create section & positions const positions = []; for (let row = 1; row <= s.rows; row++) { for (let col = 1; col <= s.columns; col++) { positions.push({ row, column: col, tier: 1, slot: 1 }); } } section = await prisma.facilitySection.create({ data: { roomId: room.id, name: s.name, code: s.code, type: SectionType.RACK, posX: s.posX, posY: s.posY, width: s.columns * s.spacing, height: s.rows * s.spacing, rows: s.rows, columns: s.columns, spacing: s.spacing, positions: { create: positions } } }); console.log(`Created Section: ${s.name} in ${r.name}`); } else { // Update section pos await prisma.facilitySection.update({ where: { id: section.id }, data: { posX: s.posX, posY: s.posY } }); } } } // ============================================ // DEMO BATCHES // ============================================ const owner = await prisma.user.findUnique({ where: { email: ownerEmail } }); const flowerRoom = await prisma.facilityRoom.findFirst({ where: { code: 'FLO' } }); const vegRoom = await prisma.facilityRoom.findFirst({ where: { code: 'VEG' } }); const demoBatches = [ { name: 'B-2026-01-GG4', strain: 'Gorilla Glue #4', startDate: new Date('2026-01-01'), status: 'ACTIVE', plantCount: 48, source: 'CLONE', stage: 'FLOWER', roomId: flowerRoom?.id }, { name: 'B-2025-12-KM', strain: 'Kush Mints', startDate: new Date('2025-12-15'), harvestDate: new Date('2026-01-08'), status: 'HARVESTED', plantCount: 36, source: 'CLONE', stage: 'DRY' }, { name: 'B-2026-01-GDP', strain: 'Grand Daddy Purple', startDate: new Date('2026-01-05'), status: 'ACTIVE', plantCount: 24, source: 'SEED', stage: 'VEG', roomId: vegRoom?.id } ]; for (const b of demoBatches) { const existing = await prisma.batch.findFirst({ where: { name: b.name } }); if (!existing) { await prisma.batch.create({ data: b as any }); console.log(`Created Batch: ${b.name} (${b.strain})`); } } // ============================================ // DEMO TASKS // ============================================ const demoTasks = [ { title: 'IPM Treatment - Flower Room', description: 'Apply weekly IPM spray to all flower room plants', status: 'PENDING', priority: 'HIGH', dueDate: new Date(Date.now() - 2 * 24 * 60 * 60 * 1000), // 2 days overdue assignedToId: owner?.id }, { title: 'Nutrient Tank pH Calibration', description: 'Calibrate pH meters and check EC levels in all nutrient tanks', status: 'PENDING', priority: 'MEDIUM', dueDate: new Date(Date.now() - 1 * 24 * 60 * 60 * 1000), // 1 day overdue assignedToId: owner?.id }, { title: 'HVAC Filter Replacement', description: 'Replace HEPA filters in all grow rooms', status: 'PENDING', priority: 'LOW', dueDate: new Date(Date.now() + 3 * 24 * 60 * 60 * 1000), // Due in 3 days assignedToId: owner?.id }, { title: 'Weekly Crop Steering Review', description: 'Review VPD charts and adjust environmental targets', status: 'IN_PROGRESS', priority: 'HIGH', dueDate: new Date(Date.now() + 1 * 24 * 60 * 60 * 1000), assignedToId: owner?.id } ]; for (const t of demoTasks) { const existing = await prisma.task.findFirst({ where: { title: t.title } }); if (!existing) { await prisma.task.create({ data: t as any }); console.log(`Created Task: ${t.title}`); } } // ============================================ // PULSE SENSOR MAPPING // ============================================ // Map Pulse sensor device 11666 to Flower Room if (flowerRoom) { const existingSensor = await prisma.sensor.findFirst({ where: { deviceId: '11666', type: 'VPD' } }); if (!existingSensor) { await prisma.sensor.create({ data: { name: 'Veridian Demo Pulse', type: 'VPD', deviceId: '11666', location: 'Flower Room', isActive: true, roomId: flowerRoom.id } as any }); console.log('Created Pulse sensor mapping for device 11666'); } } console.log('Seeding complete.'); } main() .catch((e) => { console.error(e); process.exit(1); }) .finally(async () => { await prisma.$disconnect(); });