Skip to content

Commit

Permalink
added check to see if a participant can access a challenge
Browse files Browse the repository at this point in the history
  • Loading branch information
AJaccP committed May 31, 2024
1 parent 0a9c3b2 commit fbe3854
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
15 changes: 12 additions & 3 deletions app/challenges-platform/services/submissions-service.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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
Expand Down
9 changes: 9 additions & 0 deletions db/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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`),
});

4 changes: 2 additions & 2 deletions test/challenges-platform/services/submissions-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -46,4 +46,4 @@ describe("SubmissionService", () => {
});
});
});
});
});

0 comments on commit fbe3854

Please sign in to comment.