Skip to content

Commit

Permalink
Implemented reviews-service create method, added jest tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MrRibcage committed Jun 11, 2024
1 parent f9af209 commit d23c596
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 5 deletions.
4 changes: 2 additions & 2 deletions app/challenges-platform/models/Review.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export enum Status {
}

export class Review {
id: string;
id: number;
status: Status;
comment: string | null;

Expand All @@ -13,7 +13,7 @@ export class Review {
status,
comment,
}: {
id: string;
id: number;
status: Status;
comment: string | null;
}) {
Expand Down
44 changes: 41 additions & 3 deletions app/challenges-platform/services/reviews-service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,42 @@
export const create = async () => {
// TODO: check if submission has already been approved or rejected, handle accordingly
throw new Error("Not implemented yet");
// export const create = async () => {
// // TODO: check if submission has already been approved or rejected, handle accordingly
// throw new Error("Not implemented yet");
// };

import { Ok, Err, Result } from "ts-results";
import { Review, Status } from "../models/Review";
import { db } from "../../../db";
import { reviews } from "../../../db/schema";
import { uuid } from "../../../app/common";

export const create = async (
status: Status,
submission_id: number,
body: string,
): Promise<Result<Review, Error>> => {
// TODO: add a check to make sure the submission exists

const id = uuid.create();

try {
const result = await db
.insert(reviews)
.values({
uuid: id,
status: Status[status],
body: body,
submissionId: submission_id,
})
.returning();

const review = new Review({
id: result[0].id,
status: status,
comment: result[0].body,
});

return Ok(review);
} catch (e) {
return Err(new Error("Failed to create review"));
}
};
37 changes: 37 additions & 0 deletions test/challenges-platform/services/reviews-service.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { ReviewsService } from "../../../app/challenges-platform";
import { Status } from "../../../app/challenges-platform/models";
import { submissionFactory } from "../factories/submission-factory";

describe("ReviewsService", () => {
describe("create", () => {
describe("when submission exists", () => {
it("succesfully creates a review", async () => {
const submission = await submissionFactory();

const body = "Nice work";

const result = await ReviewsService.create(
Status.APPROVED,
submission.id,
body,
);

if (!result.ok) fail("Expected result to be Ok");
expect(result.val.status).toBe(Status.APPROVED);
expect(result.val.comment).toBe(body);
});
});
describe("when submission does not exist", () => {
it("returns an error", async () => {
const result = await ReviewsService.create(
Status.REJECTED,
-1,
"bad id",
);

expect(result.err).toBe(true);
expect(result.val.toString()).toBe("Error: Failed to create review");
});
});
});
});

0 comments on commit d23c596

Please sign in to comment.