Skip to content

Commit

Permalink
Added accessible challenges check
Browse files Browse the repository at this point in the history
  • Loading branch information
AJaccP committed Jun 14, 2024
1 parent 02daa66 commit bcaf2dd
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/challenges-platform/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export {
ParticipantsService,
ReviewsService,
SubmissionService,
AccessibleChallengesService,
} from "./services";
import { Transformer } from "./models";
import { transformers } from "../../config/challenges-platform/transformers";
Expand Down
24 changes: 24 additions & 0 deletions app/challenges-platform/services/accessible-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 AccessibleChallengesService from "./accessible-challenges-service";
export * as ChallengesService from "./challenges-service";
export * as ParticipantsService from "./participants-service";
export * as ReviewsService from "./reviews-service";
Expand Down
8 changes: 8 additions & 0 deletions db/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ export const challenges = sqliteTable("challenges", {
deleted: integer("deleted", { mode: "boolean" }).notNull().default(false),
});

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
32 changes: 32 additions & 0 deletions test/challenges-platform/factories/accessible-challenge-factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { challengeFactory } from "./challenge-factory";
import { participantFactory } from "./participant-factory";
import {
Challenge,
Participant,
} from "../../../app/challenges-platform/models";
import { AccessibleChallengesService } from "../../../app/challenges-platform";
import { db } from "../../../db";
import { accessibleChallenges } from "../../../db/schema";

export const accessibleChallengeFactory = async ({
challenge,
participant,
}: {
challenge?: Challenge;
participant?: Participant;
} = {}): Promise<Number> => {
const c = challenge || (await challengeFactory());
const p = participant || (await participantFactory());

const insertResult = await db
.insert(accessibleChallenges)
.values({
challengeId: c.id,
participantId: p.id,
})
.returning();

const result = await AccessibleChallengesService.count(c, p);
if (!result.ok) fail("Expected result to be Ok");
return result.val;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { challengeFactory } from "../factories/challenge-factory";
import { participantFactory } from "../factories/participant-factory";
import { accessibleChallengeFactory } from "../factories/accessible-challenge-factory";
import { AccessibleChallengesService } from "../../../app/challenges-platform";

describe("AccessibleChallengesService", () => {
describe("count", () => {
describe("when the challenge is not accessible", () => {
it("returns 0", async () => {
const challenge = await challengeFactory();
const participant = await participantFactory();

const result = await AccessibleChallengesService.count(challenge, participant);

if (!result.ok) fail("Expected result to be Ok");
expect(result.val).toBe(0);
});
});
describe("when the challenge is accessible", () => {
it("returns 1", async () => {
const challenge = await challengeFactory();
const participant = await participantFactory();

const insert = await accessibleChallengeFactory({
challenge,
participant
});

const result = await AccessibleChallengesService.count(challenge, participant);

if (!result.ok) fail("Expected result to be Ok");
expect(result.val).toBe(1);
});
});
})
});

0 comments on commit bcaf2dd

Please sign in to comment.