Dealer_Onboarding_Backend/scripts/seed_normalized_data.ts
2026-03-23 20:12:10 +05:30

98 lines
3.7 KiB
TypeScript

import db from '../src/database/models/index.js';
const { Role, Location, LocationHierarchy, User, UserRole, Permission } = db;
async function seed() {
console.log('--- Seeding Normalized Graph Data ---');
// 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' }
];
for (const r of roles) {
await Role.findOrCreate({ where: { roleCode: r.roleCode }, defaults: r });
}
console.log('Roles seeded.');
// 2. Create Locations
const zone1 = await Location.create({ name: 'North Zone', type: 'zone' });
const region1 = await Location.create({ name: 'Delhi Region', type: 'region' });
const area1 = await Location.create({ name: 'South Delhi Area', type: 'area' });
const zone2 = await Location.create({ name: 'South Zone', type: 'zone' });
const region2 = await Location.create({ name: 'Bangalore Region', type: 'region' });
console.log('Locations created.');
// 3. Create Hierarchies (Bridge Table)
await LocationHierarchy.create({ locationId: region1.id, parentId: zone1.id });
await LocationHierarchy.create({ locationId: area1.id, parentId: region1.id });
// Example of multiple parents if needed
// await LocationHierarchy.create({ locationId: area1.id, parentId: someOtherParent.id });
await LocationHierarchy.create({ locationId: region2.id, parentId: zone2.id });
console.log('Hierarchies seeded.');
// 4. Create Users and Map them
// NBH (Global)
const nbhUser = await User.findOrCreate({
where: { email: 'nbh@example.com' },
defaults: { fullName: 'National Head', roleCode: 'NBH' }
});
await UserRole.create({ userId: nbhUser[0].id, roleId: (await Role.findOne({ where: { roleCode: 'NBH' } })).id });
// ZBH (North Zone)
const zbhUser = await User.findOrCreate({
where: { email: 'zbh.north@example.com' },
defaults: { fullName: 'North Zonal Head', roleCode: 'ZBH' }
});
const zbhRole = await Role.findOne({ where: { roleCode: 'ZBH' } });
await UserRole.create({
userId: zbhUser[0].id,
roleId: zbhRole.id,
locationId: zone1.id
});
// RBM (Delhi Region)
const rbmUser = await User.findOrCreate({
where: { email: 'rbm.delhi@example.com' },
defaults: { fullName: 'Delhi Regional Manager', roleCode: 'RBM' }
});
const rbmRole = await Role.findOne({ where: { roleCode: 'RBM' } });
await UserRole.create({
userId: rbmUser[0].id,
roleId: rbmRole.id,
locationId: region1.id
});
// ASM (South Delhi Area)
const asmUser = await User.findOrCreate({
where: { email: 'asm.sdelhi@example.com' },
defaults: { fullName: 'South Delhi ASM', roleCode: 'ASM' }
});
const asmRole = await Role.findOne({ where: { roleCode: 'ASM' } });
await UserRole.create({
userId: asmUser[0].id,
roleId: asmRole.id,
locationId: area1.id
});
console.log('Users and Mappings seeded.');
console.log('--- Seeding Complete ---');
}
seed().catch(err => {
console.error(err);
process.exit(1);
}).then(() => process.exit(0));