-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathsettings.middleware.js
More file actions
166 lines (156 loc) Β· 4.52 KB
/
settings.middleware.js
File metadata and controls
166 lines (156 loc) Β· 4.52 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
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
const Services = {
Settings: require("../services/settings.service"),
Account: require("../services/account.service")
};
const Middleware = {
Util: require("./util.middleware")
};
const Constants = {
Error: require("../constants/error.constant")
};
const Settings = require("../models/settings.model");
/**
* @function parsePatch
* @param {body: *} req
* @param {*} res
* @param {(err?) => void} next
* @return {void}
* @description Put relevent settings attributes into settingsDetails
*/
function parsePatch(req, res, next) {
let settingsDetails = {};
for (const val in req.body) {
// use .hasOwnProperty instead of 'in' to get rid of inherited properties such as 'should'
if (Settings.schema.paths.hasOwnProperty(val)) {
settingsDetails[val] = req.body[val];
delete req.body[val];
}
}
req.body.settingsDetails = settingsDetails;
return next();
}
/**
* @function updateSettings
* @param {body: *} req
* @param {*} res
* @param {(err?) => void} next
* @return {void}
* @description Update settings object
*/
async function updateSettings(req, res, next) {
const settings = await Services.Settings.updateSettings(
req.body.settingsDetails
);
if (!settings) {
return next({
status: 500,
message: Constants.Error.GENERIC_500_MESSAGE
});
} else {
next();
}
}
/**
* @function confirmValidPatch
* @param {{body:{settingsDetails:{openTime:Date, closeTime:Date, confirmTime:Date, isRemote: Boolean}}}} req
* @param {*} res
* @param {*} next
* @return {void}
* @description Confirms that openTime < closeTime < confirmTime
*/
function confirmValidPatch(req, res, next) {
if (!req.body.settingsDetails.openTime &&
!req.body.settingsDetails.closeTime &&
!req.body.settingsDetails.confirmTime) {
return next();
}
const openTime = new Date(req.body.settingsDetails.openTime);
const closeTime = new Date(req.body.settingsDetails.closeTime);
const confirmTime = new Date(req.body.settingsDetails.confirmTime);
if (openTime < closeTime && closeTime < confirmTime) {
return next();
}
return next({
status: 422,
message: Constants.Error.SETTINGS_422_MESSAGE,
error: req.body.settingsDetails
});
}
/**
* @function updateSettings
* @param {*} req
* @param {*} res
* @param {(err?) => void} next
* @return {void}
* @description get the settings object and puts it in the settingsDetails.
*/
async function getSettings(req, res, next) {
const settings = await Services.Settings.getSettings();
if (!settings) {
return next({
status: 404,
message: Constants.Error.SETTINGS_404_MESSAGE
});
} else {
req.body.settingsDetails = settings;
next();
}
}
/**
* @function confirmAppsOpen
* @param {*} req
* @param {*} res
* @param {*} next
* @description Only succeeds if the currentTime > openTime, and currentTime < closeTime
*/
async function confirmAppsOpen(req, res, next) {
const settings = await Services.Settings.getSettings();
if (!settings) {
return next({
status: 500,
message: Constants.Error.GENERIC_500_MESSAGE
});
} else {
const now = Date.now();
const openTime = new Date(settings.openTime);
const closeTime = new Date(settings.closeTime);
if (openTime < now && closeTime > now) {
return next();
}
return next({
status: 403,
message: Constants.Error.SETTINGS_403_MESSAGE
});
}
}
/**
* @function confirmCheckinOpen
* @param {*} req
* @param {*} res
* @param {*} next
* @description Only succeeds if check-in is currently open
*/
async function confirmCheckinOpen(req, res, next) {
const settings = await Services.Settings.getSettings();
if (!settings) {
return next({
status: 500,
message: Constants.Error.GENERIC_500_MESSAGE
});
}
if (settings.checkinOpen) {
return next();
}
return next({
status: 403,
message: Constants.Error.SETTINGS_403_MESSAGE
});
}
module.exports = {
parsePatch: parsePatch,
confirmValidPatch: confirmValidPatch,
confirmCheckinOpen: Middleware.Util.asyncMiddleware(confirmCheckinOpen),
confirmAppsOpen: Middleware.Util.asyncMiddleware(confirmAppsOpen),
updateSettings: Middleware.Util.asyncMiddleware(updateSettings),
getSettings: Middleware.Util.asyncMiddleware(getSettings)
};