-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.ts
301 lines (269 loc) · 13.1 KB
/
controller.ts
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import Db from './db'
import { Request, Response } from 'express'
import { Authorizer, Permission } from './authorizer'
import { Promise } from 'bluebird';
const DEFAULT_LIMIT = 20; // Default # of items to return
const MAX_LIMIT = 1000; // Maximum permissible limit
/**
* Fulfills incoming router requests.
*/
export default class Controller {
private db: Db
private auth: Authorizer
constructor(db: Db) {
this.db = db
this.auth = new Authorizer()
}
/// Attribute Operations
getAttributes = (req: Request, res: Response) => {
if (req.query.q !== undefined) this.db.getAttributesWithNameLike(req.query.q, req.query.start, req.query.limit)
.then((attributes) => this.sendResponse(res, attributes))
else this.db.getAttributes(req.query.start, req.query.limit).then((attributes) => this.sendResponse(res, attributes))
}
getAttribute = (req: Request, res: Response) =>
this.db.getAttributeById(req.params.id).then((attribute) => this.sendResponse(res, attribute))
postAttribute = (req: Request, res: Response) => {
if (!this.auth.has(Permission.AttributeAdd, req)) this.sendUnauthorized(res)
else this.db.insertAttribute(req.body)
.then((attribute) => this.sendResponse(res, { id: attribute.id }))
}
updateAttribute = (req: Request, res: Response) => {
if (!this.auth.has(Permission.AttributeEdit, req)) this.sendUnauthorized(res)
else this.db.updateAttribute(req.params.id, req.body)
.then((attribute) => this.sendResponse(res, { id: req.params.id}))
}
deleteAttribute = (req: Request, res: Response) => {
if (!this.auth.has(Permission.AttributeDelete, req)) this.sendUnauthorized(res)
else this.db.updateAttribute(req.params.id, { deleted: true })
.then((attribute) => { this.sendResponse(res, { deleted: true }) })
}
/// Type Operations
// TODO: modify so some people can see unavailable equipment types?
getTypes = (req: Request, res: Response) => {
if (req.query.q !== undefined) this.db.getTypesWithNameLike(req.query.q, req.query.start, req.query.limit)
.then((types) => this.sendResponse(res, types))
else this.db.getAvailableTypes(req.query.start, req.query.limit).then((types) => this.sendResponse(res, types))
}
getType = (req: Request, res: Response) =>
this.db.getTypeById(req.params.id).then((type) => {
this.sendResponse(res, type)
})
postType = (req: Request, res: Response) => {
if (!this.auth.has(Permission.TypeAdd, req)) this.sendUnauthorized(res)
else
if (!req.body.attributes || !req.body.attributes.find(x => x.id == req.body.nameAttribute)) {
this.sendError(res, 'nameAttribute must exist in attributes')
return
}
this.db.validateAttributeIds(req.body.attributes.map(x => x.id)).then((valid) => {
this.db.insertType(req.body)
.then((type) => {
try {
for (let attribute of req.body.attributes) {
attribute.attributeId = attribute.id
type.addAttribute(attribute.id, { through: attribute })
}
this.sendResponse(res, { id: type.id })
} catch (e) {
if (this.isKeyError(e)) this.sendError(res, '')
}
})
})
.catch((e) => this.sendError(res, 'The referenced attributes are not valid'))
}
postTypeAttribute = (req: Request, res: Response) => {
if (!this.auth.has(Permission.TypeEdit, req)) this.sendUnauthorized(res)
else this.db.getTypeById(req.params.id).then((type) => {
if (!type) this.sendNotFound(res)
else
type.getAttributes().then((attributes) => {
if (attributes.find((a) => a.id == req.body.attribute.id))
this.sendError(res, 'Attribute already exists on type')
else {
req.body.attribute.attributeId = req.body.attribute.id
type.addAttribute(req.body.attribute.id, { through: req.body.attribute }).then((x) => {
this.sendResponse(res, { id: type.id })
}).catch((e) => {
if (this.isKeyError(e)) this.sendError(res, 'Referenced attribute does not exist')
else this.sendError(res, 'Error adding attribute', 500, e)
})
}
})
})
}
updateType = (req: Request, res: Response) => {
if (!this.auth.has(Permission.TypeEdit, req)) this.sendUnauthorized(res)
else this.db.updateType(req.params.id, req.body)
.then((type) => { this.sendResponse(res, { id: req.params.id }) })
}
deleteType = (req: Request, res: Response) => {
if (!this.auth.has(Permission.TypeDelete, req)) this.sendUnauthorized(res)
else this.db.updateType(req.params.id, { deleted: true })
.then((type) => { this.sendResponse(res, { deleted: true }) })
}
/// Equipment/Item Operations
getItems = (req: Request, res: Response) => {
if (req.query.q) {
this.db.searchItemsByAttributes(req.query.q, req.query.start, req.query.limit, req.query.typeId).then((items) => {
this.sendResponse(res, items);
})
} else {
this.db.getAllItems(req.query.start, req.query.limit, req.query.typeId).then((items) => this.sendResponse(res, items))
}
}
getItem = (req: Request, res: Response) =>
this.db.getItemById(req.params.id).then((item) => this.sendResponse(res, item))
postItem = (req: Request, res: Response) => {
if (!this.auth.has(Permission.ItemAdd, req)) this.sendUnauthorized(res)
else this.validateItemBody(undefined, req.body).then((errors) => {
if (errors.length)
return this.sendError(res, errors.join(', '))
this.db.insertItem(req.body)
.then((item) => {
this.createAttributeInstances(item, req.body.attributes).then((x) =>
this.sendResponse(res, { id: item.id }))
})
})
}
postItems = (req: Request, res: Response) => {
if (!this.auth.has(Permission.ItemAdd, req)) this.sendUnauthorized(res)
else {
Promise.all(req.body.map((item) => this.validateItemBody(undefined, item))).then((errors) => {
if (errors.reduce((prev, cur, i) => cur) > 0) {
this.sendError(res, errors.map((errorSet) => (<any[]> errorSet).join(', ')).join('; '));
} else {
var inserted = [];
this.db.insertItems(req.body).then(items =>
this.sendResponse(res, {ids: items.map(item => (<any>item).id)})
)
}
})
}
}
updateItem = (req: Request, res: Response) => {
if (!this.auth.has(Permission.ItemEdit, req)) this.sendUnauthorized(res)
else this.db.getItemById(req.params.id).then((item) => {
if (!item) this.sendNotFound(res)
else this.validateItemBody(item, req.body).then((errors) => {
if (errors.length)
return this.sendError(res, errors.join(', '))
this.db.updateItem(item.id, req.body)
.then((tem) => {
this.createAttributeInstances(item, req.body.attributes).then((x) =>
this.sendResponse(res, { id: item.id }))
})
})
})
}
deleteItem = (req: Request, res: Response) => {
if (!this.auth.has(Permission.ItemDelete, req)) this.sendUnauthorized(res)
else this.db.updateItem(req.params.id, { deleted: true })
.then((item) => { this.sendResponse(res, { deleted: true }) })
}
createAttributeInstances = (item: any, attributes: any[]) =>
this.db.insertAttributeInstances(attributes.map((attribute) => { return {
attributeId: attribute.attributeId,
value: attribute.value,
itemId: item.id
}}))
validateItemBody = (item: any, body: any) => {
var errors = []
return this.db.getTypeById(body.typeId).then((type) => {
if (!type) errors.push('Non-existent type specified')
else {
for (var attributeSpec of type.attributes) {
var attribute = body.attributes.find(
(a) => a.attributeId == attributeSpec.id)
if ((!attribute || !attribute.value) && attributeSpec.attributeType.required == 'Required' && !item) {
errors.push(`"${attributeSpec.name}" requires a value`)
} else if (attribute && attribute.value) {
var value = attribute.value;
switch (attributeSpec.type) {
case 'Boolean':
if (value != '1' && value != '0')
errors.push(`"${attributeSpec.name}" must be either "1" or "0"`)
break;
case 'Currency':
if (!/^(\d)+(\.(\d){2})$/.test(value))
errors.push(`"${attributeSpec.name}" must be a currency value`)
break;
case 'Integer':
if (!/^(\d)+$/.test(value))
errors.push(`"${attributeSpec.name}" must be an integer`)
break;
case 'DateTime':
// TODO: postgres is pretty tolerant of this input
break;
case 'String':
case 'TextBox':
// Using RegExes to break up RegExes is silly, but there's no way to create a RegEx from the type
// of string generated by RegExp.toString() in JavaScript.
try {
var extractor = /\/(.*)\/([gimuy])/g.exec(attributeSpec.regex)
var re = RegExp(extractor[1], extractor[2])
if (!re.test(value))
errors.push(`"${attributeSpec.name}" must match the regex "${attributeSpec.regex}"`)
} catch {
break;
}
break;
case 'Enum':
if (!attributeSpec.choices.find(x => x == value))
errors.push(`"${attributeSpec.name}" must be one of "${attributeSpec.choices.join('", "')}"`)
break;
default:
}
}
}
for (var attribute of body.attributes) {
if (!type.attributes.find(x => x.id == attribute.attributeId)) {
errors.push(`Attribute with ID ${attribute.attributeId} could not be found on type "${type.name}"`)
}
}
}
return errors;
})
}
sendNotFound = (res: Response) =>
this.sendError(res, 'Resource not found', 404)
/**
* JSONifies and sends an error response.
*
* `code` is the HTTP status code sent back. Default: 500
* `debug` is optional debugging content that will be sent back if environment var `DEBUG` is set.
*/
sendError = (res: Response, err: string, code?: number, debug?: any) =>
res.json({
'success': false,
'error': err,
'debug': (process.env.DEBUG ? debug : undefined)
}).status(code ? code : 500).end()
/**
* JSONifies and attached a `success` attribute to an outgoing API response.
*
* If `value` is falsy, it will use `sendError` to send a 404 response back.
*/
private sendResponse = (res: Response, value: any) => {
if (!value) this.sendNotFound(res)
else res.json({
'success': true,
'result': value
}).end()
}
/**
* Sends an unauthorized message. Used primarily when `Authorizer.has` fails.
*/
private sendUnauthorized = (res: Response) =>
this.sendError(res, "Access level insufficient for this resource.", 403)
/**
* Check if an error from the DB is a foreign key error or some other kind.
*/
private isKeyError = (e: any) =>
e && e.name && e.name == "SequelizeForeignKeyConstraintError"
paginationMiddleware = (req: Request, res: Response, next) => {
if (!req.query.start || req.query.start < 0) req.query.start = 0;
if (!req.query.limit || req.query.limit < 0) req.query.limit = DEFAULT_LIMIT;
else if(req.query.limit > MAX_LIMIT) req.query.limit = MAX_LIMIT;
next()
}
}