-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implemented reviews findByUuid function
- Loading branch information
Showing
3 changed files
with
78 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}); | ||
}); |