Skip to content

Commit

Permalink
implemented reviews findByUuid function
Browse files Browse the repository at this point in the history
  • Loading branch information
AJaccP committed Jun 14, 2024
1 parent 4e3097a commit bfa3202
Show file tree
Hide file tree
Showing 3 changed files with 78 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
53 changes: 50 additions & 3 deletions app/challenges-platform/services/reviews-service.ts
Original file line number Diff line number Diff line change
@@ -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<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 convert = async (
result: any,
): Promise<Result<Review, Error>> => {
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);
}
26 changes: 26 additions & 0 deletions test/challenges-platform/services/reviews-service.test.ts
Original file line number Diff line number Diff line change
@@ -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
});
});

0 comments on commit bfa3202

Please sign in to comment.