Skip to content

Commit

Permalink
update applicant status to Admitted/Accepted and change role to train…
Browse files Browse the repository at this point in the history
…ee (#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]>
  • Loading branch information
Mugisha146 and El-zizo Mugisha authored Nov 19, 2024
1 parent fa6c505 commit 45a8f94
Show file tree
Hide file tree
Showing 4 changed files with 198 additions and 96 deletions.
41 changes: 28 additions & 13 deletions src/models/traineeApplicant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ import mongoose, { Schema } from "mongoose";
const CycleAppliedSchema = new Schema({
cycle: {
type: Schema.Types.ObjectId,
ref: 'ApplicationCycle',
required: true
ref: "ApplicationCycle",
required: true,
},
})
});

const TraineeApplicantSchema = new Schema({
const TraineeApplicantSchema = new Schema(
{
user: {
type: Schema.Types.ObjectId,
ref: 'User'
ref: "User",
},
email: {
type: String,
Expand All @@ -26,10 +27,10 @@ const TraineeApplicantSchema = new Schema({
type: String,
required: true,
},
cycle_id:{
cycle_id: {
type: mongoose.Schema.Types.ObjectId,
ref: "applicationCycle",
required: true
required: true,
},
delete_at: {
type: Boolean,
Expand All @@ -38,21 +39,35 @@ const TraineeApplicantSchema = new Schema({
cycleApplied: [CycleAppliedSchema],
applicationPhase: {
type: String,
enum: ["Applied", 'Shortlisted', 'Technical Assessment', 'Interview Assessment', 'Admitted', 'Rejected', "Enrolled"],
enum: [
"Applied",
"Shortlisted",
"Technical Assessment",
"Interview Assessment",
"Admitted",
"Rejected",
"Enrolled",
],
default: "Applied",
},
status: {
type: String,
default: "No action",
},
role: {
type: Schema.Types.ObjectId,
ref: "Role",
},
cohort: {
type: Schema.Types.ObjectId,
ref: "cohortModel",
}
},{
},
},
{
timestamps: true,
});
}
);

const TraineeApplicant = mongoose.model('Trainees', TraineeApplicantSchema);
const TraineeApplicant = mongoose.model("Trainees", TraineeApplicantSchema);

export default TraineeApplicant;
export default TraineeApplicant;
105 changes: 85 additions & 20 deletions src/resolvers/applicationStageResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import InterviewAssessment from "../models/InterviewAssessmentStageSchema";
import TechnicalAssessment from "../models/technicalAssessmentStage";
import mongoose from "mongoose";
import { traineEAttributes } from "../models/traineeAttribute";
import { LoggedUserModel } from "../models/AuthUser";

const validStages = [
"Shortlisted",
Expand Down Expand Up @@ -93,28 +94,37 @@ export const applicationStageResolvers: any = {
getTraineeCyclesApplications: async (_: any, __: any, context: any) => {
try {
if (!context.currentUser) {
throw new CustomGraphQLError("You must be logged in to view your applications");
throw new CustomGraphQLError(
"You must be logged in to view your applications"
);
}

const applications = await TraineeApplicant.findOne({ email: context.currentUser.email })
const applications = await TraineeApplicant.findOne({
email: context.currentUser.email,
})
.populate("cycle_id")
.lean();
return applications
}
catch (error: any) {
return applications;
} catch (error: any) {
console.error("Error retrieving applications with attributes:", error);
throw new CustomGraphQLError(error);
}
},
getApplicationsAttributes: async (_: any, { trainee_id }: any, context: any) => {
getApplicationsAttributes: async (
_: any,
{ trainee_id }: any,
context: any
) => {
try {
if (!context.currentUser) {
throw new CustomGraphQLError("You must be logged in to view your applications");
throw new CustomGraphQLError(
"You must be logged in to view your applications"
);
}

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

return attributes
return attributes;
} catch (error) {
console.error("Error getting attributes:", error);
throw new CustomGraphQLError(error);
Expand All @@ -123,12 +133,21 @@ export const applicationStageResolvers: any = {
getApplicationStages: async (_: any, { trainee_id }: any, context: any) => {
try {
if (!context.currentUser) {
throw new CustomGraphQLError("You must be logged in to view your applications");
throw new CustomGraphQLError(
"You must be logged in to view your applications"
);
}

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

const [shortlistStage, technicalStage, interviewStage, admittedStage, dismissedStage, AllStages] = await Promise.all([
const [
shortlistStage,
technicalStage,
interviewStage,
admittedStage,
dismissedStage,
AllStages,
] = await Promise.all([
Shortlisted.findOne({ applicantId: trainee }),
TechnicalAssessment.findOne({ applicantId: trainee }),
InterviewAssessment.findOne({ applicantId: trainee }),
Expand All @@ -143,15 +162,15 @@ export const applicationStageResolvers: any = {
interview: interviewStage,
admitted: admittedStage,
dismissed: dismissedStage,
allStages: AllStages
}


allStages: AllStages,
};
} catch (error) {
console.error("Error retrieving application stages:", error);
throw new CustomGraphQLError(error || "An error occurred while retrieving applications");
throw new CustomGraphQLError(
error || "An error occurred while retrieving applications"
);
}
}
},
},
Mutation: {
moveToNextStage: async (
Expand Down Expand Up @@ -241,6 +260,15 @@ export const applicationStageResolvers: any = {
await stageTracking.save();
}

let traineeRole = await RoleModel.findOne({ roleName: "trainee" });
if (!traineeRole) {
traineeRole = await RoleModel.create({
roleName: "trainee",
description: "A user who has been accepted as a trainee",
permissions: [],
});
}

let message = "";
switch (nextStage) {
case "Technical Assessment":
Expand Down Expand Up @@ -294,19 +322,52 @@ export const applicationStageResolvers: any = {
);
}

await Promise.all(models.map(model => updateApplicantAfterAdmitted(model, applicantId)));
await Promise.all(
models.map((model) =>
updateApplicantAfterAdmitted(model, applicantId)
)
);

await Admitted.create({
applicantId,
comments,
status: "Passed",
});
message = `Applicant passed the application stage✅.`;

await TraineeApplicant.updateOne(
{ _id: applicantId },
{ $set: { applicationPhase: nextStage, status: "Admitted" } }
{
$set: {
applicationPhase: nextStage,
status: "Admitted",
role: traineeRole._id,
},
}
);

const updatedApplicant = await TraineeApplicant.findOne({
_id: applicantId,
})
.populate("email")
.lean();

const email = updatedApplicant?.email;

if (email) {
await LoggedUserModel.updateOne(
{ email },
{
$set: {
applicationPhase: nextStage,
status: "Admitted",
role: traineeRole._id,
},
}
);
} else {
throw new Error("Email not found for the provided applicant ID");
}
break;

case "Rejected":
Expand All @@ -316,7 +377,11 @@ export const applicationStageResolvers: any = {
{ $set: { status: "Rejected", exitedAt: new Date() } }
);
}
await Promise.all(models.map(model => updateApplicantAfterDismissed(model, applicantId)));
await Promise.all(
models.map((model) =>
updateApplicantAfterDismissed(model, applicantId)
)
);
const stageDismissedFrom = await TraineeApplicant.findOne({
_id: applicantId,
});
Expand Down Expand Up @@ -420,4 +485,4 @@ export const applicationStageResolvers: any = {
}
},
},
};
};
13 changes: 8 additions & 5 deletions src/schema/traineeApplicantSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export const typeDefsTrainee = gql`
status: String!
applicationPhase: ApplicationPhase!
cohort: ID
role: Role
createdAt: String!
user: User
}
Expand All @@ -51,11 +52,11 @@ export const typeDefsTrainee = gql`
}
type applicationCycle {
_id: ID!
name: String!
startDate: String!
endDate: String!
}
_id: ID!
name: String!
startDate: String!
endDate: String!
}
type AcceptTraineeResponse {
success: Boolean!
Expand All @@ -77,6 +78,7 @@ export const typeDefsTrainee = gql`
firstName: String!
email: String!
cycle_id: ID!
role: ID
attributes: traineeAttributeInput
}
Expand All @@ -88,6 +90,7 @@ export const typeDefsTrainee = gql`
firstName: String
lastName: String
cycle_id: ID
role: ID
status: String
}
Expand Down
Loading

0 comments on commit 45a8f94

Please sign in to comment.