75 lines
2.5 KiB
TypeScript
75 lines
2.5 KiB
TypeScript
|
|
import 'dotenv/config';
|
|
import db from './src/database/models/index';
|
|
const { User, Role } = db;
|
|
import { Op } from 'sequelize';
|
|
|
|
async function testFiltering() {
|
|
console.log('--- Testing Backend Filtering Logic (Direct DB) ---');
|
|
|
|
const testCasesMap = [
|
|
{
|
|
name: 'National Role (NBH) - Should ignore location',
|
|
params: { roleCode: 'NBH', zoneId: 'some-random-id' },
|
|
expectedRoles: ['NBH']
|
|
},
|
|
{
|
|
name: 'National Role (DD Head) - Should ignore location',
|
|
params: { roleCode: 'DD Head', zoneId: 'some-random-id' },
|
|
expectedRoles: ['DD Head']
|
|
},
|
|
{
|
|
name: 'Zonal Role (RBM) - Should respect location (Matching)',
|
|
params: { roleCode: 'RBM', zoneId: 'aedf5f64-3f95-4fa7-ac64-52c97cb330e7' }, // Known RBM zone from check_db_users
|
|
expectedCount: 1
|
|
},
|
|
{
|
|
name: 'Zonal Role (RBM) - Should respect location (Mismatch)',
|
|
params: { roleCode: 'RBM', zoneId: '741e0e70-32d3-40d6-987f-5d2ffd54f152' }, // ZBH zone
|
|
expectedCount: 0
|
|
}
|
|
];
|
|
|
|
for (const testCase of testCasesMap) {
|
|
console.log(`\nTesting: ${testCase.name}`);
|
|
const { roleCode, zoneId, regionId, areaId } = testCase.params;
|
|
const whereClause: any = {};
|
|
|
|
// Replicating logic from admin.controller.ts
|
|
if (roleCode) {
|
|
if (Array.isArray(roleCode)) {
|
|
whereClause.roleCode = { [Op.in]: roleCode };
|
|
} else {
|
|
whereClause.roleCode = roleCode;
|
|
}
|
|
}
|
|
|
|
const nationalRoles = ['NBH', 'DD Head'];
|
|
const isNationalRole = (typeof roleCode === 'string' && nationalRoles.includes(roleCode)) ||
|
|
(Array.isArray(roleCode) && roleCode.some(r => typeof r === 'string' && nationalRoles.includes(r)));
|
|
|
|
if (!isNationalRole) {
|
|
if (zoneId) whereClause.zoneId = zoneId;
|
|
if (regionId) whereClause.regionId = regionId;
|
|
if (areaId) whereClause.areaId = areaId;
|
|
}
|
|
|
|
const users = await User.findAll({
|
|
where: whereClause,
|
|
attributes: ['id', 'roleCode', 'zoneId']
|
|
});
|
|
|
|
console.log(`Result: ${users.length} users found.`);
|
|
if (users.length > 0) {
|
|
console.log('Found Roles:', Array.from(new Set(users.map(u => u.roleCode))));
|
|
}
|
|
}
|
|
|
|
process.exit(0);
|
|
}
|
|
|
|
testFiltering().catch(err => {
|
|
console.error(err);
|
|
process.exit(1);
|
|
});
|