-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest-application-status-fix.js
More file actions
149 lines (124 loc) · 4.84 KB
/
test-application-status-fix.js
File metadata and controls
149 lines (124 loc) · 4.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/**
* Test script to verify application status and filled positions fixes
*/
const mongoose = require('mongoose');
const GigRequest = require('./src/models/gigRequest');
const GigApply = require('./src/models/gigApply');
const User = require('./src/models/user');
const Employer = require('./src/models/employer');
require('dotenv').config();
async function testApplicationStatusFix() {
try {
// Connect to database
await mongoose.connect(process.env.MONGODB_URI);
console.log('Connected to MongoDB');
// Find a test job and user
const job = await GigRequest.findOne({ status: 'active' }).populate('employer');
const user = await User.findOne({ role: 'job_seeker' });
if (!job || !user) {
console.log('No test data found. Creating sample data...');
return;
}
console.log('\n=== TESTING APPLICATION STATUS & FILLED POSITIONS ===');
console.log(`Job: ${job.title}`);
console.log(`Initial filled positions: ${job.filledPositions || 0} / ${job.totalPositions || 0}`);
console.log(`User: ${user.firstName} ${user.lastName}`);
// Check if user already has an application for this job
let existingApp = await GigApply.findOne({ user: user._id, gigRequest: job._id });
if (existingApp) {
console.log(`\nExisting application found with status: ${existingApp.status}`);
// Test status change from pending to accepted
if (existingApp.status === 'pending') {
console.log('\nTesting status change: pending -> accepted');
existingApp.status = 'accepted';
await existingApp.save();
// Update filled positions
await GigRequest.findByIdAndUpdate(job._id, { $inc: { filledPositions: 1 } });
const updatedJob = await GigRequest.findById(job._id);
console.log(`New filled positions: ${updatedJob.filledPositions} / ${updatedJob.totalPositions}`);
// Test status change from accepted to rejected
console.log('\nTesting status change: accepted -> rejected');
existingApp.status = 'rejected';
await existingApp.save();
// Decrease filled positions
await GigRequest.findByIdAndUpdate(job._id, { $inc: { filledPositions: -1 } });
const finalJob = await GigRequest.findById(job._id);
console.log(`Final filled positions: ${finalJob.filledPositions} / ${finalJob.totalPositions}`);
}
} else {
console.log('\nNo existing application found. Creating new application...');
// Create new application
const newApp = new GigApply({
user: user._id,
gigRequest: job._id,
status: 'pending',
coverLetter: 'Test application for status fix verification',
appliedAt: new Date()
});
await newApp.save();
console.log('New application created with status: pending');
// Add to job's applicants array
await GigRequest.findByIdAndUpdate(job._id, {
$push: {
applicants: {
user: user._id,
status: 'applied',
appliedAt: new Date(),
coverLetter: 'Test application'
}
}
});
console.log('Application added to job applicants array');
}
// Test getting applications with proper status mapping
console.log('\n=== TESTING APPLICATION RETRIEVAL ===');
const userApplications = await GigApply.find({ user: user._id })
.populate({
path: 'gigRequest',
select: 'title employer payRate location',
populate: {
path: 'employer',
select: 'companyName'
}
});
console.log(`Found ${userApplications.length} applications for user:`);
userApplications.forEach(app => {
console.log(`- ${app.gigRequest.title}: ${app.status}`);
});
// Test employer view of applications
console.log('\n=== TESTING EMPLOYER VIEW ===');
const employerJobs = await GigRequest.aggregate([
{ $match: { employer: job.employer._id } },
{
$lookup: {
from: 'gigapplies',
localField: '_id',
foreignField: 'gigRequest',
as: 'applications'
}
},
{
$addFields: {
applicationsCount: { $size: '$applications' }
}
},
{
$project: {
applications: 0
}
}
]);
console.log(`Found ${employerJobs.length} jobs for employer:`);
employerJobs.forEach(jobData => {
console.log(`- ${jobData.title}: ${jobData.applicationsCount} applications, ${jobData.filledPositions || 0}/${jobData.totalPositions || 0} filled`);
});
console.log('\n✅ Test completed successfully!');
} catch (error) {
console.error('❌ Test failed:', error);
} finally {
await mongoose.disconnect();
console.log('Disconnected from MongoDB');
}
}
// Run the test
testApplicationStatusFix();