Skip to content

Commit

Permalink
implemented submissions findByUuid function
Browse files Browse the repository at this point in the history
  • Loading branch information
AJaccP committed Jun 13, 2024
1 parent c0c15f5 commit 4e3097a
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 8 deletions.
26 changes: 24 additions & 2 deletions app/challenges-platform/services/challenges-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,34 @@ export const findByUuid = async (
.from(challenges)
.where(eq(challenges.uuid, id));

const transformer = challengesPlatform.findTransformer(result[0].type);
const challenge = transformer.newChallenge(result[0]);
const challenge = await convert(result[0]);

return Ok(challenge);
};

export const findById = async (
id: number,
): Promise<Result<Challenge, Error>> => {

const result = await db
.select()
.from(challenges)
.where(eq(challenges.id, id));

const challenge = await convert(result[0]);

return Ok(challenge);
};

export const convert = async (
result: any,
): Promise<Challenge> => {
const transformer = challengesPlatform.findTransformer(result.type);
const challenge = transformer.newChallenge(result);

return challenge;
}

export const create = async ({
title,
body,
Expand Down
32 changes: 27 additions & 5 deletions app/challenges-platform/services/participants-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,36 @@ export const findByUuid = async (
.from(participants)
.where(eq(participants.uuid, id));

const participant = await convert(result[0]);

return Ok(participant);
};

export const findById = async (
id: number,
): Promise<Result<Participant, Error>> => {

const result = await db
.select()
.from(participants)
.where(eq(participants.id, id));

const participant = await convert(result[0]);

return Ok(participant);
}

export const convert = async (
result: any,
): Promise<Participant> => {
const challenge = new Participant({
id: result[0].id,
uuid: result[0].uuid,
email: result[0].email,
id: result.id,
uuid: result.uuid,
email: result.email,
});

return Ok(challenge);
};
return challenge;
}

export const findByEmail = async (
email?: string,
Expand Down
35 changes: 35 additions & 0 deletions app/challenges-platform/services/submissions-service.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { eq } from "drizzle-orm";
import { Ok, Err, Result } from "ts-results";
import { db } from "../../../db";
import { submissions } from "../../../db/schema";
Expand All @@ -10,6 +11,40 @@ import {
} from "../services";
import { challengesPlatform } from "..";

export const findByUuid = async (
id: string,
type: string = "base",
): Promise<Result<Submission, Error>> => {
if (!uuid.isValid(id)) {
return Err(new Error("Invalid UUID"));
}

const result = await db
.select()
.from(submissions)
.where(eq(submissions.uuid, id));

const record = result[0];
if (!record.challengeId || !record.participantId) {
return Err(new Error("Invalid submission"));
}

const challengeResult = await ChallengesService.findById(record.challengeId);
if (!challengeResult.ok) {
return Err(new Error("Failed to find challenge"));
}

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

const transformer = challengesPlatform.findTransformer(type);
const submission = transformer.newSubmission(result[0], challengeResult.val, participantResult.val);

return Ok(submission);
};

export const create = async (
challengeId: string,
participantId: string,
Expand Down
17 changes: 16 additions & 1 deletion test/challenges-platform/services/challenges-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,22 @@ describe("ChallengesService", () => {
expect(result.val.points).toBe(100);
});
});
});
});

describe("findById", () => {
describe("when there is an existing record", () => {
it("returns the challenge", async () => {
const challenge = await challengeFactory();

const result = await ChallengesService.findById(challenge.id);

if (!result.ok) fail("Expected result to be Ok");
expect(result.val.title).toBe("Test Challenge");
expect(result.val.body).toBe("This is a test challenge");
expect(result.val.points).toBe(100);
});
});
});

describe("create", () => {
it("succesfully creates a challenge", async () => {
Expand Down
21 changes: 21 additions & 0 deletions test/challenges-platform/services/participants-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ describe("ParticipantsService", () => {
});

describe("findByUuid", () => {
describe("when the id is invalid", () => {
it("returns an error", async () => {
const result = await ParticipantsService.findByUuid("invalid-id");

expect(result.err).toBe(true);
expect(result.val.toString()).toBe("Error: Invalid UUID");
});
});

describe("when there is an existing record", () => {
it("returns the participant", async () => {
const participant = await participantFactory();
Expand All @@ -27,6 +36,18 @@ describe("ParticipantsService", () => {
});
});

describe("findById", () => {
describe("when there is an existing record", () => {
it("returns the participant", async () => {
const participant = await participantFactory();
const result = await ParticipantsService.findById(participant.id);

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

describe("findByEmail", () => {
it("throws an error", async () => {
const result = await ParticipantsService.findByEmail();
Expand Down
28 changes: 28 additions & 0 deletions test/challenges-platform/services/submissions-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,34 @@ import { challengeFactory } from "../factories/challenge-factory";
import { participantFactory } from "../factories/participant-factory";

describe("SubmissionService", () => {
describe("findByUuid", () => {
describe("when the id is invalid", () => {
it("returns an error", async () => {
const result = await SubmissionService.findByUuid("invalid-id");

expect(result.err).toBe(true);
expect(result.val.toString()).toBe("Error: Invalid UUID");
});
});
describe("when there is an existing record", () => {
it("returns the submission", async () => {
const challenge = await challengeFactory();
const participant = await participantFactory();

const submission = await SubmissionService.create(
challenge.uuid,
participant.uuid,
);
if (!submission.ok) fail("Expected submission to be Ok");

const result = await SubmissionService.findByUuid(submission.val.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);
});
});
});
describe("create", () => {
describe("when challenge and participant exist", () => {
it("succesfully creates a submission", async () => {
Expand Down

0 comments on commit 4e3097a

Please sign in to comment.