Skip to content

Commit 45a8f94

Browse files
Mugisha146El-zizo Mugisha
and
El-zizo Mugisha
authored
update applicant status to Admitted/Accepted and change role to trainee (#143)
* update applicant status to Admitted/Accepted and change role to trainee * update trainee in all models --------- Co-authored-by: El-zizo Mugisha <[email protected]>
1 parent fa6c505 commit 45a8f94

File tree

4 files changed

+198
-96
lines changed

4 files changed

+198
-96
lines changed

Diff for: src/models/traineeApplicant.ts

+28-13
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@ import mongoose, { Schema } from "mongoose";
33
const CycleAppliedSchema = new Schema({
44
cycle: {
55
type: Schema.Types.ObjectId,
6-
ref: 'ApplicationCycle',
7-
required: true
6+
ref: "ApplicationCycle",
7+
required: true,
88
},
9-
})
9+
});
1010

11-
const TraineeApplicantSchema = new Schema({
11+
const TraineeApplicantSchema = new Schema(
12+
{
1213
user: {
1314
type: Schema.Types.ObjectId,
14-
ref: 'User'
15+
ref: "User",
1516
},
1617
email: {
1718
type: String,
@@ -26,10 +27,10 @@ const TraineeApplicantSchema = new Schema({
2627
type: String,
2728
required: true,
2829
},
29-
cycle_id:{
30+
cycle_id: {
3031
type: mongoose.Schema.Types.ObjectId,
3132
ref: "applicationCycle",
32-
required: true
33+
required: true,
3334
},
3435
delete_at: {
3536
type: Boolean,
@@ -38,21 +39,35 @@ const TraineeApplicantSchema = new Schema({
3839
cycleApplied: [CycleAppliedSchema],
3940
applicationPhase: {
4041
type: String,
41-
enum: ["Applied", 'Shortlisted', 'Technical Assessment', 'Interview Assessment', 'Admitted', 'Rejected', "Enrolled"],
42+
enum: [
43+
"Applied",
44+
"Shortlisted",
45+
"Technical Assessment",
46+
"Interview Assessment",
47+
"Admitted",
48+
"Rejected",
49+
"Enrolled",
50+
],
4251
default: "Applied",
4352
},
4453
status: {
4554
type: String,
4655
default: "No action",
4756
},
57+
role: {
58+
type: Schema.Types.ObjectId,
59+
ref: "Role",
60+
},
4861
cohort: {
4962
type: Schema.Types.ObjectId,
5063
ref: "cohortModel",
51-
}
52-
},{
64+
},
65+
},
66+
{
5367
timestamps: true,
54-
});
68+
}
69+
);
5570

56-
const TraineeApplicant = mongoose.model('Trainees', TraineeApplicantSchema);
71+
const TraineeApplicant = mongoose.model("Trainees", TraineeApplicantSchema);
5772

58-
export default TraineeApplicant;
73+
export default TraineeApplicant;

Diff for: src/resolvers/applicationStageResolver.ts

+85-20
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import InterviewAssessment from "../models/InterviewAssessmentStageSchema";
99
import TechnicalAssessment from "../models/technicalAssessmentStage";
1010
import mongoose from "mongoose";
1111
import { traineEAttributes } from "../models/traineeAttribute";
12+
import { LoggedUserModel } from "../models/AuthUser";
1213

1314
const validStages = [
1415
"Shortlisted",
@@ -93,28 +94,37 @@ export const applicationStageResolvers: any = {
9394
getTraineeCyclesApplications: async (_: any, __: any, context: any) => {
9495
try {
9596
if (!context.currentUser) {
96-
throw new CustomGraphQLError("You must be logged in to view your applications");
97+
throw new CustomGraphQLError(
98+
"You must be logged in to view your applications"
99+
);
97100
}
98101

99-
const applications = await TraineeApplicant.findOne({ email: context.currentUser.email })
102+
const applications = await TraineeApplicant.findOne({
103+
email: context.currentUser.email,
104+
})
100105
.populate("cycle_id")
101106
.lean();
102-
return applications
103-
}
104-
catch (error: any) {
107+
return applications;
108+
} catch (error: any) {
105109
console.error("Error retrieving applications with attributes:", error);
106110
throw new CustomGraphQLError(error);
107111
}
108112
},
109-
getApplicationsAttributes: async (_: any, { trainee_id }: any, context: any) => {
113+
getApplicationsAttributes: async (
114+
_: any,
115+
{ trainee_id }: any,
116+
context: any
117+
) => {
110118
try {
111119
if (!context.currentUser) {
112-
throw new CustomGraphQLError("You must be logged in to view your applications");
120+
throw new CustomGraphQLError(
121+
"You must be logged in to view your applications"
122+
);
113123
}
114124

115125
const attributes = await traineEAttributes.findOne({ trainee_id });
116126

117-
return attributes
127+
return attributes;
118128
} catch (error) {
119129
console.error("Error getting attributes:", error);
120130
throw new CustomGraphQLError(error);
@@ -123,12 +133,21 @@ export const applicationStageResolvers: any = {
123133
getApplicationStages: async (_: any, { trainee_id }: any, context: any) => {
124134
try {
125135
if (!context.currentUser) {
126-
throw new CustomGraphQLError("You must be logged in to view your applications");
136+
throw new CustomGraphQLError(
137+
"You must be logged in to view your applications"
138+
);
127139
}
128140

129141
const trainee = new mongoose.Types.ObjectId(trainee_id);
130142

131-
const [shortlistStage, technicalStage, interviewStage, admittedStage, dismissedStage, AllStages] = await Promise.all([
143+
const [
144+
shortlistStage,
145+
technicalStage,
146+
interviewStage,
147+
admittedStage,
148+
dismissedStage,
149+
AllStages,
150+
] = await Promise.all([
132151
Shortlisted.findOne({ applicantId: trainee }),
133152
TechnicalAssessment.findOne({ applicantId: trainee }),
134153
InterviewAssessment.findOne({ applicantId: trainee }),
@@ -143,15 +162,15 @@ export const applicationStageResolvers: any = {
143162
interview: interviewStage,
144163
admitted: admittedStage,
145164
dismissed: dismissedStage,
146-
allStages: AllStages
147-
}
148-
149-
165+
allStages: AllStages,
166+
};
150167
} catch (error) {
151168
console.error("Error retrieving application stages:", error);
152-
throw new CustomGraphQLError(error || "An error occurred while retrieving applications");
169+
throw new CustomGraphQLError(
170+
error || "An error occurred while retrieving applications"
171+
);
153172
}
154-
}
173+
},
155174
},
156175
Mutation: {
157176
moveToNextStage: async (
@@ -241,6 +260,15 @@ export const applicationStageResolvers: any = {
241260
await stageTracking.save();
242261
}
243262

263+
let traineeRole = await RoleModel.findOne({ roleName: "trainee" });
264+
if (!traineeRole) {
265+
traineeRole = await RoleModel.create({
266+
roleName: "trainee",
267+
description: "A user who has been accepted as a trainee",
268+
permissions: [],
269+
});
270+
}
271+
244272
let message = "";
245273
switch (nextStage) {
246274
case "Technical Assessment":
@@ -294,19 +322,52 @@ export const applicationStageResolvers: any = {
294322
);
295323
}
296324

297-
await Promise.all(models.map(model => updateApplicantAfterAdmitted(model, applicantId)));
325+
await Promise.all(
326+
models.map((model) =>
327+
updateApplicantAfterAdmitted(model, applicantId)
328+
)
329+
);
298330

299331
await Admitted.create({
300332
applicantId,
301333
comments,
302334
status: "Passed",
303335
});
304336
message = `Applicant passed the application stage✅.`;
337+
305338
await TraineeApplicant.updateOne(
306339
{ _id: applicantId },
307-
{ $set: { applicationPhase: nextStage, status: "Admitted" } }
340+
{
341+
$set: {
342+
applicationPhase: nextStage,
343+
status: "Admitted",
344+
role: traineeRole._id,
345+
},
346+
}
308347
);
309348

349+
const updatedApplicant = await TraineeApplicant.findOne({
350+
_id: applicantId,
351+
})
352+
.populate("email")
353+
.lean();
354+
355+
const email = updatedApplicant?.email;
356+
357+
if (email) {
358+
await LoggedUserModel.updateOne(
359+
{ email },
360+
{
361+
$set: {
362+
applicationPhase: nextStage,
363+
status: "Admitted",
364+
role: traineeRole._id,
365+
},
366+
}
367+
);
368+
} else {
369+
throw new Error("Email not found for the provided applicant ID");
370+
}
310371
break;
311372

312373
case "Rejected":
@@ -316,7 +377,11 @@ export const applicationStageResolvers: any = {
316377
{ $set: { status: "Rejected", exitedAt: new Date() } }
317378
);
318379
}
319-
await Promise.all(models.map(model => updateApplicantAfterDismissed(model, applicantId)));
380+
await Promise.all(
381+
models.map((model) =>
382+
updateApplicantAfterDismissed(model, applicantId)
383+
)
384+
);
320385
const stageDismissedFrom = await TraineeApplicant.findOne({
321386
_id: applicantId,
322387
});
@@ -420,4 +485,4 @@ export const applicationStageResolvers: any = {
420485
}
421486
},
422487
},
423-
};
488+
};

Diff for: src/schema/traineeApplicantSchema.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export const typeDefsTrainee = gql`
3737
status: String!
3838
applicationPhase: ApplicationPhase!
3939
cohort: ID
40+
role: Role
4041
createdAt: String!
4142
user: User
4243
}
@@ -51,11 +52,11 @@ export const typeDefsTrainee = gql`
5152
}
5253
5354
type applicationCycle {
54-
_id: ID!
55-
name: String!
56-
startDate: String!
57-
endDate: String!
58-
}
55+
_id: ID!
56+
name: String!
57+
startDate: String!
58+
endDate: String!
59+
}
5960
6061
type AcceptTraineeResponse {
6162
success: Boolean!
@@ -77,6 +78,7 @@ export const typeDefsTrainee = gql`
7778
firstName: String!
7879
email: String!
7980
cycle_id: ID!
81+
role: ID
8082
attributes: traineeAttributeInput
8183
}
8284
@@ -88,6 +90,7 @@ export const typeDefsTrainee = gql`
8890
firstName: String
8991
lastName: String
9092
cycle_id: ID
93+
role: ID
9194
status: String
9295
}
9396

0 commit comments

Comments
 (0)