73 lines
2.9 KiB
TypeScript
73 lines
2.9 KiB
TypeScript
import { connectMongoDB } from '../config/database';
|
|
import { DealerClaimModel } from '../models/mongoose/DealerClaim.schema';
|
|
import { WorkflowRequestModel } from '../models/mongoose/WorkflowRequest.schema';
|
|
import logger from '../utils/logger';
|
|
|
|
const verifyKPIs = async () => {
|
|
try {
|
|
await connectMongoDB();
|
|
logger.info('🚀 Starting KPI Query Verification (Refined)...');
|
|
|
|
// 1. Dealer Spend Analysis (Aggregation on Consolidated Claims)
|
|
// Goal: Get total claimed amount per dealer (grouping by name)
|
|
logger.info('📊 KPI 1: Dealer Spend Analysis (Consolidated Schema Power)');
|
|
const totalClaims = await DealerClaimModel.countDocuments();
|
|
logger.info(`Total Claims in DB: ${totalClaims}`);
|
|
|
|
if (totalClaims > 0) {
|
|
const dealerSpend = await DealerClaimModel.aggregate([
|
|
{
|
|
$group: {
|
|
_id: '$dealer.name',
|
|
totalClaims: { $sum: 1 },
|
|
totalEstimatedBudget: { $sum: '$proposal.totalEstimatedBudget' },
|
|
avgBudget: { $avg: '$proposal.totalEstimatedBudget' }
|
|
}
|
|
},
|
|
{ $sort: { totalEstimatedBudget: -1 } },
|
|
{ $limit: 10 }
|
|
]);
|
|
console.table(dealerSpend);
|
|
} else {
|
|
logger.warn('⚠️ No claims found. Distribution check skipped.');
|
|
}
|
|
logger.info('✅ Dealer Spend Query executed!');
|
|
|
|
// 2. TAT Efficiency (Aggregation on Normalized Workflows)
|
|
// Goal: Stats by Status
|
|
logger.info('⏱️ KPI 2: Workflow Status Distribution (Normalized Schema Power)');
|
|
const workflowStats = await WorkflowRequestModel.aggregate([
|
|
{
|
|
$group: {
|
|
_id: '$status',
|
|
count: { $sum: 1 },
|
|
avgTatHours: { $avg: '$totalTatHours' }
|
|
}
|
|
},
|
|
{ $sort: { count: -1 } }
|
|
]);
|
|
console.table(workflowStats);
|
|
logger.info('✅ TAT Analysis Query executed successfully!');
|
|
|
|
// 3. Deep Filtering
|
|
// Goal: Find claims with ANY cost items
|
|
logger.info('🔍 Filter 1: Deep Search for Claims with Cost Items');
|
|
const complexClaims = await DealerClaimModel.find({
|
|
'proposal.costItems': { $exists: true, $not: { $size: 0 } }
|
|
}).select('claimId dealer.name proposal.totalEstimatedBudget').limit(5);
|
|
|
|
logger.info(`Found ${complexClaims.length} claims with cost items.`);
|
|
complexClaims.forEach(c => {
|
|
console.log(`- Claim ${c.claimId} (${c.dealer.name}) - Budget: ${c.proposal?.totalEstimatedBudget}`);
|
|
});
|
|
|
|
logger.info('🎉 KPI Verification Completed Successfully!');
|
|
process.exit(0);
|
|
} catch (error) {
|
|
logger.error('❌ Verification Failed:', error);
|
|
process.exit(1);
|
|
}
|
|
};
|
|
|
|
verifyKPIs();
|