From c6dacf7325ecfb3481f0243f3d1403f4ad51a4ab Mon Sep 17 00:00:00 2001 From: Alex Yang <32620988+DatProJack@users.noreply.github.com> Date: Sun, 3 Nov 2024 13:05:24 -0600 Subject: [PATCH] storing puzzle answer in db instead of env --- src/common/models.ts | 4 +++- src/services/puzzle/puzzle-router.ts | 6 ++++-- src/services/puzzle/puzzle-schemas.ts | 8 ++++++++ 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/common/models.ts b/src/common/models.ts index 9df77b9e..4bd58604 100644 --- a/src/common/models.ts +++ b/src/common/models.ts @@ -13,7 +13,7 @@ import { UserAttendance, UserFollowing, UserInfo } from "../services/user/user-s import { AnyParamConstructor, IModelOptions } from "@typegoose/typegoose/lib/types"; import { StaffShift } from "../services/staff/staff-schemas"; import { NotificationMappings, NotificationMessages } from "../services/notification/notification-schemas"; -import { PuzzleItem } from "../services/puzzle/puzzle-schemas"; +import { PuzzleItem, PuzzleAnswer } from "../services/puzzle/puzzle-schemas"; // Groups for collections export enum Group { @@ -65,6 +65,7 @@ enum NotificationCollection { enum PuzzleCollection { RUNES_AND_RIDDLES = "runesriddles", + ANSWERS = "answers", } enum RegistrationCollection { @@ -142,6 +143,7 @@ export default class Models { // Puzzle static PuzzleItem: Model = getModel(PuzzleItem, Group.PUZZLE, PuzzleCollection.RUNES_AND_RIDDLES); + static PuzzleAnswer: Model = getModel(PuzzleAnswer, Group.PUZZLE, PuzzleCollection.ANSWERS); // Registration static RegistrationApplication: Model = getModel( diff --git a/src/services/puzzle/puzzle-router.ts b/src/services/puzzle/puzzle-router.ts index 4374fc1c..8ff4d075 100644 --- a/src/services/puzzle/puzzle-router.ts +++ b/src/services/puzzle/puzzle-router.ts @@ -103,9 +103,11 @@ puzzleRouter.post( return res.status(StatusCode.ClientErrorNotFound).send(PuzzleQuestionNotFoundError); } - if (qid < 0 || qid >= Config.PUZZLE.length) { + const puzzleAnswer = await Models.PuzzleAnswer.findOne({ qid: qid }); + if (!puzzleAnswer) { return res.status(StatusCode.ClientErrorNotFound).send(PuzzleQuestionNotFoundError); } + const correctAnswer = puzzleAnswer.answer; const puzzle = await Models.PuzzleItem.findOne({ userId }); @@ -120,7 +122,7 @@ puzzleRouter.post( } // Incorrect - if (answer !== Config.PUZZLE[qid]) { + if (answer !== correctAnswer) { return res.status(StatusCode.ClientErrorBadRequest).send(PuzzleIncorrectAnswerError); } diff --git a/src/services/puzzle/puzzle-schemas.ts b/src/services/puzzle/puzzle-schemas.ts index f50e2ef6..efd6239d 100644 --- a/src/services/puzzle/puzzle-schemas.ts +++ b/src/services/puzzle/puzzle-schemas.ts @@ -31,6 +31,14 @@ export class PuzzleItem { } } +export class PuzzleAnswer { + @prop({ required: true }) + public qid: number; + + @prop({ required: true }) + public answer: string; +} + export const PuzzleQuestionIdSchema = z.string().openapi({ example: "1" }); export const PuzzleTeamNameSchema = z.string().openapi({ example: "team team" });