86 lines
4.3 KiB
TypeScript
86 lines
4.3 KiB
TypeScript
import mongoose from 'mongoose';
|
|
import * as dotenv from 'dotenv';
|
|
import { dashboardMongoService } from './src/services/dashboard.service';
|
|
import { UserModel } from './src/models/mongoose/User.schema';
|
|
import { WorkflowRequestModel } from './src/models/mongoose/WorkflowRequest.schema';
|
|
|
|
dotenv.config();
|
|
|
|
async function verify() {
|
|
try {
|
|
await mongoose.connect(process.env.MONGO_URI!);
|
|
console.log('Connected to MongoDB');
|
|
|
|
// 1. Find an Admin and a Regular User
|
|
const adminUser = await UserModel.findOne({ role: 'ADMIN', isActive: true });
|
|
const regularUser = await UserModel.findOne({ role: 'USER', isActive: true });
|
|
|
|
if (!adminUser || !regularUser) {
|
|
console.error('Could not find both Admin and Regular User for testing');
|
|
// List some users to help debug
|
|
const users = await UserModel.find({ isActive: true }).limit(5);
|
|
console.log('Available users:', users.map(u => ({ email: u.email, role: u.role })));
|
|
process.exit(1);
|
|
}
|
|
|
|
console.log(`\nTesting with:`);
|
|
console.log(`Admin User: ${adminUser.email} (${adminUser.userId})`);
|
|
console.log(`Regular User: ${regularUser.email} (${regularUser.userId})`);
|
|
|
|
// 2. Test getUpcomingDeadlines
|
|
console.log('\n--- Testing Upcoming Deadlines ---');
|
|
const adminDeadlines = await dashboardMongoService.getUpcomingDeadlines(adminUser.userId, 1, 10, false);
|
|
const userDeadlines = await dashboardMongoService.getUpcomingDeadlines(regularUser.userId, 1, 10, true);
|
|
|
|
console.log(`Admin count: ${adminDeadlines.totalRecords}`);
|
|
console.log(`User count: ${userDeadlines.totalRecords}`);
|
|
|
|
if (userDeadlines.deadlines.length > 0) {
|
|
const first = userDeadlines.deadlines[0];
|
|
console.log(`First User Deadline Approver: ${first.approverEmail} (User Email: ${regularUser.email})`);
|
|
if (first.approverEmail !== regularUser.email) {
|
|
console.warn('WARNING: Regular user sees a deadline they are not the approver for! (Wait, are they just a participant?)');
|
|
// Check if they are actually the current approver
|
|
console.log('Actual Approver in data:', first.approverEmail);
|
|
} else {
|
|
console.log('SUCCESS: User only sees their own deadlines.');
|
|
}
|
|
} else {
|
|
console.log('No deadlines found for regular user.');
|
|
}
|
|
|
|
// 3. Test getCriticalRequests
|
|
console.log('\n--- Testing Critical Requests ---');
|
|
const adminCritical = await dashboardMongoService.getCriticalRequests(adminUser.userId, 1, 10, false);
|
|
const userCritical = await dashboardMongoService.getCriticalRequests(regularUser.userId, 1, 10, true);
|
|
|
|
console.log(`Admin count: ${adminCritical.totalRecords}`);
|
|
console.log(`User count: ${userCritical.totalRecords}`);
|
|
|
|
// 4. Test getRecentActivity
|
|
console.log('\n--- Testing Recent Activity ---');
|
|
const adminActivity = await dashboardMongoService.getRecentActivity(adminUser.userId, 1, 10, false);
|
|
const userActivity = await dashboardMongoService.getRecentActivity(regularUser.userId, 1, 10, true);
|
|
|
|
console.log(`Admin count: ${adminActivity.totalRecords}`);
|
|
console.log(`User count: ${userActivity.totalRecords}`);
|
|
|
|
// 5. Test getRequestStats
|
|
console.log('\n--- Testing Request Stats ---');
|
|
// userId, dateRange, startDate, endDate, status, priority, templateType, department, initiator, approver, approverType, search, slaCompliance, viewAsUser
|
|
const adminStats = await dashboardMongoService.getRequestStats(adminUser.userId, 'all', undefined, undefined, 'all', 'all', 'all', 'all', 'all', 'all', 'any', undefined, 'all', false);
|
|
const userStats = await dashboardMongoService.getRequestStats(regularUser.userId, 'all', undefined, undefined, 'all', 'all', 'all', 'all', 'all', 'all', 'any', undefined, 'all', true);
|
|
|
|
console.log(`Admin Total Requests: ${adminStats.totalRequests}`);
|
|
console.log(`User Total (Involved): ${userStats.totalRequests}`);
|
|
|
|
console.log('\nVerification Complete');
|
|
} catch (error) {
|
|
console.error('Verification failed:', error);
|
|
} finally {
|
|
await mongoose.disconnect();
|
|
}
|
|
}
|
|
|
|
verify();
|