import 'dotenv/config'; import db from '../src/database/models/index.js'; import bcrypt from 'bcryptjs'; const { Role, Zone, Region, State, Location, User, UserRole } = db; async function resetAndSeed() { console.log('--- RESETTING DATABASE TO DENORMALIZED DISTRICT MODEL ---'); try { await db.sequelize.authenticate(); console.log('Database connected.'); // 1. Force Sync await db.sequelize.sync({ force: true }); console.log('Database schema reset (force synced).'); const hashedPassword = await bcrypt.hash('Admin@123', 10); // 2. Seed Roles const roles = [ { roleCode: 'NBH', roleName: 'National Business Head', category: 'NATIONAL' }, { roleCode: 'DD Head', roleName: 'DD Head', category: 'NATIONAL' }, { roleCode: 'ZBH', roleName: 'Zonal Business Head', category: 'ZONAL' }, { roleCode: 'DD-ZM', roleName: 'DD Zonal Manager', category: 'ZONAL' }, { roleCode: 'RM', roleName: 'Regional Manager', category: 'REGIONAL' }, { roleCode: 'ASM', roleName: 'Area Sales Manager', category: 'AREA' }, { roleCode: 'Super Admin', roleName: 'Super Admin', category: 'NATIONAL' }, { roleCode: 'Dealer', roleName: 'Dealer', category: 'EXTERNAL' } ]; for (const r of roles) await Role.create(r); console.log('Roles seeded.'); // 3. Seed Hierarchy (Zone -> State & Region -> District) // Zone const northZone = await Zone.create({ name: 'North Zone', code: 'ZONE-N' }); const southZone = await Zone.create({ name: 'South Zone', code: 'ZONE-S' }); // State const delhiState = await State.create({ name: 'Delhi', zoneId: northZone.id }); const haryanaState = await State.create({ name: 'Haryana', zoneId: northZone.id }); const karnatakaState = await State.create({ name: 'Karnataka', zoneId: southZone.id }); // Region const ncrRegion = await Region.create({ name: 'NCR Region', zoneId: northZone.id }); const bangaloreRegion = await Region.create({ name: 'Bangalore Region', zoneId: southZone.id }); // District (Location) await Location.create({ name: 'Central Delhi', stateId: delhiState.id, regionId: ncrRegion.id, zoneId: northZone.id }); await Location.create({ name: 'Gurgaon', stateId: haryanaState.id, regionId: ncrRegion.id, zoneId: northZone.id }); await Location.create({ name: 'Bangalore Urban', stateId: karnatakaState.id, regionId: bangaloreRegion.id, zoneId: southZone.id }); console.log('Denormalized Hierarchy seeded.'); // 4. Seed Admin Users const adminUser = await User.create({ fullName: 'Super Admin', email: 'admin@royalenfield.com', roleCode: 'Super Admin', password: hashedPassword, status: 'active' }); const superAdminRole = await Role.findOne({ where: { roleCode: 'Super Admin' } }); if (superAdminRole) { await UserRole.create({ userId: adminUser.id, roleId: superAdminRole.id, isActive: true, isPrimary: true }); } const zbhUser = await User.create({ fullName: 'Yashwin (ZBH North)', email: 'yashwin@gmail.com', roleCode: 'ZBH', password: hashedPassword, status: 'active' }); const zbhRole = await Role.findOne({ where: { roleCode: 'ZBH' } }); if (zbhRole) { await UserRole.create({ userId: zbhUser.id, roleId: zbhRole.id, zoneId: northZone.id, isActive: true, isPrimary: true }); } console.log('Admin Users seeded.'); console.log('--- DATABASE RESET & SEEDING COMPLETE ---'); process.exit(0); } catch (error) { console.error('Error during database reset/seed:', error); process.exit(1); } } resetAndSeed();