@natlibfi/melinda-rest-api-commons 2.3.1-alpha.1 → 2.3.1-alpha.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/dist/utils.js +18 -17
- package/dist/utils.js.map +1 -1
- package/package.json +1 -1
package/dist/utils.js
CHANGED
|
@@ -104,6 +104,9 @@ function getRecordResponseStatusAndMessage(responseStatus, responsePayload, id)
|
|
|
104
104
|
const responseStatusName = _httpStatus.default[`${responseStatus}_NAME`];
|
|
105
105
|
logger.debug(`Response status name: ${responseStatusName}`);
|
|
106
106
|
const message = getMessageFromResponsePayload(responsePayload);
|
|
107
|
+
const {
|
|
108
|
+
ids
|
|
109
|
+
} = responsePayload || [];
|
|
107
110
|
logger.debug(`Response message: ${message}`); // Non-http statuses
|
|
108
111
|
|
|
109
112
|
if (['UPDATED', 'CREATED', 'INVALID', 'ERROR', 'SKIPPED'].includes(responseStatus)) {
|
|
@@ -114,41 +117,37 @@ function getRecordResponseStatusAndMessage(responseStatus, responsePayload, id)
|
|
|
114
117
|
}
|
|
115
118
|
|
|
116
119
|
if (['UNKNOWN'].includes(responseStatus)) {
|
|
117
|
-
const possibleIds = responsePayload.ids || [];
|
|
118
120
|
return {
|
|
119
121
|
recordStatus: responseStatus,
|
|
120
122
|
message,
|
|
121
|
-
|
|
123
|
+
ids
|
|
122
124
|
};
|
|
123
125
|
} // Duplicates and other CONFLICT statuses
|
|
124
126
|
|
|
125
127
|
|
|
126
128
|
if ([_httpStatus.default.CONFLICT, 'CONFLICT'].includes(responseStatus)) {
|
|
127
129
|
if (/^Duplicates in database/u.test(message)) {
|
|
128
|
-
const duplicateIds = responsePayload.ids || [];
|
|
129
130
|
return {
|
|
130
131
|
recordStatus: 'DUPLICATE',
|
|
131
132
|
message,
|
|
132
|
-
|
|
133
|
+
ids
|
|
133
134
|
};
|
|
134
135
|
}
|
|
135
136
|
|
|
136
137
|
if (/^MatchValidation for all/u.test(message)) {
|
|
137
|
-
const duplicateIds = responsePayload.ids || [];
|
|
138
138
|
return {
|
|
139
139
|
recordStatus: 'CONFLICT',
|
|
140
140
|
message,
|
|
141
|
-
|
|
141
|
+
ids
|
|
142
142
|
};
|
|
143
|
-
}
|
|
143
|
+
} // Use ids only if there are more than one id or the id in payload does not match databaseId
|
|
144
144
|
|
|
145
|
-
const conflictIds = responsePayload.ids || []; // use conflictIds only if there are more than one id or the id in payload does not match databaseId
|
|
146
145
|
|
|
147
|
-
if (
|
|
146
|
+
if (ids.length > 1 || ids[0] !== id) {
|
|
148
147
|
return {
|
|
149
148
|
recordStatus: 'CONFLICT',
|
|
150
149
|
message,
|
|
151
|
-
|
|
150
|
+
ids
|
|
152
151
|
};
|
|
153
152
|
}
|
|
154
153
|
|
|
@@ -159,16 +158,18 @@ function getRecordResponseStatusAndMessage(responseStatus, responsePayload, id)
|
|
|
159
158
|
}
|
|
160
159
|
|
|
161
160
|
if ([_httpStatus.default.UNPROCESSABLE_ENTITY, 'UNPROCESSABLE_ENTITY'].includes(responseStatus)) {
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
161
|
+
if (ids.length > 1 || ids[0] !== id) {
|
|
162
|
+
return {
|
|
163
|
+
recordStatus: 'UNPROCESSABLE_ENTITY',
|
|
164
|
+
message,
|
|
165
|
+
ids
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
return {
|
|
168
170
|
recordStatus: 'UNPROCESSABLE_ENTITY',
|
|
169
171
|
message
|
|
170
172
|
};
|
|
171
|
-
return response;
|
|
172
173
|
}
|
|
173
174
|
|
|
174
175
|
if ([_httpStatus.default.NOT_FOUND, 'NOT_FOUND'].includes(responseStatus)) {
|
package/dist/utils.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","names":["logger","createLogger","logError","err","ApiError","error","JSON","stringify","stack","undefined","createImportJobState","operation","state","queryImportJobState","IMPORT_JOB_STATE","Error","OPERATIONS","importJobStateWithOperation","importJobStateForQuery","createRecordResponseItem","responsePayload","responseStatus","recordMetadata","id","recordResponseStatusAndMessage","getRecordResponseStatusAndMessage","recordResponseItem","databaseId","debug","responseStatusName","httpStatus","message","getMessageFromResponsePayload","
|
|
1
|
+
{"version":3,"file":"utils.js","names":["logger","createLogger","logError","err","ApiError","error","JSON","stringify","stack","undefined","createImportJobState","operation","state","queryImportJobState","IMPORT_JOB_STATE","Error","OPERATIONS","importJobStateWithOperation","importJobStateForQuery","createRecordResponseItem","responsePayload","responseStatus","recordMetadata","id","recordResponseStatusAndMessage","getRecordResponseStatusAndMessage","recordResponseItem","databaseId","debug","responseStatusName","httpStatus","message","getMessageFromResponsePayload","ids","includes","recordStatus","CONFLICT","test","length","UNPROCESSABLE_ENTITY","NOT_FOUND","String","addRecordResponseItem","correlationId","mongoOperator","pushMessages","messages","messageField"],"sources":["../src/utils.js"],"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 {createLogger} from '@natlibfi/melinda-backend-commons';\nimport {Error as ApiError} from '@natlibfi/melinda-commons';\nimport {IMPORT_JOB_STATE, OPERATIONS} from './constants';\nimport httpStatus from 'http-status';\n\nconst logger = createLogger();\n\nexport function logError(err) {\n if (err instanceof ApiError) {\n logger.error(JSON.stringify(err));\n return;\n }\n\n if (err === 'SIGINT') {\n logger.error(err);\n return;\n }\n\n logger.error(err.stack === undefined ? err : err.stack);\n}\n\nexport function createImportJobState(operation, state, queryImportJobState = false) {\n if (!(state in IMPORT_JOB_STATE)) {\n throw new Error('Invalid IMPORT_JOB_STATE');\n }\n\n if (!(operation in OPERATIONS)) {\n throw new Error('Invalid operation');\n }\n\n if (queryImportJobState) {\n const importJobStateWithOperation = `importJobState.${operation}`;\n const importJobStateForQuery = {[importJobStateWithOperation]: state};\n\n return importJobStateForQuery;\n }\n\n return {[operation]: state};\n}\n\nexport function createRecordResponseItem({responsePayload, responseStatus, recordMetadata, id}) {\n const recordResponseStatusAndMessage = getRecordResponseStatusAndMessage(responseStatus, responsePayload, id);\n const recordResponseItem = {\n databaseId: id || '000000000',\n recordMetadata: responsePayload.recordMetadata || recordMetadata || undefined,\n ...recordResponseStatusAndMessage\n };\n return recordResponseItem;\n}\n\n// eslint-disable-next-line max-statements\nfunction getRecordResponseStatusAndMessage(responseStatus, responsePayload, id) {\n\n logger.debug(`Response status: ${responseStatus} responsePayload: ${JSON.stringify(responsePayload)}`);\n const responseStatusName = httpStatus[`${responseStatus}_NAME`];\n logger.debug(`Response status name: ${responseStatusName}`);\n\n const message = getMessageFromResponsePayload(responsePayload);\n const {ids} = responsePayload || [];\n\n logger.debug(`Response message: ${message}`);\n\n // Non-http statuses\n if (['UPDATED', 'CREATED', 'INVALID', 'ERROR', 'SKIPPED'].includes(responseStatus)) {\n return {recordStatus: responseStatus, message};\n }\n\n if (['UNKNOWN'].includes(responseStatus)) {\n return {recordStatus: responseStatus, message, ids};\n }\n\n // Duplicates and other CONFLICT statuses\n if ([httpStatus.CONFLICT, 'CONFLICT'].includes(responseStatus)) {\n if ((/^Duplicates in database/u).test(message)) {\n return {recordStatus: 'DUPLICATE', message, ids};\n }\n if ((/^MatchValidation for all/u).test(message)) {\n return {recordStatus: 'CONFLICT', message, ids};\n }\n\n // Use ids only if there are more than one id or the id in payload does not match databaseId\n if (ids.length > 1 || ids[0] !== id) {\n return {recordStatus: 'CONFLICT', message, ids};\n }\n return {recordStatus: 'CONFLICT', message};\n }\n\n if ([httpStatus.UNPROCESSABLE_ENTITY, 'UNPROCESSABLE_ENTITY'].includes(responseStatus)) {\n if (ids.length > 1 || ids[0] !== id) {\n return {recordStatus: 'UNPROCESSABLE_ENTITY', message, ids};\n }\n return {recordStatus: 'UNPROCESSABLE_ENTITY', message};\n }\n\n if ([httpStatus.NOT_FOUND, 'NOT_FOUND'].includes(responseStatus)) {\n return {recordStatus: 'NOT_FOUND', message};\n }\n\n // Otherwise\n return {recordStatus: 'ERROR', message};\n}\n\nfunction getMessageFromResponsePayload(responsePayload) {\n if (responsePayload.message) {\n return responsePayload.message;\n }\n if (typeof responsePayload === 'string' || responsePayload instanceof String) {\n return responsePayload;\n }\n return '';\n}\n\nexport async function addRecordResponseItem({recordResponseItem, correlationId, mongoOperator}) {\n await mongoOperator.pushMessages({correlationId, messages: [recordResponseItem], messageField: 'records'});\n return true;\n}\n\n"],"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;AAOA,MAAMA,MAAM,GAAG,IAAAC,mCAAA,GAAf;;AAEO,SAASC,QAAT,CAAkBC,GAAlB,EAAuB;EAC5B,IAAIA,GAAG,YAAYC,qBAAnB,EAA6B;IAC3BJ,MAAM,CAACK,KAAP,CAAaC,IAAI,CAACC,SAAL,CAAeJ,GAAf,CAAb;IACA;EACD;;EAED,IAAIA,GAAG,KAAK,QAAZ,EAAsB;IACpBH,MAAM,CAACK,KAAP,CAAaF,GAAb;IACA;EACD;;EAEDH,MAAM,CAACK,KAAP,CAAaF,G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