Dealer_Onboarding_Backend/scripts/seed_normalized_data.ts

173 lines
6.6 KiB
TypeScript

import 'dotenv/config';
import db from '../src/database/models/index.js';
import bcrypt from 'bcryptjs';
const { Role, Location, LocationHierarchy, User, UserRole } = db;
async function seed() {
console.log('--- Seeding Normalized Graph Data ---');
// Ensure schema exists when seed is run on a fresh/empty database.
// This is non-destructive (does not drop data).
await db.sequelize.authenticate();
await db.sequelize.sync({ alter: false });
// Hash default password for test users
const hashedPassword = await bcrypt.hash('Admin@123', 10);
// 1. Create 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 Lead', roleName: 'DD Lead', category: 'ZONAL' },
{ roleCode: 'RBM', roleName: 'Regional Business Manager', category: 'REGIONAL' },
{ roleCode: 'DD-ZM', roleName: 'DD Zonal Manager', category: 'ZONAL' },
{ roleCode: 'ASM', roleName: 'Area Sales Manager', category: 'AREA' },
{ roleCode: 'Super Admin', roleName: 'Super Admin', category: 'NATIONAL' },
{ roleCode: 'Finance', roleName: 'Finance', category: 'DEPARTMENT' },
{ roleCode: 'Dealer', roleName: 'Dealer', category: 'EXTERNAL' },
{ roleCode: 'DD Admin', roleName: 'DD Admin', category: 'ADMIN' },
{ roleCode: 'Legal Admin', roleName: 'Legal Admin', category: 'DEPARTMENT' }
];
for (const r of roles) {
await Role.findOrCreate({ where: { roleCode: r.roleCode }, defaults: r });
}
console.log('Roles seeded.');
// 2. Create Locations
const existingZones = await Location.findAll({
where: { type: 'zone' },
order: [['createdAt', 'ASC']]
});
let zone1: any = existingZones[0];
let zone2: any = existingZones[1];
if (!zone1) {
const [createdZone1] = await Location.findOrCreate({
where: { name: 'North Zone', type: 'zone' },
defaults: { name: 'North Zone', type: 'zone' }
});
zone1 = createdZone1;
}
if (!zone2) {
const [createdZone2] = await Location.findOrCreate({
where: { name: 'South Zone', type: 'zone' },
defaults: { name: 'South Zone', type: 'zone' }
});
zone2 = createdZone2;
}
const [region1] = await Location.findOrCreate({
where: { name: 'Delhi Region', type: 'region' },
defaults: { name: 'Delhi Region', type: 'region' }
});
const [area1] = await Location.findOrCreate({
where: { name: 'South Delhi Area', type: 'area' },
defaults: { name: 'South Delhi Area', type: 'area' }
});
const [region2] = await Location.findOrCreate({
where: { name: 'Bangalore Region', type: 'region' },
defaults: { name: 'Bangalore Region', type: 'region' }
});
console.log('Locations created.');
// 3. Create Hierarchies (Bridge Table)
await LocationHierarchy.findOrCreate({
where: { locationId: region1.id, parentId: zone1.id },
defaults: { locationId: region1.id, parentId: zone1.id }
});
await LocationHierarchy.findOrCreate({
where: { locationId: area1.id, parentId: region1.id },
defaults: { locationId: area1.id, parentId: region1.id }
});
await LocationHierarchy.findOrCreate({
where: { locationId: region2.id, parentId: zone2.id },
defaults: { locationId: region2.id, parentId: zone2.id }
});
console.log('Hierarchies seeded.');
const mapUserRole = async (userRec: any, roleCode: string, locationId?: string) => {
const role = await Role.findOne({ where: { roleCode } });
if (role) {
await UserRole.findOrCreate({
where: {
userId: userRec.id,
roleId: role.id,
locationId: locationId || null
},
defaults: {
userId: userRec.id,
roleId: role.id,
locationId: locationId || null
}
});
}
};
// 4. Create Users and Map them
// Custom Seed Users
const nbhUser = await User.findOrCreate({
where: { email: 'nbh@example.com' },
defaults: { fullName: 'National Head', roleCode: 'NBH', password: hashedPassword }
});
await mapUserRole(nbhUser[0], 'NBH');
const zbhUser = await User.findOrCreate({
where: { email: 'zbh.north@example.com' },
defaults: { fullName: 'North Zonal Head', roleCode: 'ZBH', password: hashedPassword }
});
await mapUserRole(zbhUser[0], 'ZBH', zone1.id);
const rbmUser = await User.findOrCreate({
where: { email: 'rbm.delhi@example.com' },
defaults: { fullName: 'Delhi Regional Manager', roleCode: 'RBM', password: hashedPassword }
});
await mapUserRole(rbmUser[0], 'RBM', region1.id);
const asmUser = await User.findOrCreate({
where: { email: 'asm.sdelhi@example.com' },
defaults: { fullName: 'South Delhi ASM', roleCode: 'ASM', password: hashedPassword }
});
await mapUserRole(asmUser[0], 'ASM', area1.id);
// Requested Mock Users
const mockUsers = [
{ email: 'ddlead@royalenfield.com', name: 'Meera Iyer', roleCode: 'DD Lead', location: zone1.id },
{ email: 'finance@royalenfield.com', name: 'Rahul Verma', roleCode: 'Finance', location: null },
{ email: 'dealer@royalenfield.com', name: 'Amit Sharma', roleCode: 'Dealer', location: area1.id, isExt: true },
{ email: 'admin@royalenfield.com', name: 'Laxman H', roleCode: 'DD Lead', location: zone2.id },
{ email: 'yashwin@gmail.com', name: 'Yashwin', roleCode: 'ZBH', location: zone1.id },
{ email: 'kenil@gmail.com', name: 'Kenil', roleCode: 'DD Lead', location: zone1.id },
{ email: 'lince@gmail.com', name: 'Lince', roleCode: 'DD Admin', location: null }
];
for (const m of mockUsers) {
const u = await User.findOrCreate({
where: { email: m.email },
defaults: {
fullName: m.name,
roleCode: m.roleCode,
password: hashedPassword,
isExternal: m.isExt || false,
status: 'active'
}
});
await mapUserRole(u[0], m.roleCode, m.location);
}
console.log('Users and Mappings seeded.');
console.log('--- Seeding Complete ---');
}
seed().catch(err => {
console.error(err);
process.exit(1);
}).then(() => process.exit(0));