const { db, toISODate } = require('./db'); const crypto = require('crypto'); // Helper function to hash API keys function hashApiKey(key) { return crypto.createHash('sha256').update(key).digest('hex'); } // Helper function to generate API key function generateApiKey() { const randomBytes = crypto.randomBytes(24); return 'cpa_' + randomBytes.toString('hex'); } // Seed data from mockData/cycleData.ts const cycles = [ { id: 'CYCLE-JBW-2025-05-20', totalDays: 42, currentDay: 42, startDate: new Date('2025-05-20'), status: 'Completed', docInCount: 20000, chickInWeight: null, }, { id: 'CYCLE-JBW-2025-07-11', totalDays: 42, currentDay: 42, startDate: new Date('2025-07-11'), status: 'Completed', docInCount: 20000, chickInWeight: null, }, { id: 'CYCLE-JBW-2025-10-22', totalDays: 42, currentDay: 42, startDate: new Date('2025-10-22'), status: 'Completed', docInCount: 20000, chickInWeight: null, }, { id: 'CYCLE-JBW-2025-12-10', totalDays: 42, currentDay: 7, startDate: new Date('2025-12-10'), status: 'Active', docInCount: 20000, chickInWeight: null, }, ]; // Location hierarchy seed data const locations = [ { id: 'loc1', name: 'Peternakan Pusat, Jawa Barat', address: 'Jl. Peternakan No. 123, Bandung, Jawa Barat', }, ]; const coops = [ { id: 'coop1', name: 'Kandang A (Broiler)', locationId: 'loc1', capacity: 15000, }, { id: 'coop2', name: 'Kandang B (Layer)', locationId: 'loc1', capacity: 12000, }, ]; const floors = [ { id: 'floor1', name: 'Lantai 1', coopId: 'coop1', floorNumber: 1, }, { id: 'floor2', name: 'Lantai 2', coopId: 'coop1', floorNumber: 2, }, { id: 'floor3', name: 'Lantai 1', coopId: 'coop2', floorNumber: 1, }, ]; const cameras = [ { id: 'cam1', name: 'Kamera Sudut Kanan Zona A', cameraType: 'STANDARD', floorId: 'floor1', model: 'HIK-2MP-IR', status: 'online', }, { id: 'cam2', name: 'Kamera Sudut Kiri Zona A', cameraType: 'STANDARD', floorId: 'floor1', model: 'HIK-2MP-IR', status: 'online', }, { id: 'cam3', name: 'Kamera Tengah Zona B', cameraType: 'INFRARED', floorId: 'floor2', model: 'HIK-4MP-IR', status: 'online', }, { id: 'cam4', name: 'Kamera Pintu Masuk', cameraType: 'STANDARD', floorId: 'floor2', model: 'HIK-2MP', status: 'online', }, { id: 'cam5', name: 'Kamera Zona Layer 1', cameraType: 'STANDARD', floorId: 'floor3', model: 'HIK-2MP-IR', status: 'online', }, ]; function seedDatabase() { try { // Check if cycles already exist const existingCycles = db.prepare('SELECT COUNT(*) as count FROM cycles').get(); if (existingCycles.count > 0) { return; } // Insert cycles const insertCycle = db.prepare(` INSERT INTO cycles (id, total_days, current_day, start_date, end_date, chick_in_weight, doc_in_count, status) VALUES (?, ?, ?, ?, ?, ?, ?, ?) `); const insertMany = db.transaction((cycles) => { for (const cycle of cycles) { const endDate = new Date(cycle.startDate); endDate.setDate(endDate.getDate() + cycle.totalDays - 1); insertCycle.run( cycle.id, cycle.totalDays, cycle.currentDay, toISODate(cycle.startDate), toISODate(endDate), cycle.chickInWeight, cycle.docInCount, cycle.status ); } }); insertMany(cycles); // Display seeded data const seededCycles = db.prepare('SELECT * FROM cycles ORDER BY start_date').all(); seededCycles.forEach((c) => {}); // Insert locations const insertLocation = db.prepare(` INSERT INTO locations (id, name, address) VALUES (?, ?, ?) `); const seedLocations = db.transaction((locs) => { for (const loc of locs) { insertLocation.run(loc.id, loc.name, loc.address || null); } }); seedLocations(locations); // Insert coops const insertCoop = db.prepare(` INSERT INTO coops (id, name, location_id, capacity) VALUES (?, ?, ?, ?) `); const seedCoops = db.transaction((coopList) => { for (const coop of coopList) { insertCoop.run(coop.id, coop.name, coop.locationId, coop.capacity || null); } }); seedCoops(coops); // Insert floors const insertFloor = db.prepare(` INSERT INTO floors (id, name, coop_id, floor_number) VALUES (?, ?, ?, ?) `); const seedFloors = db.transaction((floorList) => { for (const floor of floorList) { insertFloor.run(floor.id, floor.name, floor.coopId, floor.floorNumber || null); } }); seedFloors(floors); // Insert cameras const insertCamera = db.prepare(` INSERT INTO cameras (id, name, camera_type, floor_id, model, status) VALUES (?, ?, ?, ?, ?, ?) `); const seedCameras = db.transaction((camList) => { for (const cam of camList) { insertCamera.run( cam.id, cam.name, cam.cameraType, cam.floorId || null, cam.model || null, cam.status ); } }); seedCameras(cameras); const devKey = generateApiKey(); const keyHash = hashApiKey(devKey); const keyId = 'apk_dev_' + Date.now(); db.prepare( ` INSERT INTO api_keys (id, key_hash, name, description, created_by, is_active) VALUES (?, ?, ?, ?, ?, ?) ` ).run( keyId, keyHash, 'Development Key', 'Auto-generated key for testing and development', 'seed-script', 1 ); } catch (error) { console.error('✗ Error seeding database:', error.message); throw error; } } // Run seed if this file is executed directly if (require.main === module) { seedDatabase(); process.exit(0); } module.exports = { seedDatabase };