From b640c60bf47cb4731ba87cd430bbc251e9a3b69a Mon Sep 17 00:00:00 2001 From: Cameron Stirrup <43656137+MrRibcage@users.noreply.github.com> Date: Sat, 22 Jun 2024 12:16:52 -0400 Subject: [PATCH] Add review create (#12) * toString isn't needed on string objects * Implemented reviews-service create method, added jest tests * Update reviews-service.ts camelCase * Node vulnerability fix * Add minor format fix * Add minor fix --------- Co-authored-by: Matthew M-B --- app/challenges-platform/models/Review.ts | 4 +- .../services/challenges-service.ts | 2 +- .../services/participants-service.ts | 2 +- .../services/reviews-service.ts | 38 ++++++++++++++++++- .../services/submissions-service.ts | 2 +- package-lock.json | 14 +++---- .../services/reviews-service.test.ts | 37 ++++++++++++++++++ 7 files changed, 85 insertions(+), 14 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/challenges-service.ts b/app/challenges-platform/services/challenges-service.ts index e6604f4..1b9accb 100644 --- a/app/challenges-platform/services/challenges-service.ts +++ b/app/challenges-platform/services/challenges-service.ts @@ -50,7 +50,7 @@ export const create = async ({ const result = await db .insert(challenges) .values({ - uuid: id.toString(), + uuid: id, title: title, body: body, points: points, diff --git a/app/challenges-platform/services/participants-service.ts b/app/challenges-platform/services/participants-service.ts index c136bdf..2da5bb6 100644 --- a/app/challenges-platform/services/participants-service.ts +++ b/app/challenges-platform/services/participants-service.ts @@ -43,7 +43,7 @@ export const create = async ( const result = await db .insert(participants) .values({ - uuid: id.toString(), + uuid: id, email: email, }) .returning(); diff --git a/app/challenges-platform/services/reviews-service.ts b/app/challenges-platform/services/reviews-service.ts index 3d27499..a308b50 100644 --- a/app/challenges-platform/services/reviews-service.ts +++ b/app/challenges-platform/services/reviews-service.ts @@ -1,4 +1,38 @@ -export const create = async () => { +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, + submissionId: number, + body: string, +): Promise> => { + // TODO: add a check to make sure the submission exists // TODO: check if submission has already been approved or rejected, handle accordingly - throw new Error("Not implemented yet"); + + const id = uuid.create(); + + try { + const result = await db + .insert(reviews) + .values({ + uuid: id, + status: Status[status], + body: body, + submissionId: submissionId, + }) + .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")); + } }; diff --git a/app/challenges-platform/services/submissions-service.ts b/app/challenges-platform/services/submissions-service.ts index c45f63b..5f8fd4e 100644 --- a/app/challenges-platform/services/submissions-service.ts +++ b/app/challenges-platform/services/submissions-service.ts @@ -45,7 +45,7 @@ export const create = async ( const result = await db .insert(submissions) .values({ - uuid: id.toString(), + uuid: id, challengeId: challengeResult.val.id, participantId: participantResult.val.id, }) diff --git a/package-lock.json b/package-lock.json index ad5a28a..76a7cc7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2456,12 +2456,12 @@ } }, "node_modules/braces": { - "version": "3.0.2", - "resolved": "https://registry.npmjs.org/braces/-/braces-3.0.2.tgz", - "integrity": "sha512-b8um+L1RzM3WDSzvhm6gIz1yfTbBt6YTlcEKAvsmqCZZFw46z626lVj9j1yEPW33H5H+lBQpZMP1k8l+78Ha0A==", + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/braces/-/braces-3.0.3.tgz", + "integrity": "sha512-yQbXgO/OSZVD2IsiLlro+7Hf6Q18EJrKSEsdoMzKePKXct3gvD8oLcOQdIzGupr5Fj+EDe8gO/lxc1BzfMpxvA==", "dev": true, "dependencies": { - "fill-range": "^7.0.1" + "fill-range": "^7.1.1" }, "engines": { "node": ">=8" @@ -3583,9 +3583,9 @@ "integrity": "sha512-0Zt+s3L7Vf1biwWZ29aARiVYLx7iMGnEUl9x33fbB/j3jR81u/O2LbqK+Bm1CDSNDKVtJ/YjwY7TUd5SkeLQLw==" }, "node_modules/fill-range": { - "version": "7.0.1", - "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz", - "integrity": "sha512-qOo9F+dMUmC2Lcb4BbVvnKJxTPjCm+RRpe4gDuGrzkL7mEVl/djYSu2OdQ2Pa302N4oqkSg9ir6jaLWJ2USVpQ==", + "version": "7.1.1", + "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.1.1.tgz", + "integrity": "sha512-YsGpe3WHLK8ZYi4tWDg2Jy3ebRz2rXowDxnld4bkQB00cc/1Zw9AWnC0i9ztDJitivtQvaI9KaLyKrc+hBW0yg==", "dev": true, "dependencies": { "to-regex-range": "^5.0.1" 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..d3c85e3 --- /dev/null +++ b/test/challenges-platform/services/reviews-service.test.ts @@ -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 invalidId = -1; + const result = await ReviewsService.create( + Status.REJECTED, + invalidId, + "body", + ); + + expect(result.err).toBe(true); + expect(result.val.toString()).toBe("Error: Failed to create review"); + }); + }); + }); +});