-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.js
218 lines (190 loc) · 4.99 KB
/
model.js
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
const db = require("./data/db-config");
const selectScopes = (key) => {
return db.select("scopes").from("api_keys").where("key", key);
};
const selectKeys = (key) => {
return db("api_keys");
};
const insertKey = (name, prefix, key, scopes) => {
return db
.insert({ name: name, prefix: prefix, key: key, scopes: scopes })
.into("api_keys");
};
const selectEvents = () => {
return db("events");
};
const insertEvent = (identifier, name, description, status) => {
return db
.insert({
identifier: identifier,
name: name,
description: description,
status: status,
})
.into("events");
};
const createEventTable = (identifier) => {
return db.schema
.dropTableIfExists(`event_${identifier}`)
.createTable(`event_${identifier}`, function (table) {
table.bigInteger("barcodeNum", 255).unique();
table.integer("attended", 255).notNullable().defaultTo(0);
table.string("modifiedBy", 255);
table.timestamps(true, true);
});
};
const updateEvent = (identifier, name, description, status) => {
return db
.update({
name: name,
description: description,
status: status,
})
.where("identifier", identifier)
.from("events");
};
const selectUsers = () => {
return db("users");
};
const insertUser = (barcodeNum, name, email) => {
return db
.insert({
barcodeNum: barcodeNum,
name: name,
email: email,
})
.into("users");
};
const updateUser = (barcodeNum, name, email) => {
return db
.update({
name: name,
email: email,
})
.where("barcodeNum", barcodeNum)
.from("users");
};
const selectEventsCount = (identifier) => {
return db
.select(db.raw("COUNT(*) AS count"))
.from("events")
.where("identifier", identifier);
};
const selectEventsStatus = (identifier) => {
return db.select("status").from("events").where("identifier", identifier);
};
const selectUserCheckedInExists = (identifier, barcodeNum) => {
return db
.select(db.raw("COUNT(*) AS count"))
.from(`event_${identifier}`)
.where("barcodeNum", barcodeNum);
};
const selectUserCheckedIn = (identifier, barcodeNum) => {
return db
.select(db.raw("COUNT(*) AS count"))
.from(`event_${identifier}`)
.where("barcodeNum", barcodeNum)
.andWhere("attended", 1);
};
const updateUserCheckIn = (identifier, barcodeNum, attended, modifiedBy) => {
return db
.update({
attended: attended,
modifiedBy: modifiedBy,
})
.where("barcodeNum", barcodeNum)
.from(`event_${identifier}`);
};
const selectUserExists = (barcodeNum) => {
return db
.select(db.raw("COUNT(*) AS count"))
.from("users")
.where("barcodeNum", barcodeNum);
};
const checkinUser = (identifier, barcodeNum, attended, modifiedBy) => {
return db
.insert({
barcodeNum: barcodeNum,
attended: attended,
modifiedBy: modifiedBy,
})
.into(`event_${identifier}`);
};
const selectUserCount = () => {
return db.select(db.raw("COUNT(*) AS count")).from("users");
};
const selectUserCountAttending = (identifier) => {
return db
.select(db.raw("COUNT(*) AS count"))
.from(`event_${identifier}`)
.where("attended", 1);
};
const removeUser = (barcodeNum) => {
return db.where("barcodeNum", barcodeNum).from("users").del();
};
const removeEvent = (identifier) => {
return db.where("identifier", identifier).from("events").del();
};
const dropEventTable = (identifier) => {
return db.schema.dropTableIfExists(`event_${identifier}`);
};
const selectUserEmail = (barcodeNum) => {
return db.select("email").where("barcodeNum", barcodeNum).from("users");
};
const insertAccount = (type, username, password) => {
return db
.insert({
type: type,
username: username,
password: password,
})
.into("login");
};
const selectAccountType = (username, password) => {
return db
.select("type")
.from("login")
.whereRaw(`UPPER(username) = '${username.toUpperCase()}'`)
.andWhere("password", password);
};
const insertSession = (type, token, username, expires) => {
return db
.insert({
type: type,
token: token,
username: username,
expires: expires,
})
.into("session");
};
const selectSession = (token) => {
return db.select("expires", "type").from("session").where("token", token);
};
module.exports = {
selectScopes,
selectKeys,
selectEvents,
insertEvent,
createEventTable,
updateEvent,
selectUsers,
insertUser,
updateUser,
selectEventsCount,
selectEventsStatus,
selectUserCheckedInExists,
selectUserCheckedIn,
updateUserCheckIn,
selectUserExists,
checkinUser,
selectUserCount,
selectUserCountAttending,
removeUser,
removeEvent,
dropEventTable,
selectUserEmail,
insertAccount,
selectAccountType,
insertSession,
selectSession,
};