@natlibfi/melinda-rest-api-commons 2.0.1-alpha.7 → 2.1.0

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
package/dist/amqp.js CHANGED
@@ -63,7 +63,7 @@ async function _default(AMQP_URL) {
63
63
  sendToQueue,
64
64
  removeQueue,
65
65
  messagesToRecords
66
- };
66
+ }; // eslint-disable-next-line max-statements
67
67
 
68
68
  async function checkQueue({
69
69
  queue,
@@ -71,9 +71,10 @@ async function _default(AMQP_URL) {
71
71
  toRecord = true,
72
72
  purge = false
73
73
  }) {
74
- logger.silly(`checkQueue: ${queue}, Style: ${style}: toRecord: ${toRecord}, Purge: ${purge}, `);
74
+ logger.silly(`checkQueue: ${queue}, Style: ${style}: toRecord: ${toRecord}, Purge: ${purge}`);
75
75
 
76
76
  try {
77
+ errorUndefinedQueue(queue);
77
78
  const channelInfo = await channel.assertQueue(queue, {
78
79
  durable: true
79
80
  });
@@ -217,6 +218,7 @@ async function _default(AMQP_URL) {
217
218
  logger.silly(`CorrelationId ${correlationId}`);
218
219
  logger.silly(`Data ${JSON.stringify(data)}`);
219
220
  logger.silly(`Headers ${JSON.stringify(headers)}`);
221
+ errorUndefinedQueue(queue);
220
222
  await channel.assertQueue(queue, {
221
223
  durable: true
222
224
  });
@@ -297,5 +299,11 @@ async function _default(AMQP_URL) {
297
299
  function getHeaderInfo(data) {
298
300
  return data.properties.headers;
299
301
  }
302
+
303
+ function errorUndefinedQueue(queue) {
304
+ if (queue === undefined || queue === '' || queue === null) {
305
+ throw new _melindaCommons.Error(_httpStatus.default.INTERNAL_SERVER_ERROR, `Undefined queue!`);
306
+ }
307
+ }
300
308
  }
301
309
  //# sourceMappingURL=amqp.js.map
package/dist/amqp.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/amqp.js"],"names":["AMQP_URL","connection","amqplib","connect","channel","createChannel","logger","checkQueue","consumeChunk","consumeOne","ackMessages","nackMessages","sendToQueue","removeQueue","messagesToRecords","queue","style","toRecord","purge","silly","channelInfo","assertQueue","durable","purgeQueue","verbose","messageCount","ApiError","httpStatus","UNPROCESSABLE_ENTITY","error","messages","getData","headers","getHeaderInfo","debug","length","records","message","get","colors","maxArrayLength","depth","forEach","properties","correlationId","ack","nack","data","JSON","stringify","Buffer","from","persistent","tempChannel","deleteQueue","close","map","content","parse","toString","MarcRecord","messagesToGet","CHUNK_SIZE","pump","count","results","identifiers","identifier","deliveryTag","fields","includes","concat"],"mappings":";;;;;;;AA6BA;;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;AAWe,wBAAgBA,QAAhB,EAA0B;AACvC,QAAMC,UAAU,GAAG,MAAMC,iBAAQC,OAAR,CAAgBH,QAAhB,CAAzB;AACA,QAAMI,OAAO,GAAG,MAAMH,UAAU,CAACI,aAAX,EAAtB;AACA,QAAMC,MAAM,GAAG,0CAAf;AAEA,SAAO;AAACC,IAAAA,UAAD;AAAaC,IAAAA,YAAb;AAA2BC,IAAAA,U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A,aAAOL,QAAP;AACD,KAXD,CAWE,OAAOD,KAAP,EAAc;AACd,2BAASA,KAAT;AACD;;AAED,mBAAekC,IAAf,CAAoBC,KAApB,EAA2BC,OAAO,GAAG,EAArC,EAAyCC,WAAW,GAAG,EAAvD,EAA2D;AACzD,UAAIF,KAAK,KAAK,CAAd,EAAiB;AACf,eAAOC,OAAP;AACD;;AAED,YAAM5B,OAAO,GAAG,MAAMjC,OAAO,CAACkC,GAAR,CAAYvB,KAAZ,CAAtB;AACA,YAAMoD,UAAU,GAAG;AACjBvB,QAAAA,aAAa,EAAEP,OAAO,CAACM,UAAR,CAAmBC,aADjB;AAEjBwB,QAAAA,WAAW,EAAE/B,OAAO,CAACgC,MAAR,CAAeD;AAFX,OAAnB,CANyD,CAUzD;;AACA,UAAIF,WAAW,CAACI,QAAZ,CAAqBH,UAArB,CAAJ,EAAsC;AACpC,eAAOJ,IAAI,CAACC,KAAK,GAAG,CAAT,EAAYC,OAAZ,EAAqBC,WAArB,CAAX;AACD;;AAED,aAAOH,IAAI,CAACC,KAAK,GAAG,CAAT,EAAYC,OAAO,CAACM,MAAR,CAAelC,OAAf,CAAZ,EAAqC6B,WAAW,CAACK,MAAZ,CAAmBJ,UAAnB,CAArC,CAAX;AACD;AACF;;AAED,WAASlC,aAAT,CAAuBc,IAAvB,EAA6B;AAC3B,WAAOA,IAAI,CAACJ,UAAL,CAAgBX,OAAvB;AACD;AACF","sourcesContent":["\n/**\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 amqplib from 'amqplib';\nimport {MarcRecord} from '@natlibfi/marc-record';\nimport {Error as ApiError} from '@natlibfi/melinda-commons';\nimport {createLogger} from '@natlibfi/melinda-backend-commons';\nimport {CHUNK_SIZE} from './constants';\nimport {logError} from './utils';\nimport {inspect} from 'util';\nimport httpStatus from 'http-status';\n\nexport default async function (AMQP_URL) {\n const connection = await amqplib.connect(AMQP_URL);\n const channel = await connection.createChannel();\n const logger = createLogger();\n\n return {checkQueue, consumeChunk, consumeOne, ackMessages, nackMessages, sendToQueue, removeQueue, messagesToRecords};\n\n async function checkQueue({queue, style = 'basic', toRecord = true, purge = false}) {\n logger.silly(`checkQueue: ${queue}, Style: ${style}: toRecord: ${toRecord}, Purge: ${purge}, `);\n try {\n const channelInfo = await channel.assertQueue(queue, {durable: true});\n if (purge) {\n await purgeQueue(purge);\n logger.verbose(`Queue ${queue} has purged ${channelInfo.messageCount} messages`);\n return checkQueue({queue, style});\n }\n\n if (channelInfo.messageCount < 1) {\n logger.silly(`checkQueue: ${channelInfo.messageCount} - ${queue} is empty`);\n return false;\n }\n logger.silly(`Queue ${queue} has ${channelInfo.messageCount} messages`);\n\n if (style === 'messages') {\n return channelInfo.messageCount;\n }\n\n // Note: returns one message (+ record, of toRecord: true)\n // note: if toRecord is false returns just plain message / false\n // note: if toRecord is true returns {headers, records, messages} -object / false\n // should this be more consistent?\n if (style === 'one') {\n return consumeOne(queue, toRecord);\n }\n\n // Note: returns a chunk of (100) messages (+ records, if toRecord: true)\n // returns {headers, records, messages} object or {headers, messages} object depending on toRecord\n if (style === 'basic') {\n return consumeChunk(queue, toRecord);\n }\n\n // Defaults:\n throw new ApiError(httpStatus.UNPROCESSABLE_ENTITY);\n } catch (error) {\n logError(error);\n }\n\n function purgeQueue(purge) {\n if (purge) {\n return channel.purgeQueue(queue);\n }\n }\n }\n\n async function consumeChunk(queue, toRecord) {\n logger.silly(`Prepared to consumeChunk from queue: ${queue}`);\n try {\n await channel.assertQueue(queue, {durable: true});\n\n // getData: next chunk (100) messages\n const messages = await getData(queue);\n const headers = getHeaderInfo(messages[0]);\n logger.debug(`consumeChunk (${messages ? messages.length : '0'} from queue ${queue}) ${toRecord ? 'to records' : 'just messages'}`);\n\n if (toRecord) {\n const records = await messagesToRecords(messages);\n return {headers, records, messages};\n }\n\n return {headers, messages};\n } catch (error) {\n logError(error);\n }\n }\n\n async function consumeOne(queue, toRecord) {\n logger.silly(`Prepared to consumeOne from queue: ${queue}`);\n try {\n await channel.assertQueue(queue, {durable: true});\n\n // Returns false if 0 items in queue\n const message = await channel.get(queue);\n\n logger.silly(`Message: ${inspect(message, {colors: true, maxArrayLength: 3, depth: 3})}`);\n // Do not spam the logs\n logger.debug(`consumeOne from queue: ${queue} ${toRecord ? 'to records' : 'just the message'}`);\n\n if (message) {\n if (toRecord) {\n const headers = getHeaderInfo(message);\n const records = messagesToRecords([message]);\n return {headers, records, messages: [message]};\n }\n\n return message;\n }\n\n return false;\n } catch (error) {\n logError(error);\n }\n }\n\n function ackMessages(messages) {\n messages.forEach(message => {\n logger.silly(`Ack message ${message.properties.correlationId}`);\n channel.ack(message);\n });\n }\n\n function nackMessages(messages) {\n messages.forEach(message => {\n logger.silly(`Nack message ${message.properties.correlationId}`);\n channel.nack(message);\n });\n }\n\n async function sendToQueue({queue, correlationId, headers, data}) {\n try {\n logger.silly(`Queue ${queue}`);\n logger.silly(`CorrelationId ${correlationId}`);\n logger.silly(`Data ${JSON.stringify(data)}`);\n logger.silly(`Headers ${JSON.stringify(headers)}`);\n\n await channel.assertQueue(queue, {durable: true});\n\n channel.sendToQueue(\n queue,\n Buffer.from(JSON.stringify({data})),\n {\n correlationId,\n persistent: true,\n headers\n }\n );\n logger.silly(`Send message for ${correlationId} to queue: ${queue}`);\n } catch (error) {\n logError(error);\n }\n }\n\n async function removeQueue(queue) {\n // deleteQueue borks the channel if the queue does not exist\n // -> use throwaway tempChannel to avoid killing actual channel in use\n // this might be doable also with assertQueue before deleteQueue\n const tempChannel = await connection.createChannel();\n logger.verbose(`Removing queue ${queue}.`);\n await tempChannel.deleteQueue(queue);\n if (tempChannel) { // eslint-disable-line functional/no-conditional-statement\n await tempChannel.close();\n }\n }\n\n // ----------------\n // Helper functions\n // ----------------\n\n function messagesToRecords(messages) {\n logger.debug(`Parsing messages (${messages.length}) to records`);\n\n return messages.map(message => {\n const content = JSON.parse(message.content.toString());\n return new MarcRecord(content.data);\n });\n }\n\n async function getData(queue) {\n logger.debug(`Getting queue data from ${queue}`);\n try {\n const {messageCount} = await channel.checkQueue(queue);\n logger.silly(`There is ${messageCount} messages in queue ${queue}`);\n const messagesToGet = messageCount >= CHUNK_SIZE ? CHUNK_SIZE : messageCount;\n logger.silly(`Getting ${messagesToGet} messages from queue ${queue}`);\n\n const messages = await pump(messagesToGet);\n\n logger.debug(`Returning ${messages.length} unique messages`);\n\n return messages;\n } catch (error) {\n logError(error);\n }\n\n async function pump(count, results = [], identifiers = []) {\n if (count === 0) {\n return results;\n }\n\n const message = await channel.get(queue);\n const identifier = {\n correlationId: message.properties.correlationId,\n deliveryTag: message.fields.deliveryTag\n };\n // Filter not unique messages\n if (identifiers.includes(identifier)) {\n return pump(count - 1, results, identifiers);\n }\n\n return pump(count - 1, results.concat(message), identifiers.concat(identifier));\n }\n }\n\n function getHeaderInfo(data) {\n return data.properties.headers;\n }\n}\n"],"file":"amqp.js"}
1
+ {"version":3,"sources":["../src/amqp.js"],"names":["AMQP_URL","connection","amqplib","connect","channel","createChannel","logger","checkQueue","consumeChunk","consumeOne","ackMessages","nackMessages","sendToQueue","removeQueue","messagesToRecords","queue","style","toRecord","purge","silly","errorUndefinedQueue","channelInfo","assertQueue","durable","purgeQueue","verbose","messageCount","ApiError","httpStatus","UNPROCESSABLE_ENTITY","error","messages","getData","headers","getHeaderInfo","debug","length","records","message","get","colors","maxArrayLength","depth","forEach","properties","correlationId","ack","nack","data","JSON","stringify","Buffer","from","persistent","tempChannel","deleteQueue","close","map","content","parse","toString","MarcRecord","messagesToGet","CHUNK_SIZE","pump","count","results","identifiers","identifier","deliveryTag","fields","includes","concat","undefined","INTERNAL_SERVER_ERROR"],"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;AAWe,wBAAgBA,QAAhB,EAA0B;AACvC,QAAMC,UAAU,GAAG,MAAMC,iBAAQC,OAAR,CAAgBH,QAAhB,CAAzB;AACA,QAAMI,OAAO,GAAG,MAAMH,UAAU,CAACI,aAAX,EAAtB;AACA,QAAMC,MAAM,GAAG,0CAAf;AAEA,SAAO;AAACC,IAAAA,UAAD;AAAaC,IAAAA,YAAb;AAA2BC,IAAAA,UAA3B;AAAuCC,IAAAA,WAAvC;AAAoDC,IAAAA,YAApD;AAAkEC,IAAAA,WAAlE;AAA+EC,IAAAA,WAA/E;AAA4FC,IAAAA;AAA5F,GAAP,CALuC,CAOvC;;AACA,iBAAeP,UAAf,CAA0B;AAACQ,IAAAA,KAAD;AAAQC,IAAAA,KAAK,GAAG,OAAhB;AAAyBC,IAAAA,QAAQ,GAAG,IAApC;AAA0CC,IAAAA,KAAK,GAAG;AAAlD,GAA1B,EAAoF;AAClFZ,IAAAA,MAAM,CAACa,KAAP,CAAc,eAAcJ,KAAM,YAAWC,KAAM,eAAcC,QAAS,YAAWC,KAAM,EAA3F;;AAEA,QAAI;AAEFE,MAAAA,mBAAmB,CAACL,KAAD,CAAnB;AACA,YAAMM,WAAW,GAAG,MAAMjB,OAAO,CAACkB,WAAR,CAAoBP,KAApB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,EAAE/B,OAAO,CAACgC,MAAR,CAAeD;AAFX,OAAnB,CANyD,CAUzD;;AACA,UAAIF,WAAW,CAACI,QAAZ,CAAqBH,UAArB,CAAJ,EAAsC;AACpC,eAAOJ,IAAI,CAACC,KAAK,GAAG,CAAT,EAAYC,OAAZ,EAAqBC,WAArB,CAAX;AACD;;AAED,aAAOH,IAAI,CAACC,KAAK,GAAG,CAAT,EAAYC,OAAO,CAACM,MAAR,CAAelC,OAAf,CAAZ,EAAqC6B,WAAW,CAACK,MAAZ,CAAmBJ,UAAnB,CAArC,CAAX;AACD;AACF;;AAED,WAASlC,aAAT,CAAuBc,IAAvB,EAA6B;AAC3B,WAAOA,IAAI,CAACJ,UAAL,CAAgBX,OAAvB;AACD;;AAED,WAASb,mBAAT,CAA6BL,KAA7B,EAAoC;AAClC,QAAIA,KAAK,KAAK0D,SAAV,IAAuB1D,KAAK,KAAK,EAAjC,IAAuCA,KAAK,KAAK,IAArD,EAA2D;AACzD,YAAM,IAAIY,qBAAJ,CAAaC,oBAAW8C,qBAAxB,EAAgD,kBAAhD,CAAN;AACD;AACF;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 amqplib from 'amqplib';\nimport {MarcRecord} from '@natlibfi/marc-record';\nimport {Error as ApiError} from '@natlibfi/melinda-commons';\nimport {createLogger} from '@natlibfi/melinda-backend-commons';\nimport {CHUNK_SIZE} from './constants';\nimport {logError} from './utils';\nimport {inspect} from 'util';\nimport httpStatus from 'http-status';\n\nexport default async function (AMQP_URL) {\n const connection = await amqplib.connect(AMQP_URL);\n const channel = await connection.createChannel();\n const logger = createLogger();\n\n return {checkQueue, consumeChunk, consumeOne, ackMessages, nackMessages, sendToQueue, removeQueue, messagesToRecords};\n\n // eslint-disable-next-line max-statements\n async function checkQueue({queue, style = 'basic', toRecord = true, purge = false}) {\n logger.silly(`checkQueue: ${queue}, Style: ${style}: toRecord: ${toRecord}, Purge: ${purge}`);\n\n try {\n\n errorUndefinedQueue(queue);\n const channelInfo = await channel.assertQueue(queue, {durable: true});\n\n if (purge) {\n await purgeQueue(purge);\n logger.verbose(`Queue ${queue} has purged ${channelInfo.messageCount} messages`);\n return checkQueue({queue, style});\n }\n\n if (channelInfo.messageCount < 1) {\n logger.silly(`checkQueue: ${channelInfo.messageCount} - ${queue} is empty`);\n return false;\n }\n logger.silly(`Queue ${queue} has ${channelInfo.messageCount} messages`);\n\n if (style === 'messages') {\n return channelInfo.messageCount;\n }\n\n // Note: returns one message (+ record, of toRecord: true)\n // note: if toRecord is false returns just plain message / false\n // note: if toRecord is true returns {headers, records, messages} -object / false\n // should this be more consistent?\n if (style === 'one') {\n return consumeOne(queue, toRecord);\n }\n\n // Note: returns a chunk of (100) messages (+ records, if toRecord: true)\n // returns {headers, records, messages} object or {headers, messages} object depending on toRecord\n if (style === 'basic') {\n return consumeChunk(queue, toRecord);\n }\n\n // Defaults:\n throw new ApiError(httpStatus.UNPROCESSABLE_ENTITY);\n } catch (error) {\n logError(error);\n }\n\n function purgeQueue(purge) {\n if (purge) {\n return channel.purgeQueue(queue);\n }\n }\n }\n\n async function consumeChunk(queue, toRecord) {\n logger.silly(`Prepared to consumeChunk from queue: ${queue}`);\n try {\n await channel.assertQueue(queue, {durable: true});\n\n // getData: next chunk (100) messages\n const messages = await getData(queue);\n const headers = getHeaderInfo(messages[0]);\n logger.debug(`consumeChunk (${messages ? messages.length : '0'} from queue ${queue}) ${toRecord ? 'to records' : 'just messages'}`);\n\n if (toRecord) {\n const records = await messagesToRecords(messages);\n return {headers, records, messages};\n }\n\n return {headers, messages};\n } catch (error) {\n logError(error);\n }\n }\n\n async function consumeOne(queue, toRecord) {\n logger.silly(`Prepared to consumeOne from queue: ${queue}`);\n try {\n await channel.assertQueue(queue, {durable: true});\n\n // Returns false if 0 items in queue\n const message = await channel.get(queue);\n\n logger.silly(`Message: ${inspect(message, {colors: true, maxArrayLength: 3, depth: 3})}`);\n // Do not spam the logs\n logger.debug(`consumeOne from queue: ${queue} ${toRecord ? 'to records' : 'just the message'}`);\n\n if (message) {\n if (toRecord) {\n const headers = getHeaderInfo(message);\n const records = messagesToRecords([message]);\n return {headers, records, messages: [message]};\n }\n\n return message;\n }\n\n return false;\n } catch (error) {\n logError(error);\n }\n }\n\n function ackMessages(messages) {\n messages.forEach(message => {\n logger.silly(`Ack message ${message.properties.correlationId}`);\n channel.ack(message);\n });\n }\n\n function nackMessages(messages) {\n messages.forEach(message => {\n logger.silly(`Nack message ${message.properties.correlationId}`);\n channel.nack(message);\n });\n }\n\n async function sendToQueue({queue, correlationId, headers, data}) {\n try {\n logger.silly(`Queue ${queue}`);\n logger.silly(`CorrelationId ${correlationId}`);\n logger.silly(`Data ${JSON.stringify(data)}`);\n logger.silly(`Headers ${JSON.stringify(headers)}`);\n\n errorUndefinedQueue(queue);\n await channel.assertQueue(queue, {durable: true});\n\n channel.sendToQueue(\n queue,\n Buffer.from(JSON.stringify({data})),\n {\n correlationId,\n persistent: true,\n headers\n }\n );\n logger.silly(`Send message for ${correlationId} to queue: ${queue}`);\n } catch (error) {\n logError(error);\n }\n }\n\n async function removeQueue(queue) {\n // deleteQueue borks the channel if the queue does not exist\n // -> use throwaway tempChannel to avoid killing actual channel in use\n // this might be doable also with assertQueue before deleteQueue\n const tempChannel = await connection.createChannel();\n logger.verbose(`Removing queue ${queue}.`);\n await tempChannel.deleteQueue(queue);\n if (tempChannel) { // eslint-disable-line functional/no-conditional-statement\n await tempChannel.close();\n }\n }\n\n // ----------------\n // Helper functions\n // ----------------\n\n function messagesToRecords(messages) {\n logger.debug(`Parsing messages (${messages.length}) to records`);\n\n return messages.map(message => {\n const content = JSON.parse(message.content.toString());\n return new MarcRecord(content.data);\n });\n }\n\n async function getData(queue) {\n logger.debug(`Getting queue data from ${queue}`);\n try {\n const {messageCount} = await channel.checkQueue(queue);\n logger.silly(`There is ${messageCount} messages in queue ${queue}`);\n const messagesToGet = messageCount >= CHUNK_SIZE ? CHUNK_SIZE : messageCount;\n logger.silly(`Getting ${messagesToGet} messages from queue ${queue}`);\n\n const messages = await pump(messagesToGet);\n\n logger.debug(`Returning ${messages.length} unique messages`);\n\n return messages;\n } catch (error) {\n logError(error);\n }\n\n async function pump(count, results = [], identifiers = []) {\n if (count === 0) {\n return results;\n }\n\n const message = await channel.get(queue);\n const identifier = {\n correlationId: message.properties.correlationId,\n deliveryTag: message.fields.deliveryTag\n };\n // Filter not unique messages\n if (identifiers.includes(identifier)) {\n return pump(count - 1, results, identifiers);\n }\n\n return pump(count - 1, results.concat(message), identifiers.concat(identifier));\n }\n }\n\n function getHeaderInfo(data) {\n return data.properties.headers;\n }\n\n function errorUndefinedQueue(queue) {\n if (queue === undefined || queue === '' || queue === null) {\n throw new ApiError(httpStatus.INTERNAL_SERVER_ERROR, `Undefined queue!`);\n }\n }\n}\n\n\n"],"file":"amqp.js"}
@@ -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,MAAM,CAACC,OAAP,CAAe,oBAAf;;AACA,QAAMC,SAAS,GAAGC,uBAAWC,KAAX,CAAiBN,MAAjB,EAAyB;AAACO,IAAAA,cAAc,EAAE;AAAjB,GAAzB,CAAlB;;AAEAC,EAAAA,mBAAmB;AAEnBP,EAAAA,QAAQ,CAACQ,OAAT,CAAiBC,OAAO,IAAI;AAC1BC,IAAAA,eAAe,CAACD,OAAD,CAAf;AACD,GAFD;AAIA,SAAON,SAAS,CAACQ,QAAV,EAAP,CAZkD,CAclD;;AACA,WAASD,eAAT,CAAyBD,OAAzB,EAAkC;AAChC,UAAM;AAACG,MAAAA,SAAD;AAAYC,MAAAA,SAAZ;AAAuBC,MAAAA;AAAvB,QAA6CL,OAAnD;AACA,UAAMM,OAAO,GAAI,IAAGH,SAAU,GAA9B;AACA,UAAMI,WAAW,GAAI,IAAGH,SAAU,GAAlC;AACAV,IAAAA,SAAS,CAACc,aAAV,GACGT,OADH,CACWU,KAAK,IAAI;AAChBA,MAAAA,KAAK,CAACC,SAAN,CACGC,MADH,CACU,CAAC;AAACC,QAAAA;AAAD,OAAD,KAAYP,kBAAkB,CAACQ,QAAnB,CAA4BD,IAA5B,CADtB,EAEGb,OAFH,CAEWe,QAAQ,IAAI;AACnBA,QAAAA,QAAQ,CAACC,KAAT,GAAiBD,QAAQ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YAAAA,GAAG,EAAEZ,WAAW,CAACD,WAAW,CAAC4B,SAAZ,CAAsBF,MAAM,IAAIA,MAAM,CAACC,IAAP,CAAYF,SAAS,CAAC3B,KAAtB,CAAhC,CAAD,CADX;AAELA,YAAAA,KAAK,EAAE2B,SAAS,CAAC3B,KAAV,CAAgB+B,KAAhB,CAAsBJ,SAAS,CAAC3B,KAAV,CAAgBgC,OAAhB,CAAwB,GAAxB,IAA+B,CAArD;AAFF,WAAP;AAID;;AAED,eAAON,SAAP;AACD,OAXM,EAWJ9B,MAXI,CAWGI,KAAK,IAAIA,KAAK,KAAK0B,SAXtB,CAAP;AAYD;AACF;AACF,C,CAED;;;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;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,GAAf,CAArB;AAEAD,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,EAA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Q,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,IAAA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Af,CAAkBC,GAAlB,CAAsBF,QAAtB;AACD,KAhBC,CAAF;AAiBD,GAlBO,CAAR;AAoBAf,EAAAA,QAAQ,CAAC,kBAAD,EAAqB,MAAM;AACjCQ,IAAAA,EAAE,CAAC,yCAAD,EAA4C,MAAM;AAClD,YAAMC,MAAM,GAAG,IAAIC,sBAAJ,CAAeP,UAAU,CAAC;AACvCQ,QAAAA,UAAU,EAAE,CACV,IADU,EAEV,uBAFU;AAD2B,OAAD,CAAzB,CAAf;AAMA,YAAMC,MAAM,GAAG,0BAAaH,MAAM,CAACI,QAAP,EAAb,CAAf;AACA,YAAME,QAAQ,GAAGZ,UAAU,CAAC;AAC1BQ,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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EAA6B,YAAY;AACzC,YAAMC,SAAS,GAAG,MAAM,0BAAxB;AACA,YAAMC,MAAM,GAAG,IAAIC,sBAAJ,CAAeC,IAAI,CAACC,KAAL,CAAWP,UAAU,CAAC;AAClDQ,QAAAA,UAAU,EAAE,CACV,IADU,EAEV,0BAFU;AADsC,OAAD,CAArB,CAAf,CAAf;AAMA,YAAMC,MAAM,GAAG,MAAMN,SAAS,CAACC,MAAM,CAACM,QAAP,EAAD,CAA9B;AACA,YAAMC,QAAQ,GAAGX,UAAU,CAAC;AAC1BQ,QAAAA,UAAU,EAAE,CACV,KADU,EAEV,0BAFU;AADc,OAAD,CAA3B;AAMA,YAAMI,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,IAH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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/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-alpha.7",
17
+ "version": "2.1.0",
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",