This repository was archived by the owner on Jul 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcategory-routes.js
More file actions
67 lines (61 loc) · 2.3 KB
/
Copy pathcategory-routes.js
File metadata and controls
67 lines (61 loc) · 2.3 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
const router = require('express').Router();
const constants = require('../../config/constants');
const {isPermitted} = require('../../services/auth-service');
const Rooms = require('../../models/room-booking/rooms-model');
//Resident and up: get list of categories
router.get('/', async (req, res) => {
const permitted = await isPermitted(req.user.role, constants.categories.roomsManagement, constants.actions.read);
if (!permitted) {
res.sendStatus(401);
} else {
Rooms.byTenant(req.user.residence).find({}).lean()
.then(results => {
let uniqueCategories = new Set();
let uniques = [];
for (let i = 0; i < results.length; i++) {
uniqueCategories.add(results[i].category);
}
let it = uniqueCategories.keys();
let result = it.next();
while(!result.done) {
uniques.push(result.value);
result = it.next();
}
res.json({
categories: uniques
})})
.catch(error => {
res.status(500).send("Database Error");
});
}
});
//Resident and up: Get an array of rooms belonging to a particular category
router.get('/:category', async (req, res) => {
const permitted = await isPermitted(req.user.role, constants.categories.roomsManagement, constants.actions.read);
if (!permitted) {
res.sendStatus(401);
} else {
const category = req.params.category;
Rooms.byTenant(req.user.residence).find({ category: category }).sort({ name : 1 }).lean()
.then(resp => {
const response = [];
resp.forEach((doc) => {
response.push({
roomId: doc._id,
name: doc.name,
recommendedCapacity: doc.recommendedCapacity,
contactName: doc.contactName,
contactEmail: doc.contactEmail,
contactNumber: doc.contactNumber,
checklist: doc.checklist,
placeholderText: doc.placeholderText
});
});
res.send(response);
})
.catch(err => {
res.status(500).send("Database Error");
});
}
});
module.exports = router;