Skip to content

Commit

Permalink
storing puzzle answer in db instead of env
Browse files Browse the repository at this point in the history
  • Loading branch information
aletya committed Nov 3, 2024
1 parent 19a35aa commit c6dacf7
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/common/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -65,6 +65,7 @@ enum NotificationCollection {

enum PuzzleCollection {
RUNES_AND_RIDDLES = "runesriddles",
ANSWERS = "answers",
}

enum RegistrationCollection {
Expand Down Expand Up @@ -142,6 +143,7 @@ export default class Models {

// Puzzle
static PuzzleItem: Model<PuzzleItem> = getModel(PuzzleItem, Group.PUZZLE, PuzzleCollection.RUNES_AND_RIDDLES);
static PuzzleAnswer: Model<PuzzleAnswer> = getModel(PuzzleAnswer, Group.PUZZLE, PuzzleCollection.ANSWERS);

// Registration
static RegistrationApplication: Model<RegistrationApplication> = getModel(
Expand Down
6 changes: 4 additions & 2 deletions src/services/puzzle/puzzle-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });

Expand All @@ -120,7 +122,7 @@ puzzleRouter.post(
}

// Incorrect
if (answer !== Config.PUZZLE[qid]) {
if (answer !== correctAnswer) {
return res.status(StatusCode.ClientErrorBadRequest).send(PuzzleIncorrectAnswerError);
}

Expand Down
8 changes: 8 additions & 0 deletions src/services/puzzle/puzzle-schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" });

Expand Down

0 comments on commit c6dacf7

Please sign in to comment.