@natlibfi/melinda-rest-api-commons 4.2.4-alpha.5 → 4.2.4-alpha.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/mongo.js +146 -71
- package/dist/mongo.js.map +1 -1
- package/dist/mongo.spec.js +768 -0
- package/dist/mongo.spec.js.map +1 -0
- package/dist/mongoLog.spec.js.map +1 -1
- package/package.json +6 -6
- package/dist/mongo.spec.js.draft +0 -54
package/dist/mongo.js
CHANGED
|
@@ -13,6 +13,7 @@ var _utils = require("./utils.js");
|
|
|
13
13
|
var _moment = _interopRequireDefault(require("moment"));
|
|
14
14
|
var _httpStatus = _interopRequireDefault(require("http-status"));
|
|
15
15
|
var _mongoSanitize = _interopRequireDefault(require("mongo-sanitize"));
|
|
16
|
+
var _debug = _interopRequireDefault(require("debug"));
|
|
16
17
|
/* eslint-disable max-lines */
|
|
17
18
|
|
|
18
19
|
//import isDeepStrictEqual from 'util';
|
|
@@ -46,22 +47,28 @@ var _mongoSanitize = _interopRequireDefault(require("mongo-sanitize"));
|
|
|
46
47
|
}
|
|
47
48
|
*/
|
|
48
49
|
|
|
49
|
-
async function _default(MONGO_URI, collection) {
|
|
50
|
+
async function _default(MONGO_URI, collection, db = 'rest-api') {
|
|
50
51
|
const logger = (0, _melindaBackendCommons.createLogger)();
|
|
52
|
+
const debug = (0, _debug.default)('@natlibfi/melinda-rest-api-commons/mongo');
|
|
53
|
+
const debugDev = debug.extend(':dev');
|
|
51
54
|
|
|
52
55
|
// Connect to mongo (MONGO)
|
|
53
56
|
const client = await _mongodb.MongoClient.connect(MONGO_URI);
|
|
54
|
-
const
|
|
55
|
-
const gridFSBucket = new _mongodb.GridFSBucket(
|
|
57
|
+
const dbConnection = client.db(db);
|
|
58
|
+
const gridFSBucket = new _mongodb.GridFSBucket(dbConnection, {
|
|
56
59
|
bucketName: collection
|
|
57
60
|
});
|
|
61
|
+
const operator = dbConnection.collection(collection);
|
|
62
|
+
debugDev(`mongo: ${MONGO_URI}, db: ${db}, collection: ${collection}`);
|
|
58
63
|
return {
|
|
59
64
|
createPrio,
|
|
60
65
|
createBulk,
|
|
61
66
|
checkAndSetState,
|
|
62
67
|
checkAndSetImportJobState,
|
|
63
68
|
query,
|
|
69
|
+
createProjection,
|
|
64
70
|
queryById,
|
|
71
|
+
checkTimeOut,
|
|
65
72
|
remove,
|
|
66
73
|
readContent,
|
|
67
74
|
removeContent,
|
|
@@ -83,6 +90,7 @@ async function _default(MONGO_URI, collection) {
|
|
|
83
90
|
operation,
|
|
84
91
|
operationSettings
|
|
85
92
|
}) {
|
|
93
|
+
debugDev('createPrio');
|
|
86
94
|
const time = (0, _moment.default)().toDate();
|
|
87
95
|
const newQueueItem = {
|
|
88
96
|
correlationId,
|
|
@@ -108,13 +116,17 @@ async function _default(MONGO_URI, collection) {
|
|
|
108
116
|
modificationTime: time
|
|
109
117
|
};
|
|
110
118
|
try {
|
|
111
|
-
|
|
119
|
+
debugDev(`Inserting: ${JSON.stringify(newQueueItem)} to ${collection}}`);
|
|
120
|
+
const result = await operator.insertOne(newQueueItem);
|
|
121
|
+
debugDev(JSON.stringify(result));
|
|
112
122
|
if (result.acknowledged) {
|
|
113
123
|
logger.info(`New PRIO queue item for ${operation} ${correlationId} has been made in ${collection}`);
|
|
114
124
|
return;
|
|
115
125
|
}
|
|
126
|
+
// TEST-COVERAGE: this error is not (currently) tested
|
|
116
127
|
throw new _melindaCommons.Error(_httpStatus.default.INTERNAL_SERVER_ERROR);
|
|
117
128
|
} catch (error) {
|
|
129
|
+
// TEST-COVERAGE: this error is not (currently) tested
|
|
118
130
|
const errorMessage = error.payload || error.message || '';
|
|
119
131
|
(0, _utils.logError)(error);
|
|
120
132
|
throw new _melindaCommons.Error(_httpStatus.default.INTERNAL_SERVER_ERROR, `Mongo errored: ${errorMessage}`);
|
|
@@ -161,16 +173,17 @@ async function _default(MONGO_URI, collection) {
|
|
|
161
173
|
|
|
162
174
|
// eslint-disable-next-line functional/no-conditional-statements
|
|
163
175
|
if (stream) {
|
|
176
|
+
debug(`Creating streamBulk`);
|
|
164
177
|
try {
|
|
165
|
-
|
|
166
|
-
db.collection(collection).insertOne(newQueueItem);
|
|
167
|
-
logger.info(`New BULK queue item for ${operation} ${correlationId} has been made in ${collection}!`);
|
|
168
|
-
return new Promise((resolve, reject) => {
|
|
178
|
+
await new Promise((resolve, reject) => {
|
|
169
179
|
const outputStream = gridFSBucket.openUploadStream(correlationId);
|
|
170
180
|
stream.on('error', reject).on('data', chunk => outputStream.write(chunk)).on('end', () => outputStream.end(undefined, undefined, () => {
|
|
171
181
|
resolve(correlationId);
|
|
172
182
|
}));
|
|
173
183
|
});
|
|
184
|
+
logger.info(`New BULK queue item for ${operation} ${correlationId} has been made in ${collection}!`);
|
|
185
|
+
debug(`New BULK queue item for ${operation} ${correlationId} has been made in ${collection}!`);
|
|
186
|
+
return operator.insertOne(newQueueItem);
|
|
174
187
|
} catch (error) {
|
|
175
188
|
const errorMessage = error.payload || error.message || '';
|
|
176
189
|
(0, _utils.logError)(error);
|
|
@@ -178,19 +191,23 @@ async function _default(MONGO_URI, collection) {
|
|
|
178
191
|
}
|
|
179
192
|
}
|
|
180
193
|
logger.debug(`No stream`);
|
|
194
|
+
debug(`Creating noStreamBulk`);
|
|
181
195
|
// eslint-disable-next-line functional/no-conditional-statements
|
|
182
196
|
if (!stream) {
|
|
183
197
|
try {
|
|
184
|
-
const result = await
|
|
198
|
+
const result = await operator.insertOne(newQueueItem);
|
|
185
199
|
if (result.acknowledged) {
|
|
186
200
|
logger.info(`New noStream BULK queue item for ${operation} ${correlationId} has been made in ${collection}`);
|
|
201
|
+
debug(`New noStream BULK queue item for ${operation} ${correlationId} has been made in ${collection}`);
|
|
187
202
|
return {
|
|
188
203
|
correlationId,
|
|
189
204
|
queueItemState: _constants.QUEUE_ITEM_STATE.VALIDATOR.WAITING_FOR_RECORDS
|
|
190
205
|
};
|
|
191
206
|
}
|
|
207
|
+
// TEST-COVERAGE: this error is not (currently) tested
|
|
192
208
|
throw new _melindaCommons.Error(_httpStatus.default.INTERNAL_SERVER_ERROR);
|
|
193
209
|
} catch (error) {
|
|
210
|
+
// TEST-COVERAGE: this error is not (currently) tested
|
|
194
211
|
const errorMessage = error.payload || error.message || '';
|
|
195
212
|
(0, _utils.logError)(error);
|
|
196
213
|
throw new _melindaCommons.Error(_httpStatus.default.INTERNAL_SERVER_ERROR, `Mongo errored: ${errorMessage}`);
|
|
@@ -199,14 +216,15 @@ async function _default(MONGO_URI, collection) {
|
|
|
199
216
|
}
|
|
200
217
|
|
|
201
218
|
// Check state that the queueItem has not waited too long and set state
|
|
219
|
+
// Optionally set also queueItems errorStatus & errorMessage
|
|
202
220
|
async function checkAndSetState({
|
|
203
221
|
correlationId,
|
|
204
222
|
state,
|
|
205
223
|
errorMessage = undefined,
|
|
206
224
|
errorStatus = undefined
|
|
207
225
|
}) {
|
|
208
|
-
// checkTimeOut returns true, if queueItem is fresher than 1 minute and it's state is not ABORT/ERROR
|
|
209
|
-
// otherwise it sets queueItem to state ABORT (408, 'Timeout')
|
|
226
|
+
// checkTimeOut returns true, if queueItem is fresher than 1 minute and it's state is not ABORT/ERROR/DONE
|
|
227
|
+
// otherwise it sets queueItem and importJobStates to state ABORT and add errorStatus and errorMessage (408, 'Timeout')
|
|
210
228
|
const timeOut = await checkTimeOut({
|
|
211
229
|
correlationId
|
|
212
230
|
});
|
|
@@ -221,16 +239,15 @@ async function _default(MONGO_URI, collection) {
|
|
|
221
239
|
return false;
|
|
222
240
|
}
|
|
223
241
|
|
|
224
|
-
// Check state that the queueItem has not waited too long and set
|
|
242
|
+
// Check state that the queueItem has not waited too long and set importJobState
|
|
243
|
+
// Note: checkAndSetImportJobState cannot be used to set queueItems errorStatus & errorMessage (expect in case of a TimeOut)
|
|
225
244
|
async function checkAndSetImportJobState({
|
|
226
245
|
correlationId,
|
|
227
246
|
operation,
|
|
228
|
-
importJobState
|
|
229
|
-
errorMessage = '',
|
|
230
|
-
errorStatus = ''
|
|
247
|
+
importJobState
|
|
231
248
|
}) {
|
|
232
|
-
// checkTimeOut returns true, if queueItem is fresher than 1 minute and it's state is not ABORT/ERROR
|
|
233
|
-
// otherwise it sets queueItem to state ABORT (408, 'Timeout')
|
|
249
|
+
// checkTimeOut returns true, if queueItem is fresher than 1 minute and it's state is not ABORT/ERROR/DONE
|
|
250
|
+
// otherwise it sets queueItem and importJobStates to state ABORT and errorStatue and errorMessage (408, 'Timeout')
|
|
234
251
|
logger.debug(`${correlationId}, ${importJobState}, ${operation}`);
|
|
235
252
|
const timeOut = await checkTimeOut({
|
|
236
253
|
correlationId,
|
|
@@ -241,25 +258,25 @@ async function _default(MONGO_URI, collection) {
|
|
|
241
258
|
return setImportJobState({
|
|
242
259
|
correlationId,
|
|
243
260
|
operation,
|
|
244
|
-
importJobState
|
|
245
|
-
errorMessage,
|
|
246
|
-
errorStatus
|
|
261
|
+
importJobState
|
|
247
262
|
});
|
|
248
263
|
}
|
|
249
264
|
return false;
|
|
250
265
|
}
|
|
251
266
|
async function query(params, showParams = {}) {
|
|
252
267
|
logger.debug(`Querying: ${JSON.stringify(params)}, ${JSON.stringify(showParams)}`);
|
|
268
|
+
debug(`Querying: ${JSON.stringify(params)}, ${JSON.stringify(showParams)}`);
|
|
253
269
|
const {
|
|
254
270
|
limit = 1000,
|
|
255
271
|
skip = 0,
|
|
256
272
|
...rest
|
|
257
273
|
} = params;
|
|
258
|
-
const result = await
|
|
274
|
+
const result = await operator.find(rest, {
|
|
259
275
|
projection: createProjection(showParams)
|
|
260
276
|
}).limit(parseInt(limit, 10)).skip(parseInt(skip, 10)).toArray();
|
|
261
277
|
logger.debug(`Query result: ${result.length > 0 ? 'Found!' : 'Not found!'}`);
|
|
262
278
|
logger.silly(`${JSON.stringify(result)}`);
|
|
279
|
+
debug(`${JSON.stringify(result)}`);
|
|
263
280
|
return result;
|
|
264
281
|
}
|
|
265
282
|
|
|
@@ -269,6 +286,7 @@ async function _default(MONGO_URI, collection) {
|
|
|
269
286
|
|
|
270
287
|
function createProjection(showParams = {}) {
|
|
271
288
|
logger.silly(`Creating projection for query: ${JSON.stringify(showParams)}`);
|
|
289
|
+
debug(`Creating projection for query: ${JSON.stringify(showParams)}`);
|
|
272
290
|
const {
|
|
273
291
|
showAll = 0,
|
|
274
292
|
showOperations = 0,
|
|
@@ -276,8 +294,21 @@ async function _default(MONGO_URI, collection) {
|
|
|
276
294
|
showRecordLoadParams = 0,
|
|
277
295
|
showImportJobState = 0
|
|
278
296
|
} = showParams;
|
|
297
|
+
const combinedShowParams = {
|
|
298
|
+
showAll,
|
|
299
|
+
showOperations,
|
|
300
|
+
showOperationSettings,
|
|
301
|
+
showRecordLoadParams,
|
|
302
|
+
showImportJobState
|
|
303
|
+
};
|
|
279
304
|
logger.debug(`showAll: ${showAll}, showOperations: ${showOperations}, showOperationSettings: ${showOperationSettings}, showRecordLoadParams: ${showRecordLoadParams}, showImportJobState: ${showImportJobState}`);
|
|
280
|
-
|
|
305
|
+
debug(`showAll: ${showAll}, showOperations: ${showOperations}, showOperationSettings: ${showOperationSettings}, showRecordLoadParams: ${showRecordLoadParams}, showImportJobState: ${showImportJobState}`);
|
|
306
|
+
debug(`${JSON.stringify(combinedShowParams)}`);
|
|
307
|
+
|
|
308
|
+
// NOTE: parseBoolean parses any non-empty, non-"false" string as true!
|
|
309
|
+
if ((0, _melindaCommons.parseBoolean)(showAll)) {
|
|
310
|
+
//if (showAll === true || showAll === '1' || showAll === 1) {
|
|
311
|
+
debug(`ShowAll: ${showAll}`);
|
|
281
312
|
return {
|
|
282
313
|
_id: 0
|
|
283
314
|
};
|
|
@@ -288,52 +319,61 @@ async function _default(MONGO_URI, collection) {
|
|
|
288
319
|
'showRecordLoadParams': 'recordLoadParams',
|
|
289
320
|
'showImportJobState': 'importJobState'
|
|
290
321
|
};
|
|
291
|
-
const result = Object.keys(
|
|
322
|
+
const result = Object.keys(combinedShowParams)
|
|
323
|
+
//.filter(param => param !== 'showAll' && combinedShowParams[param] !== true && combinedShowParams[param] !== 1 && combinedShowParams[param] !== '1')
|
|
324
|
+
.filter(param => param !== 'showAll' && !(0, _melindaCommons.parseBoolean)(combinedShowParams[param])).filter(param => showParamToField[param]).map(param => showParamToField[param]);
|
|
292
325
|
logger.silly(`We want to exclude from projection: ${JSON.stringify(result)}`);
|
|
326
|
+
debug(`We want to exclude from projection: ${JSON.stringify(result)}`);
|
|
293
327
|
const excludeObject = Object.fromEntries(result.map(param => [param, 0]));
|
|
294
328
|
logger.silly(`We want to exclude from projection: ${JSON.stringify(excludeObject)}`);
|
|
329
|
+
debug(`We want to exclude from projection: ${JSON.stringify(excludeObject)}`);
|
|
295
330
|
return {
|
|
296
331
|
_id: 0,
|
|
297
332
|
...excludeObject
|
|
298
333
|
};
|
|
299
334
|
}
|
|
335
|
+
|
|
336
|
+
// Note: edited in v4.2.4 to return queueItem *after* possible timout has aborted it
|
|
337
|
+
// Note: previous versions returned queueItem *before* its state/queueItemState is timeOuted
|
|
300
338
|
async function queryById({
|
|
301
339
|
correlationId,
|
|
302
340
|
checkModTime = false
|
|
303
341
|
}) {
|
|
304
|
-
const result = await db.collection(collection).findOne({
|
|
305
|
-
correlationId
|
|
306
|
-
});
|
|
307
342
|
if (checkModTime) {
|
|
308
|
-
|
|
343
|
+
debug(`queryById checkModTime: ${checkModTime}`);
|
|
344
|
+
await checkTimeOut({
|
|
345
|
+
correlationId
|
|
346
|
+
});
|
|
347
|
+
return queryById({
|
|
309
348
|
correlationId
|
|
310
349
|
});
|
|
311
|
-
if (timeOut) {
|
|
312
|
-
return queryById({
|
|
313
|
-
correlationId
|
|
314
|
-
});
|
|
315
|
-
}
|
|
316
|
-
return result;
|
|
317
350
|
}
|
|
351
|
+
const result = await operator.findOne({
|
|
352
|
+
correlationId
|
|
353
|
+
}, {
|
|
354
|
+
projection: {
|
|
355
|
+
_id: 0
|
|
356
|
+
}
|
|
357
|
+
});
|
|
318
358
|
return result;
|
|
319
359
|
}
|
|
320
360
|
|
|
321
361
|
// Check that if the item has waited too long
|
|
322
362
|
// If the last modification time for the queueItem is older than 1 minute
|
|
323
363
|
// set state to ABORT and return false, otherwise return true
|
|
324
|
-
// If the state is already ABORT or ERROR return false
|
|
364
|
+
// If the state is already ABORT or ERROR or DONE return false
|
|
325
365
|
|
|
326
366
|
async function checkTimeOut({
|
|
327
367
|
correlationId
|
|
328
368
|
}) {
|
|
329
369
|
const {
|
|
330
370
|
modificationTime,
|
|
331
|
-
|
|
371
|
+
queueItemState: oldState,
|
|
332
372
|
importJobState
|
|
333
|
-
} = await
|
|
373
|
+
} = await operator.findOne({
|
|
334
374
|
correlationId
|
|
335
375
|
});
|
|
336
|
-
|
|
376
|
+
//debug(`${modificationTime} - oldState: ${oldState}`);
|
|
337
377
|
// should we check for DONE too?
|
|
338
378
|
if ([_constants.QUEUE_ITEM_STATE.ABORT, _constants.QUEUE_ITEM_STATE.ERROR, _constants.QUEUE_ITEM_STATE.DONE].includes(oldState)) {
|
|
339
379
|
logger.silly(`${correlationId} has already state: ${oldState}`);
|
|
@@ -377,26 +417,36 @@ async function _default(MONGO_URI, collection) {
|
|
|
377
417
|
}
|
|
378
418
|
return true;
|
|
379
419
|
}
|
|
420
|
+
|
|
421
|
+
// eslint-disable-next-line max-statements
|
|
380
422
|
async function remove(params) {
|
|
381
423
|
logger.silly(`${JSON.stringify(params)}`);
|
|
382
424
|
logger.info(`Removing from Mongo (${collection}) id: ${params.correlationId}`);
|
|
425
|
+
debug(`${JSON.stringify(params)}`);
|
|
426
|
+
debug(`Removing from Mongo (${collection}) id: ${params.correlationId}`);
|
|
383
427
|
const clean = (0, _mongoSanitize.default)(params.correlationId);
|
|
384
428
|
logger.silly(`mongo/remove: clean: ${JSON.stringify(clean)}`);
|
|
429
|
+
debug(`mongo/remove: clean: ${JSON.stringify(clean)}`);
|
|
385
430
|
try {
|
|
386
431
|
//const metadataResult = await getFileMetadata({filename: clean});
|
|
387
432
|
//logger.debug(`mongo/remove: metadataResult: ${JSON.stringify(metadataResult)}`);
|
|
388
|
-
const noContent = await removeContent(params);
|
|
433
|
+
const noContent = await removeContent(params.correlationId);
|
|
434
|
+
debug(`noContent (removeContent result): ${JSON.stringify(noContent)}`);
|
|
389
435
|
if (noContent) {
|
|
390
|
-
await
|
|
436
|
+
const deleteResult = await operator.deleteOne({
|
|
391
437
|
correlationId: clean
|
|
392
438
|
});
|
|
439
|
+
debug(`deleteResult: ${JSON.stringify(deleteResult)}`);
|
|
393
440
|
return true;
|
|
394
441
|
}
|
|
395
442
|
} catch (err) {
|
|
443
|
+
// TEST-COVERAGE: this error is not (currently) tested
|
|
396
444
|
if (err instanceof _mongodb.MongoDriverError) {
|
|
445
|
+
debug();
|
|
397
446
|
if (err.message.indexOf('File not found for id') !== -1) {
|
|
398
447
|
logger.silly(`mongo/remove: File not found, removing queueItem ${JSON.stringify(clean)} from ${collection}`);
|
|
399
|
-
|
|
448
|
+
debug(`mongo/remove: File not found, removing queueItem ${JSON.stringify(clean)} from ${collection}`);
|
|
449
|
+
await operator.deleteOne({
|
|
400
450
|
correlationId: clean
|
|
401
451
|
});
|
|
402
452
|
return true;
|
|
@@ -409,11 +459,13 @@ async function _default(MONGO_URI, collection) {
|
|
|
409
459
|
}
|
|
410
460
|
async function readContent(correlationId) {
|
|
411
461
|
logger.info(`Reading content from mongo for id: ${correlationId} in ${collection}`);
|
|
462
|
+
debug(`Reading content from mongo for id: ${correlationId} in ${collection}`);
|
|
412
463
|
const clean = (0, _mongoSanitize.default)(correlationId);
|
|
413
|
-
|
|
464
|
+
debug(clean);
|
|
465
|
+
const result = await operator.findOne({
|
|
414
466
|
correlationId: clean
|
|
415
467
|
}); // njsscan-ignore: node_nosqli_injection
|
|
416
|
-
|
|
468
|
+
debug(result);
|
|
417
469
|
if (result) {
|
|
418
470
|
const {
|
|
419
471
|
operationSettings
|
|
@@ -425,6 +477,8 @@ async function _default(MONGO_URI, collection) {
|
|
|
425
477
|
};
|
|
426
478
|
}
|
|
427
479
|
logger.debug(`OperationSettings for ${correlationId}: ${JSON.stringify(operationSettings)}`);
|
|
480
|
+
debug(`OperationSettings for ${correlationId}: ${JSON.stringify(operationSettings)}`);
|
|
481
|
+
debug(`Content is only available for streamBulk jobs. ${correlationId} is not a streamBulk job.`);
|
|
428
482
|
throw new _melindaCommons.Error(_httpStatus.default.BAD_REQUEST, {
|
|
429
483
|
message: `Content is only available for streamBulk jobs. ${correlationId} is not a streamBulk job.`
|
|
430
484
|
});
|
|
@@ -436,50 +490,59 @@ async function _default(MONGO_URI, collection) {
|
|
|
436
490
|
async function removeContent(params) {
|
|
437
491
|
logger.info(`Removing content from mongo for id: ${params.correlationId} in ${collection}`);
|
|
438
492
|
const clean = (0, _mongoSanitize.default)(params.correlationId);
|
|
439
|
-
const result = await
|
|
493
|
+
const result = await operator.findOne({
|
|
440
494
|
correlationId: clean
|
|
441
495
|
}); // njsscan-ignore: node_nosqli_injection
|
|
442
496
|
logger.silly(`mongo/removeContent: result ${JSON.stringify(result)}`);
|
|
443
497
|
if (result) {
|
|
498
|
+
// TEST-COVERAGE: next two lines are not (currently) tested
|
|
444
499
|
await gridFSBucket.delete(clean);
|
|
445
500
|
return true;
|
|
446
501
|
}
|
|
447
502
|
return true;
|
|
448
503
|
}
|
|
449
|
-
|
|
450
|
-
// eslint-disable-next-line max-statements
|
|
451
504
|
function getOne({
|
|
452
505
|
queueItemState,
|
|
453
506
|
importJobState = undefined
|
|
454
507
|
}) {
|
|
508
|
+
//params example: "queueItemState": "IMPORTER.IN_QUEUE", "importJobState":{"importJobState.UPDATE": "IN_QUEUE"}
|
|
455
509
|
logger.silly(`queueItemState: ${queueItemState}, importJobState: ${JSON.stringify(importJobState)}`);
|
|
510
|
+
//debug(`queueItemState: ${queueItemState}, importJobState: ${JSON.stringify(importJobState)}`);
|
|
456
511
|
const cleanQueueItemState = queueItemState ? {
|
|
457
512
|
queueItemState: (0, _mongoSanitize.default)(queueItemState)
|
|
458
513
|
} : undefined;
|
|
514
|
+
const options = {
|
|
515
|
+
projection: {
|
|
516
|
+
_id: 0
|
|
517
|
+
}
|
|
518
|
+
};
|
|
459
519
|
try {
|
|
460
520
|
// Just queueItemState
|
|
461
521
|
if (queueItemState && importJobState === undefined) {
|
|
462
522
|
logger.silly(`Checking DB ${collection} for just ${JSON.stringify(cleanQueueItemState.queueItemState)}`);
|
|
463
|
-
|
|
523
|
+
//debug(`Checking DB ${collection} for just ${JSON.stringify(cleanQueueItemState.queueItemState)}`);
|
|
524
|
+
return operator.findOne({
|
|
464
525
|
...cleanQueueItemState
|
|
465
|
-
});
|
|
526
|
+
}, options);
|
|
466
527
|
}
|
|
467
528
|
|
|
468
529
|
// importJobState
|
|
469
530
|
if (importJobState && queueItemState === undefined) {
|
|
470
531
|
logger.silly(`Checking DB ${collection} for ${JSON.stringify(importJobState)}`);
|
|
471
|
-
|
|
532
|
+
//debug(`Checking DB ${collection} for ${JSON.stringify(importJobState)}`);
|
|
533
|
+
return operator.findOne({
|
|
472
534
|
...importJobState
|
|
473
|
-
});
|
|
535
|
+
}, options);
|
|
474
536
|
}
|
|
475
537
|
|
|
476
538
|
// importJobState and queueItemState
|
|
477
539
|
if (importJobState && queueItemState) {
|
|
478
540
|
logger.silly(`Checking DB ${collection} for ${queueItemState} and ${JSON.stringify(importJobState)}`);
|
|
479
|
-
|
|
541
|
+
//debug(`Checking DB ${collection} for ${queueItemState} and ${JSON.stringify(importJobState)}`);
|
|
542
|
+
return operator.findOne({
|
|
480
543
|
...cleanQueueItemState,
|
|
481
544
|
...importJobState
|
|
482
|
-
});
|
|
545
|
+
}, options);
|
|
483
546
|
}
|
|
484
547
|
logger.debug(`getOne not working!`);
|
|
485
548
|
throw new _melindaCommons.Error(_httpStatus.default.INTERNAL_SERVER_ERROR, 'Missing parameters, cannot get queueItem');
|
|
@@ -496,6 +559,7 @@ async function _default(MONGO_URI, collection) {
|
|
|
496
559
|
// Return content stream
|
|
497
560
|
return gridFSBucket.openDownloadStreamByName(clean);
|
|
498
561
|
} catch (error) {
|
|
562
|
+
// TEST-COVERAGE: this error is not (currently) tested
|
|
499
563
|
const errorMessage = error.payload || error.message || '';
|
|
500
564
|
(0, _utils.logError)(error);
|
|
501
565
|
throw new _melindaCommons.Error(_httpStatus.default.INTERNAL_SERVER_ERROR, `Mongo errored: ${errorMessage}`);
|
|
@@ -509,7 +573,7 @@ async function _default(MONGO_URI, collection) {
|
|
|
509
573
|
logger.verbose(`Push ids (${handledIds.length}) and rejectedIds (${rejectedIds.length}) ${correlationId} to ${collection}`);
|
|
510
574
|
logger.debug(`ids (${handledIds.length}): ${JSON.stringify(handledIds)}, rejectedIds ${rejectedIds.length}: ${JSON.stringify(rejectedIds)}`);
|
|
511
575
|
const clean = (0, _mongoSanitize.default)(correlationId);
|
|
512
|
-
await
|
|
576
|
+
await operator.updateOne({
|
|
513
577
|
correlationId: clean
|
|
514
578
|
}, {
|
|
515
579
|
$set: {
|
|
@@ -534,7 +598,8 @@ async function _default(MONGO_URI, collection) {
|
|
|
534
598
|
logger.silly(`Messages (${messages.length}): ${JSON.stringify(messages)}}`);
|
|
535
599
|
const clean = (0, _mongoSanitize.default)(correlationId);
|
|
536
600
|
const cleanMessageField = (0, _mongoSanitize.default)(messageField);
|
|
537
|
-
|
|
601
|
+
debug(`${messageField} -> ${cleanMessageField}`);
|
|
602
|
+
await operator.updateOne({
|
|
538
603
|
correlationId: clean
|
|
539
604
|
}, {
|
|
540
605
|
$set: {
|
|
@@ -559,6 +624,7 @@ async function _default(MONGO_URI, collection) {
|
|
|
559
624
|
const stateInQueueItemStatesValidator = Object.values(_constants.QUEUE_ITEM_STATE.VALIDATOR).indexOf(state) > -1;
|
|
560
625
|
const stateInQueueItemStatesImporter = Object.values(_constants.QUEUE_ITEM_STATE.IMPORTER).indexOf(state) > -1;
|
|
561
626
|
if (!stateInQueueItemStates && !stateInQueueItemStatesValidator && !stateInQueueItemStatesImporter) {
|
|
627
|
+
debug(`Trying to set invalid state ${state}`);
|
|
562
628
|
throw new _melindaCommons.Error(_httpStatus.default.BAD_REQUEST, 'Trying to set invalid state');
|
|
563
629
|
}
|
|
564
630
|
const clean = (0, _mongoSanitize.default)(correlationId);
|
|
@@ -572,7 +638,7 @@ async function _default(MONGO_URI, collection) {
|
|
|
572
638
|
// Do not update value that are undefined
|
|
573
639
|
// eslint-disable-next-line functional/immutable-data
|
|
574
640
|
Object.keys(updateValues).forEach(key => updateValues[key] === undefined && delete updateValues[key]);
|
|
575
|
-
return
|
|
641
|
+
return operator.findOneAndUpdate({
|
|
576
642
|
correlationId: clean
|
|
577
643
|
}, {
|
|
578
644
|
$set: updateValues
|
|
@@ -580,7 +646,7 @@ async function _default(MONGO_URI, collection) {
|
|
|
580
646
|
projection: {
|
|
581
647
|
_id: 0
|
|
582
648
|
},
|
|
583
|
-
|
|
649
|
+
returnDocument: 'after'
|
|
584
650
|
});
|
|
585
651
|
}
|
|
586
652
|
function setImportJobState({
|
|
@@ -590,13 +656,16 @@ async function _default(MONGO_URI, collection) {
|
|
|
590
656
|
}) {
|
|
591
657
|
// should this also get importJobState as previously created object? {}
|
|
592
658
|
logger.info(`Setting queue-item importJobState: {${operation}: ${importJobState}} for ${correlationId}`);
|
|
659
|
+
debug(`Setting queue-item importJobState: {${operation}: ${importJobState}} for ${correlationId}`);
|
|
593
660
|
const cleanCorrelationId = (0, _mongoSanitize.default)(correlationId);
|
|
594
661
|
const cleanImportJobState = (0, _mongoSanitize.default)(importJobState);
|
|
595
662
|
if (!(operation in _constants.OPERATIONS)) {
|
|
596
|
-
|
|
663
|
+
debug(`Error: ${operation} is not in ${JSON.stringify(_constants.OPERATIONS)}`);
|
|
664
|
+
throw new _melindaCommons.Error(_httpStatus.default.BAD_REQUEST, 'Invalid operation for import job state');
|
|
597
665
|
}
|
|
598
666
|
if (!(cleanImportJobState in _constants.IMPORT_JOB_STATE)) {
|
|
599
|
-
|
|
667
|
+
debug(`Error: ${cleanImportJobState} is not in ${JSON.stringify(_constants.IMPORT_JOB_STATE)}`);
|
|
668
|
+
throw new _melindaCommons.Error(_httpStatus.default.BAD_REQUEST, 'Invalid import job state');
|
|
600
669
|
}
|
|
601
670
|
const newJobState = getNewJobState(operation, cleanImportJobState);
|
|
602
671
|
function getNewJobState(operation, cleanImportJobState) {
|
|
@@ -615,9 +684,11 @@ async function _default(MONGO_URI, collection) {
|
|
|
615
684
|
'importJobState.FIX': cleanImportJobState
|
|
616
685
|
};
|
|
617
686
|
}
|
|
687
|
+
// istanbul ignore next
|
|
688
|
+
// Ignoring for coverage - we error this already
|
|
618
689
|
throw new _melindaCommons.Error('400', 'Invalid operation for import job state');
|
|
619
690
|
}
|
|
620
|
-
return
|
|
691
|
+
return operator.findOneAndUpdate({
|
|
621
692
|
correlationId: cleanCorrelationId
|
|
622
693
|
}, {
|
|
623
694
|
$set: {
|
|
@@ -628,7 +699,7 @@ async function _default(MONGO_URI, collection) {
|
|
|
628
699
|
projection: {
|
|
629
700
|
_id: 0
|
|
630
701
|
},
|
|
631
|
-
|
|
702
|
+
returnDocument: 'after'
|
|
632
703
|
});
|
|
633
704
|
}
|
|
634
705
|
async function setOperation({
|
|
@@ -638,13 +709,13 @@ async function _default(MONGO_URI, collection) {
|
|
|
638
709
|
const newOperation = operation;
|
|
639
710
|
const {
|
|
640
711
|
operation: oldOperation
|
|
641
|
-
} = await
|
|
712
|
+
} = await operator.findOne({
|
|
642
713
|
correlationId
|
|
643
714
|
});
|
|
644
715
|
logger.info(`Setting queue-item operation from ${oldOperation} to ${newOperation} for ${correlationId} to ${collection}`);
|
|
645
716
|
const cleanCorrelationId = (0, _mongoSanitize.default)(correlationId);
|
|
646
717
|
const cleanNewOperation = (0, _mongoSanitize.default)(newOperation);
|
|
647
|
-
const result = await
|
|
718
|
+
const result = await operator.findOneAndUpdate({
|
|
648
719
|
correlationId: cleanCorrelationId
|
|
649
720
|
}, {
|
|
650
721
|
$set: {
|
|
@@ -655,9 +726,10 @@ async function _default(MONGO_URI, collection) {
|
|
|
655
726
|
projection: {
|
|
656
727
|
_id: 0
|
|
657
728
|
},
|
|
658
|
-
|
|
729
|
+
returnDocument: 'after'
|
|
659
730
|
});
|
|
660
|
-
|
|
731
|
+
debugDev(`${JSON.stringify(result)}`);
|
|
732
|
+
return result.ok || result;
|
|
661
733
|
// logger.debug(JSON.stringify(result));
|
|
662
734
|
}
|
|
663
735
|
async function setOperations({
|
|
@@ -666,7 +738,7 @@ async function _default(MONGO_URI, collection) {
|
|
|
666
738
|
removeOperation = undefined
|
|
667
739
|
}) {
|
|
668
740
|
const cleanCorrelationId = (0, _mongoSanitize.default)(correlationId);
|
|
669
|
-
const queueItem = await
|
|
741
|
+
const queueItem = await operator.findOne({
|
|
670
742
|
correlationId: cleanCorrelationId
|
|
671
743
|
});
|
|
672
744
|
logger.silly(`We found a queueItem: ${JSON.stringify(queueItem)}`);
|
|
@@ -676,7 +748,7 @@ async function _default(MONGO_URI, collection) {
|
|
|
676
748
|
const operationsAfterRemoveAndAdd = operationsAfterRemove.includes(addOperation) ? operationsAfterRemove : [...operationsAfterRemove, addOperation];
|
|
677
749
|
logger.silly(`operationsAfterRemove: ${operationsAfterRemove}`);
|
|
678
750
|
logger.silly(`operationAfterRemoveAndAdd: ${operationsAfterRemoveAndAdd}`);
|
|
679
|
-
const result = await
|
|
751
|
+
const result = await operator.findOneAndUpdate({
|
|
680
752
|
correlationId: cleanCorrelationId
|
|
681
753
|
}, {
|
|
682
754
|
$set: {
|
|
@@ -687,16 +759,17 @@ async function _default(MONGO_URI, collection) {
|
|
|
687
759
|
projection: {
|
|
688
760
|
_id: 0
|
|
689
761
|
},
|
|
690
|
-
|
|
762
|
+
returnDocument: 'after'
|
|
691
763
|
});
|
|
692
|
-
|
|
764
|
+
debugDev(`${JSON.stringify(result)}`);
|
|
765
|
+
return result.ok || result;
|
|
693
766
|
// logger.debug(JSON.stringify(result));
|
|
694
767
|
}
|
|
695
768
|
async function addBlobSize({
|
|
696
769
|
correlationId
|
|
697
770
|
}) {
|
|
698
771
|
const cleanCorrelationId = (0, _mongoSanitize.default)(correlationId);
|
|
699
|
-
const result = await
|
|
772
|
+
const result = await operator.findOneAndUpdate({
|
|
700
773
|
correlationId: cleanCorrelationId,
|
|
701
774
|
queueItemState: _constants.QUEUE_ITEM_STATE.VALIDATOR.WAITING_FOR_RECORDS
|
|
702
775
|
}, {
|
|
@@ -710,9 +783,10 @@ async function _default(MONGO_URI, collection) {
|
|
|
710
783
|
projection: {
|
|
711
784
|
_id: 0
|
|
712
785
|
},
|
|
713
|
-
|
|
786
|
+
returnDocument: 'after'
|
|
714
787
|
});
|
|
715
788
|
logger.verbose(`AddBlobSizeResult in mongo: ${JSON.stringify(result)}`);
|
|
789
|
+
debugDev(`${JSON.stringify(result)}`);
|
|
716
790
|
return result;
|
|
717
791
|
}
|
|
718
792
|
async function setBlobSize({
|
|
@@ -721,7 +795,7 @@ async function _default(MONGO_URI, collection) {
|
|
|
721
795
|
}) {
|
|
722
796
|
const cleanCorrelationId = (0, _mongoSanitize.default)(correlationId);
|
|
723
797
|
const cleanBlobSize = (0, _mongoSanitize.default)(blobSize);
|
|
724
|
-
const result = await
|
|
798
|
+
const result = await operator.findOneAndUpdate({
|
|
725
799
|
correlationId: cleanCorrelationId
|
|
726
800
|
}, {
|
|
727
801
|
$set: {
|
|
@@ -732,9 +806,10 @@ async function _default(MONGO_URI, collection) {
|
|
|
732
806
|
projection: {
|
|
733
807
|
_id: 0
|
|
734
808
|
},
|
|
735
|
-
|
|
809
|
+
returnDocument: 'after'
|
|
736
810
|
});
|
|
737
811
|
logger.verbose(`SetBlobSizeResult in mongo: ${JSON.stringify(result)}`);
|
|
812
|
+
debugDev(`${JSON.stringify(result)}`);
|
|
738
813
|
return result;
|
|
739
814
|
}
|
|
740
815
|
}
|