75 lines
1.9 KiB
TypeScript
75 lines
1.9 KiB
TypeScript
|
|
import dotenv from 'dotenv';
|
|
import path from 'path';
|
|
import { fileURLToPath } from 'url';
|
|
|
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
dotenv.config({ path: path.join(__dirname, '../.env') });
|
|
|
|
import db from '../src/database/models/index.js';
|
|
import { syncLocationManagers } from '../src/modules/master/syncHierarchy.service.js';
|
|
|
|
async function run() {
|
|
try {
|
|
// 1. Find the South Delhi district
|
|
const district = await db.District.findOne({
|
|
where: { name: { [db.Sequelize.Op.iLike]: '%South Delhi%' } }
|
|
});
|
|
|
|
if (!district) {
|
|
console.log('District "South Delhi" not found');
|
|
return;
|
|
}
|
|
console.log(`Found District: ${district.name} (${district.id})`);
|
|
|
|
// 2. Find a DD-AM user
|
|
// The role code might be 'DD AM' or 'DD-AM' based on constants
|
|
const user = await db.User.findOne({
|
|
where: {
|
|
[db.Sequelize.Op.or]: [
|
|
{ roleCode: 'DD AM' },
|
|
{ roleCode: 'DD-AM' }
|
|
],
|
|
isActive: true
|
|
}
|
|
});
|
|
|
|
if (!user) {
|
|
console.log('No active DD-AM user found');
|
|
return;
|
|
}
|
|
console.log(`Found DD-AM User: ${user.fullName} (${user.id})`);
|
|
|
|
// 3. Create/Update UserRole mapping
|
|
const [userRole, created] = await db.UserRole.findOrCreate({
|
|
where: {
|
|
userId: user.id,
|
|
districtId: district.id,
|
|
isActive: true
|
|
},
|
|
defaults: {
|
|
roleId: (await db.Role.findOne({ where: { roleCode: user.roleCode } })).id,
|
|
isPrimary: true,
|
|
isActive: true
|
|
}
|
|
});
|
|
|
|
if (created) {
|
|
console.log('Created new UserRole assignment');
|
|
} else {
|
|
console.log('UserRole assignment already exists');
|
|
}
|
|
|
|
// 4. Sync Location Managers
|
|
await syncLocationManagers(district.id);
|
|
console.log('Sync completed');
|
|
|
|
process.exit(0);
|
|
} catch (error) {
|
|
console.error('Error:', error);
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
run();
|