Skip to content

Commit

Permalink
Added reviews service findByUuid method
Browse files Browse the repository at this point in the history
  • Loading branch information
AJaccP authored and MathyouMB committed Jun 22, 2024
1 parent b640c60 commit ad05ec8
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 0 deletions.
4 changes: 4 additions & 0 deletions app/challenges-platform/models/Review.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,23 @@ export enum Status {

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

constructor({
id,
uuid: uuid,
status,
comment,
}: {
id: number;
uuid: string;
status: Status;
comment: string | null;
}) {
this.id = id;
this.uuid = uuid;
this.status = status;
this.comment = comment;
}
Expand Down
42 changes: 42 additions & 0 deletions app/challenges-platform/services/reviews-service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,28 @@
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<Result<Review, Error>> => {
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 create = async (
status: Status,
submissionId: number,
Expand All @@ -27,6 +46,7 @@ export const create = async (

const review = new Review({
id: result[0].id,
uuid: id,
status: status,
comment: result[0].body,
});
Expand All @@ -36,3 +56,25 @@ export const create = async (
return Err(new Error("Failed to create review"));
}
};
export const convert = async (result: any): Promise<Result<Review, Error>> => {
let status: Status;
switch (result.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.id,
uuid: result.uuid,
status: status,
comment: result.body,
});

return Ok(review);
};
43 changes: 43 additions & 0 deletions test/challenges-platform/factories/review-factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import {
Review,
Status,
Submission,
} from "../../../app/challenges-platform/models";
import { uuid } from "../../../app/common";
import { db } from "../../../db";
import { reviews } from "../../../db/schema";
import { submissionFactory } from "./submission-factory";

export const reviewFactory = async ({
status,
submission,
body,
}: {
status?: Status;
submission?: Submission;
body?: string;
} = {}): Promise<Review> => {
const st = status || Status.APPROVED;
const s = submission || (await submissionFactory());
const b = body || "test review";

const statusString = st === Status.APPROVED ? "approved" : "rejected";
const insertResult = await db
.insert(reviews)
.values({
uuid: uuid.create(),
status: statusString.toString(),
submissionId: s.id,
body: b,
})
.returning();

const review = new Review({
id: insertResult[0].id,
uuid: insertResult[0].uuid,
status: st,
comment: b,
});

return review;
};
39 changes: 39 additions & 0 deletions test/challenges-platform/services/reviews-service.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { ReviewsService } from "../../../app/challenges-platform";
import { Status } from "../../../app/challenges-platform/models";
import { uuid } from "../../../app/common";
import { reviewFactory } from "../factories/review-factory";
import { submissionFactory } from "../factories/submission-factory";

describe("ReviewsService", () => {
Expand Down Expand Up @@ -34,4 +36,41 @@ describe("ReviewsService", () => {
});
});
});
describe("findByUuid", () => {
describe("when review exists", () => {
it("returns the review", async () => {
const body = "Nice work";
const submission = await submissionFactory();
const review = await reviewFactory({
submission: submission,
body: body,
});

const result = await ReviewsService.findByUuid(review.uuid);

if (!result.ok) fail("Expected result to be Ok");
expect(result.val.status).toBe(Status.APPROVED);
expect(result.val.comment).toBe(body);
});
});
describe("when review does not exist", () => {
it("returns an error", async () => {
const invalidUuid = "invalid-uuid";
const result = await ReviewsService.findByUuid(invalidUuid);

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

describe("when uuid is invalid", () => {
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");
});
});
});
});

0 comments on commit ad05ec8

Please sign in to comment.