@natlibfi/melinda-rest-api-commons 2.0.1 → 2.1.2

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/README.md CHANGED
@@ -12,8 +12,8 @@ Shared modules for microservices of Melinda rest api import system
12
12
 
13
13
  |microservice | states |
14
14
  |-------------| -------------------------------------------------------------------------|
15
- |`http` | -> VALIDATOR.UPLOADING -> VALIDATOR.PENDING_QUEUEING -> |
16
- |`validator` | VALIDATOR.QUEUEING_IN_PROGRESS -> IMPORTER.IN_QUEUE -> |
15
+ |`http` | -> VALIDATOR.UPLOADING -> VALIDATOR.PENDING_QUEUING -> |
16
+ |`validator` | VALIDATOR.QUEUING_IN_PROGRESS -> IMPORTER.IN_QUEUE -> |
17
17
  |`importer` | IMPORTER.IMPORTING -> IMPORTER.IN_PROCESS -> IMPORTER.IMPORTING -> DONE |
18
18
  | | |
19
19
  |any | any -> ERROR if errored
@@ -37,7 +37,7 @@ var _melindaBackendCommons = require("@natlibfi/melinda-backend-commons");
37
37
  * for the JavaScript code in this file.
38
38
  *
39
39
  */
40
- function formatRecord(record, settings) {
40
+ function formatRecord(record, settings = []) {
41
41
  const logger = (0, _melindaBackendCommons.createLogger)();
42
42
  logger.verbose('Applying formating');
43
43
 
@@ -45,10 +45,11 @@ function formatRecord(record, settings) {
45
45
  subfieldValues: false
46
46
  });
47
47
 
48
+ generateMissingSIDs();
48
49
  settings.forEach(options => {
49
50
  replacePrefixes(options);
50
51
  });
51
- return newRecord; // Replace prefix in all specified subfields
52
+ return newRecord.toObject(); // Replace prefix in all specified subfields
52
53
 
53
54
  function replacePrefixes(options) {
54
55
  const {
@@ -66,6 +67,67 @@ function formatRecord(record, settings) {
66
67
  });
67
68
  });
68
69
  }
70
+
71
+ function generateMissingSIDs() {
72
+ const f035Filters = [/^\(FI-BTJ\)/u, /^\(FI-TATI\)/u];
73
+ const fSIDFilters = ['FI-BTJ', 'tati'];
74
+ const f035s = f035ToSidInfo(newRecord.get(/^035$/u));
75
+ const fSIDs = sidsToSidInfo(newRecord.get(/^SID$/u));
76
+ const sidsToBeAdded = genNewSids(fSIDs.length === 0 ? f035s // test that SIDs are not there yet
77
+ : f035s.filter(f035SidInfo => fSIDs.some(fSIDInfo => f035SidInfo.SID !== fSIDInfo.SID && f035SidInfo.value !== fSIDInfo.value))); // Add new SIDs
78
+
79
+ return sidsToBeAdded.forEach(sidField => newRecord.insertField(sidField));
80
+
81
+ function genNewSids(sidsToBeAdded) {
82
+ return sidsToBeAdded.map(sidInfo => ({
83
+ tag: 'SID',
84
+ ind1: ' ',
85
+ ind2: ' ',
86
+ subfields: [{
87
+ code: 'c',
88
+ value: sidInfo.value
89
+ }, {
90
+ code: 'b',
91
+ value: sidInfo.SID
92
+ }]
93
+ }));
94
+ }
95
+
96
+ function sidsToSidInfo(SIDs) {
97
+ return SIDs.flatMap(({
98
+ subfields
99
+ }) => {
100
+ const [SID] = subfields.filter(sub => sub.code === 'b' && fSIDFilters.includes(sub.value)).map(sub => sub.value);
101
+ const [value] = subfields.filter(sub => sub.code === 'c' && sub.value !== undefined).map(sub => sub.value);
102
+
103
+ if (SID && value) {
104
+ return {
105
+ SID,
106
+ value
107
+ };
108
+ }
109
+
110
+ return undefined;
111
+ }).filter(value => value !== undefined);
112
+ }
113
+
114
+ function f035ToSidInfo(f035s) {
115
+ return f035s.flatMap(({
116
+ subfields
117
+ }) => {
118
+ const [wantedSub] = subfields.filter(sub => sub.code === 'a' && f035Filters.some(regexp => regexp.test(sub.value)));
119
+
120
+ if (wantedSub) {
121
+ return {
122
+ SID: fSIDFilters[f035Filters.findIndex(regexp => regexp.test(wantedSub.value))],
123
+ value: wantedSub.value.slice(wantedSub.value.indexOf(')') + 1)
124
+ };
125
+ }
126
+
127
+ return undefined;
128
+ }).filter(value => value !== undefined);
129
+ }
130
+ }
69
131
  } // If placed in config.js testing needs envs
70
132
 
