@natlibfi/melinda-rest-api-commons 2.3.2-alpha.1 → 2.3.2-alpha.10

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.
Files changed (42) hide show
  1. package/dist/amqp.health.spec.js +69 -0
  2. package/dist/amqp.health.spec.js.map +1 -0
  3. package/dist/amqp.js +135 -92
  4. package/dist/amqp.js.map +1 -1
  5. package/dist/amqp.tests.spec.js +338 -0
  6. package/dist/amqp.tests.spec.js.map +1 -0
  7. package/dist/constants.js +5 -3
  8. package/dist/constants.js.map +1 -1
  9. package/dist/helpers/conversion.js +5 -25
  10. package/dist/helpers/conversion.js.map +1 -1
  11. package/dist/helpers/conversion.spec.js +1 -7
  12. package/dist/helpers/conversion.spec.js.map +1 -1
  13. package/dist/helpers/fix-constants.js +98 -0
  14. package/dist/helpers/fix-constants.js.map +1 -0
  15. package/dist/helpers/fix-handle-tempurns.js +153 -0
  16. package/dist/helpers/fix-handle-tempurns.js.map +1 -0
  17. package/dist/helpers/fix-strip-f884s.js +87 -0
  18. package/dist/helpers/fix-strip-f884s.js.map +1 -0
  19. package/dist/helpers/fix-utils.js +82 -0
  20. package/dist/helpers/fix-utils.js.map +1 -0
  21. package/dist/helpers/fix.js +178 -0
  22. package/dist/helpers/fix.js.map +1 -0
  23. package/dist/helpers/fix.spec.js +46 -0
  24. package/dist/helpers/fix.spec.js.map +1 -0
  25. package/dist/helpers/validation.js +0 -6
  26. package/dist/helpers/validation.js.map +1 -1
  27. package/dist/helpers/validation.spec.js +1 -7
  28. package/dist/helpers/validation.spec.js.map +1 -1
  29. package/dist/index.js +12 -18
  30. package/dist/index.js.map +1 -1
  31. package/dist/mongo.js +50 -89
  32. package/dist/mongo.js.map +1 -1
  33. package/dist/mongo.spec.js.draft +54 -0
  34. package/dist/mongoLog.js +16 -35
  35. package/dist/mongoLog.js.map +1 -1
  36. package/dist/utils.js +33 -35
  37. package/dist/utils.js.map +1 -1
  38. package/package.json +122 -116
  39. package/dist/helpers/format.js +0 -305
  40. package/dist/helpers/format.js.map +0 -1
  41. package/dist/helpers/format.spec.js +0 -224
  42. package/dist/helpers/format.spec.js.map +0 -1
package/dist/mongo.js CHANGED
@@ -4,25 +4,16 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = _default;
7
-
8
7
  var _mongodb = require("mongodb");
9
-
10
8
  var _melindaBackendCommons = require("@natlibfi/melinda-backend-commons");
11
-
12
9
  var _melindaCommons = require("@natlibfi/melinda-commons");
13
-
14
10
  var _constants = require("./constants");
15
-
16
11
  var _utils = require("./utils.js");
17
-
18
12
  var _moment = _interopRequireDefault(require("moment"));
19
-
20
13
  var _httpStatus = _interopRequireDefault(require("http-status"));
21
-
22
14
  var _mongoSanitize = _interopRequireDefault(require("mongo-sanitize"));
23
-
24
15
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
25
-
16
+ /* eslint-disable max-lines */
26
17
  /**
27
18
  *
28
19
  * @licstart The following is the entire license notice for the JavaScript code in this file.
@@ -50,6 +41,7 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
50
41
  * for the JavaScript code in this file.
51
42
  *
52
43
  */
44
+
53
45
  //import isDeepStrictEqual from 'util';
54
46
 
55
47
  /* QueueItem:
@@ -80,9 +72,11 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
80
72
  "modificationTime":"2020-01-01T00:00:01.000Z",
81
73
  }
82
74
  */
