Skip to content

Commit

Permalink
Cleanup submissions validations
Browse files Browse the repository at this point in the history
  • Loading branch information
MathyouMB committed Jun 4, 2024
1 parent fbe3854 commit 0aaee4f
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 30 deletions.
24 changes: 24 additions & 0 deletions app/challenges-platform/services/accesible-challenges-service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { accessibleChallenges } from "../../../db/schema";
import { Ok, Result } from "ts-results";
import { eq } from "drizzle-orm";
import { db } from "../../../db";
import { Challenge, Participant } from "../models";

export const count = async (
challenge: Challenge,
participant: Participant,
): Promise<Result<Number, Error>> => {
const accessibleChallengesResult = await db
.select({
pId: accessibleChallenges.participantId,
cId: accessibleChallenges.challengeId,
})
.from(accessibleChallenges)
.where(
eq(accessibleChallenges.participantId, participant.id) &&
eq(accessibleChallenges.challengeId, challenge.id),
)
.execute();

return Ok(accessibleChallengesResult.length);
};
1 change: 1 addition & 0 deletions app/challenges-platform/services/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * as AccesibleChallengesService from "./accesible-challenges-service";
export * as ChallengesService from "./challenges-service";
export * as ParticipantsService from "./participants-service";
export * as ReviewsService from "./reviews-service";
Expand Down
73 changes: 47 additions & 26 deletions app/challenges-platform/services/submissions-service.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,25 @@
import { Ok, Err, Result } from "ts-results";
import { db } from "../../../db";
import { submissions, accessibleChallenges, participants } from "../../../db/schema";
import { submissions } from "../../../db/schema";
import { uuid } from "../../../app/common";
import { Submission } from "../models";
import { ChallengesService, ParticipantsService } from "../services";
import { Challenge, Participant, Submission } from "../models";
import {
AccesibleChallengesService,
ChallengesService,
ParticipantsService,
} from "../services";
import { challengesPlatform } from "..";
import { eq } from "drizzle-orm";

export const create = async (
challengeId: string,
participantId: string,
type: string = "base",
metadata?: any,
): Promise<Result<Submission, Error>> => {
const challengeResult = await ChallengesService.findByUuid(challengeId);
if (!challengeResult.ok) {
return Err(new Error("Failed to find challenge"));
}
const participantResult = await ParticipantsService.findByUuid(participantId);
if (!participantResult.ok) {
return Err(new Error("Failed to find participant"));
}

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" ));
}
const result = await beforeCreate(challengeId, participantId);
if (!result.ok) return result;

const [challenge, participant] = result.val;

// TODO: switch on submission.challenge.evaluation
// if submission.challenge.evaluation is MANUAL, than save it to the database
Expand All @@ -52,18 +40,51 @@ export const create = async (
.insert(submissions)
.values({
uuid: id.toString(),
challengeId: challengeResult.val.id,
participantId: participantResult.val.id,
challengeId: challenge.id,
participantId: participant.id,
})
.returning();

const submission = transformer.newSubmission(
result[0],
challengeResult.val,
participantResult.val,
challenge,
participant,
);
return Ok(submission);
} catch (e) {
return Err(new Error("Failed to create submission"));
}
};

// private
const beforeCreate = async (
challengeId: string,
participantId: string,
): Promise<Result<[Challenge, Participant], Error>> => {
const challengeResult = await ChallengesService.findByUuid(challengeId);
if (!challengeResult.ok) {
return Err(new Error("Failed to find challenge"));
}

const participantResult = await ParticipantsService.findByUuid(participantId);
if (!participantResult.ok) {
return Err(new Error("Failed to find participant"));
}

const countResult = await AccesibleChallengesService.count(
challengeResult.val,
participantResult.val,
);
if (!countResult.ok) {
return Err(new Error("Failed to count accessible challenges"));
}

const count = countResult.val;
if (count === 0) {
return Err(
new Error("Participant is not allowed to submit this challenge"),
);
}

return Ok([challengeResult.val, participantResult.val]);
};
3 changes: 1 addition & 2 deletions db/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const accessibleChallenges = sqliteTable("accessible_challenges", {
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(),
Expand Down Expand Up @@ -48,4 +48,3 @@ 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 0aaee4f

Please sign in to comment.