From fbe38546fda203e087dbc7ab4ce5834115b4157f Mon Sep 17 00:00:00 2001 From: AJaccP Date: Sat, 1 Jun 2024 01:47:33 +0530 Subject: [PATCH] added check to see if a participant can access a challenge --- .../services/submissions-service.ts | 15 ++++++++++++--- db/schema.ts | 9 +++++++++ .../services/submissions-service.test.ts | 4 ++-- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/app/challenges-platform/services/submissions-service.ts b/app/challenges-platform/services/submissions-service.ts index c2832ce..223aaf1 100644 --- a/app/challenges-platform/services/submissions-service.ts +++ b/app/challenges-platform/services/submissions-service.ts @@ -1,10 +1,11 @@ import { Ok, Err, Result } from "ts-results"; import { db } from "../../../db"; -import { submissions } from "../../../db/schema"; +import { submissions, accessibleChallenges, participants } from "../../../db/schema"; import { uuid } from "../../../app/common"; import { Submission } from "../models"; import { ChallengesService, ParticipantsService } from "../services"; import { challengesPlatform } from ".."; +import { eq } from "drizzle-orm"; export const create = async ( challengeId: string, @@ -21,8 +22,16 @@ export const create = async ( return Err(new Error("Failed to find participant")); } - // TODO: Validate that the participant is allowed to submit this challenge (available challenges) - // throw new Error("Participant is not allowed to submit this challenge") if not allowed (use participant-service) + const accessibleChallengesResult = await db + .select({pId: accessibleChallenges.participantId,cId: accessibleChallenges.challengeId}) + .from(accessibleChallenges) + .where(eq(accessibleChallenges.participantId, participantResult.val.id) && eq(accessibleChallenges.challengeId, challengeResult.val.id)) + .execute(); + + if (accessibleChallengesResult.length === 0) { + return Err(new Error("Participant is not allowed to submit this challenge" )); + } + // TODO: switch on submission.challenge.evaluation // if submission.challenge.evaluation is MANUAL, than save it to the database diff --git a/db/schema.ts b/db/schema.ts index 66282c7..4ada70f 100644 --- a/db/schema.ts +++ b/db/schema.ts @@ -13,6 +13,14 @@ export const challenges = sqliteTable("challenges", { updatedAt: text("updated_at").default(sql`CURRENT_TIMESTAMP`), }); +export const accessibleChallenges = sqliteTable("accessible_challenges", { + id: integer("id").primaryKey(), + challengeId: integer("challenge_id").references(() => challenges.id), + participantId: integer("participant_id").references(() => participants.id), + createdAt: text("created_at").default(sql`CURRENT_TIMESTAMP`), + updatedAt: text("updated_at").default(sql`CURRENT_TIMESTAMP`), +}) + export const participants = sqliteTable("participants", { id: integer("id").primaryKey(), uuid: text("uuid").notNull(), @@ -40,3 +48,4 @@ export const reviews = sqliteTable("reviews", { createdAt: text("created_at").default(sql`CURRENT_TIMESTAMP`), updatedAt: text("updated_at").default(sql`CURRENT_TIMESTAMP`), }); + diff --git a/test/challenges-platform/services/submissions-service.test.ts b/test/challenges-platform/services/submissions-service.test.ts index 25508b5..0788d86 100644 --- a/test/challenges-platform/services/submissions-service.test.ts +++ b/test/challenges-platform/services/submissions-service.test.ts @@ -13,7 +13,7 @@ describe("SubmissionService", () => { challenge.uuid, participant.uuid, ); - + if (!result.ok) fail("Expected result to be Ok"); expect(result.val.challenge.id).toBe(challenge.id); expect(result.val.participant.id).toBe(participant.id); @@ -46,4 +46,4 @@ describe("SubmissionService", () => { }); }); }); -}); +}); \ No newline at end of file