Skip to content

Commit 8feeab2

Browse files
committed
add score validation logic
1 parent b8d5593 commit 8feeab2

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

backend/typescript/graphql/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ const executableSchema = makeExecutableSchema({
5151
adminCommentType,
5252
applicantRecordType,
5353
reviewPageType,
54-
reviewedApplicantRecordTypes
54+
reviewedApplicantRecordTypes,
5555
],
5656
resolvers: merge(
5757
authResolvers,

backend/typescript/services/implementations/reviewedApplicantRecordService.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,40 @@ import {
66
CreateReviewedApplicantRecordDTO,
77
DeleteReviewedApplicantRecordDTO,
88
UpdateReviewedApplicantRecordDTO,
9+
Review,
910
} from "../../types";
1011
import { getErrorMessage } from "../../utilities/errorUtils";
1112
import logger from "../../utilities/logger";
1213
import IReviewApplicantRecordService from "../interfaces/IReviewedApplicantRecordService";
1314

1415
const Logger = logger(__filename);
1516

17+
function validateReviewScores(review: Review | undefined): void {
18+
if (!review) return;
19+
20+
const scores = {
21+
passionFSG: review.passionFSG,
22+
teamPlayer: review.teamPlayer,
23+
desireToLearn: review.desireToLearn,
24+
skill: review.skill,
25+
};
26+
27+
Object.entries(scores).forEach(([field, value]) => {
28+
if (value !== undefined && (value < 1 || value > 5)) {
29+
throw new Error(
30+
`Invalid score for ${field}: ${value}. Scores must be between 1 and 5.`,
31+
);
32+
}
33+
});
34+
}
35+
1636
class ReviewedApplicantRecordService implements IReviewApplicantRecordService {
1737
/* eslint-disable class-methods-use-this */
1838
async createReviewedApplicantRecord(
1939
dto: CreateReviewedApplicantRecordDTO,
2040
): Promise<ReviewedApplicantRecordDTO> {
2141
try {
42+
validateReviewScores(dto.review);
2243
const record = await ReviewedApplicantRecord.create(dto);
2344
return record.toJSON() as ReviewedApplicantRecordDTO;
2445
} catch (error: unknown) {
@@ -35,6 +56,10 @@ class ReviewedApplicantRecordService implements IReviewApplicantRecordService {
3556
createReviewedApplicantRecordDTOs: CreateReviewedApplicantRecordDTO[],
3657
): Promise<ReviewedApplicantRecordDTO[]> {
3758
try {
59+
createReviewedApplicantRecordDTOs.forEach((dto) => {
60+
validateReviewScores(dto.review);
61+
});
62+
3863
const reviewedApplicantRecords = await sequelize.transaction(
3964
async (t) => {
4065
const records = await ReviewedApplicantRecord.bulkCreate(
@@ -146,6 +171,8 @@ class ReviewedApplicantRecordService implements IReviewApplicantRecordService {
146171
const oldReviewedScore = reviewedRecord.score || 0;
147172

148173
if (review !== undefined) {
174+
validateReviewScores(review);
175+
149176
reviewedRecord.review = {
150177
...reviewedRecord.review,
151178
...review,

0 commit comments

Comments
 (0)