-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCourseInviteDB.ts
More file actions
67 lines (65 loc) · 2.29 KB
/
Copy pathCourseInviteDB.ts
File metadata and controls
67 lines (65 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import {CourseInvite, convertCourseInvite} from "../../../models/database/CourseInvites";
import {UUIDHelper} from "./helpers/UUIDHelper";
import {pool, extract, map, checkAvailable, one, pgDB} from "./HelperDB";
/**
* Class with methods for providing course invites.
*/
export class CourseInviteDB {
static async filterInvite(invite: CourseInvite) {
const {
inviteID = undefined,
creatorID = undefined,
courseID = undefined,
type = undefined,
joinRole = undefined,
limit = undefined,
offset = undefined,
client = pool
} = invite;
const
inviteid = UUIDHelper.toUUID(inviteID),
creatorid = UUIDHelper.toUUID(creatorID),
courseid = UUIDHelper.toUUID(courseID);
return client.query(`
SELECT *
FROM "CourseInvites"
WHERE
($1::uuid IS NULL OR inviteID=$1)
AND ($2::uuid IS NULL OR creatorID=$2)
AND ($3::uuid IS NULL OR courseID=$3)
AND ($4::text IS NULL OR type=$4)
AND ($5::text IS NULL OR joinRole=$5)
LIMIT $6
OFFSET $7
`, [inviteid, creatorid, courseid, type, joinRole, limit, offset])
.then(extract).then(map(convertCourseInvite));
}
static async addInvite(invite: CourseInvite) {
checkAvailable(["creatorID", "courseID", "type", "joinRole"], invite);
const {
creatorID,
courseID,
type,
joinRole,
client = pool
} = invite;
const
creatorid = UUIDHelper.toUUID(creatorID),
courseid = UUIDHelper.toUUID(courseID);
return client.query(`
INSERT INTO "CourseInvites"
VALUES (DEFAULT, $1,$2, $3, $4)
RETURNING *
`, [creatorid, courseid, type, joinRole])
.then(extract).then(map(convertCourseInvite)).then(one);
}
static async deleteInvite(inviteID: string, client: pgDB = pool) {
const inviteid = UUIDHelper.toUUID(inviteID);
return client.query(`
DELETE FROM "CourseInvites"
WHERE inviteID = $1
RETURNING *
`, [inviteid])
.then(extract).then(map(convertCourseInvite)).then(one);
}
}