-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathcommentperiod.js
432 lines (384 loc) · 15.2 KB
/
commentperiod.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
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
var _ = require('lodash');
var defaultLog = require('winston').loggers.get('default');
var mongoose = require('mongoose');
var Actions = require('../helpers/actions');
var Utils = require('../helpers/utils');
var getSanitizedFields = function (fields) {
return _.remove(fields, function (f) {
return (_.indexOf([
'_schemaName',
'addedBy',
'additionalText',
'ceaaAdditionalText',
'ceaaInformationLabel',
'ceaaRelatedDocuments',
'classificationRoles',
'classifiedPercent',
'commenterRoles',
'dateAdded',
'dateCompleted',
'dateCompletedEst',
'dateStarted',
'dateStartedEst',
'dateUpdated',
'downloadRoles',
'informationLabel',
'instructions',
'commentTip',
'isClassified',
'isPublished',
'isResolved',
'isVetted',
'isMet',
'metURL',
'metURLAdmin',
'milestone',
'openHouses',
'periodType',
'phase',
'phaseName',
'project',
'publishedPercent',
'rangeOption',
'rangeType',
'relatedDocuments',
'resolvedPercent',
'updatedBy',
'userCan',
'vettedPercent',
'vettingRoles',
'read',
'write',
'delete'
], f) !== -1);
});
};
exports.protectedOptions = function (args, res) {
res.status(200).send();
};
exports.publicGet = async function (args, res) {
defaultLog.info('Public get for comment period');
// Build match query if on CommentPeriodId route
var query = {}, sort = {};
if (args.swagger.params.commentPeriodId && args.swagger.params.commentPeriodId.value) {
query = Utils.buildQuery('_id', args.swagger.params.commentPeriodId.value, query);
}
if (args.swagger.params.project && args.swagger.params.project.value) {
query = Utils.buildQuery('project', args.swagger.params.project.value, query);
}
// sort
if (args.swagger.params.sortBy && args.swagger.params.sortBy.value) {
args.swagger.params.sortBy.value.forEach(function (value) {
var order_by = value.charAt(0) == '-' ? -1 : 1;
var sort_by = value.slice(1);
// only accept certain fields
switch (sort_by) {
case 'dateStarted':
case 'dateCompleted':
case 'author':
sort[sort_by] = order_by;
break;
}
}, this);
}
// Set query type
_.assignIn(query, { '_schemaName': 'CommentPeriod' });
try {
var data = await Utils.runDataQuery('CommentPeriod',
['public'],
query,
getSanitizedFields(args.swagger.params.fields.value), // Fields
null, // sort warmup
sort, // sort
null, // skip
null, // limit
false); // count
Utils.recordAction('Get', 'CommentPeriod', 'public', args.swagger.params.commentPeriodId && args.swagger.params.commentPeriodId.value ? args.swagger.params.commentPeriodId.value : null);
return Actions.sendResponse(res, 200, data);
} catch (e) {
defaultLog.info('Error:', e);
return Actions.sendResponse(res, 400, e);
}
};
exports.protectedHead = async function (args, res) {
defaultLog.info('Head for comment period');
// Build match query if on CommentPeriodId route
var query = {};
if (args.swagger.params.commentPeriodId && args.swagger.params.commentPeriodId.value) {
query = Utils.buildQuery('_id', args.swagger.params.commentPeriodId.value, query);
}
if (args.swagger.params.project && args.swagger.params.project.value) {
query = Utils.buildQuery('project', args.swagger.params.project.value, query);
}
// Unless they specifically ask for it, hide deleted results.
if (args.swagger.params.isDeleted && args.swagger.params.isDeleted.value != undefined) {
_.assignIn(query, { isDeleted: args.swagger.params.isDeleted.value });
}
// Set query type
_.assignIn(query, { '_schemaName': 'CommentPeriod' });
try {
var data = await Utils.runDataQuery('CommentPeriod',
args.swagger.params.auth_payload.realm_access.roles,
query,
['_id', 'read', 'write', 'delete'], // Fields
null, // sort warmup
null, // sort
null, // skip
null, // limit
true); // count
Utils.recordAction('Head', 'CommentPeriod', args.swagger.params.auth_payload.preferred_username, args.swagger.params.commentPeriodId && args.swagger.params.commentPeriodId.value ? args.swagger.params.commentPeriodId.value : null);
// /api/commentperiod/ route, return 200 OK with 0 items if necessary
if (!(args.swagger.params.commentPeriodId && args.swagger.params.commentPeriodId.value) || (data && data.length > 0)) {
res.setHeader('x-total-count', data && data.length > 0 ? data[0].total_items : 0);
return Actions.sendResponse(res, 200, data);
} else {
return Actions.sendResponse(res, 404, data);
}
} catch (e) {
defaultLog.info('Error:', e);
return Actions.sendResponse(res, 400, e);
}
};
exports.protectedSummary = async function (args, res) {
defaultLog.info('Head for comment period summaries');
if (args.swagger.params.commentPeriodId && args.swagger.params.commentPeriodId.value && !mongoose.Types.ObjectId.isValid(args.swagger.params.commentPeriodId.value)) {
return Actions.sendResponse(res, 400, { });
}
// Build match query if on CommentPeriodId route
var query = {};
if (args.swagger.params.commentPeriodId && args.swagger.params.commentPeriodId.value) {
_.assignIn(query, { period: mongoose.Types.ObjectId(args.swagger.params.commentPeriodId.value) });
}
// Unless they specifically ask for it, hide deleted results.
if (args.swagger.params.isDeleted && args.swagger.params.isDeleted.value != undefined) {
_.assignIn(query, { isDeleted: args.swagger.params.isDeleted.value });
}
// Set query type
_.assignIn(query, { '_schemaName': 'Comment' });
Utils.recordAction('summary', 'commentPeriod', args.swagger.params.auth_payload.preferred_username);
var options = ['Pending', 'Deferred', 'Published', 'Rejected'];
try {
var summary = {
'Pending': 0,
'Deferred': 0,
'Published': 0,
'Rejected': 0
};
await Promise.all(options.map(async (item) => {
var optionQuery = {};
_.assignIn(optionQuery, { 'eaoStatus': item, period: mongoose.Types.ObjectId(args.swagger.params.commentPeriodId.value) });
console.log('optionQuery:', optionQuery);
var res = await Utils.runDataQuery('CommentPeriod',
args.swagger.params.auth_payload.realm_access.roles,
optionQuery,
['_id', 'read', 'write', 'delete'], // Fields
null, // sort warmup
null, // sort
null, // skip
null, // limit
true); // count
Utils.recordAction('Summary', 'CommentPeriod', args.swagger.params.auth_payload.preferred_username, args.swagger.params.commentPeriodId && args.swagger.params.commentPeriodId.value ? args.swagger.params.commentPeriodId.value : null);
console.log('RES:', res);
if (res && res[0]) {
summary[item] = res[0]['total_items'];
}
return summary;
}));
console.log('sending summary:', summary);
return Actions.sendResponse(res, 200, summary);
} catch (e) {
defaultLog.info('Error:', e);
return Actions.sendResponse(res, 400, e);
}
};
exports.protectedGet = async function (args, res) {
defaultLog.info('Getting comment period(s)');
var query = {}, sort = null, skip = null, limit = null, count = false;
// Build match query if on CommentPeriodId route
if (args.swagger.params.commentPeriodId && args.swagger.params.commentPeriodId.value) {
defaultLog.info('Comment period id:', args.swagger.params.commentPeriodId.value);
query = Utils.buildQuery('_id', args.swagger.params.commentPeriodId.value, query);
}
// Build match query if on project's id
if (args.swagger.params.project && args.swagger.params.project.value) {
if (!mongoose.Types.ObjectId.isValid(args.swagger.params.project.value)) {
return Actions.sendResponse(res, 400, { });
}
_.assignIn(query, { project: mongoose.Types.ObjectId(args.swagger.params.project.value) });
}
// sort
if (args.swagger.params.sortBy && args.swagger.params.sortBy.value) {
sort = {};
args.swagger.params.sortBy.value.forEach(function (value) {
var order_by = value.charAt(0) == '-' ? -1 : 1;
var sort_by = value.slice(1);
sort[sort_by] = order_by;
}, this);
}
// Skip and limit
var processedParameters = Utils.getSkipLimitParameters(args.swagger.params.pageSize, args.swagger.params.pageNum);
skip = processedParameters.skip;
limit = processedParameters.limit;
// Count
if (args.swagger.params.count && args.swagger.params.count.value) {
count = args.swagger.params.count.value;
}
// Set query type
_.assignIn(query, { '_schemaName': 'CommentPeriod' });
try {
var data = await Utils.runDataQuery('CommentPeriod',
args.swagger.params.auth_payload.realm_access.roles,
query,
getSanitizedFields(args.swagger.params.fields.value), // Fields
null, // sort warmup
sort, // sort
skip, // skip
limit, // limit
count); // count
Utils.recordAction('Get', 'CommentPeriod', args.swagger.params.auth_payload.preferred_username, args.swagger.params.commentPeriodId && args.swagger.params.commentPeriodId.value ? args.swagger.params.commentPeriodId.value : null);
defaultLog.info('Got comment period(s):', data);
return Actions.sendResponse(res, 200, data);
} catch (e) {
defaultLog.info('Error:', e);
return Actions.sendResponse(res, 400, e);
}
};
// Create a new CommentPeriod
exports.protectedPost = async function (args, res) {
var obj = args.swagger.params.period.value;
defaultLog.info('Incoming new comment period:', obj);
console.log("Object:", obj);
var CommentPeriod = mongoose.model('CommentPeriod');
// TDOO: Make milestone lookup against "Time Limit Imposition" set for all isMet = true based
// calls.
var commentPeriod = new CommentPeriod({
_schemaName: 'CommentPeriod',
addedBy: args.swagger.params.auth_payload.preferred_username,
commentIdCount: 0,
dateAdded: new Date(),
isMet: obj.isMet,
metURL: obj.metURL,
metURLAdmin: obj.metURLAdmin,
dateCompleted: obj.dateCompleted,
dateStarted: obj.dateStarted,
instructions: obj.instructions,
commentTip: obj.commentTip,
milestone: mongoose.Types.ObjectId(obj.milestone),
openHouses: obj.openHouses,
relatedDocuments: obj.relatedDocuments,
project: mongoose.Types.ObjectId(obj.project),
read: ['staff', 'sysadmin'],
write: ['staff', 'sysadmin'],
delete: ['staff', 'sysadmin']
});
if (obj.isPublished) {
commentPeriod.read.push('public');
}
try {
console.log("period:", commentPeriod);
var cp = await commentPeriod.save();
console.log("CP:", cp);
Utils.recordAction('Put', 'CommentPeriod', args.swagger.params.auth_payload.preferred_username, cp._id);
defaultLog.info('Saved new comment period object:', cp);
return Actions.sendResponse(res, 200, cp);
} catch (e) {
defaultLog.info('Error:', e);
return Actions.sendResponse(res, 400, e);
}
};
// Update an existing CommentPeriod
exports.protectedPut = async function (args, res) {
var objId = args.swagger.params.commentPeriodId.value;
if (args.swagger.params.commentPeriodId && args.swagger.params.commentPeriodId.value && !mongoose.Types.ObjectId.isValid(args.swagger.params.commentPeriodId.value)) {
return Actions.sendResponse(res, 400, { });
}
var obj = args.swagger.params.cp.value;
defaultLog.info('Put comment period:', objId);
var CommentPeriod = mongoose.model('CommentPeriod');
var commentPeriod = {
commentTip: obj.commentTip,
dateCompleted: obj.dateCompleted,
dateStarted: obj.dateStarted,
dateUpdated: new Date(),
isMet: obj.isMet,
instructions: obj.instructions,
metURL: obj.metURL,
milestone: mongoose.Types.ObjectId(obj.milestone),
openHouses: obj.openHouses,
relatedDocuments: obj.relatedDocuments,
updatedBy: args.swagger.params.auth_payload.preferred_username,
};
// TODO: Revise this so we are not explicitly setting permissions
if (obj.isPublished) {
commentPeriod['read'] = ['public', 'staff', 'sysadmin'];
} else {
commentPeriod['read'] = ['staff', 'sysadmin'];
}
defaultLog.info('Incoming updated object:', commentPeriod);
try {
var cp = await CommentPeriod.update({ _id: objId }, { $set: commentPeriod });
Utils.recordAction('Put', 'CommentPeriod', args.swagger.params.auth_payload.preferred_username, objId);
defaultLog.info('Comment period updated:', cp);
return Actions.sendResponse(res, 200, cp);
} catch (e) {
defaultLog.info('Error:', e);
return Actions.sendResponse(res, 400, e);
}
};
// Delete a new CommentPeriod
exports.protectedDelete = async function (args, res) {
var objId = args.swagger.params.commentPeriodId.value;
defaultLog.info('Delete comment period:', objId);
if (args.swagger.params.commentPeriodId && args.swagger.params.commentPeriodId.value && !mongoose.Types.ObjectId.isValid(args.swagger.params.commentPeriodId.value)) {
return Actions.sendResponse(res, 400, { });
}
var CommentPeriod = mongoose.model('CommentPeriod');
try {
await CommentPeriod.findOneAndRemove({ _id: objId });
Utils.recordAction('Delete', 'CommentPeriod', args.swagger.params.auth_payload.preferred_username, objId);
return Actions.sendResponse(res, 200, {});
} catch (e) {
defaultLog.info('Error:', e);
return Actions.sendResponse(res, 400, e);
}
};
// Publish/Unpublish the CommentPeriod
exports.protectedPublish = async function (args, res) {
var objId = args.swagger.params.commentPeriodId.value;
defaultLog.info('Publish comment period:', objId);
if (args.swagger.params.commentPeriodId && args.swagger.params.commentPeriodId.value && !mongoose.Types.ObjectId.isValid(args.swagger.params.commentPeriodId.value)) {
return Actions.sendResponse(res, 400, { });
}
var CommentPeriod = mongoose.model('CommentPeriod');
try {
var commentPeriod = await CommentPeriod.findOne({ _id: objId });
delete commentPeriod.__v;
defaultLog.info('Comment period object:', commentPeriod);
// Add public to read array.
var published = await Actions.publish(commentPeriod);
Utils.recordAction('Publish', 'CommentPeriod', args.swagger.params.auth_payload.preferred_username, objId);
return Actions.sendResponse(res, 200, published);
} catch (e) {
return Actions.sendResponse(res, 400, e);
}
};
exports.protectedUnPublish = async function (args, res) {
var objId = args.swagger.params.commentPeriodId.value;
defaultLog.info('UnPublish comment period:', objId);
if (args.swagger.params.commentPeriodId && args.swagger.params.commentPeriodId.value && !mongoose.Types.ObjectId.isValid(args.swagger.params.commentPeriodId.value)) {
return Actions.sendResponse(res, 400, { });
}
var CommentPeriod = mongoose.model('CommentPeriod');
try {
var commentPeriod = await CommentPeriod.findOne({ _id: objId });
delete commentPeriod.__v;
defaultLog.info('Comment period object:', commentPeriod);
// Remove public from read array.
var unpublished = await Actions.unPublish(commentPeriod);
Utils.recordAction('Unpublish', 'CommentPeriod', args.swagger.params.auth_payload.preferred_username, objId);
return Actions.sendResponse(res, 200, unpublished);
} catch (e) {
defaultLog.info('Error:', e);
return Actions.sendResponse(res, 400, e);
}
};