-
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-service create method, added jest tests
- Loading branch information
Showing
3 changed files
with
80 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,42 @@ | ||
export const create = async () => { | ||
// TODO: check if submission has already been approved or rejected, handle accordingly | ||
throw new Error("Not implemented yet"); | ||
// export const create = async () => { | ||
// // TODO: check if submission has already been approved or rejected, handle accordingly | ||
// throw new Error("Not implemented yet"); | ||
// }; | ||
|
||
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, | ||
submission_id: number, | ||
body: string, | ||
): Promise<Result<Review, Error>> => { | ||
// TODO: add a check to make sure the submission exists | ||
|
||
const id = uuid.create(); | ||
|
||
try { | ||
const result = await db | ||
.insert(reviews) | ||
.values({ | ||
uuid: id, | ||
status: Status[status], | ||
body: body, | ||
submissionId: submission_id, | ||
}) | ||
.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")); | ||
} | ||
}; |
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,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 result = await ReviewsService.create( | ||
Status.REJECTED, | ||
-1, | ||
"bad id", | ||
); | ||
|
||
expect(result.err).toBe(true); | ||
expect(result.val.toString()).toBe("Error: Failed to create review"); | ||
}); | ||
}); | ||
}); | ||
}); |