75
+
83
76
  async function _default(MONGO_URI, collection) {
84
- const logger = (0, _melindaBackendCommons.createLogger)(); // Connect to mongo (MONGO)
77
+ const logger = (0, _melindaBackendCommons.createLogger)();
85
78
 
79
+ // Connect to mongo (MONGO)
86
80
  const client = await _mongodb.MongoClient.connect(MONGO_URI, {
87
81
  useNewUrlParser: true,
88
82
  useUnifiedTopology: true
@@ -112,7 +106,6 @@ async function _default(MONGO_URI, collection) {
112
106
  addBlobSize,
113
107
  setBlobSize
114
108
  };
115
-
116
109
  async function createPrio({
117
110
  correlationId,
118
111
  cataloger,
@@ -143,22 +136,21 @@ async function _default(MONGO_URI, collection) {
143
136
  creationTime: time,
144
137
  modificationTime: time
145
138
  };
146
-
147
139
  try {
148
140
  const result = await db.collection(collection).insertOne(newQueueItem);
149
-
150
141
  if (result.acknowledged) {
151
142
  logger.info(`New PRIO queue item for ${operation} ${correlationId} has been made in ${collection}`);
152
143
  return;
153
144
  }
154
-
155
145
  throw new _melindaCommons.Error(_httpStatus.default.INTERNAL_SERVER_ERROR);
156
146
  } catch (error) {
147
+ const errorMessage = error.payload || error.message || '';
157
148
  (0, _utils.logError)(error);
158
- throw new _melindaCommons.Error(_httpStatus.default.INTERNAL_SERVER_ERROR);
149
+ throw new _melindaCommons.Error(_httpStatus.default.INTERNAL_SERVER_ERROR, `Mongo errored: ${errorMessage}`);
159
150
  }
160
151
  }
161
152
 
153
+ // eslint-disable-next-line max-statements
162
154
  async function createBulk({
163
155
  correlationId,
164
156
  cataloger,
@@ -193,8 +185,9 @@ async function _default(MONGO_URI, collection) {
193
185
  errorStatus: '',
194
186
  creationTime: time,
195
187
  modificationTime: time
196
- }; // eslint-disable-next-line functional/no-conditional-statement
188
+ };
197
189
 
190
+ // eslint-disable-next-line functional/no-conditional-statement
198
191
  if (stream) {
199
192
  try {
200
193
  // No await here, promises later
@@ -207,17 +200,16 @@ async function _default(MONGO_URI, collection) {
207
200
  }));
208
201
  });
209
202
  } catch (error) {
203
+ const errorMessage = error.payload || error.message || '';
210
204
  (0, _utils.logError)(error);
211
- throw new _melindaCommons.Error(_httpStatus.default.INTERNAL_SERVER_ERROR);
205
+ throw new _melindaCommons.Error(_httpStatus.default.INTERNAL_SERVER_ERROR, `Mongo errored: ${errorMessage}`);
212
206
  }
213
207
  }
214
-
215
- logger.debug(`No stream`); // eslint-disable-next-line functional/no-conditional-statement
216
-
208
+ logger.debug(`No stream`);
209
+ // eslint-disable-next-line functional/no-conditional-statement
217
210
  if (!stream) {
218
211
  try {
219
212
  const result = await db.collection(collection).insertOne(newQueueItem);
220
-
221
213
  if (result.acknowledged) {
222
214
  logger.info(`New noStream BULK queue item for ${operation} ${correlationId} has been made in ${collection}`);
223
215
  return {
@@ -225,16 +217,16 @@ async function _default(MONGO_URI, collection) {
225
217
  queueItemState: _constants.QUEUE_ITEM_STATE.VALIDATOR.WAITING_FOR_RECORDS
226
218
  };
227
219
  }
228
-
229
220
  throw new _melindaCommons.Error(_httpStatus.default.INTERNAL_SERVER_ERROR);
230
221
  } catch (error) {
222
+ const errorMessage = error.payload || error.message || '';
231
223
  (0, _utils.logError)(error);
232
- throw new _melindaCommons.Error(_httpStatus.default.INTERNAL_SERVER_ERROR);
224
+ throw new _melindaCommons.Error(_httpStatus.default.INTERNAL_SERVER_ERROR, `Mongo errored: ${errorMessage}`);
233
225
  }
234
226
  }
235
- } // Check state that the queueItem has not waited too long and set state
236
-
227
+ }
237
228
 
