From bfa3202a987998b651da11d06cde14bd25e620e0 Mon Sep 17 00:00:00 2001 From: AJaccP Date: Fri, 14 Jun 2024 12:34:14 +0530 Subject: [PATCH] implemented reviews findByUuid function --- app/challenges-platform/models/Review.ts | 4 +- .../services/reviews-service.ts | 53 +++++++++++++++++-- .../services/reviews-service.test.ts | 26 +++++++++ 3 files changed, 78 insertions(+), 5 deletions(-) create mode 100644 test/challenges-platform/services/reviews-service.test.ts diff --git a/app/challenges-platform/models/Review.ts b/app/challenges-platform/models/Review.ts index ac7afcb..42cd456 100644 --- a/app/challenges-platform/models/Review.ts +++ b/app/challenges-platform/models/Review.ts @@ -4,7 +4,7 @@ export enum Status { } export class Review { - id: string; + id: number; status: Status; comment: string | null; @@ -13,7 +13,7 @@ export class Review { status, comment, }: { - id: string; + id: number; status: Status; comment: string | null; }) { diff --git a/app/challenges-platform/services/reviews-service.ts b/app/challenges-platform/services/reviews-service.ts index 3d27499..edd7656 100644 --- a/app/challenges-platform/services/reviews-service.ts +++ b/app/challenges-platform/services/reviews-service.ts @@ -1,4 +1,51 @@ -export const create = async () => { - // TODO: check if submission has already been approved or rejected, handle accordingly - throw new Error("Not implemented yet"); +import { eq } from "drizzle-orm"; +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 findByUuid = async ( + id: string, +): Promise> => { + if (!uuid.isValid(id)) { + return Err(new Error("Invalid UUID")); + } + + const result = await db + .select() + .from(reviews) + .where(eq(reviews.uuid, id)); + + if (result.length === 0) { + return Err(new Error("Review not found")); + } + + const review = await convert(result[0]); + + return review; }; + +export const convert = async ( + result: any, +): Promise> => { + let status: Status; + switch (result[0].status) { + case "approved": + status = Status.APPROVED; + break; + case "rejected": + status = Status.REJECTED; + break; + default: + return Err(new Error("Invalid status")); + } + + const review = new Review({ + id: result[0].id, + status: status, + comment: result[0].body, + }); + + return Ok(review); +} diff --git a/test/challenges-platform/services/reviews-service.test.ts b/test/challenges-platform/services/reviews-service.test.ts new file mode 100644 index 0000000..44ae62c --- /dev/null +++ b/test/challenges-platform/services/reviews-service.test.ts @@ -0,0 +1,26 @@ +import { ReviewsService } from "../../../app/challenges-platform"; +import { Status } from "../../../app/challenges-platform/models"; +import { uuid } from "../../../app/common"; + +describe("ReviewsService", () => { + describe("findByUuid", () => { + describe("when the id is invalid", () => { + it("returns an error", async () => { + const result = await ReviewsService.findByUuid("invalid-id"); + + expect(result.err).toBe(true); + expect(result.val.toString()).toBe("Error: Invalid UUID"); + }); + }); + describe("when there is no record", () => { + it("returns an error", async () => { + const testUuid = uuid.create(); + const result = await ReviewsService.findByUuid(testUuid); + + expect(result.err).toBe(true); + expect(result.val.toString()).toBe("Error: Review not found"); + }); + }); + // implement test for when there is an existing record, need factory(?) + create method + }); +}); \ No newline at end of file