import 'dotenv/config'; import fs from 'fs'; import path from 'path'; import { fileURLToPath } from 'url'; import db from '../src/database/models/index.js'; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); async function seedStateDistrictOnly() { console.log('--- Seeding State + District only (no Zone/Region) ---'); try { await db.sequelize.authenticate(); const seederPath = path.join(__dirname, '../seeders/20240127-seed-geo-data.js'); const content = fs.readFileSync(seederPath, 'utf8'); const statesMatch = content.match(/const STATES_DATA = \[([\s\S]*?)\];/); const citiesMatch = content.match(/const CITIES_DATA = \[([\s\S]*?)\];/); if (!statesMatch || !citiesMatch) { throw new Error('Could not parse STATES_DATA / CITIES_DATA from geo seeder.'); } // Data source is internal seeder, safe to eval. const STATES_DATA = eval(`[${statesMatch[1]}]`); const CITIES_DATA = eval(`[${citiesMatch[1]}]`); const { State, District } = db; const stateIdMap = new Map(); for (const s of STATES_DATA) { const [stateRecord] = await State.findOrCreate({ where: { name: s.name }, defaults: { name: s.name, zoneId: null } }); stateIdMap.set(s.id, stateRecord.id); } let districtCount = 0; for (const c of CITIES_DATA) { const stateId = stateIdMap.get(c.state_id); if (!stateId) continue; await District.findOrCreate({ where: { name: c.name, stateId }, defaults: { name: c.name, stateId, regionId: null, zoneId: null, city: c.name, isActive: true } }); districtCount += 1; } console.log(`✅ Seeded ${stateIdMap.size} states and ${districtCount} districts.`); process.exit(0); } catch (error) { console.error('❌ State/District seed failed:', error); process.exit(1); } } seedStateDistrictOnly();