Dealer_Onboarding_Backend/scripts/diagnose_associations.ts

77 lines
2.3 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';
async function run() {
try {
console.log('Associations for User:');
const userAssoc = db.User.associations;
Object.keys(userAssoc).forEach(key => {
console.log(`- ${key}: ${userAssoc[key].associationType} to ${userAssoc[key].target.name}`);
});
console.log('\nTrying findAll with managedAsmDistricts...');
await db.User.findAll({
limit: 1,
include: [{ model: db.District, as: 'managedAsmDistricts' }]
});
console.log('Success with managedAsmDistricts');
console.log('\nTrying findAll with managedAreaDistricts...');
await db.User.findAll({
limit: 1,
include: [{ model: db.District, as: 'managedAreaDistricts' }]
});
console.log('Success with managedAreaDistricts');
console.log('\nTrying FULL query from getASMs...');
await db.User.findAll({
where: {
roleCode: { [db.Sequelize.Op.in]: ['ASM', 'AREA SALES MANAGER', 'DD-AM', 'DD AM'] },
isActive: true
},
include: [
{
model: db.UserRole,
as: 'userRoles',
where: { isActive: true },
required: false,
include: [{ model: db.Role, as: 'role', where: { roleCode: { [db.Sequelize.Op.in]: ['ASM', 'DD-AM', 'DD AM'] } } }]
},
{
model: db.District,
as: 'managedAsmDistricts',
include: [
{ model: db.State, as: 'state', attributes: ['id', 'name'] },
{ model: db.Region, as: 'region', attributes: ['id', 'name'] },
{ model: db.Zone, as: 'zone', attributes: ['id', 'name'] }
]
},
{
model: db.District,
as: 'managedAreaDistricts',
include: [
{ model: db.State, as: 'state', attributes: ['id', 'name'] },
{ model: db.Region, as: 'region', attributes: ['id', 'name'] },
{ model: db.Zone, as: 'zone', attributes: ['id', 'name'] }
]
}
],
});
console.log('Success with FULL query');
process.exit(0);
} catch (error) {
console.error('Error:', error);
process.exit(1);
}
}
run();