diff --git a/services/registration/src/routes/grading.ts b/services/registration/src/routes/grading.ts index ad0c376..cfee322 100644 --- a/services/registration/src/routes/grading.ts +++ b/services/registration/src/routes/grading.ts @@ -6,6 +6,7 @@ import { Types } from "mongoose"; import _ from "lodash"; import { ApplicationModel, Essay, StatusType } from "../models/application"; +import { ReferralModel, ReferralStatusType } from "../models/referral"; import { GraderModel } from "../models/grader"; import { Review, ReviewModel } from "../models/review"; import { BranchModel, BranchType, GradingGroupType } from "../models/branch"; @@ -15,6 +16,21 @@ import { getCalibrationMapping } from "../common/adjustScores"; const MAX_REVIEWS_PER_ESSAY = 2; // NOTE: No. of essays for each application. As such, will need to be updated whenever we add/remove essays. const ESSAY_COUNT = 4; +const REFERRAL_BONUS = 2; + +async function checkForReferralBonus(email?: string, hexathon?: Types.ObjectId) { + if (!email || !hexathon) { + return 0; + } + + const referral = await ReferralModel.exists({ + hexathon, + "status": ReferralStatusType.SUBMITTED, + "referralData.email": new RegExp(`^${_.escapeRegExp(email.trim())}$`, "i"), + }); + + return referral ? REFERRAL_BONUS : 0; +} type AggregatedEssay = { applicationId: string; @@ -304,7 +320,9 @@ gradingRouter.route("/actions/submit-review").post( if (allEssayReviews.length >= MAX_REVIEWS_PER_ESSAY * ESSAY_COUNT) { application.gradingComplete = true; const sumScores = allEssayReviews.reduce((prev, review) => prev + review.adjustedScore, 0); - application.finalScore = sumScores / allEssayReviews.length; + const baseScore = sumScores / allEssayReviews.length; + const referralBonus = await checkForReferralBonus(application); + application.finalScore = baseScore + referralBonus; await application.save(); }