-
Notifications
You must be signed in to change notification settings - Fork 48
/
samples.js
292 lines (263 loc) · 9.8 KB
/
samples.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
/**
* Copyright (c) 2016, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or
* https://opensource.org/licenses/BSD-3-Clause
*/
/**
* api/v1/controllers/samples.js
*/
'use strict'; // eslint-disable-line strict
const featureToggles = require('feature-toggles');
const helper = require('../helpers/nouns/samples');
const subHelper = require('../helpers/nouns/subjects');
const doDelete = require('../helpers/verbs/doDelete');
const doFind = require('../helpers/verbs/doFind');
const doGet = require('../helpers/verbs/doGet');
const doPatch = require('../helpers/verbs/doPatch');
const doPost = require('../helpers/verbs/doPost');
const doPut = require('../helpers/verbs/doPut');
const u = require('../helpers/verbs/utils');
const httpStatus = require('../constants').httpStatus;
const sampleStore = require('../../../cache/sampleStore');
const constants = sampleStore.constants;
const redisModelSample = require('../../../cache/models/samples');
const utils = require('./utils');
const publisher = u.publisher;
module.exports = {
/**
* DELETE /samples/{key}
*
* Deletes the sample and sends it back in the response.
*
* @param {IncomingMessage} req - The request object
* @param {ServerResponse} res - The response object
* @param {Function} next - The next middleware function in the stack
*/
deleteSample(req, res, next) {
doDelete(req, res, next, helper);
},
/**
* GET /samples
*
* Finds zero or more samples and sends them back in the response.
*
* @param {IncomingMessage} req - The request object
* @param {ServerResponse} res - The response object
* @param {Function} next - The next middleware function in the stack
*/
findSamples(req, res, next) {
if (featureToggles.isFeatureEnabled(constants.featureName)) {
const resultObj = { reqStartTime: new Date() }; // for logging
redisModelSample.findSamples(req, res, resultObj)
.then((response) => {
// loop through remove values to delete property
if (helper.fieldsToExclude) {
for (let i = response.length - 1; i >= 0; i--) {
u.removeFieldsFromResponse(helper.fieldsToExclude, response[i]);
}
}
u.logAPI(req, resultObj, response); // audit log
res.status(httpStatus.OK).json(response);
})
.catch((err) => u.handleError(next, err, helper.modelName));
} else {
doFind(req, res, next, helper);
}
},
/**
* GET /samples/{key}
*
* Retrieves the sample and sends it back in the response.
*
* @param {IncomingMessage} req - The request object
* @param {ServerResponse} res - The response object
* @param {Function} next - The next middleware function in the stack
*/
getSample(req, res, next) {
doGet(req, res, next, helper);
},
/**
* PATCH /samples/{key}
*
* Updates the sample and sends it back in the response. PATCH will only
* update the attributes of the sample provided in the body of the request.
* Other attributes will not be updated.
*
* @param {IncomingMessage} req - The request object
* @param {ServerResponse} res - The response object
* @param {Function} next - The next middleware function in the stack
*/
patchSample(req, res, next) {
utils.noReadOnlyFieldsInReq(req, helper);
doPatch(req, res, next, helper);
},
/**
* POST /samples
*
* Creates a new sample and sends it back in the response.
*
* @param {IncomingMessage} req - The request object
* @param {ServerResponse} res - The response object
* @param {Function} next - The next middleware function in the stack
*/
postSample(req, res, next) {
utils.noReadOnlyFieldsInReq(req, helper);
doPost(req, res, next, helper);
},
/**
* PUT /samples/{key}
*
* Updates a sample and sends it back in the response. If any attributes
* are missing from the body of the request, those attributes are cleared.
*
* @param {IncomingMessage} req - The request object
* @param {ServerResponse} res - The response object
* @param {Function} next - The next middleware function in the stack
*/
putSample(req, res, next) {
utils.noReadOnlyFieldsInReq(req, helper);
doPut(req, res, next, helper);
},
/**
* POST /samples/upsert
*
* Updates existing sample if one already exists with the name specified in
* the request body. If no sample exists with that name, creates a new
* sample. Sends the new/updated sample back in the response.
*
* @param {IncomingMessage} req - The request object
* @param {ServerResponse} res - The response object
* @param {Function} next - The next middleware function in the stack
*/
upsertSample(req, res, next) {
utils.noReadOnlyFieldsInReq(req, helper);
const resultObj = { reqStartTime: new Date() };
const sampleQueryBody = req.swagger.params.queryBody.value;
u.getUserNameFromToken(req,
featureToggles.isFeatureEnabled('enforceWritePermission'))
.then((userName) => {
if (sampleQueryBody.relatedLinks) {
u.checkDuplicateRLinks(sampleQueryBody.relatedLinks);
}
const upsertSamplePromise =
featureToggles.isFeatureEnabled(constants.featureName) ?
redisModelSample.upsertSample(
sampleQueryBody, userName) :
helper.model.upsertByName(sampleQueryBody, userName);
return upsertSamplePromise;
})
.then((samp) => {
resultObj.dbTime = new Date() - resultObj.reqStartTime;
const dataValues = samp.dataValues ? samp.dataValues : samp;
// loop through remove values to delete property
if (helper.fieldsToExclude) {
u.removeFieldsFromResponse(helper.fieldsToExclude, dataValues);
}
/*
*send the upserted sample to the client by publishing it to the redis
*channel
*/
publisher.publishSample(samp, subHelper.model);
u.logAPI(req, resultObj, dataValues);
return res.status(httpStatus.OK)
.json(u.responsify(samp, helper, req.method));
})
.catch((err) => u.handleError(next, err, helper.modelName));
},
/**
* POST /samples/upsert/bulk
*
* Upserts multiple samples. Returns "OK" without waiting for the upserts to
* happen. When "enableWorkerProcess" is enabled, the bulk upsert is enqueued
* to be processed by a separate worker process.
*
* @param {IncomingMessage} req - The request object
* @param {ServerResponse} res - The response object
* @returns {ServerResponse} - The response object indicating merely that the
* bulk upsert request has been received.
*/
bulkUpsertSample(req, res/* , next */) {
utils.noReadOnlyFieldsInReq(req, helper);
const resultObj = { reqStartTime: new Date() };
const reqStartTime = Date.now();
const value = req.swagger.params.queryBody.value;
u.getUserNameFromToken(req,
featureToggles.isFeatureEnabled('enforceWritePermission'))
.then((userName) => {
if (featureToggles.isFeatureEnabled('enableWorkerProcess')) {
const jobType = require('../../../jobQueue/setup').jobType;
const jobWrapper = require('../../../jobQueue/jobWrapper');
const wrappedBulkUpsertData = {};
wrappedBulkUpsertData.upsertData = value;
wrappedBulkUpsertData.userName = userName;
wrappedBulkUpsertData.reqStartTime = reqStartTime;
const j = jobWrapper.createJob(jobType.BULKUPSERTSAMPLES,
wrappedBulkUpsertData, req);
} else {
const bulkUpsertPromise =
featureToggles.isFeatureEnabled(constants.featureName) ?
redisModelSample.bulkUpsertSample(value, userName) :
helper.model.bulkUpsertByName(value, userName);
/*
*send the upserted sample to the client by publishing it to the redis
*channel
*/
bulkUpsertPromise.then((samples) => {
samples.forEach((sample) => {
if (!sample.isFailed) {
publisher.publishSample(sample, subHelper.model);
}
});
});
}
});
const body = { status: 'OK' };
u.logAPI(req, resultObj, body, value.length);
return res.status(httpStatus.OK).json(body);
},
/**
* DELETE /v1/samples/{key}/relatedLinks/
* DELETE /v1/samples/{key}/relatedLinks/{akey}
*
* Deletes the specified/all related link from the samples and sends updated
* samples in the response.
*
* @param {IncomingMessage} req - The request object
* @param {ServerResponse} res - The response object
* @param {Function} next - The next middleware function in the stack
*/
deleteSampleRelatedLinks(req, res, next) {
const resultObj = { reqStartTime: new Date() };
const params = req.swagger.params;
let delRlinksPromise;
if (featureToggles.isFeatureEnabled(constants.featureName) &&
helper.modelName === 'Sample') {
delRlinksPromise = redisModelSample.deleteSampleRelatedLinks(params);
} else {
delRlinksPromise = u.findByKey(helper, params)
.then((o) => u.isWritable(req, o,
featureToggles.isFeatureEnabled('enforceWritePermission')))
.then((o) => {
let jsonData = [];
if (params.relName) {
jsonData =
u.deleteAJsonArrayElement(o.relatedLinks, params.relName.value);
}
return o.update({ relatedLinks: jsonData });
});
}
delRlinksPromise.then((o) => {
resultObj.dbTime = new Date() - resultObj.reqStartTime;
const retval = u.responsify(o, helper, req.method);
// loop through remove values to delete property
if (helper.fieldsToExclude) {
u.removeFieldsFromResponse(helper.fieldsToExclude, retval);
}
u.logAPI(req, resultObj, retval);
res.status(httpStatus.OK).json(retval);
})
.catch((err) => u.handleError(next, err, helper.modelName));
},
}; // exports