71
133
 
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/helpers/format.js"],"names":["formatRecord","record","settings","logger","verbose","newRecord","MarcRecord","clone","subfieldValues","forEach","options","replacePrefixes","oldPrefix","newPrefix","prefixReplaceCodes","pattern","replacement","getDatafields","field","subfields","filter","code","includes","subfield","value","replace","BIB_FORMAT_SETTINGS"],"mappings":";;;;;;;;AA4BA;;AACA;;AA7BA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAKO,SAASA,YAAT,CAAsBC,MAAtB,EAA8BC,QAA9B,EAAwC;AAC7C,QAAMC,MAAM,GAAG,0CAAf;AAEAA,EAAAA,MAAM,CAACC,OAAP,CAAe,oBAAf;;AACA,QAAMC,SAAS,GAAGC,uBAAWC,KAAX,CAAiBN,MAAjB,EAAyB;AAACO,IAAAA,cAAc,EAAE;AAAjB,GAAzB,CAAlB;;AAEAN,EAAAA,QAAQ,CAACO,OAAT,CAAiBC,OAAO,IAAI;AAC1BC,IAAAA,eAAe,CAACD,OAAD,CAAf;AACD,GAFD;AAIA,SAAOL,SAAP,CAV6C,CAY7C;;AACA,WAASM,eAAT,CAAyBD,OAAzB,EAAkC;AAChC,UAAM;AAACE,MAAAA,SAAD;AAAYC,MAAAA,SAAZ;AAAuBC,MAAAA;AAAvB,QAA6CJ,OAAnD;AACA,UAAMK,OAAO,GAAI,IAAGH,SAAU,GAA9B;AACA,UAAMI,WAAW,GAAI,IAAGH,SAAU,GAAlC;AACAR,IAAAA,SAAS,CAACY,aAAV,GACGR,OADH,CACWS,KAAK,IAAI;AAChBA,MAAAA,KAAK,CAACC,SAAN,CACGC,MADH,CACU,CAAC;AAACC,QAAAA;AAAD,OAAD,KAAYP,kBAAkB,CAACQ,QAAnB,CAA4BD,IAA5B,CADtB,EAEGZ,OAFH,CAEWc,QAAQ,IAAI;AACnBA,QAAAA,QAAQ,CAACC,KAAT,GAAiBD,QAAQ,CAACC,KAAT,CAAeC,OAAf,CAAuBV,OAAvB,EAAgCC,WAAhC,CAAjB,CADmB,CAC4C;AAChE,OAJH;AAKD,KAPH;AAQD;AACF,C,CAED;;;AACO,MAAMU,mBAAmB,GAAG,CACjC;AACEd,EAAAA,SAAS,EAAE,YADb;AAEEC,EAAAA,SAAS,EAAE,OAFb;AAGEC,EAAAA,kBAAkB,EAAE,CAAC,GAAD;AAHtB,CADiC,EAMjC;AACEF,EAAAA,SAAS,EAAE,aADb;AAEEC,EAAAA,SAAS,EAAE,OAFb;AAGEC,EAAAA,kBAAkB,EAAE,CAAC,GAAD;AAHtB,CANiC,EAWjC;AACEF,EAAAA,SAAS,EAAE,aADb;AAEEC,EAAAA,SAAS,EAAE,OAFb;AAGEC,EAAAA,kBAAkB,EAAE,CAAC,GAAD;AAHtB,CAXiC,CAA5B","sourcesContent":["/**\n*\n* @licstart The following is the entire license notice for the JavaScript code in this file.\n*\n* Shared modules for microservices of Melinda rest api batch import system\n*\n* Copyright (C) 2020 University Of Helsinki (The National Library Of Finland)\n*\n* This file is part of melinda-rest-api-commons\n*\n* melinda-rest-api-commons program is free software: you can redistribute it and/or modify\n* it under the terms of the GNU Affero General Public License as\n* published by the Free Software Foundation, either version 3 of the\n* License, or (at your option) any later version.\n*\n* melinda-rest-api-commons is distributed in the hope that it will be useful,\n* but WITHOUT ANY WARRANTY; without even the implied warranty of\n* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n* GNU Affero General Public License for more details.\n*\n* You should have received a copy of the GNU Affero General Public License\n* along with this program. If not, see <http://www.gnu.org/licenses/>.\n*\n* @licend The above is the entire license notice\n* for the JavaScript code in this file.\n*\n*/\n\nimport {MarcRecord} from '@natlibfi/marc-record';\nimport {createLogger} from '@natlibfi/melinda-backend-commons';\n\nexport function formatRecord(record, settings) {\n const logger = createLogger();\n\n logger.verbose('Applying formating');\n const newRecord = MarcRecord.clone(record, {subfieldValues: false});\n\n settings.forEach(options => {\n replacePrefixes(options);\n });\n\n return newRecord;\n\n // Replace prefix in all specified subfields\n function replacePrefixes(options) {\n const {oldPrefix, newPrefix, prefixReplaceCodes} = options;\n const pattern = `(${oldPrefix})`;\n const replacement = `(${newPrefix})`;\n newRecord.getDatafields()\n .forEach(field => {\n field.subfields\n .filter(({code}) => prefixReplaceCodes.includes(code))\n .forEach(subfield => {\n subfield.value = subfield.value.replace(pattern, replacement); // eslint-disable-line functional/immutable-data\n });\n });\n }\n}\n\n// If placed in config.js testing needs envs\nexport const BIB_FORMAT_SETTINGS = [\n {\n oldPrefix: 'FI-MELINDA',\n newPrefix: 'FIN01',\n prefixReplaceCodes: ['w']\n },\n {\n oldPrefix: 'FI-ASTERI-S',\n newPrefix: 'FIN10',\n prefixReplaceCodes: ['0']\n },\n {\n oldPrefix: 'FI-ASTERI-N',\n newPrefix: 'FIN11',\n prefixReplaceCodes: ['0']\n }\n];\n"],"file":"format.js"}
1
+ {"version":3,"sources":["../../src/helpers/format.js"],"names":["formatRecord","record","settings","logger","verbose","newRecord","MarcRecord","clone","subfieldValues","generateMissingSIDs","forEach","options","replacePrefixes","toObject","oldPrefix","newPrefix","prefixReplaceCodes","pattern","replacement","getDatafields","field","subfields","filter","code","includes","subfield","value","replace","f035Filters","fSIDFilters","f035s","f035ToSidInfo","get","fSIDs","sidsToSidInfo","sidsToBeAdded","genNewSids","length","f035SidInfo","some","fSIDInfo","SID","sidField","insertField","map","sidInfo","tag","ind1","ind2","SIDs","flatMap","sub","undefined","wantedSub","regexp","test","findIndex","slice","indexOf","BIB_FORMAT_SETTINGS"],"mappings":";;;;;;;;AA4BA;;AACA;;AA7BA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAKO,SAASA,YAAT,CAAsBC,MAAtB,EAA8BC,QAAQ,GAAG,EAAzC,EAA6C;AAClD,QAAMC,MAAM,GAAG,0CAAf;AAEAA,EAAAA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;;;AACO,MAAMO,mBAAmB,GAAG,CACjC;AACE7C,EAAAA,SAAS,EAAE,YADb;AAEEC,EAAAA,SAAS,EAAE,OAFb;AAGEC,EAAAA,kBAAkB,EAAE,CAAC,GAAD;AAHtB,CADiC,EAMjC;AACEF,EAAAA,SAAS,EAAE,aADb;AAEEC,EAAAA,SAAS,EAAE,OAFb;AAGEC,EAAAA,kBAAkB,EAAE,CAAC,GAAD;AAHtB,CANiC,EAWjC;AACEF,EAAAA,SAAS,EAAE,aADb;AAEEC,EAAAA,SAAS,EAAE,OAFb;AAGEC,EAAAA,kBAAkB,EAAE,CAAC,GAAD;AAHtB,CAXiC,CAA5B","sourcesContent":["/**\n*\n* @licstart The following is the entire license notice for the JavaScript code in this file.\n*\n* Shared modules for microservices of Melinda rest api batch import system\n*\n* Copyright (C) 2020 University Of Helsinki (The National Library Of Finland)\n*\n* This file is part of melinda-rest-api-commons\n*\n* melinda-rest-api-commons program is free software: you can redistribute it and/or modify\n* it under the terms of the GNU Affero General Public License as\n* published by the Free Software Foundation, either version 3 of the\n* License, or (at your option) any later version.\n*\n* melinda-rest-api-commons is distributed in the hope that it will be useful,\n* but WITHOUT ANY WARRANTY; without even the implied warranty of\n* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n* GNU Affero General Public License for more details.\n*\n* You should have received a copy of the GNU Affero General Public License\n* along with this program. If not, see <http://www.gnu.org/licenses/>.\n*\n* @licend The above is the entire license notice\n* for the JavaScript code in this file.\n*\n*/\n\nimport {MarcRecord} from '@natlibfi/marc-record';\nimport {createLogger} from '@natlibfi/melinda-backend-commons';\n\nexport function formatRecord(record, settings = []) {\n const logger = createLogger();\n\n logger.verbose('Applying formating');\n const newRecord = MarcRecord.clone(record, {subfieldValues: false});\n\n generateMissingSIDs();\n\n settings.forEach(options => {\n replacePrefixes(options);\n });\n\n return newRecord.toObject();\n\n // Replace prefix in all specified subfields\n function replacePrefixes(options) {\n const {oldPrefix, newPrefix, prefixReplaceCodes} = options;\n const pattern = `(${oldPrefix})`;\n const replacement = `(${newPrefix})`;\n newRecord.getDatafields()\n .forEach(field => {\n field.subfields\n .filter(({code}) => prefixReplaceCodes.includes(code))\n .forEach(subfield => {\n subfield.value = subfield.value.replace(pattern, replacement); // eslint-disable-line functional/immutable-data\n });\n });\n }\n\n\n function generateMissingSIDs() {\n const f035Filters = [/^\\(FI-BTJ\\)/u, /^\\(FI-TATI\\)/u];\n const fSIDFilters = ['FI-BTJ', 'tati'];\n const f035s = f035ToSidInfo(newRecord.get(/^035$/u));\n const fSIDs = sidsToSidInfo(newRecord.get(/^SID$/u));\n\n const sidsToBeAdded = genNewSids(fSIDs.length === 0\n ? f035s\n // test that SIDs are not there yet\n : f035s.filter(f035SidInfo => fSIDs.some(fSIDInfo => f035SidInfo.SID !== fSIDInfo.SID && f035SidInfo.value !== fSIDInfo.value)));\n\n // Add new SIDs\n return sidsToBeAdded.forEach(sidField => newRecord.insertField(sidField));\n\n function genNewSids(sidsToBeAdded) {\n return sidsToBeAdded.map(sidInfo => ({tag: 'SID', ind1: ' ', ind2: ' ', subfields: [{code: 'c', value: sidInfo.value}, {code: 'b', value: sidInfo.SID}]}));\n }\n\n function sidsToSidInfo(SIDs) {\n return SIDs.flatMap(({subfields}) => {\n const [SID] = subfields.filter(sub => sub.code === 'b' && fSIDFilters.includes(sub.value)).map(sub => sub.value);\n const [value] = subfields.filter(sub => sub.code === 'c' && sub.value !== undefined).map(sub => sub.value);\n\n if (SID && value) {\n return {\n SID,\n value\n };\n }\n\n return undefined;\n }).filter(value => value !== undefined);\n }\n\n function f035ToSidInfo(f035s) {\n return f035s.flatMap(({subfields}) => {\n const [wantedSub] = subfields.filter(sub => sub.code === 'a' && f035Filters.some(regexp => regexp.test(sub.value)));\n\n if (wantedSub) {\n return {\n SID: fSIDFilters[f035Filters.findIndex(regexp => regexp.test(wantedSub.value))],\n value: wantedSub.value.slice(wantedSub.value.indexOf(')') + 1)\n };\n }\n\n return undefined;\n }).filter(value => value !== undefined);\n }\n }\n}\n\n// If placed in config.js testing needs envs\nexport const BIB_FORMAT_SETTINGS = [\n {\n oldPrefix: 'FI-MELINDA',\n newPrefix: 'FIN01',\n prefixReplaceCodes: ['w']\n },\n {\n oldPrefix: 'FI-ASTERI-S',\n newPrefix: 'FIN10',\n prefixReplaceCodes: ['0']\n },\n {\n oldPrefix: 'FI-ASTERI-N',\n newPrefix: 'FIN11',\n prefixReplaceCodes: ['0']\n }\n];\n"],"file":"format.js"}
@@ -2,13 +2,15 @@
2
2
 
3
3
  var _chai = require("chai");
4
4
 
5
- var _fixura = _interopRequireDefault(require("@natlibfi/fixura"));
5
+ var _fixura = _interopRequireWildcard(require("@natlibfi/fixura"));
6
6
 
7
7
  var _marcRecord = require("@natlibfi/marc-record");
8
8
 
9
9
  var _format = require("./format");
10
10
 
11
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
12
+
13
+ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
12
14
 
13
15
  /**
14
16
  *
@@ -42,48 +44,79 @@ describe('services/format', () => {
42
44
  const {
43
45
  getFixture
44
46
  } = (0, _fixura.default)({
45
- root: FIXTURES_PATH
47
+ root: FIXTURES_PATH,
48
+ reader: _fixura.READERS.JSON
46
49
  });
47
50
  describe('fiAsteriN0Fin11', () => {
48
51
  it('Should succeed', () => {
49
- const record = new _marcRecord.MarcRecord(JSON.parse(getFixture({
52
+ const record = new _marcRecord.MarcRecord(getFixture({
50
53
  components: ['in', 'fiAsteriN0Fin11.json']
51
- })));
54
+ }));
52
55
  const result = (0, _format.formatRecord)(record.toObject(), _format.BIB_FORMAT_SETTINGS);
53
56
  const expected = getFixture({
54
57
  components: ['out', 'fiAsteriN0Fin11.json']
55
58
  });
56
- const stringResult = JSON.stringify({ ...result
57
- }, undefined, 2);
58
- (0, _chai.expect)(stringResult).to.eql(expected);
59
+ (0, _chai.expect)(result).to.eql(expected);
59
60
  });
60
61
  });
61
62
  describe('fiAsteriS0Fin10', () => {
62
63
  it('Should succeed', () => {
63
- const record = new _marcRecord.MarcRecord(JSON.parse(getFixture({
64
+ const record = new _marcRecord.MarcRecord(getFixture({
64
65
  components: ['in', 'fiAsteriS0Fin10.json']
65
- })));
66
+ }));
66
67
  const result = (0, _format.formatRecord)(record.toObject(), _format.BIB_FORMAT_SETTINGS);
67
68
  const expected = getFixture({
68
69
  components: ['out', 'fiAsteriS0Fin10.json']
69
70
  });
70
- const stringResult = JSON.stringify({ ...result
71
- }, undefined, 2);
72
- (0, _chai.expect)(stringResult).to.eql(expected);
71
+ (0, _chai.expect)(result).to.eql(expected);
73
72
  });
74
73
  });
75
74
  describe('fiMelindaWFin01', () => {
76
75
  it('Should succeed', () => {
77
- const record = new _marcRecord.MarcRecord(JSON.parse(getFixture({
76
+ const record = new _marcRecord.MarcRecord(getFixture({
78
77
  components: ['in', 'fiMelindaWFin01.json']
79
- })));
78
+ }));
80
79
  const result = (0, _format.formatRecord)(record.toObject(), _format.BIB_FORMAT_SETTINGS);
81
80
  const expected = getFixture({
82
81
  components: ['out', 'fiMelindaWFin01.json']
83
82
  });
84
- const stringResult = JSON.stringify({ ...result
85
- }, undefined, 2);
86
- (0, _chai.expect)(stringResult).to.eql(expected);
83
+ (0, _chai.expect)(result).to.eql(expected);
84
+ });
85
+ });
86
+ describe('f035fibtj', () => {
87
+ it('Should add FI-BTJ SID', () => {
88
+ const record = new _marcRecord.MarcRecord(getFixture({
89
+ components: ['in', 'f035fibtj.json']
90
+ }));
91
+ const result = (0, _format.formatRecord)(record.toObject());
92
+ const expected = getFixture({
93
+ components: ['out', 'f035fibtj.json']
94
+ });
95
+ (0, _chai.expect)(result).to.eql(expected);
96
+ });
97
+ });
98
+ describe('f035tati', () => {
99
+ it('Should add tati SID', () => {
100
+ const record = new _marcRecord.MarcRecord(getFixture({
101
+ components: ['in', 'f035tati.json']
102
+ }));
103
+ const result = (0, _format.formatRecord)(record.toObject());
104
+ const expected = getFixture({
105
+ components: ['out', 'f035tati.json']
106
+ });
107
+ (0, _chai.expect)(result).to.eql(expected);
108
+ });
109
+ });
110
+ describe('f035fibtjAndTati', () => {
111
+ it('Should add tati SID and skip FI-BTJ SID', () => {
112
+ const record = new _marcRecord.MarcRecord(getFixture({
113
+ components: ['in', 'f035fibtjAndTati.json']
114
+ }));
115
+ const result = (0, _format.formatRecord)(record.toObject());
116
+ const expected = getFixture({
117
+ components: ['out', 'f035fibtjAndTati.json']
118
+ });
119
+ (0, _chai.expect)(result).to.eql(expected);
87
120
  });
88
121
  });
89
122
  });
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/helpers/format.spec.js"],"names":["describe","FIXTURES_PATH","__dirname","getFixture","root","it","record","MarcRecord","JSON","parse","components","result","toObject","BIB_FORMAT_SETTINGS","expected","stringResult","stringify","undefined","to","eql"],"mappings":";;AA4BA;;AACA;;AACA;;AACA;;;;AA/BA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAC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hC,CAAf;AACA,YAAMC,QAAQ,GAAGX,UAAU,CAAC;AAC1BO,QAAAA,UAAU,EAAE,CACV,KADU,EAEV,sBAFU;AADc,OAAD,CAA3B;AAMA,YAAMK,YAAY,GAAGP,IAAI,CAACQ,SAAL,CAAe,EAAC,GAAGL;AAAJ,OAAf,EAA4BM,SAA5B,EAAuC,CAAvC,CAArB;AACA,wBAAOF,YAAP,EAAqBG,EAArB,CAAwBC,GAAxB,CAA4BL,QAA5B;AACD,KAhBC,CAAF;AAiBD,GAlBO,CAAR;AAoBAd,EAAAA,QAAQ,CAAC,iBAAD,EAAoB,MAAM;AAChCK,IAAAA,EAAE,CAAC,gBAAD,EAAmB,MAAM;AACzB,YAAMC,MAAM,GAAG,IAAIC,sBAAJ,CAAeC,IAAI,CAACC,KAAL,CAAWN,UAAU,CAAC;AAClDO,QAAAA,UAAU,EAAE,CACV,IADU,EAEV,sBAFU;AADsC,OAAD,CAArB,CAAf,CAAf;AAMA,YAAMC,MAAM,GAAG,0BAAaL,MAAM,CAACM,QAAP,EAAb,EAAgCC,2BAAhC,CAAf;AACA,YAAMC,QAAQ,GAAGX,UAAU,CAAC;AAC1BO,QAAAA,UAAU,EAAE,CACV,KADU,EAEV,sBAFU;AADc,OAAD,CAA3B;AAMA,YAAMK,YAAY,GAAGP,IAAI,CAACQ,SAAL,CAAe,EAAC,GAAGL;AAAJ,OAAf,EAA4BM,SAA5B,EAAuC,CAAvC,CAArB;AACA,wBAAOF,YAAP,EAAqBG,EAArB,CAAwBC,GAAxB,CAA4BL,QAA5B;AACD,KAhBC,CAAF;AAiBD,GAlBO,CAAR;AAmBD,CArEO,CAAR","sourcesContent":["/**\n*\n* @licstart The following is the entire license notice for the JavaScript code in this file.\n*\n* Shared modules for microservices of Melinda rest api batch import system\n*\n* Copyright (C) 2020 University Of Helsinki (The National Library Of Finland)\n*\n* This file is part of melinda-rest-api-commons\n*\n* melinda-rest-api-commons program is free software: you can redistribute it and/or modify\n* it under the terms of the GNU Affero General Public License as\n* published by the Free Software Foundation, either version 3 of the\n* License, or (at your option) any later version.\n*\n* melinda-rest-api-commons is distributed in the hope that it will be useful,\n* but WITHOUT ANY WARRANTY; without even the implied warranty of\n* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n* GNU Affero General Public License for more details.\n*\n* You should have received a copy of the GNU Affero General Public License\n* along with this program. If not, see <http://www.gnu.org/licenses/>.\n*\n* @licend The above is the entire license notice\n* for the JavaScript code in this file.\n*\n*/\n\nimport {expect} from 'chai';\nimport fixtureFactory from '@natlibfi/fixura';\nimport {MarcRecord} from '@natlibfi/marc-record';\nimport {formatRecord, BIB_FORMAT_SETTINGS} from './format';\n\ndescribe('services/format', () => {\n const FIXTURES_PATH = [\n __dirname,\n '..',\n '..',\n 'test-fixtures',\n 'format'\n ];\n const {getFixture} = fixtureFactory({root: FIXTURES_PATH});\n\n describe('fiAsteriN0Fin11', () => {\n it('Should succeed', () => {\n const record = new MarcRecord(JSON.parse(getFixture({\n components: [\n 'in',\n 'fiAsteriN0Fin11.json'\n ]\n })));\n const result = formatRecord(record.toObject(), BIB_FORMAT_SETTINGS);\n const expected = getFixture({\n components: [\n 'out',\n 'fiAsteriN0Fin11.json'\n ]\n });\n const stringResult = JSON.stringify({...result}, undefined, 2);\n expect(stringResult).to.eql(expected);\n });\n });\n\n describe('fiAsteriS0Fin10', () => {\n it('Should succeed', () => {\n const record = new MarcRecord(JSON.parse(getFixture({\n components: [\n 'in',\n 'fiAsteriS0Fin10.json'\n ]\n })));\n const result = formatRecord(record.toObject(), BIB_FORMAT_SETTINGS);\n const expected = getFixture({\n components: [\n 'out',\n 'fiAsteriS0Fin10.json'\n ]\n });\n const stringResult = JSON.stringify({...result}, undefined, 2);\n expect(stringResult).to.eql(expected);\n });\n });\n\n describe('fiMelindaWFin01', () => {\n it('Should succeed', () => {\n const record = new MarcRecord(JSON.parse(getFixture({\n components: [\n 'in',\n 'fiMelindaWFin01.json'\n ]\n })));\n const result = formatRecord(record.toObject(), BIB_FORMAT_SETTINGS);\n const expected = getFixture({\n components: [\n 'out',\n 'fiMelindaWFin01.json'\n ]\n });\n const stringResult = JSON.stringify({...result}, undefined, 2);\n expect(stringResult).to.eql(expected);\n });\n });\n});\n"],"file":"format.spec.js"}
1
+ {"version":3,"sources":["../../src/helpers/format.spec.js"],"names":["describe","FIXTURES_PATH","__dirname","getFixture","root","reader","READERS","JSON","it","record","MarcRecord","components","result","toObject","BIB_FORMAT_SETTINGS","expected","to","eql"],"mappings":";;AA4BA;;AACA;;AACA;;AACA;;;;;;AA/BA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAOAA,QAAQ,CAAC,iBAAD,EAAoB,MAAM;AAChC,QAAMC,aAAa,GAAG,CACpBC,SADoB,EAEpB,IAFoB,EAGpB,IAHoB,EAIpB,eAJoB,EAKpB,QALoB,CAAtB;AAOA,QAAM;AAACC,IAAAA;AAAD,MAAe,qBAAe;AAACC,IAAAA,IAAI,EAAEH,aAAP;AAAsBI,IAAAA,MAAM,EAAEC,gBAAQC;AAAtC,GAAf,CAArB;AAEAP,EAAAA,QAAQ,CAAC,iBAAD,EAAoB,MAAM;AAChCQ,IAAAA,EAAE,CAAC,gBAAD,EAAmB,MAAM;AACzB,YAAMC,MAAM,GAAG,IAAIC,sBAAJ,CAAeP,UAAU,CAAC;AACvCQ,QAAAA,UAAU,EAAE,CACV,IADU,EAEV,sBAFU;AAD2B,OAAD,CAAzB,CAAf;AAMA,YAAMC,MAAM,GAAG,0BAAaH,MAAM,CAACI,QAAP,EAAb,EAAgCC,2BAAhC,CAAf;AACA,YAAMC,QAAQ,GAAGZ,UAAU,CAAC;AAC1BQ,QAAAA,UAAU,EAAE,CACV,KADU,EAEV,sBAFU;AADc,OAAD,CAA3B;AAOA,wBAAOC,MAAP,EAAeI,EAAf,CAAkBC,GAAlB,C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C1BQ,QAAAA,UAAU,EAAE,CACV,KADU,EAEV,uBAFU;AADc,OAAD,CAA3B;AAOA,wBAAOC,MAAP,EAAeI,EAAf,CAAkBC,GAAlB,CAAsBF,QAAtB;AACD,KAhBC,CAAF;AAiBD,GAlBO,CAAR;AAmBD,CAjIO,CAAR","sourcesContent":["/**\n*\n* @licstart The following is the entire license notice for the JavaScript code in this file.\n*\n* Shared modules for microservices of Melinda rest api batch import system\n*\n* Copyright (C) 2020 University Of Helsinki (The National Library Of Finland)\n*\n* This file is part of melinda-rest-api-commons\n*\n* melinda-rest-api-commons program is free software: you can redistribute it and/or modify\n* it under the terms of the GNU Affero General Public License as\n* published by the Free Software Foundation, either version 3 of the\n* License, or (at your option) any later version.\n*\n* melinda-rest-api-commons is distributed in the hope that it will be useful,\n* but WITHOUT ANY WARRANTY; without even the implied warranty of\n* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n* GNU Affero General Public License for more details.\n*\n* You should have received a copy of the GNU Affero General Public License\n* along with this program. If not, see <http://www.gnu.org/licenses/>.\n*\n* @licend The above is the entire license notice\n* for the JavaScript code in this file.\n*\n*/\n\nimport {expect} from 'chai';\nimport fixtureFactory, {READERS} from '@natlibfi/fixura';\nimport {MarcRecord} from '@natlibfi/marc-record';\nimport {formatRecord, BIB_FORMAT_SETTINGS} from './format';\n\ndescribe('services/format', () => {\n const FIXTURES_PATH = [\n __dirname,\n '..',\n '..',\n 'test-fixtures',\n 'format'\n ];\n const {getFixture} = fixtureFactory({root: FIXTURES_PATH, reader: READERS.JSON});\n\n describe('fiAsteriN0Fin11', () => {\n it('Should succeed', () => {\n const record = new MarcRecord(getFixture({\n components: [\n 'in',\n 'fiAsteriN0Fin11.json'\n ]\n }));\n const result = formatRecord(record.toObject(), BIB_FORMAT_SETTINGS);\n const expected = getFixture({\n components: [\n 'out',\n 'fiAsteriN0Fin11.json'\n ]\n });\n\n expect(result).to.eql(expected);\n });\n });\n\n describe('fiAsteriS0Fin10', () => {\n it('Should succeed', () => {\n const record = new MarcRecord(getFixture({\n components: [\n 'in',\n 'fiAsteriS0Fin10.json'\n ]\n }));\n const result = formatRecord(record.toObject(), BIB_FORMAT_SETTINGS);\n const expected = getFixture({\n components: [\n 'out',\n 'fiAsteriS0Fin10.json'\n ]\n });\n\n expect(result).to.eql(expected);\n });\n });\n\n describe('fiMelindaWFin01', () => {\n it('Should succeed', () => {\n const record = new MarcRecord(getFixture({\n components: [\n 'in',\n 'fiMelindaWFin01.json'\n ]\n }));\n const result = formatRecord(record.toObject(), BIB_FORMAT_SETTINGS);\n const expected = getFixture({\n components: [\n 'out',\n 'fiMelindaWFin01.json'\n ]\n });\n\n expect(result).to.eql(expected);\n });\n });\n\n describe('f035fibtj', () => {\n it('Should add FI-BTJ SID', () => {\n const record = new MarcRecord(getFixture({\n components: [\n 'in',\n 'f035fibtj.json'\n ]\n }));\n const result = formatRecord(record.toObject());\n const expected = getFixture({\n components: [\n 'out',\n 'f035fibtj.json'\n ]\n });\n\n expect(result).to.eql(expected);\n });\n });\n\n describe('f035tati', () => {\n it('Should add tati SID', () => {\n const record = new MarcRecord(getFixture({\n components: [\n 'in',\n 'f035tati.json'\n ]\n }));\n const result = formatRecord(record.toObject());\n const expected = getFixture({\n components: [\n 'out',\n 'f035tati.json'\n ]\n });\n\n expect(result).to.eql(expected);\n });\n });\n\n describe('f035fibtjAndTati', () => {\n it('Should add tati SID and skip FI-BTJ SID', () => {\n const record = new MarcRecord(getFixture({\n components: [\n 'in',\n 'f035fibtjAndTati.json'\n ]\n }));\n const result = formatRecord(record.toObject());\n const expected = getFixture({\n components: [\n 'out',\n 'f035fibtjAndTati.json'\n ]\n });\n\n expect(result).to.eql(expected);\n });\n });\n});\n"],"file":"format.spec.js"}
@@ -2,7 +2,7 @@
2
2
 
3
3
  var _chai = require("chai");
4
4
 
5
- var _fixura = _interopRequireDefault(require("@natlibfi/fixura"));
5
+ var _fixura = _interopRequireWildcard(require("@natlibfi/fixura"));
6
6
 
7
7
  var _marcRecord = require("@natlibfi/marc-record");
8
8
 
@@ -10,6 +10,10 @@ var _validation = _interopRequireDefault(require("./validation"));
10
10
 
11
11
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
12
12
 
13
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
14
+
15
+ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
16
+
13
17
  /**
14
18
  *
15
19
  * @licstart The following is the entire license notice for the JavaScript code in this file.
@@ -42,37 +46,34 @@ describe('services/validate', () => {
42
46
  const {
43
47
  getFixture
44
48
  } = (0, _fixura.default)({
45
- root: FIXTURES_PATH
49
+ root: FIXTURES_PATH,
50
+ reader: _fixura.READERS.JSON
46
51
  });
47
52
  describe('f003-fi-melinda', () => {
48
53
  it('Should have failed: false', async () => {
49
54
  const validator = await (0, _validation.default)();
50
- const record = new _marcRecord.MarcRecord(JSON.parse(getFixture({
55
+ const record = new _marcRecord.MarcRecord(getFixture({
51
56
  components: ['in', 'f003-fi-melinda.json']
52
- })));
57
+ }));
53
58
  const result = await validator(record.toObject());
54
59
  const expected = getFixture({
55
60
  components: ['out', 'f003-fi-melinda.json']
56
61
  });
57
- const stringResult = JSON.stringify({ ...result
58
- }, undefined, 2);
59
- (0, _chai.expect)(stringResult).to.eql(expected);
62
+ (0, _chai.expect)(result).to.eql(expected);
60
63
  (0, _chai.expect)(result.failed).to.equal(false);
61
64
  });
62
65
  });
63
66
  describe('f003-not-fi-melinda', () => {
64
67
  it('Should have failed: true', async () => {
65
68
  const validator = await (0, _validation.default)();
66
- const record = new _marcRecord.MarcRecord(JSON.parse(getFixture({
69
+ const record = new _marcRecord.MarcRecord(getFixture({
67
70
  components: ['in', 'f003-not-fi-melinda.json']
68
- })));
71
+ }));
69
72
  const result = await validator(record.toObject());
70
73
  const expected = getFixture({
71
74
  components: ['out', 'f003-not-fi-melinda.json']
72
75
  });
73
- const stringResult = JSON.stringify({ ...result
74
- }, undefined, 2);
75
- (0, _chai.expect)(stringResult).to.eql(expected);
76
+ (0, _chai.expect)(result).to.eql(expected);
76
77
  (0, _chai.expect)(result.failed).to.equal(true);
77
78
  });
78
79
  });
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/helpers/validation.spec.js"],"names":["describe","FIXTURES_PATH","__dirname","getFixture","root","it","validator","record","MarcRecord","JSON","parse","components","result","toObject","expected","stringResult","stringify","undefined","to","eql","failed","equal"],"mappings":";;AA4BA;;AACA;;AACA;;AACA;;;;AA/BA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAOAA,QAAQ,CAAC,mBAAD,EAAsB,MAAM;AAClC,QAAMC,aAAa,GAAG,CACpBC,SADoB,EAEpB,IAFoB,EAGpB,IAHoB,EAIpB,eAJoB,EAKpB,YALoB,CAAtB;AAOA,QAAM;AAACC,IAAAA;AAAD,MAAe,qBAAe;AAACC,IAAAA,IAAI,EAAEH;AAAP,GAAf,CAArB;AAEAD,EAAAA,QAAQ,CAAC,iBAAD,EAAoB,M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I,YAAY,GAAGN,IAAI,CAACO,SAAL,CAAe,EAAC,GAAGJ;AAAJ,OAAf,EAA4BK,SAA5B,EAAuC,CAAvC,CAArB;AACA,wBAAOF,YAAP,EAAqBG,EAArB,CAAwBC,GAAxB,CAA4BL,QAA5B;AACA,wBAAOF,MAAM,CAACQ,MAAd,EAAsBF,EAAtB,CAAyBG,KAAzB,CAA+B,IAA/B;AACD,KAlBC,CAAF;AAmBD,GApBO,CAAR;AAqBD,CArDO,CAAR","sourcesContent":["/**\n*\n* @licstart The following is the entire license notice for the JavaScript code in this file.\n*\n* Shared modules for microservices of Melinda rest api batch import system\n*\n* Copyright (C) 2020 University Of Helsinki (The National Library Of Finland)\n*\n* This file is part of melinda-rest-api-commons\n*\n* melinda-rest-api-commons program is free software: you can redistribute it and/or modify\n* it under the terms of the GNU Affero General Public License as\n* published by the Free Software Foundation, either version 3 of the\n* License, or (at your option) any later version.\n*\n* melinda-rest-api-commons is distributed in the hope that it will be useful,\n* but WITHOUT ANY WARRANTY; without even the implied warranty of\n* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n* GNU Affero General Public License for more details.\n*\n* You should have received a copy of the GNU Affero General Public License\n* along with this program. If not, see <http://www.gnu.org/licenses/>.\n*\n* @licend The above is the entire license notice\n* for the JavaScript code in this file.\n*\n*/\n\nimport {expect} from 'chai';\nimport fixtureFactory from '@natlibfi/fixura';\nimport {MarcRecord} from '@natlibfi/marc-record';\nimport createValidator from './validation';\n\ndescribe('services/validate', () => {\n const FIXTURES_PATH = [\n __dirname,\n '..',\n '..',\n 'test-fixtures',\n 'validation'\n ];\n const {getFixture} = fixtureFactory({root: FIXTURES_PATH});\n\n describe('f003-fi-melinda', () => {\n it('Should have failed: false', async () => {\n const validator = await createValidator();\n const record = new MarcRecord(JSON.parse(getFixture({\n components: [\n 'in',\n 'f003-fi-melinda.json'\n ]\n })));\n const result = await validator(record.toObject());\n const expected = getFixture({\n components: [\n 'out',\n 'f003-fi-melinda.json'\n ]\n });\n const stringResult = JSON.stringify({...result}, undefined, 2);\n expect(stringResult).to.eql(expected);\n expect(result.failed).to.equal(false);\n });\n });\n\n describe('f003-not-fi-melinda', () => {\n it('Should have failed: true', async () => {\n const validator = await createValidator();\n const record = new MarcRecord(JSON.parse(getFixture({\n components: [\n 'in',\n 'f003-not-fi-melinda.json'\n ]\n })));\n const result = await validator(record.toObject());\n const expected = getFixture({\n components: [\n 'out',\n 'f003-not-fi-melinda.json'\n ]\n });\n const stringResult = JSON.stringify({...result}, undefined, 2);\n expect(stringResult).to.eql(expected);\n expect(result.failed).to.equal(true);\n });\n });\n});\n\n"],"file":"validation.spec.js"}
1
+ {"version":3,"sources":["../../src/helpers/validation.spec.js"],"names":["describe","FIXTURES_PATH","__dirname","getFixture","root","reader","READERS","JSON","it","validator","record","MarcRecord","components","result","toObject","expected","to","eql","failed","equal"],"mappings":";;AA4BA;;AACA;;AACA;;AACA;;;;;;;;AA/BA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAOAA,QAAQ,CAAC,mBAAD,EAAsB,MAAM;AAClC,QAAMC,aAAa,GAAG,CACpBC,SADoB,EAEpB,IAFoB,EAGpB,IAHoB,EAIpB,eAJoB,EAKpB,YALoB,CAAtB;AAOA,QAAM;AAACC,IAAAA;AAAD,MAAe,qBAAe;AAACC,IAAAA,IAAI,EAAEH,aAAP;AAAsBI,IAAAA,MAAM,EAAEC,gBAAQC;AAAtC,GAAf,CAArB;AAEAP,EAAAA,QAAQ,CAAC,iBAAD,EAAoB,MAAM;AAChCQ,IAAAA,EAAE,CAAC,2BAAD,EAA8B,YAAY;AAC1C,YAAMC,SAAS,GAAG,MAAM,0BAAxB;AACA,YAAMC,MAAM,GAAG,IAAIC,sBAAJ,CAAeR,UAAU,CAAC;AACvCS,QAAAA,UAAU,EAAE,CACV,IADU,EAEV,sBAFU;AAD2B,OAAD,CAAzB,CAAf;AAMA,YAAMC,MAAM,GAAG,MAAMJ,SAAS,CAACC,MAAM,CAACI,QAAP,EAAD,CAA9B;AACA,YAAMC,QAAQ,GAAGZ,UAAU,CAAC;AAC1BS,QAAAA,UAAU,EAAE,CACV,KADU,EAEV,sBAFU;AADc,OAAD,CAA3B;AAMA,wBAAOC,MAAP,EAAeG,EAAf,CAAkBC,GAAlB,CAAsBF,QAAtB;AACA,wBAAOF,MAAM,CAACK,MAAd,EAAsBF,EAAtB,CAAyBG,KAAzB,CAA+B,KAA/B;AACD,KAjBC,CAAF;AAkBD,GAnBO,CAAR;AAqBAnB,EAAAA,QAAQ,CAAC,qBAAD,EAAwB,MAAM;AACpCQ,IAAAA,EAAE,CAAC,0BAAD,EAA6B,YAAY;AACzC,YAAMC,SAAS,GAAG,MAAM,0BAAxB;AACA,YAAMC,MAAM,GAAG,IAAIC,sBAAJ,CAAeR,UAAU,CAAC;AACvCS,QAAAA,UAAU,EAAE,CACV,IADU,EAEV,0BAFU;AAD2B,OAAD,CAAzB,CAAf;AAMA,YAAMC,MAAM,GAAG,MAAMJ,SAAS,CAACC,MAAM,CAACI,QAAP,EAAD,CAA9B;AACA,YAAMC,QAAQ,GAAGZ,UAAU,CAAC;AAC1BS,QAAAA,UAAU,EAAE,CACV,KADU,EAEV,0BAFU;AADc,OAAD,CAA3B;AAMA,wBAAOC,MAAP,EAAeG,EAAf,CAAkBC,GAAlB,CAAsBF,QAAtB;AACA,wBAAOF,MAAM,CAACK,MAAd,EAAsBF,EAAtB,CAAyBG,KAAzB,CAA+B,IAA/B;AACD,KAjBC,CAAF;AAkBD,GAnBO,CAAR;AAoBD,CAnDO,CAAR","sourcesContent":["/**\n*\n* @licstart The following is the entire license notice for the JavaScript code in this file.\n*\n* Shared modules for microservices of Melinda rest api batch import system\n*\n* Copyright (C) 2020 University Of Helsinki (The National Library Of Finland)\n*\n* This file is part of melinda-rest-api-commons\n*\n* melinda-rest-api-commons program is free software: you can redistribute it and/or modify\n* it under the terms of the GNU Affero General Public License as\n* published by the Free Software Foundation, either version 3 of the\n* License, or (at your option) any later version.\n*\n* melinda-rest-api-commons is distributed in the hope that it will be useful,\n* but WITHOUT ANY WARRANTY; without even the implied warranty of\n* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n* GNU Affero General Public License for more details.\n*\n* You should have received a copy of the GNU Affero General Public License\n* along with this program. If not, see <http://www.gnu.org/licenses/>.\n*\n* @licend The above is the entire license notice\n* for the JavaScript code in this file.\n*\n*/\n\nimport {expect} from 'chai';\nimport fixtureFactory, {READERS} from '@natlibfi/fixura';\nimport {MarcRecord} from '@natlibfi/marc-record';\nimport createValidator from './validation';\n\ndescribe('services/validate', () => {\n const FIXTURES_PATH = [\n __dirname,\n '..',\n '..',\n 'test-fixtures',\n 'validation'\n ];\n const {getFixture} = fixtureFactory({root: FIXTURES_PATH, reader: READERS.JSON});\n\n describe('f003-fi-melinda', () => {\n it('Should have failed: false', async () => {\n const validator = await createValidator();\n const record = new MarcRecord(getFixture({\n components: [\n 'in',\n 'f003-fi-melinda.json'\n ]\n }));\n const result = await validator(record.toObject());\n const expected = getFixture({\n components: [\n 'out',\n 'f003-fi-melinda.json'\n ]\n });\n expect(result).to.eql(expected);\n expect(result.failed).to.equal(false);\n });\n });\n\n describe('f003-not-fi-melinda', () => {\n it('Should have failed: true', async () => {\n const validator = await createValidator();\n const record = new MarcRecord(getFixture({\n components: [\n 'in',\n 'f003-not-fi-melinda.json'\n ]\n }));\n const result = await validator(record.toObject());\n const expected = getFixture({\n components: [\n 'out',\n 'f003-not-fi-melinda.json'\n ]\n });\n expect(result).to.eql(expected);\n expect(result.failed).to.equal(true);\n });\n });\n});\n\n"],"file":"validation.spec.js"}
package/dist/mongo.js CHANGED
@@ -192,7 +192,7 @@ async function _default(MONGO_URI, collection) {
192
192
  errorMessage = '',
193
193
  errorStatus = ''
194
194
  }) {
195
- // checkTimeOut returns true, if queueItem is fresher than 1 minute
195
+ // checkTimeOut returns true, if queueItem is fresher than 1 minute and it's state is not ABORT/ERROR
196
196
  // otherwise it sets queueItem to state ABORT (408, 'Timeout')
197
197
  const timeOut = await checkTimeOut(correlationId);
198
198
 
@@ -237,17 +237,26 @@ async function _default(MONGO_URI, collection) {
237
237
  } // Check that if the item has waited too long
238
238
  // If the last modification time for the queueItem is older than 1 minute
239
239
  // set state to ABORT and return false, otherwise return true
240
+ // If the state is already ABORT or ERROR return false
240
241
 
241
242
 
242
243
  async function checkTimeOut(correlationId) {
243
- const result = await db.collection(collection).findOne({
244
+ const {
245
+ modificationTime,
246
+ queueitemState: oldState
247
+ } = await db.collection(collection).findOne({
244
248
  correlationId
245
249
  });
246
- const timeoutTime = (0, _moment.default)(result.modificationTime).add(1, 'm');
250
+
251
+ if ([_constants.QUEUE_ITEM_STATE.ABORT, _constants.QUEUE_ITEM_STATE.ERROR].includes(oldState)) {
252
+ logger.silly(`${correlationId} has already state: ${oldState}`);
253
+ return false;
254
+ }
255
+
256
+ const timeoutTime = (0, _moment.default)(modificationTime).add(1, 'm');
247
257
  logger.silly(`timeOut @ ${timeoutTime}`);
248
258
 
249
259
  if (timeoutTime.isBefore()) {
250
- const oldState = result.queueItemState;
251
260
  await setState({
252
261
  correlationId,
253
262
  state: _constants.QUEUE_ITEM_STATE.ABORT,
package/dist/mongo.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/mongo.js"],"names":["MONGO_URI","collection","logger","client","MongoClient","connect","useNewUrlParser","useUnifiedTopology","db","gridFSBucket","GridFSBucket","bucketName","createPrio","createBulk","checkAndSetState","query","queryById","remove","readContent","removeContent","getOne","getStream","setState","pushIds","pushMessages","correlationId","cataloger","oCatalogerIn","operation","noop","undefined","unique","prio","time","toDate","newQueueItem","operationSettings","queueItemState","QUEUE_ITEM_STATE","VALIDATOR","PENDING_VALIDATION","creationTime","modificationTime","handledIds","rejectedIds","result","insertOne","acknowledged","info","ApiError","httpStatus","INTERNAL_SERVER_ERROR","error","contentType","recordLoadParams","stream","UPLOADING","Promise","resolve","reject","outputStream","openUploadStream","on","chunk","write","end","state","errorMessage","errorStatus","timeOut","checkTimeOut","params","find","projection","_id","toArray","debug","length","checkModTime","findOne","timeoutTime","add","silly","isBefore","oldState","ABORT","REQUEST_TIMEOUT","JSON","stringify","clean","noContent","deleteOne","err","MongoDriverError","message","indexOf","readStream","openDownloadStreamByName","NOT_FOUND","delete","clean2","verbose","updateOne","$set","$push","$each","messages","messageField","cleanMessageField","errorString","findOneAndUpdate","returnNewDocument"],"mappings":";;;;;;;AA4BA;;AACA;;AACA;;AACA;;AACA;;AACA;;AACA;;AACA;;;;AAnCA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAWA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAEe,wBAAgBA,SAAhB,EAA2BC,UAA3B,EAAuC;AACpD,QAAMC,MAAM,GAAG,0CAAf,CADoD,CAGpD;;AACA,QAAMC,MAAM,GAAG,MAAMC,qBAAYC,OAAZ,CAAoBL,SAApB,EAA+B;AAACM,IAAAA,eAAe,EAAE,IAAlB;AAAwBC,IAAAA,kBAAkB,EAAE;AAA5C,GAA/B,CAArB;AACA,QAAMC,EAAE,GAAGL,MAAM,CAACK,EAAP,CAAU,UAAV,CAAX;AACA,QAAMC,YAAY,GAAG,IAAIC,qBAAJ,CAAiBF,EAAjB,EAAqB;AAACG,IAAAA,UAAU,EAAEV;AAAb,GAArB,CAArB;AAEA,SAAO;AAACW,IAAAA,UAAD;AAAaC,IAAAA,UAAb;AAAyBC,IAAAA,gBAAzB;AAA2CC,IAAAA,KAA3C;AAAkDC,IAAAA,SAAlD;AAA6DC,IAAAA,MAA7D;AAAqEC,IAAAA,WAArE;AAAkFC,IAAAA,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,CAAkB;AAACG,IAAAA,aAAD;AAAgByC,IAAAA,KAAhB;AAAuBC,IAAAA,YAAY,GAAG,EAAtC;AAA0CC,IAAAA,WAAW,GAAG;AAAxD,GAAlB,EAA+E;AAC7E,UAAMyC,WAAW,GAAG1C,YAAY,IAAIC,WAAhB,GAA+B,qBAAoBD,YAAa,qBAAoBC,WAAY,GAAhG,GAAqG,EAAzH;AACAlE,IAAAA,MAAM,CAAC8C,IAAP,CAAa,4BAA2BkB,KAAM,QAAOzC,aAAc,GAAEoF,WAAY,OAAM5G,UAAW,EAAlG;AACA,UAAMwF,KAAK,GAAG,4BAAShE,aAAT,CAAd;AACA,WAAOjB,EAAE,CAACP,UAAH,CAAcA,UAAd,EAA0B6G,gBAA1B,CAA2C;AAChDrF,MAAAA,aAAa,EAAEgE;AADiC,KAA3C,EAEJ;AACDc,MAAAA,IAAI,EAAE;AACJlE,QAAAA,cAAc,EAAE6B,KADZ;AAEJxB,QAAAA,gBAAgB,EAAE,uBAASR,MAAT,EAFd;AAGJiC,QAAAA,YAHI;AAIJC,QAAAA;AAJI;AADL,KAFI,EASJ;AAACK,MAAAA,UAAU,EAAE;AAACC,QAAAA,GAAG,EAAE;AAAN,OAAb;AAAuBqC,MAAAA,iBAAiB,EAAE;AAA1C,KATI,CAAP;AAUD;AACF","sourcesContent":["/**\n*\n* @licstart The following is the entire license notice for the JavaScript code in this file.\n*\n* Shared modules for microservices of Melinda rest api batch import system\n*\n* Copyright (C) 2020-2021 University Of Helsinki (The National Library Of Finland)\n*\n* This file is part of melinda-rest-api-commons\n*\n* melinda-rest-api-commons program is free software: you can redistribute it and/or modify\n* it under the terms of the GNU Affero General Public License as\n* published by the Free Software Foundation, either version 3 of the\n* License, or (at your option) any later version.\n*\n* melinda-rest-api-commons is distributed in the hope that it will be useful,\n* but WITHOUT ANY WARRANTY; without even the implied warranty of\n* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n* GNU Affero General Public License for more details.\n*\n* You should have received a copy of the GNU Affero General Public License\n* along with this program. If not, see <http://www.gnu.org/licenses/>.\n*\n* @licend The above is the entire license notice\n* for the JavaScript code in this file.\n*\n*/\n\nimport {MongoClient, GridFSBucket, MongoDriverError} from 'mongodb';\nimport {createLogger} from '@natlibfi/melinda-backend-commons';\nimport {Error as ApiError} from '@natlibfi/melinda-commons';\nimport {QUEUE_ITEM_STATE} from './constants';\nimport {logError} from './utils.js';\nimport moment from 'moment';\nimport httpStatus from 'http-status';\nimport sanitize from 'mongo-sanitize';\n\n/* QueueItem:\n{\n \"correlationId\":\"FOO\",\n \"cataloger\":\"xxx0000\",\n \"oCatalogerIn\":\"xxx0000\"\n \"operation\":\"UPDATE\",\n \"contentType\":\"application/json\",\n \"recordLoadParams\": {\n \"pActiveLibrary\": \"XXX00\",\n \"pInputFile\": \"filename.seq\",\n \"pRejectFile\": \"filename.rej\",\n \"pLogFile\": \"filename.syslog\",\n \"pOldNew\": \"NEW\"\n },\n \"queueItemState\":\"PENDING_QUEUING\",\n \"creationTime\":\"2020-01-01T00:00:00.000Z\",\n \"modificationTime\":\"2020-01-01T00:00:01.000Z\",\n \"handledIds\": []\n}\n*/\n\nexport default async function (MONGO_URI, collection) {\n const logger = createLogger();\n\n // Connect to mongo (MONGO)\n const client = await MongoClient.connect(MONGO_URI, {useNewUrlParser: true, useUnifiedTopology: true});\n const db = client.db('rest-api');\n const gridFSBucket = new GridFSBucket(db, {bucketName: collection});\n\n return {createPrio, createBulk, checkAndSetState, query, queryById, remove, readContent, removeContent, getOne, getStream, setState, pushIds, pushMessages};\n\n async function createPrio({correlationId, cataloger, oCatalogerIn, operation, noop = undefined, unique = undefined, prio = true}) {\n const time = moment().toDate();\n const newQueueItem = {\n correlationId,\n cataloger,\n operation,\n oCatalogerIn,\n operationSettings: {unique, noop, prio},\n queueItemState: QUEUE_ITEM_STATE.VALIDATOR.PENDING_VALIDATION,\n creationTime: time,\n modificationTime: time,\n handledIds: [],\n rejectedIds: []\n };\n try {\n const result = await db.collection(collection).insertOne(newQueueItem);\n if (result.acknowledged) {\n logger.info(`New PRIO queue item for ${operation} ${correlationId} has been made in ${collection}`);\n return;\n }\n throw new ApiError(httpStatus.INTERNAL_SERVER_ERROR);\n } catch (error) {\n logError(error);\n throw new ApiError(httpStatus.INTERNAL_SERVER_ERROR);\n }\n }\n\n function createBulk({correlationId, cataloger, oCatalogerIn, operation, contentType, recordLoadParams, stream, prio = false}) {\n const time = moment().toDate();\n const newQueueItem = {\n correlationId,\n cataloger,\n oCatalogerIn,\n operation,\n operationSettings: {prio},\n contentType,\n recordLoadParams,\n queueItemState: QUEUE_ITEM_STATE.VALIDATOR.UPLOADING,\n creationTime: time,\n modificationTime: time,\n handledIds: [],\n rejectedIds: []\n };\n try {\n // No await here, promises later\n db.collection(collection).insertOne(newQueueItem);\n logger.info(`New BULK queue item for ${operation} ${correlationId} has been made in ${collection}!`);\n } catch (error) {\n logError(error);\n throw new ApiError(httpStatus.INTERNAL_SERVER_ERROR);\n }\n\n return new Promise((resolve, reject) => {\n const outputStream = gridFSBucket.openUploadStream(correlationId);\n\n stream\n .on('error', reject)\n .on('data', chunk => outputStream.write(chunk))\n .on('end', () => outputStream.end(undefined, undefined, () => {\n resolve(correlationId);\n }));\n });\n }\n\n // Check state that the queueItem has not waited too long and set state\n async function checkAndSetState({correlationId, state, errorMessage = '', errorStatus = ''}) {\n // checkTimeOut returns true, if queueItem is fresher than 1 minute\n // otherwise it sets queueItem to state ABORT (408, 'Timeout')\n const timeOut = await checkTimeOut(correlationId);\n if (timeOut) {\n return setState({correlationId, state, errorMessage, errorStatus});\n }\n return false;\n }\n\n async function query(params) {\n const result = await db.collection(collection).find(params, {projection: {_id: 0}})\n .toArray();\n logger.debug(`Query result: ${result.length > 0 ? 'Found!' : 'Not found!'}`);\n return result;\n }\n\n async function queryById(correlationId, checkModTime) {\n const result = await db.collection(collection).findOne({correlationId});\n if (checkModTime) {\n const timeOut = await checkTimeOut(correlationId);\n if (timeOut) {\n return queryById(correlationId);\n }\n return result;\n }\n\n return result;\n }\n\n // Check that if the item has waited too long\n // If the last modification time for the queueItem is older than 1 minute\n // set state to ABORT and return false, otherwise return true\n async function checkTimeOut(correlationId) {\n const result = await db.collection(collection).findOne({correlationId});\n const timeoutTime = moment(result.modificationTime).add(1, 'm');\n logger.silly(`timeOut @ ${timeoutTime}`);\n if (timeoutTime.isBefore()) {\n const oldState = result.queueItemState;\n await setState({correlationId, state: QUEUE_ITEM_STATE.ABORT, errorStatus: httpStatus.REQUEST_TIMEOUT, errorMessage: `Timeout in ${oldState}`});\n return false;\n }\n\n return true;\n }\n\n async function remove(params) {\n logger.silly(`${JSON.stringify(params)}`);\n logger.info(`Removing from Mongo (${collection}) id: ${params.correlationId}`);\n const clean = sanitize(params.correlationId);\n logger.silly(`mongo/remove: clean: ${JSON.stringify(clean)}`);\n\n try {\n //const metadataResult = await getFileMetadata({filename: clean});\n //logger.debug(`mongo/remove: metadataResult: ${JSON.stringify(metadataResult)}`);\n const noContent = await removeContent(params);\n if (noContent) {\n await db.collection(collection).deleteOne({correlationId: clean});\n return true;\n }\n } catch (err) {\n if (err instanceof MongoDriverError) {\n if (err.message.indexOf('File not found for id') !== -1) {\n logger.silly(`mongo/remove: File not found, removing queueItem ${JSON.stringify(clean)} from ${collection}`);\n await db.collection(collection).deleteOne({correlationId: clean});\n return true;\n }\n logger.error(err.message);\n throw new ApiError(httpStatus.INTERNAL_SERVER_ERROR, err.message);\n }\n throw new ApiError(httpStatus.INTERNAL_SERVER_ERROR, err.message);\n }\n }\n\n async function readContent(correlationId) {\n logger.info(`Reading content from mongo for id: ${correlationId} in ${collection}`);\n const clean = sanitize(correlationId);\n const result = await db.collection(collection).findOne({correlationId: clean}); // njsscan-ignore: node_nosqli_injection\n\n if (result) {\n return {\n contentType: result.contentType,\n readStream: gridFSBucket.openDownloadStreamByName(clean)\n };\n }\n\n throw new ApiError(httpStatus.NOT_FOUND);\n }\n\n async function removeContent(params) {\n logger.info(`Removing content from mongo for id: ${params.correlationId} in ${collection}`);\n const clean = sanitize(params.correlationId);\n\n const result = await db.collection(collection).findOne({correlationId: clean}); // njsscan-ignore: node_nosqli_injection\n logger.silly(`mongo/removeContent: result ${JSON.stringify(result)}`);\n\n if (result) {\n await gridFSBucket.delete(clean);\n return true;\n }\n\n return true;\n }\n\n function getOne({operation, queueItemState}) {\n const clean = {queueItemState: sanitize(queueItemState)};\n try {\n if (operation === undefined) {\n logger.silly(`Checking DB ${collection} for ${JSON.stringify(clean.queueItemState)}`);\n return db.collection(collection).findOne({...clean});\n }\n\n const clean2 = {operation: sanitize(operation)};\n logger.silly(`Checking DB ${collection} for ${clean.queueItemState} + ${clean2.operation}`);\n return db.collection(collection).findOne({...clean, ...clean2});\n } catch (error) {\n logError(error);\n }\n }\n\n function getStream(correlationId) {\n logger.info(`Forming stream from mongo for ${correlationId} in ${collection}`);\n const clean = sanitize(correlationId);\n try {\n // Return content stream\n return gridFSBucket.openDownloadStreamByName(clean);\n } catch (error) {\n logError(error);\n }\n }\n\n async function pushIds({correlationId, handledIds, rejectedIds}) {\n logger.verbose(`Push ids (${handledIds.length}) and rejectedIds (${rejectedIds.length}) ${correlationId} to ${collection}`);\n logger.debug(`ids (${handledIds.length}): ${JSON.stringify(handledIds)}, rejectedIds ${rejectedIds.length}: ${JSON.stringify(rejectedIds)}`);\n const clean = sanitize(correlationId);\n await db.collection(collection).updateOne({\n correlationId: clean\n }, {\n $set: {\n modificationTime: moment().toDate()\n },\n $push: {\n handledIds: {$each: handledIds},\n rejectedIds: {$each: rejectedIds}\n }\n });\n }\n\n async function pushMessages({correlationId, messages, messageField = 'messages'}) {\n logger.verbose(`Push messages (${messages.length}) to ${messageField} ${correlationId} to ${collection}`);\n logger.silly(`Messages (${messages.length}): ${JSON.stringify(messages)}}`);\n const clean = sanitize(correlationId);\n const cleanMessageField = sanitize(messageField);\n await db.collection(collection).updateOne({\n correlationId: clean\n }, {\n $set: {\n modificationTime: moment().toDate()\n },\n $push: {\n [cleanMessageField]: {$each: messages}\n }\n });\n }\n\n function setState({correlationId, state, errorMessage = '', errorStatus = ''}) {\n const errorString = errorMessage || errorStatus ? `, Error message: '${errorMessage}', Error status: '${errorStatus}'` : '';\n logger.info(`Setting queue-item state ${state} for ${correlationId}${errorString} to ${collection}`);\n const clean = sanitize(correlationId);\n return db.collection(collection).findOneAndUpdate({\n correlationId: clean\n }, {\n $set: {\n queueItemState: state,\n modificationTime: moment().toDate(),\n errorMessage,\n errorStatus\n }\n }, {projection: {_id: 0}, returnNewDocument: true});\n }\n}\n"],"file":"mongo.js"}
1
+ {"version":3,"sources":["../src/mongo.js"],"names":["MONGO_URI","collection","logger","client","MongoClient","connect","useNewUrlParser","useUnifiedTopology","db","gridFSBucket","GridFSBucket","bucketName","createPrio","createBulk","checkAndSetState","query","queryById","remove","readContent","removeContent","getOne","getStream","setState","pushIds","pushMessages","correlationId","cataloger","oCatalogerIn","operation","noop","undefined","unique","prio","time","toDate","newQueueItem","operationSettings","queueItemState","QUEUE_ITEM_STATE","VALIDATOR","PENDING_VALIDATION","creationTime","modificationTime","handledIds","rejectedIds","result","insertOne","acknowledged","info","ApiError","httpStatus","INTERNAL_SERVER_ERROR","error","contentType","recordLoadParams","stream","UPLOADING","Promise","resolve","reject","outputStream","openUploadStream","on","chunk","write","end","state","errorMessage","errorStatus","timeOut","checkTimeOut","params","find","projection","_id","toArray","debug","length","checkModTime","findOne","queueitemState","oldState","ABORT","ERROR","includes","silly","timeoutTime","add","isBefore","REQUEST_TIMEOUT","JSON","stringify","clean","noContent","deleteOne","err","MongoDriverError","message","indexOf","readStream","openDownloadStreamByName","NOT_FOUND","delete","clean2","verbose","updateOne","$set","$push","$each","messages","messageField","cleanMessageField","errorString","findOneAndUpdate","returnNewDocument"],"mappings":";;;;;;;AA4BA;;AACA;;AACA;;AACA;;AACA;;AACA;;AACA;;AACA;;;;AAnCA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAC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CAAa,4BAA2BkB,KAAM,QAAOzC,aAAc,GAAEuF,WAAY,OAAM/G,UAAW,EAAlG;AACA,UAAM2F,KAAK,GAAG,4BAASnE,aAAT,CAAd;AACA,WAAOjB,EAAE,CAACP,UAAH,CAAcA,UAAd,EAA0BgH,gBAA1B,CAA2C;AAChDxF,MAAAA,aAAa,EAAEmE;AADiC,KAA3C,EAEJ;AACDc,MAAAA,IAAI,EAAE;AACJrE,QAAAA,cAAc,EAAE6B,KADZ;AAEJxB,QAAAA,gBAAgB,EAAE,uBAASR,MAAT,EAFd;AAGJiC,QAAAA,YAHI;AAIJC,QAAAA;AAJI;AADL,KAFI,EASJ;AAACK,MAAAA,UAAU,EAAE;AAACC,QAAAA,GAAG,EAAE;AAAN,OAAb;AAAuBwC,MAAAA,iBAAiB,EAAE;AAA1C,KATI,CAAP;AAUD;AACF","sourcesContent":["/**\n*\n* @licstart The following is the entire license notice for the JavaScript code in this file.\n*\n* Shared modules for microservices of Melinda rest api batch import system\n*\n* Copyright (C) 2020-2021 University Of Helsinki (The National Library Of Finland)\n*\n* This file is part of melinda-rest-api-commons\n*\n* melinda-rest-api-commons program is free software: you can redistribute it and/or modify\n* it under the terms of the GNU Affero General Public License as\n* published by the Free Software Foundation, either version 3 of the\n* License, or (at your option) any later version.\n*\n* melinda-rest-api-commons is distributed in the hope that it will be useful,\n* but WITHOUT ANY WARRANTY; without even the implied warranty of\n* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n* GNU Affero General Public License for more details.\n*\n* You should have received a copy of the GNU Affero General Public License\n* along with this program. If not, see <http://www.gnu.org/licenses/>.\n*\n* @licend The above is the entire license notice\n* for the JavaScript code in this file.\n*\n*/\n\nimport {MongoClient, GridFSBucket, MongoDriverError} from 'mongodb';\nimport {createLogger} from '@natlibfi/melinda-backend-commons';\nimport {Error as ApiError} from '@natlibfi/melinda-commons';\nimport {QUEUE_ITEM_STATE} from './constants';\nimport {logError} from './utils.js';\nimport moment from 'moment';\nimport httpStatus from 'http-status';\nimport sanitize from 'mongo-sanitize';\n\n/* QueueItem:\n{\n \"correlationId\":\"FOO\",\n \"cataloger\":\"xxx0000\",\n \"oCatalogerIn\":\"xxx0000\"\n \"operation\":\"UPDATE\",\n \"contentType\":\"application/json\",\n \"recordLoadParams\": {\n \"pActiveLibrary\": \"XXX00\",\n \"pInputFile\": \"filename.seq\",\n \"pRejectFile\": \"filename.rej\",\n \"pLogFile\": \"filename.syslog\",\n \"pOldNew\": \"NEW\"\n },\n \"queueItemState\":\"PENDING_QUEUING\",\n \"creationTime\":\"2020-01-01T00:00:00.000Z\",\n \"modificationTime\":\"2020-01-01T00:00:01.000Z\",\n \"handledIds\": []\n}\n*/\n\nexport default async function (MONGO_URI, collection) {\n const logger = createLogger();\n\n // Connect to mongo (MONGO)\n const client = await MongoClient.connect(MONGO_URI, {useNewUrlParser: true, useUnifiedTopology: true});\n const db = client.db('rest-api');\n const gridFSBucket = new GridFSBucket(db, {bucketName: collection});\n\n return {createPrio, createBulk, checkAndSetState, query, queryById, remove, readContent, removeContent, getOne, getStream, setState, pushIds, pushMessages};\n\n async function createPrio({correlationId, cataloger, oCatalogerIn, operation, noop = undefined, unique = undefined, prio = true}) {\n const time = moment().toDate();\n const newQueueItem = {\n correlationId,\n cataloger,\n operation,\n oCatalogerIn,\n operationSettings: {unique, noop, prio},\n queueItemState: QUEUE_ITEM_STATE.VALIDATOR.PENDING_VALIDATION,\n creationTime: time,\n modificationTime: time,\n handledIds: [],\n rejectedIds: []\n };\n try {\n const result = await db.collection(collection).insertOne(newQueueItem);\n if (result.acknowledged) {\n logger.info(`New PRIO queue item for ${operation} ${correlationId} has been made in ${collection}`);\n return;\n }\n throw new ApiError(httpStatus.INTERNAL_SERVER_ERROR);\n } catch (error) {\n logError(error);\n throw new ApiError(httpStatus.INTERNAL_SERVER_ERROR);\n }\n }\n\n function createBulk({correlationId, cataloger, oCatalogerIn, operation, contentType, recordLoadParams, stream, prio = false}) {\n const time = moment().toDate();\n const newQueueItem = {\n correlationId,\n cataloger,\n oCatalogerIn,\n operation,\n operationSettings: {prio},\n contentType,\n recordLoadParams,\n queueItemState: QUEUE_ITEM_STATE.VALIDATOR.UPLOADING,\n creationTime: time,\n modificationTime: time,\n handledIds: [],\n rejectedIds: []\n };\n try {\n // No await here, promises later\n db.collection(collection).insertOne(newQueueItem);\n logger.info(`New BULK queue item for ${operation} ${correlationId} has been made in ${collection}!`);\n } catch (error) {\n logError(error);\n throw new ApiError(httpStatus.INTERNAL_SERVER_ERROR);\n }\n\n return new Promise((resolve, reject) => {\n const outputStream = gridFSBucket.openUploadStream(correlationId);\n\n stream\n .on('error', reject)\n .on('data', chunk => outputStream.write(chunk))\n .on('end', () => outputStream.end(undefined, undefined, () => {\n resolve(correlationId);\n }));\n });\n }\n\n // Check state that the queueItem has not waited too long and set state\n async function checkAndSetState({correlationId, state, errorMessage = '', errorStatus = ''}) {\n // checkTimeOut returns true, if queueItem is fresher than 1 minute and it's state is not ABORT/ERROR\n // otherwise it sets queueItem to state ABORT (408, 'Timeout')\n const timeOut = await checkTimeOut(correlationId);\n if (timeOut) {\n return setState({correlationId, state, errorMessage, errorStatus});\n }\n return false;\n }\n\n async function query(params) {\n const result = await db.collection(collection).find(params, {projection: {_id: 0}})\n .toArray();\n logger.debug(`Query result: ${result.length > 0 ? 'Found!' : 'Not found!'}`);\n return result;\n }\n\n async function queryById(correlationId, checkModTime) {\n const result = await db.collection(collection).findOne({correlationId});\n if (checkModTime) {\n const timeOut = await checkTimeOut(correlationId);\n if (timeOut) {\n return queryById(correlationId);\n }\n return result;\n }\n\n return result;\n }\n\n // Check that if the item has waited too long\n // If the last modification time for the queueItem is older than 1 minute\n // set state to ABORT and return false, otherwise return true\n // If the state is already ABORT or ERROR return false\n\n async function checkTimeOut(correlationId) {\n const {modificationTime, queueitemState: oldState} = await db.collection(collection).findOne({correlationId});\n\n if ([QUEUE_ITEM_STATE.ABORT, QUEUE_ITEM_STATE.ERROR].includes(oldState)) {\n logger.silly(`${correlationId} has already state: ${oldState}`);\n return false;\n }\n\n const timeoutTime = moment(modificationTime).add(1, 'm');\n logger.silly(`timeOut @ ${timeoutTime}`);\n\n if (timeoutTime.isBefore()) {\n await setState({correlationId, state: QUEUE_ITEM_STATE.ABORT, errorStatus: httpStatus.REQUEST_TIMEOUT, errorMessage: `Timeout in ${oldState}`});\n return false;\n }\n\n return true;\n }\n\n async function remove(params) {\n logger.silly(`${JSON.stringify(params)}`);\n logger.info(`Removing from Mongo (${collection}) id: ${params.correlationId}`);\n const clean = sanitize(params.correlationId);\n logger.silly(`mongo/remove: clean: ${JSON.stringify(clean)}`);\n\n try {\n //const metadataResult = await getFileMetadata({filename: clean});\n //logger.debug(`mongo/remove: metadataResult: ${JSON.stringify(metadataResult)}`);\n const noContent = await removeContent(params);\n if (noContent) {\n await db.collection(collection).deleteOne({correlationId: clean});\n return true;\n }\n } catch (err) {\n if (err instanceof MongoDriverError) {\n if (err.message.indexOf('File not found for id') !== -1) {\n logger.silly(`mongo/remove: File not found, removing queueItem ${JSON.stringify(clean)} from ${collection}`);\n await db.collection(collection).deleteOne({correlationId: clean});\n return true;\n }\n logger.error(err.message);\n throw new ApiError(httpStatus.INTERNAL_SERVER_ERROR, err.message);\n }\n throw new ApiError(httpStatus.INTERNAL_SERVER_ERROR, err.message);\n }\n }\n\n async function readContent(correlationId) {\n logger.info(`Reading content from mongo for id: ${correlationId} in ${collection}`);\n const clean = sanitize(correlationId);\n const result = await db.collection(collection).findOne({correlationId: clean}); // njsscan-ignore: node_nosqli_injection\n\n if (result) {\n return {\n contentType: result.contentType,\n readStream: gridFSBucket.openDownloadStreamByName(clean)\n };\n }\n\n throw new ApiError(httpStatus.NOT_FOUND);\n }\n\n async function removeContent(params) {\n logger.info(`Removing content from mongo for id: ${params.correlationId} in ${collection}`);\n const clean = sanitize(params.correlationId);\n\n const result = await db.collection(collection).findOne({correlationId: clean}); // njsscan-ignore: node_nosqli_injection\n logger.silly(`mongo/removeContent: result ${JSON.stringify(result)}`);\n\n if (result) {\n await gridFSBucket.delete(clean);\n return true;\n }\n\n return true;\n }\n\n function getOne({operation, queueItemState}) {\n const clean = {queueItemState: sanitize(queueItemState)};\n try {\n if (operation === undefined) {\n logger.silly(`Checking DB ${collection} for ${JSON.stringify(clean.queueItemState)}`);\n return db.collection(collection).findOne({...clean});\n }\n\n const clean2 = {operation: sanitize(operation)};\n logger.silly(`Checking DB ${collection} for ${clean.queueItemState} + ${clean2.operation}`);\n return db.collection(collection).findOne({...clean, ...clean2});\n } catch (error) {\n logError(error);\n }\n }\n\n function getStream(correlationId) {\n logger.info(`Forming stream from mongo for ${correlationId} in ${collection}`);\n const clean = sanitize(correlationId);\n try {\n // Return content stream\n return gridFSBucket.openDownloadStreamByName(clean);\n } catch (error) {\n logError(error);\n }\n }\n\n async function pushIds({correlationId, handledIds, rejectedIds}) {\n logger.verbose(`Push ids (${handledIds.length}) and rejectedIds (${rejectedIds.length}) ${correlationId} to ${collection}`);\n logger.debug(`ids (${handledIds.length}): ${JSON.stringify(handledIds)}, rejectedIds ${rejectedIds.length}: ${JSON.stringify(rejectedIds)}`);\n const clean = sanitize(correlationId);\n await db.collection(collection).updateOne({\n correlationId: clean\n }, {\n $set: {\n modificationTime: moment().toDate()\n },\n $push: {\n handledIds: {$each: handledIds},\n rejectedIds: {$each: rejectedIds}\n }\n });\n }\n\n async function pushMessages({correlationId, messages, messageField = 'messages'}) {\n logger.verbose(`Push messages (${messages.length}) to ${messageField} ${correlationId} to ${collection}`);\n logger.silly(`Messages (${messages.length}): ${JSON.stringify(messages)}}`);\n const clean = sanitize(correlationId);\n const cleanMessageField = sanitize(messageField);\n await db.collection(collection).updateOne({\n correlationId: clean\n }, {\n $set: {\n modificationTime: moment().toDate()\n },\n $push: {\n [cleanMessageField]: {$each: messages}\n }\n });\n }\n\n function setState({correlationId, state, errorMessage = '', errorStatus = ''}) {\n const errorString = errorMessage || errorStatus ? `, Error message: '${errorMessage}', Error status: '${errorStatus}'` : '';\n logger.info(`Setting queue-item state ${state} for ${correlationId}${errorString} to ${collection}`);\n const clean = sanitize(correlationId);\n return db.collection(collection).findOneAndUpdate({\n correlationId: clean\n }, {\n $set: {\n queueItemState: state,\n modificationTime: moment().toDate(),\n errorMessage,\n errorStatus\n }\n }, {projection: {_id: 0}, returnNewDocument: true});\n }\n}\n"],"file":"mongo.js"}
package/package.json CHANGED
@@ -14,7 +14,7 @@
14
14
  "url": "git@github.com:natlibfi/melinda-rest-api-commons.git"
15
15
  },
16
16
  "license": "AGPL-3.0+",
17
- "version": "2.0.1",
17
+ "version": "2.1.2",
18
18
  "main": "./dist/index.js",
19
19
  "engines": {
20
20
  "node": ">=14"
@@ -37,7 +37,7 @@
37
37
  "@natlibfi/marc-record": "^6.1.2",
38
38
  "@natlibfi/marc-record-serializers": "^8.0.0",
39
39
  "@natlibfi/marc-record-validate": "^6.0.1",
40
- "@natlibfi/marc-record-validators-melinda": "^8.4.4",
40
+ "@natlibfi/marc-record-validators-melinda": "^8.4.6",
41
41
  "@natlibfi/melinda-backend-commons": "^2.0.5",
42
42
  "@natlibfi/melinda-commons": "^12.0.0",
43
43
  "amqplib": ">=0.8.0 <1.0.0",