229
+ // Check state that the queueItem has not waited too long and set state
238
230
  async function checkAndSetState({
239
231
  correlationId,
240
232
  state,
@@ -246,7 +238,6 @@ async function _default(MONGO_URI, collection) {
246
238
  const timeOut = await checkTimeOut({
247
239
  correlationId
248
240
  });
249
-
250
241
  if (timeOut) {
251
242
  return setState({
252
243
  correlationId,
@@ -255,11 +246,10 @@ async function _default(MONGO_URI, collection) {
255
246
  errorStatus
256
247
  });
257
248
  }
258
-
259
249
  return false;
260
- } // Check state that the queueItem has not waited too long and set state
261
-
250
+ }
262
251
 
252
+ // Check state that the queueItem has not waited too long and set state
263
253
  async function checkAndSetImportJobState({
264
254
  correlationId,
265
255
  operation,
@@ -275,7 +265,6 @@ async function _default(MONGO_URI, collection) {
275
265
  operation,
276
266
  importJobState
277
267
  });
278
-
279
268
  if (timeOut) {
280
269
  return setImportJobState({
281
270
  correlationId,
@@ -285,10 +274,8 @@ async function _default(MONGO_URI, collection) {
285
274
  errorStatus
286
275
  });
287
276
  }
288
-
289
277
  return false;
290
278
  }
291
-
292
279
  async function query(params, showParams = {}) {
293
280
  const {
294
281
  showOperations = 0,
@@ -313,7 +300,6 @@ async function _default(MONGO_URI, collection) {
313
300
  logger.debug(`Query result: ${result.length > 0 ? 'Found!' : 'Not found!'}`);
314
301
  return result;
315
302
  }
316
-
317
303
  async function queryById({
318
304
  correlationId,
319
305
  checkModTime = false
@@ -321,28 +307,25 @@ async function _default(MONGO_URI, collection) {
321
307
  const result = await db.collection(collection).findOne({
322
308
  correlationId
323
309
  });
324
-
325
310
  if (checkModTime) {
326
311
  const timeOut = await checkTimeOut({
327
312
  correlationId
328
313
  });
329
-
330
314
  if (timeOut) {
331
315
  return queryById({
332
316
  correlationId
333
317
  });
334
318
  }
335
-
336
319
  return result;
337
320
  }
338
-
339
321
  return result;
340
- } // Check that if the item has waited too long
322
+ }
323
+
324
+ // Check that if the item has waited too long
341
325
  // If the last modification time for the queueItem is older than 1 minute
342
326
  // set state to ABORT and return false, otherwise return true
343
327
  // If the state is already ABORT or ERROR return false
344
328
 
345
-
346
329
  async function checkTimeOut({
347
330
  correlationId
348
331
  }) {
@@ -352,19 +335,17 @@ async function _default(MONGO_URI, collection) {
352
335
  importJobState
353
336
  } = await db.collection(collection).findOne({
354
337
  correlationId
355
- }); // should we check for DONE too?
338
+ });
356
339
 
340
+ // should we check for DONE too?
357
341
  if ([_constants.QUEUE_ITEM_STATE.ABORT, _constants.QUEUE_ITEM_STATE.ERROR, _constants.QUEUE_ITEM_STATE.DONE].includes(oldState)) {
358
342
  logger.silly(`${correlationId} has already state: ${oldState}`);
359
343
  return false;
360
344
  }
361
-
362
345
  const timeoutTime = (0, _moment.default)(modificationTime).add(1, 'm');
363
346
  logger.silly(`timeOut @ ${timeoutTime}`);
364
-
365
347
  if (timeoutTime.isBefore()) {
366
348
  const finalImportJobStates = [_constants.IMPORT_JOB_STATE.ABORT, _constants.IMPORT_JOB_STATE.DONE, _constants.IMPORT_JOB_STATE.EMPTY, _constants.IMPORT_JOB_STATE.ERROR];
367
-
368
349
  if (!finalImportJobStates.includes(importJobState.CREATE)) {
369
350
  // eslint-disable-line
370
351
  await setImportJobState({
@@ -373,7 +354,6 @@ async function _default(MONGO_URI, collection) {
373
354
  importJobState: _constants.IMPORT_JOB_STATE.ABORT
374
355
  });
375
356
  }
376
-
377
357
  if (!finalImportJobStates.includes(importJobState.UPDATE)) {
378
358
  // eslint-disable-line
379
359
  await setImportJobState({
@@ -382,7 +362,6 @@ async function _default(MONGO_URI, collection) {
382
362
  importJobState: _constants.IMPORT_JOB_STATE.ABORT
383
363
  });
384
364
  }
385
-
386
365
  await setState({
387
366
  correlationId,
388
367
  state: _constants.QUEUE_ITEM_STATE.ABORT,
@@ -391,21 +370,17 @@ async function _default(MONGO_URI, collection) {
391
370
  });
392
371
  return false;
393
372
  }
394
-
395
373
  return true;
396
374
  }
397
-
398
375
  async function remove(params) {
399
376
  logger.silly(`${JSON.stringify(params)}`);
400
377
  logger.info(`Removing from Mongo (${collection}) id: ${params.correlationId}`);
401
378
  const clean = (0, _mongoSanitize.default)(params.correlationId);
402
379
  logger.silly(`mongo/remove: clean: ${JSON.stringify(clean)}`);
403
-
404
380
  try {
405
381
  //const metadataResult = await getFileMetadata({filename: clean});
406
382
  //logger.debug(`mongo/remove: metadataResult: ${JSON.stringify(metadataResult)}`);
407
383
  const noContent = await removeContent(params);
408
-
409
384
  if (noContent) {
410
385
  await db.collection(collection).deleteOne({
411
386
  correlationId: clean
@@ -421,15 +396,12 @@ async function _default(MONGO_URI, collection) {
421
396
  });
422
397
  return true;
423
398
  }
424
-
425
399
  logger.error(err.message);
426
400
  throw new _melindaCommons.Error(_httpStatus.default.INTERNAL_SERVER_ERROR, err.message);
427
401
  }
428
-
429
402
  throw new _melindaCommons.Error(_httpStatus.default.INTERNAL_SERVER_ERROR, err.message);
430
403
  }
431
404
  }
432
-
433
405
  async function readContent(correlationId) {
434
406
  logger.info(`Reading content from mongo for id: ${correlationId} in ${collection}`);
435
407
  const clean = (0, _mongoSanitize.default)(correlationId);
@@ -441,43 +413,36 @@ async function _default(MONGO_URI, collection) {
441
413
  const {
442
414
  operationSettings
443
415
  } = result;
444
-
445
416
  if (operationSettings.prio === false && operationSettings.noStream === false) {
446
417
  return {
447
418
  contentType: result.contentType,
448
419
  readStream: gridFSBucket.openDownloadStreamByName(clean)
449
420
  };
450
421
  }
451
-
452
422
  logger.debug(`OperationSetting for ${correlationId}: ${JSON.stringify(operationSettings)}`);
453
423
  throw new _melindaCommons.Error(_httpStatus.default.BAD_REQUEST, {
454
424
  message: `Content is only available for streamBulk jobs. ${correlationId} is not a streamBulk job.`
455
425
  });
456
426
  }
457
-
458
427
  throw new _melindaCommons.Error(_httpStatus.default.NOT_FOUND, {
459
428
  message: `Job ${correlationId} not found.`
460
429
  });
461
430
  }
462
-
463
431
  async function removeContent(params) {
464
432
  logger.info(`Removing content from mongo for id: ${params.correlationId} in ${collection}`);
465
433
  const clean = (0, _mongoSanitize.default)(params.correlationId);
466
434
  const result = await db.collection(collection).findOne({
467
435
  correlationId: clean
468
436
  }); // njsscan-ignore: node_nosqli_injection
469
-
470
437
  logger.silly(`mongo/removeContent: result ${JSON.stringify(result)}`);
471
-
472
438
  if (result) {
473
439
  await gridFSBucket.delete(clean);
474
440
  return true;
475
441
  }
476
-
477
442
  return true;
478
- } // eslint-disable-next-line max-statements
479
-
443
+ }
480
444
 
445
+ // eslint-disable-next-line max-statements
481
446
  function getOne({
482
447
  queueItemState,
483
448
  importJobState = undefined
@@ -486,49 +451,51 @@ async function _default(MONGO_URI, collection) {
486
451
  const cleanQueueItemState = queueItemState ? {
487
452
  queueItemState: (0, _mongoSanitize.default)(queueItemState)
488
453
  } : undefined;
489
-
490
454
  try {
491
455
  // Just queueItemState
492
456
  if (queueItemState && importJobState === undefined) {
493
457
  logger.silly(`Checking DB ${collection} for just ${JSON.stringify(cleanQueueItemState.queueItemState)}`);
494
- return db.collection(collection).findOne({ ...cleanQueueItemState
458
+ return db.collection(collection).findOne({
459
+ ...cleanQueueItemState
495
460
  });
496
- } // importJobState
497
-
461
+ }
498
462
 
463
+ // importJobState
499
464
  if (importJobState && queueItemState === undefined) {
500
465
  logger.silly(`Checking DB ${collection} for ${JSON.stringify(importJobState)}`);
501
- return db.collection(collection).findOne({ ...importJobState
466
+ return db.collection(collection).findOne({
467
+ ...importJobState
502
468
  });
503
- } // importJobState and queueItemState
504
-
469
+ }
505
470
 
471
+ // importJobState and queueItemState
506
472
  if (importJobState && queueItemState) {
507
473
  logger.silly(`Checking DB ${collection} for ${queueItemState} and ${JSON.stringify(importJobState)}`);
508
- return db.collection(collection).findOne({ ...cleanQueueItemState,
474
+ return db.collection(collection).findOne({
475
+ ...cleanQueueItemState,
509
476
  ...importJobState
510
477
  });
511
478
  }
512
-
513
479
  logger.debug(`getOne not working!`);
514
480
  throw new _melindaCommons.Error(_httpStatus.default.INTERNAL_SERVER_ERROR, 'Missing parameters, cannot get queueItem');
515
481
  } catch (error) {
482
+ const errorMessage = error.payload || error.message || '';
516
483
  (0, _utils.logError)(error);
484
+ throw new _melindaCommons.Error(_httpStatus.default.INTERNAL_SERVER_ERROR, `Mongo errored: ${errorMessage}`);
517
485
  }
518
486
  }
519
-
520
487
  function getStream(correlationId) {
521
488
  logger.info(`Forming stream from mongo for ${correlationId} in ${collection}`);
522
489
  const clean = (0, _mongoSanitize.default)(correlationId);
523
-
524
490
  try {
525
491
  // Return content stream
526
492
  return gridFSBucket.openDownloadStreamByName(clean);
527
493
  } catch (error) {
494
+ const errorMessage = error.payload || error.message || '';
528
495
  (0, _utils.logError)(error);
496
+ throw new _melindaCommons.Error(_httpStatus.default.INTERNAL_SERVER_ERROR, `Mongo errored: ${errorMessage}`);
529
497
  }
530
498
  }
531
-
532
499
  async function pushIds({
533
500
  correlationId,
534
501
  handledIds,
@@ -553,7 +520,6 @@ async function _default(MONGO_URI, collection) {
553
520
  }
554
521
  });
555
522
  }
556
-
557
523
  async function pushMessages({
558
524
  correlationId,
559
525
  messages,
@@ -576,7 +542,6 @@ async function _default(MONGO_URI, collection) {
576
542
  }
577
543
  });
578
544
  }
579
-
580
545
  function setState({
581
546
  correlationId,
582
547
  state,
@@ -588,20 +553,19 @@ async function _default(MONGO_URI, collection) {
588
553
  const stateInQueueItemStates = Object.values(_constants.QUEUE_ITEM_STATE).indexOf(state) > -1;
589
554
  const stateInQueueItemStatesValidator = Object.values(_constants.QUEUE_ITEM_STATE.VALIDATOR).indexOf(state) > -1;
590
555
  const stateInQueueItemStatesImporter = Object.values(_constants.QUEUE_ITEM_STATE.IMPORTER).indexOf(state) > -1;
591
-
592
556
  if (!stateInQueueItemStates && !stateInQueueItemStatesValidator && !stateInQueueItemStatesImporter) {
593
557
  throw new _melindaCommons.Error(_httpStatus.default.BAD_REQUEST, 'Trying to set invalid state');
594
558
  }
595
-
596
559
  const clean = (0, _mongoSanitize.default)(correlationId);
597
560
  const updateValues = {
598
561
  queueItemState: state,
599
562
  modificationTime: (0, _moment.default)().toDate(),
600
563
  errorMessage,
601
564
  errorStatus
602
- }; // Do not update value that are undefined
603
- // eslint-disable-next-line functional/immutable-data
565
+ };
604
566
 
567
+ // Do not update value that are undefined
568
+ // eslint-disable-next-line functional/immutable-data
605
569
  Object.keys(updateValues).forEach(key => updateValues[key] === undefined && delete updateValues[key]);
606
570
  return db.collection(collection).findOneAndUpdate({
607
571
  correlationId: clean
@@ -614,7 +578,6 @@ async function _default(MONGO_URI, collection) {
614
578
  returnNewDocument: true
615
579
  });
616
580
  }
617
-
618
581
  function setImportJobState({
619
582
  correlationId,
620
583
  operation,
@@ -624,15 +587,12 @@ async function _default(MONGO_URI, collection) {
624
587
  logger.info(`Setting queue-item importJobState: {${operation}: ${importJobState}} for ${correlationId}`);
625
588
  const cleanCorrelationId = (0, _mongoSanitize.default)(correlationId);
626
589
  const cleanImportJobState = (0, _mongoSanitize.default)(importJobState);
627
-
628
590
  if (!(operation in _constants.OPERATIONS)) {
629
591
  throw new _melindaCommons.Error('400', 'Invalid operation for import job state');
630
592
  }
631
-
632
593
  if (!(cleanImportJobState in _constants.IMPORT_JOB_STATE)) {
633
594
  throw new _melindaCommons.Error('400', 'Invalid import job state');
634
595
  }
635
-
636
596
  const newJobState = operation === _constants.OPERATIONS.CREATE ? {
637
597
  'importJobState.CREATE': cleanImportJobState
638
598
  } : {
@@ -641,7 +601,8 @@ async function _default(MONGO_URI, collection) {
641
601
  return db.collection(collection).findOneAndUpdate({
642
602
  correlationId: cleanCorrelationId
643
603
  }, {
644
- $set: { ...newJobState,
604
+ $set: {
605
+ ...newJobState,
645
606
  modificationTime: (0, _moment.default)().toDate()
646
607
  }
647
608
  }, {
@@ -651,7 +612,6 @@ async function _default(MONGO_URI, collection) {
651
612
  returnNewDocument: true
652
613
  });
653
614
  }
654
-
655
615
  async function setOperation({
656
616
  correlationId,
657
617
  operation
@@ -678,7 +638,8 @@ async function _default(MONGO_URI, collection) {
678
638
  },
679
639
  returnNewDocument: true
680
640
  });
681
- return result.ok; // logger.debug(JSON.stringify(result));
641
+ return result.ok;
642
+ // logger.debug(JSON.stringify(result));
682
643
  }
683
644
 
684
645
  async function setOperations({
@@ -710,7 +671,8 @@ async function _default(MONGO_URI, collection) {
710
671
  },
711
672
  returnNewDocument: true
712
673
  });
713
- return result.ok; // logger.debug(JSON.stringify(result));
674
+ return result.ok;
675
+ // logger.debug(JSON.stringify(result));
714
676
  }
715
677
 
716
678
  async function addBlobSize({
@@ -736,7 +698,6 @@ async function _default(MONGO_URI, collection) {
736
698
  logger.verbose(`AddBlobSizeResult in mongo: ${JSON.stringify(result)}`);
737
699
  return result;
738
700
  }
739
-
740
701
  async function setBlobSize({
741
702
  correlationId,
742
703
  blobSize