Skip to content

Commit

Permalink
Add review create (#12)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
MrRibcage and MathyouMB authored Jun 22, 2024
1 parent a913a25 commit b640c60
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 14 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
2 changes: 1 addition & 1 deletion app/challenges-platform/services/challenges-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion app/challenges-platform/services/participants-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const create = async (
const result = await db
.insert(participants)
.values({
uuid: id.toString(),
uuid: id,
email: email,
})
.returning();
Expand Down
38 changes: 36 additions & 2 deletions app/challenges-platform/services/reviews-service.ts
Original file line number Diff line number Diff line change
@@ -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<Result<Review, Error>> => {
// 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"));
}
};
2 changes: 1 addition & 1 deletion app/challenges-platform/services/submissions-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
Expand Down
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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 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");
});
});
});
});

0 comments on commit b640c60

Please sign in to comment.