import 'dotenv/config'; import bcrypt from 'bcryptjs'; import db from '../src/database/models/index.js'; import { ROLES } from '../src/common/config/constants.js'; const ADMIN_EMAIL = 'admin@gmail.com'; const ADMIN_PASSWORD = 'Admin@123'; const ADMIN_NAME = 'System Admin'; async function seedMinimalAdmin() { console.log('--- Seeding minimal admin-only data ---'); try { await db.sequelize.authenticate(); const stateCount = await db.State.count(); const districtCount = await db.District.count(); const regionCount = await db.Region.count(); const zoneCount = await db.Zone.count(); if (!stateCount || !districtCount) { throw new Error( 'Geo master data is incomplete. Run "npm run seed:state-district" before "seed:minimal-admin".' ); } if (!regionCount || !zoneCount) { console.log('ℹ️ Region/Zone not seeded (expected for minimal setup). You can add them manually later.'); } // Ensure only one user remains for this minimal environment. await db.UserRole.destroy({ where: {} }); await db.User.destroy({ where: {} }); // Ensure required roles exist. await db.Role.findOrCreate({ where: { roleCode: ROLES.SUPER_ADMIN }, defaults: { roleCode: ROLES.SUPER_ADMIN, roleName: 'Super Admin', category: 'ADMIN', description: 'Full system access' } }); await db.Role.findOrCreate({ where: { roleCode: ROLES.DD_ADMIN }, defaults: { roleCode: ROLES.DD_ADMIN, roleName: 'DD Admin', category: 'ADMIN', description: 'Dealer Development Admin' } }); const hashedPassword = await bcrypt.hash(ADMIN_PASSWORD, 10); const adminUser = await db.User.create({ email: ADMIN_EMAIL, fullName: ADMIN_NAME, password: hashedPassword, roleCode: ROLES.SUPER_ADMIN, status: 'active', isActive: true, isExternal: false }); const role = await db.Role.findOne({ where: { roleCode: ROLES.SUPER_ADMIN } }); if (role) { await db.UserRole.create({ userId: adminUser.id, roleId: role.id, isPrimary: true, isActive: true }); } console.log('✅ Minimal admin seed completed.'); console.log(`✅ Login: ${ADMIN_EMAIL}`); console.log(`✅ Password: ${ADMIN_PASSWORD}`); process.exit(0); } catch (error) { console.error('❌ Minimal admin seed failed:', error); process.exit(1); } } seedMinimalAdmin();