-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an endpoint to create new assignments
- Loading branch information
Showing
12 changed files
with
296 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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
CREATE MIGRATION m1h2a4cqyvh3sfyprjlvecaakpxyoaz53e3egs4qdlf2zeapdzfvqa | ||
ONTO m1g3bsb7rj3ypvusgnkekg6g7zetyzd7xvl564gqjkdb4zwjfphyyq | ||
{ | ||
CREATE TYPE default::Assignment { | ||
CREATE REQUIRED LINK class: default::Class { | ||
SET readonly := true; | ||
}; | ||
CREATE REQUIRED MULTI LINK completedBy: default::User; | ||
CREATE REQUIRED MULTI LINK updatedBy: default::User; | ||
CREATE REQUIRED PROPERTY description: std::str; | ||
CREATE REQUIRED PROPERTY dueDate: std::datetime; | ||
CREATE REQUIRED PROPERTY fromDate: std::datetime; | ||
CREATE REQUIRED PROPERTY subject: std::str; | ||
CREATE REQUIRED MULTI PROPERTY updates: std::datetime { | ||
SET default := (std::datetime_current()); | ||
}; | ||
}; | ||
ALTER TYPE default::Class { | ||
CREATE LINK assignments := (.<class[IS default::Assignment]); | ||
}; | ||
ALTER TYPE default::User { | ||
CREATE LINK assignments := (.<updatedBy[IS default::Assignment]); | ||
}; | ||
}; |
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,9 @@ | ||
CREATE MIGRATION m1whcftw3ixmaofcafcskg2ttd5f4kpecobinvtejd66vge4p5whxq | ||
ONTO m1h2a4cqyvh3sfyprjlvecaakpxyoaz53e3egs4qdlf2zeapdzfvqa | ||
{ | ||
ALTER TYPE default::Assignment { | ||
ALTER LINK completedBy { | ||
RESET OPTIONALITY; | ||
}; | ||
}; | ||
}; |
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
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,124 @@ | ||
import e from "@edgedb"; | ||
import { | ||
DATABASE_READ_FAILED, | ||
DATABASE_WRITE_FAILED, | ||
FORBIDDEN, | ||
UNAUTHORIZED, | ||
} from "constants/responses"; | ||
import Elysia, { t } from "elysia"; | ||
import { HttpStatusCode } from "elysia-http-status-code"; | ||
import { client } from "index"; | ||
import { auth } from "plugins/auth"; | ||
import { customDateToNormal } from "utils/dates/customAndNormal"; | ||
import { doesClassExist } from "utils/db/classes"; | ||
import { promiseResult } from "utils/errors"; | ||
import { responseBuilder } from "utils/response"; | ||
|
||
export const createAssignment = new Elysia() | ||
.use(HttpStatusCode()) | ||
.use(auth) | ||
.post( | ||
"/", | ||
async ({ auth, set, httpStatus, body }) => { | ||
if (!auth.isAuthorized) { | ||
set.status = httpStatus.HTTP_401_UNAUTHORIZED; | ||
return UNAUTHORIZED; | ||
} | ||
|
||
const from = customDateToNormal(body.from) | ||
const due = customDateToNormal(body.due) | ||
if (due < from) { | ||
set.status = httpStatus.HTTP_400_BAD_REQUEST; | ||
return responseBuilder('error', { error: "Due must not be earlier then from" }); | ||
} | ||
|
||
|
||
const classExists = await promiseResult(() => doesClassExist({ | ||
schoolName: body.school, | ||
className: body.class | ||
})) | ||
if (classExists.isError) { | ||
set.status = httpStatus.HTTP_500_INTERNAL_SERVER_ERROR | ||
return DATABASE_READ_FAILED | ||
} | ||
if (!classExists.data) { | ||
set.status = httpStatus.HTTP_404_NOT_FOUND; | ||
return responseBuilder('error', { | ||
error: "Can't find that school or class" | ||
}) | ||
} | ||
|
||
|
||
const isUserInClassQuery = e.count( | ||
e.select(e.Class, (c) => { | ||
const classNameMatches = e.op(c.name, "=", body.class); | ||
const schoolMatches = e.op( | ||
classNameMatches, | ||
"and", | ||
e.op(c.school.name, "=", body.school), | ||
); | ||
const userMatches = e.op(c.students.username, "=", auth.username); | ||
|
||
return { filter_single: e.op(schoolMatches, "and", userMatches) }; | ||
}), | ||
); | ||
const isUserInClassResult = await promiseResult(() => | ||
isUserInClassQuery.run(client), | ||
); | ||
if (isUserInClassResult.isError) { | ||
set.status = httpStatus.HTTP_500_INTERNAL_SERVER_ERROR; | ||
return DATABASE_READ_FAILED; | ||
} | ||
if (isUserInClassResult.data === 0) { | ||
set.status = httpStatus.HTTP_403_FORBIDDEN; | ||
return FORBIDDEN; | ||
} | ||
|
||
const insertQuery = e.insert(e.Assignment, { | ||
subject: body.subject, | ||
description: body.description, | ||
fromDate: from, | ||
dueDate: due, | ||
class: e.select(e.Class, (c) => { | ||
const classNameMatches = e.op(c.name, "=", body.class); | ||
const schoolMatches = e.op(c.school.name, "=", body.school); | ||
return { | ||
filter_single: e.op(classNameMatches, "and", schoolMatches), | ||
}; | ||
}), | ||
updatedBy: e.select(e.User, (u) => ({ | ||
filter_single: e.op(u.username, "=", auth.username), | ||
})), | ||
}); | ||
const insertResult = await promiseResult(() => insertQuery.run(client)); | ||
if (insertResult.isError) { | ||
set.status = httpStatus.HTTP_500_INTERNAL_SERVER_ERROR; | ||
return DATABASE_WRITE_FAILED; | ||
} | ||
|
||
set.status = httpStatus.HTTP_201_CREATED; | ||
return responseBuilder("success", { | ||
message: "Successfully created assignment", | ||
data: insertResult.data | ||
}); | ||
}, | ||
{ | ||
body: t.Object({ | ||
school: t.String({ minLength: 1 }), | ||
class: t.String({ minLength: 1 }), | ||
|
||
subject: t.String({ minLength: 1 }), | ||
description: t.String({ minLength: 1 }), | ||
from: t.Object( { | ||
day: t.Number({ minimum: 1, maximum: 31 }), | ||
month: t.Number({ minimum: 1, maximum: 12 }), | ||
year: t.Number({ minimum: 1970 }), | ||
}), | ||
due: t.Object({ | ||
day: t.Number({ minimum: 1, maximum: 31 }), | ||
month: t.Number({ minimum: 1, maximum: 12 }), | ||
year: t.Number({ minimum: 1970 }), | ||
}), | ||
}), | ||
}, | ||
); |
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,6 @@ | ||
import Elysia from "elysia"; | ||
import { createAssignment } from "./create"; | ||
|
||
export const assignmentsRouter = new Elysia({ prefix: "/assignments" }).use( | ||
createAssignment, | ||
); |
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,5 @@ | ||
export type CustomDate = { | ||
day: number; | ||
month: number; | ||
year: number; | ||
}; |
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,27 @@ | ||
/** | ||
* Returns the current day | ||
* Ranging from 1 to 31 (inclusive) | ||
*/ | ||
export const currentDay = () => new Date().getDate(); | ||
|
||
/** | ||
* Returns the current month | ||
* Ranging from 1 to 12 (inclusive) | ||
*/ | ||
export const currentMonth = () => new Date().getMonth() + 1; | ||
|
||
/** | ||
* Returns the current year | ||
* e.g. 2021 | ||
*/ | ||
export const currentYear = () => new Date().getFullYear(); | ||
|
||
/** | ||
* Returns the current date | ||
* e.g. { day: 1, month: 1, year: 2021 } | ||
*/ | ||
export const currentCustomDate = () => ({ | ||
day: currentDay(), | ||
month: currentMonth(), | ||
year: currentYear(), | ||
}); |
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,19 @@ | ||
import type { CustomDate } from "types/date"; | ||
|
||
/** | ||
* Converts a custom date record to a normal js date | ||
*/ | ||
export const customDateToNormal = (customDate: CustomDate) => { | ||
return new Date(customDate.year, customDate.month - 1, customDate.day); | ||
}; | ||
|
||
/** | ||
* Converts a normal js date to a custom date record | ||
*/ | ||
export const normalDateToCustom = (date: Date) => { | ||
const day = date.getDate(); | ||
const month = date.getMonth() + 1; | ||
const year = date.getFullYear(); | ||
|
||
return { day, month, year }; | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { describe, expect, it } from "bun:test"; | ||
import { currentCustomDate, currentDay } from "utils/dates/current"; | ||
|
||
describe("current date", () => { | ||
it("current day", () => { | ||
const day = currentDay(); | ||
expect(day).toBeGreaterThanOrEqual(1); | ||
expect(day).toBeLessThanOrEqual(31); | ||
}); | ||
it("current custom date", () => { | ||
const date = currentCustomDate(); | ||
|
||
expect(date.day).toBeGreaterThanOrEqual(1); | ||
expect(date.day).toBeLessThanOrEqual(31); | ||
expect(date.month).toBeGreaterThanOrEqual(1); | ||
expect(date.month).toBeLessThanOrEqual(12); | ||
expect(date.year).toBeGreaterThanOrEqual(1970); | ||
}); | ||
}); |