@natlibfi/melinda-rest-api-commons 6.1.0-alpha.0 → 6.2.0-alpha.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/.github/workflows/melinda-node-tests-and-publish.yml +4 -2
- package/README.md +53 -5
- package/dist/amqp.d.ts +41 -0
- package/dist/amqp.d.ts.map +1 -0
- package/dist/amqp.js +237 -235
- package/dist/amqp.js.map +1 -7
- package/dist/constants.d.ts +49 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +38 -38
- package/dist/constants.js.map +1 -7
- package/dist/helpers/conversion.d.ts +8 -0
- package/dist/helpers/conversion.d.ts.map +1 -0
- package/dist/helpers/conversion.js +70 -64
- package/dist/helpers/conversion.js.map +1 -7
- package/dist/helpers/fix-constants.d.ts +76 -0
- package/dist/helpers/fix-constants.d.ts.map +1 -0
- package/dist/helpers/fix-constants.js +41 -41
- package/dist/helpers/fix-constants.js.map +1 -7
- package/dist/helpers/fix-handle-tempurns.d.ts +3 -0
- package/dist/helpers/fix-handle-tempurns.d.ts.map +1 -0
- package/dist/helpers/fix-handle-tempurns.js +73 -71
- package/dist/helpers/fix-handle-tempurns.js.map +1 -7
- package/dist/helpers/fix-strip-f884s.d.ts +10 -0
- package/dist/helpers/fix-strip-f884s.d.ts.map +1 -0
- package/dist/helpers/fix-strip-f884s.js +161 -163
- package/dist/helpers/fix-strip-f884s.js.map +1 -7
- package/dist/helpers/fix.d.ts +6 -0
- package/dist/helpers/fix.d.ts.map +1 -0
- package/dist/helpers/fix.js +133 -121
- package/dist/helpers/fix.js.map +1 -7
- package/dist/helpers/validation.d.ts +9 -0
- package/dist/helpers/validation.d.ts.map +1 -0
- package/dist/helpers/validation.js +14 -16
- package/dist/helpers/validation.js.map +1 -7
- package/dist/index.d.ts +13 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -7
- package/dist/mongo.d.ts +165 -0
- package/dist/mongo.d.ts.map +1 -0
- package/dist/mongo.js +595 -547
- package/dist/mongo.js.map +1 -7
- package/dist/mongoError.d.ts +2 -0
- package/dist/mongoError.d.ts.map +1 -0
- package/dist/mongoError.js +19 -0
- package/dist/mongoError.js.map +1 -0
- package/dist/mongoLog.d.ts +50 -0
- package/dist/mongoLog.d.ts.map +1 -0
- package/dist/mongoLog.js +274 -242
- package/dist/mongoLog.js.map +1 -7
- package/dist/utils.d.ts +36 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +112 -110
- package/dist/utils.js.map +1 -7
- package/eslint.config.ts +66 -0
- package/package.json +15 -11
- package/{tests/amqp-error-branches.test.js → test/amqp-error-branches.test.ts} +68 -52
- package/{tests/amqp.healthtest.js → test/amqp.healthtest.ts} +7 -3
- package/{tests/amqp.tests.localtest.js → test/amqp.tests.localtest.ts} +54 -35
- package/{tests/helpers/conversion.test.js → test/helpers/conversion.test.ts} +26 -14
- package/{tests/helpers/fix-edge.test.js → test/helpers/fix-edge.test.ts} +16 -12
- package/{tests/helpers/fix.test.js → test/helpers/fix.test.ts} +14 -8
- package/{tests/helpers/validation.test.js → test/helpers/validation.test.ts} +7 -6
- package/{tests/mongo-add-blob-size.test.js → test/mongo-add-blob-size.test.ts} +14 -15
- package/{tests/mongo-check-and-check-state.test.js → test/mongo-check-and-check-state.test.ts} +6 -8
- package/{tests/mongo-check-and-set-import-job-state.test.js → test/mongo-check-and-set-import-job-state.test.ts} +6 -9
- package/{tests/mongo-check-timeout.test.js → test/mongo-check-timeout.test.ts} +6 -9
- package/{tests/mongo-create-bulk.test.js → test/mongo-create-bulk.test.ts} +6 -7
- package/{tests/mongo-create-prio.test.js → test/mongo-create-prio.test.ts} +6 -6
- package/{tests/mongo-error-branches.test.js → test/mongo-error-branches.test.ts} +147 -51
- package/{tests/mongo-get-one.test.js → test/mongo-get-one.test.ts} +6 -6
- package/{tests/mongo-get-stream.test.js → test/mongo-get-stream.test.ts} +6 -8
- package/{tests/mongo-push-ids.test.js → test/mongo-push-ids.test.ts} +6 -6
- package/{tests/mongo-push-messages.test.js → test/mongo-push-messages.test.ts} +6 -6
- package/{tests/mongo-query-by-id.test.js → test/mongo-query-by-id.test.ts} +6 -9
- package/{tests/mongo-query.test.js → test/mongo-query.test.ts} +6 -7
- package/{tests/mongo-read-content.test.js → test/mongo-read-content.test.ts} +6 -9
- package/{tests/mongo-remove.test.js → test/mongo-remove.test.ts} +6 -8
- package/{tests/mongo-set-blob-size.test.js → test/mongo-set-blob-size.test.ts} +6 -6
- package/{tests/mongo-set-import-job-state.test.js → test/mongo-set-import-job-state.test.ts} +6 -6
- package/{tests/mongo-set-operation.test.js → test/mongo-set-operation.test.ts} +6 -6
- package/{tests/mongo-set-operations.test.js → test/mongo-set-operations.test.ts} +6 -6
- package/{tests/mongo-set-state.test.js → test/mongo-set-state.test.ts} +6 -6
- package/{tests/mongoLog-protect.test.js → test/mongoLog-protect.test.ts} +18 -17
- package/{tests/mongoLog.test.js → test/mongoLog.test.ts} +8 -8
- package/{tests/testUtils.js → test/testUtils.ts} +49 -15
- package/{tests/utils.test.js → test/utils.test.ts} +5 -5
- package/tsconfig.json +62 -0
- package/tsconfig.test.json +17 -0
- package/dist/helpers/fix-utils.js +0 -39
- package/dist/helpers/fix-utils.js.map +0 -7
- package/eslint.config.js +0 -53
package/dist/mongo.js
CHANGED
|
@@ -1,577 +1,625 @@
|
|
|
1
|
-
import { MongoClient, GridFSBucket, MongoDriverError } from
|
|
2
|
-
import httpStatus from
|
|
3
|
-
import createDebugLogger from
|
|
4
|
-
import { createLogger, Error as ApiError, parseBoolean } from
|
|
1
|
+
import { MongoClient, GridFSBucket, MongoDriverError } from 'mongodb';
|
|
2
|
+
import httpStatus from 'http-status';
|
|
3
|
+
import createDebugLogger from 'debug';
|
|
4
|
+
import { createLogger, Error as ApiError, parseBoolean } from '@natlibfi/melinda-commons';
|
|
5
5
|
import { QUEUE_ITEM_STATE, IMPORT_JOB_STATE, OPERATIONS } from "./constants.js";
|
|
6
6
|
import { logError, assertString, assertNumber, assertImportJobStateFilter } from "./utils.js";
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
debugDev(`mongo: ${MONGO_URI}, db: ${db}, collection: ${collection}`);
|
|
23
|
-
return { createPrio, createBulk, checkAndSetState, checkAndSetImportJobState, query, createProjection, queryById, checkTimeOut, remove, readContent, removeContent, getOne, getStream, setState, setImportJobState, pushIds, pushMessages, setOperation, setOperations, addBlobSize, setBlobSize };
|
|
24
|
-
async function createPrio({ correlationId, cataloger, oCatalogerIn, operation, operationSettings }) {
|
|
25
|
-
debugDev("createPrio");
|
|
26
|
-
const time = /* @__PURE__ */ new Date();
|
|
27
|
-
const newQueueItem = {
|
|
28
|
-
correlationId,
|
|
29
|
-
cataloger,
|
|
30
|
-
operation,
|
|
31
|
-
operations: [operation],
|
|
32
|
-
oCatalogerIn,
|
|
33
|
-
operationSettings: {
|
|
34
|
-
prio: true,
|
|
35
|
-
...operationSettings
|
|
36
|
-
},
|
|
37
|
-
queueItemState: QUEUE_ITEM_STATE.VALIDATOR.PENDING_VALIDATION,
|
|
38
|
-
blobSize: 1,
|
|
39
|
-
importJobState: {
|
|
40
|
-
CREATE: IMPORT_JOB_STATE.EMPTY,
|
|
41
|
-
UPDATE: IMPORT_JOB_STATE.EMPTY,
|
|
42
|
-
FIX: IMPORT_JOB_STATE.EMPTY
|
|
43
|
-
},
|
|
44
|
-
records: [],
|
|
45
|
-
errorMessage: "",
|
|
46
|
-
errorStatus: "",
|
|
47
|
-
creationTime: time,
|
|
48
|
-
modificationTime: time
|
|
49
|
-
};
|
|
50
|
-
try {
|
|
51
|
-
debugDev(`Inserting: ${JSON.stringify(newQueueItem)} to ${collection}}`);
|
|
52
|
-
const result = await operator.insertOne(newQueueItem);
|
|
53
|
-
debugDev(JSON.stringify(result));
|
|
54
|
-
if (result.acknowledged) {
|
|
55
|
-
logger.info(`New PRIO queue item for ${operation} ${correlationId} has been made in ${collection}`);
|
|
56
|
-
return;
|
|
57
|
-
}
|
|
58
|
-
throw new ApiError(httpStatus.INTERNAL_SERVER_ERROR);
|
|
59
|
-
} catch (error) {
|
|
60
|
-
const errorMessage = error.payload || error.message || "";
|
|
61
|
-
logError(error);
|
|
62
|
-
throw new ApiError(httpStatus.INTERNAL_SERVER_ERROR, `Mongo errored: ${errorMessage}`);
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
async function createBulk({ correlationId, cataloger, oCatalogerIn, operation, contentType, recordLoadParams, stream, operationSettings }) {
|
|
66
|
-
const time = /* @__PURE__ */ new Date();
|
|
67
|
-
const newQueueItem = {
|
|
68
|
-
correlationId,
|
|
69
|
-
cataloger,
|
|
70
|
-
oCatalogerIn,
|
|
71
|
-
operation,
|
|
72
|
-
operations: [operation],
|
|
73
|
-
operationSettings: {
|
|
74
|
-
prio: false,
|
|
75
|
-
...operationSettings
|
|
76
|
-
},
|
|
77
|
-
contentType,
|
|
78
|
-
recordLoadParams,
|
|
79
|
-
queueItemState: stream ? QUEUE_ITEM_STATE.VALIDATOR.UPLOADING : QUEUE_ITEM_STATE.VALIDATOR.WAITING_FOR_RECORDS,
|
|
80
|
-
blobSize: 0,
|
|
81
|
-
importJobState: {
|
|
82
|
-
CREATE: IMPORT_JOB_STATE.EMPTY,
|
|
83
|
-
UPDATE: IMPORT_JOB_STATE.EMPTY,
|
|
84
|
-
FIX: IMPORT_JOB_STATE.EMPTY
|
|
85
|
-
},
|
|
86
|
-
records: [],
|
|
87
|
-
errorMessage: "",
|
|
88
|
-
errorStatus: "",
|
|
89
|
-
creationTime: time,
|
|
90
|
-
modificationTime: time
|
|
91
|
-
};
|
|
92
|
-
if (stream) {
|
|
93
|
-
debug(`Creating streamBulk`);
|
|
94
|
-
try {
|
|
95
|
-
await new Promise((resolve, reject) => {
|
|
96
|
-
const outputStream = gridFSBucket.openUploadStream(correlationId);
|
|
97
|
-
outputStream.on("error", (error) => {
|
|
98
|
-
outputStream.destroy();
|
|
99
|
-
reject(error);
|
|
100
|
-
});
|
|
101
|
-
stream.on("error", (error) => {
|
|
102
|
-
outputStream.destroy();
|
|
103
|
-
reject(error);
|
|
104
|
-
}).on("data", (chunk) => outputStream.write(chunk)).on("end", () => outputStream.end(void 0, void 0, () => {
|
|
105
|
-
resolve(correlationId);
|
|
106
|
-
}));
|
|
107
|
-
});
|
|
108
|
-
logger.info(`New BULK queue item for ${operation} ${correlationId} has been made in ${collection}!`);
|
|
109
|
-
debug(`New BULK queue item for ${operation} ${correlationId} has been made in ${collection}!`);
|
|
110
|
-
const result = await operator.insertOne(newQueueItem);
|
|
111
|
-
if (!result.acknowledged) {
|
|
112
|
-
throw new ApiError(httpStatus.INTERNAL_SERVER_ERROR);
|
|
113
|
-
}
|
|
114
|
-
return { correlationId, acknowledged: result.acknowledged, queueItemState: newQueueItem.queueItemState };
|
|
115
|
-
} catch (error) {
|
|
116
|
-
const errorMessage = error.payload || error.message || "";
|
|
117
|
-
logError(error);
|
|
118
|
-
await removePartialContent(correlationId);
|
|
119
|
-
throw new ApiError(httpStatus.INTERNAL_SERVER_ERROR, `Mongo errored: ${errorMessage}`);
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
async function removePartialContent(correlationId2) {
|
|
123
|
-
try {
|
|
124
|
-
const files = await gridFSBucket.find({ filename: correlationId2 }).toArray();
|
|
125
|
-
for (const file of files) {
|
|
126
|
-
await gridFSBucket.delete(file._id);
|
|
127
|
-
logger.debug(`mongo/createBulk: removed partial GridFS file for ${correlationId2}`);
|
|
128
|
-
}
|
|
129
|
-
} catch (cleanupError) {
|
|
130
|
-
logger.debug(`mongo/createBulk: partial GridFS cleanup failed for ${correlationId2}: ${cleanupError.message}`);
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
logger.debug(`No stream`);
|
|
134
|
-
debug(`Creating noStreamBulk`);
|
|
135
|
-
if (!stream) {
|
|
136
|
-
try {
|
|
137
|
-
const result = await operator.insertOne(newQueueItem);
|
|
138
|
-
if (result.acknowledged) {
|
|
139
|
-
logger.info(`New noStream BULK queue item for ${operation} ${correlationId} has been made in ${collection}`);
|
|
140
|
-
debug(`New noStream BULK queue item for ${operation} ${correlationId} has been made in ${collection}`);
|
|
141
|
-
return { correlationId, acknowledged: result.acknowledged, queueItemState: QUEUE_ITEM_STATE.VALIDATOR.WAITING_FOR_RECORDS };
|
|
142
|
-
}
|
|
143
|
-
throw new ApiError(httpStatus.INTERNAL_SERVER_ERROR);
|
|
144
|
-
} catch (error) {
|
|
145
|
-
const errorMessage = error.payload || error.message || "";
|
|
146
|
-
logError(error);
|
|
147
|
-
throw new ApiError(httpStatus.INTERNAL_SERVER_ERROR, `Mongo errored: ${errorMessage}`);
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
async function checkAndSetState({ correlationId, state, errorMessage = void 0, errorStatus = void 0 }) {
|
|
152
|
-
assertString(correlationId, "correlationId");
|
|
153
|
-
const timeOut = await checkTimeOut({ correlationId });
|
|
154
|
-
if (timeOut) {
|
|
155
|
-
const queueItem2 = await setState({ correlationId, state, errorMessage, errorStatus });
|
|
156
|
-
return { status: "updated", queueItem: queueItem2 };
|
|
157
|
-
}
|
|
158
|
-
const queueItem = await operator.findOne({ correlationId }, { projection: { _id: 0 } });
|
|
159
|
-
const status = queueItem && queueItem.queueItemState === QUEUE_ITEM_STATE.ABORT ? "timeout" : "unchanged";
|
|
160
|
-
return { status, queueItem };
|
|
161
|
-
}
|
|
162
|
-
async function checkAndSetImportJobState({ correlationId, operation, importJobState }) {
|
|
163
|
-
assertString(correlationId, "correlationId");
|
|
164
|
-
logger.debug(`${correlationId}, ${importJobState}, ${operation}`);
|
|
165
|
-
const timeOut = await checkTimeOut({ correlationId, operation, importJobState });
|
|
166
|
-
if (timeOut) {
|
|
167
|
-
const queueItem2 = await setImportJobState({ correlationId, operation, importJobState });
|
|
168
|
-
return { status: "updated", queueItem: queueItem2 };
|
|
7
|
+
import { withMongoError } from "./mongoError.js";
|
|
8
|
+
export default async function (MONGO_URI, collection, db = 'rest-api', deps = {}) {
|
|
9
|
+
const logger = createLogger();
|
|
10
|
+
const timeoutMinutes = Number.parseInt(process.env['MONGO_TIMEOUT_MINUTES'], 10) || 1;
|
|
11
|
+
logger.debug(`checkTimeOut window: ${timeoutMinutes} minute(s) (override with MONGO_TIMEOUT_MINUTES)`);
|
|
12
|
+
const debug = createDebugLogger('@natlibfi/melinda-rest-api-commons/mongo');
|
|
13
|
+
const debugDev = debug.extend(':dev');
|
|
14
|
+
const stubbed = Boolean(deps.operator) && Boolean(deps.gridFSBucket);
|
|
15
|
+
let gridFSBucket = deps.gridFSBucket;
|
|
16
|
+
let operator = deps.operator;
|
|
17
|
+
if (!stubbed) {
|
|
18
|
+
const client = await MongoClient.connect(MONGO_URI);
|
|
19
|
+
const realDb = client.db(db);
|
|
20
|
+
gridFSBucket = gridFSBucket ?? new GridFSBucket(realDb, { bucketName: collection });
|
|
21
|
+
operator = operator ?? realDb.collection(collection);
|
|
169
22
|
}
|
|
170
|
-
|
|
171
|
-
const status = queueItem && queueItem.queueItemState === QUEUE_ITEM_STATE.ABORT ? "timeout" : "unchanged";
|
|
172
|
-
return { status, queueItem };
|
|
173
|
-
}
|
|
174
|
-
async function query(params, showParams = {}) {
|
|
175
|
-
logger.debug(`Querying: ${JSON.stringify(params)}, ${JSON.stringify(showParams)}`);
|
|
176
|
-
debug(`Querying: ${JSON.stringify(params)}, ${JSON.stringify(showParams)}`);
|
|
177
|
-
const { limit = 1e3, skip = 0, ...rest } = params;
|
|
178
|
-
const result = await operator.find(rest, { projection: createProjection(showParams) }).limit(parseInt(limit, 10)).skip(parseInt(skip, 10)).toArray();
|
|
179
|
-
logger.debug(`Query result: ${result.length > 0 ? "Found!" : "Not found!"}`);
|
|
180
|
-
logger.silly(`${JSON.stringify(result)}`);
|
|
181
|
-
debug(`${JSON.stringify(result)}`);
|
|
182
|
-
return result;
|
|
183
|
-
}
|
|
184
|
-
function createProjection(showParams = {}) {
|
|
185
|
-
logger.silly(`Creating projection for query: ${JSON.stringify(showParams)}`);
|
|
186
|
-
debug(`Creating projection for query: ${JSON.stringify(showParams)}`);
|
|
187
|
-
const { showAll = 0, showOperations = 0, showOperationSettings = 0, showRecordLoadParams = 0, showImportJobState = 0 } = showParams;
|
|
188
|
-
const combinedShowParams = {
|
|
189
|
-
showAll,
|
|
190
|
-
showOperations,
|
|
191
|
-
showOperationSettings,
|
|
192
|
-
showRecordLoadParams,
|
|
193
|
-
showImportJobState
|
|
194
|
-
};
|
|
195
|
-
logger.debug(`showAll: ${showAll}, showOperations: ${showOperations}, showOperationSettings: ${showOperationSettings}, showRecordLoadParams: ${showRecordLoadParams}, showImportJobState: ${showImportJobState}`);
|
|
196
|
-
debug(`showAll: ${showAll}, showOperations: ${showOperations}, showOperationSettings: ${showOperationSettings}, showRecordLoadParams: ${showRecordLoadParams}, showImportJobState: ${showImportJobState}`);
|
|
197
|
-
debug(`${JSON.stringify(combinedShowParams)}`);
|
|
198
|
-
if (parseBoolean(showAll)) {
|
|
199
|
-
debug(`ShowAll: ${showAll}`);
|
|
200
|
-
return {
|
|
201
|
-
_id: 0
|
|
202
|
-
};
|
|
203
|
-
}
|
|
204
|
-
const showParamToField = {
|
|
205
|
-
"showOperations": "operations",
|
|
206
|
-
"showOperationSettings": "operationSettings",
|
|
207
|
-
"showRecordLoadParams": "recordLoadParams",
|
|
208
|
-
"showImportJobState": "importJobState"
|
|
209
|
-
};
|
|
210
|
-
const result = Object.keys(combinedShowParams).filter((param) => param !== "showAll" && !parseBoolean(combinedShowParams[param])).filter((param) => showParamToField[param]).map((param) => showParamToField[param]);
|
|
211
|
-
logger.silly(`We want to exclude from projection: ${JSON.stringify(result)}`);
|
|
212
|
-
debug(`We want to exclude from projection: ${JSON.stringify(result)}`);
|
|
213
|
-
const excludeObject = Object.fromEntries(result.map((param) => [param, 0]));
|
|
214
|
-
logger.silly(`We want to exclude from projection: ${JSON.stringify(excludeObject)}`);
|
|
215
|
-
debug(`We want to exclude from projection: ${JSON.stringify(excludeObject)}`);
|
|
23
|
+
debugDev(`mongo: ${MONGO_URI}, db: ${db}, collection: ${collection}`);
|
|
216
24
|
return {
|
|
217
|
-
|
|
218
|
-
|
|
25
|
+
createPrio,
|
|
26
|
+
createBulk,
|
|
27
|
+
checkAndSetState: (params) => withMongoError(() => checkAndSetState(params)),
|
|
28
|
+
checkAndSetImportJobState: (params) => withMongoError(() => checkAndSetImportJobState(params)),
|
|
29
|
+
query: (params, showParams) => withMongoError(() => query(params, showParams)),
|
|
30
|
+
createProjection,
|
|
31
|
+
queryById: (params) => withMongoError(() => queryById(params)),
|
|
32
|
+
checkTimeOut: (params) => withMongoError(() => checkTimeOut(params)),
|
|
33
|
+
remove,
|
|
34
|
+
readContent: (correlationId) => withMongoError(() => readContent(correlationId)),
|
|
35
|
+
removeContent: (params) => withMongoError(() => removeContent(params)),
|
|
36
|
+
getOne,
|
|
37
|
+
getStream,
|
|
38
|
+
setState: (params) => withMongoError(() => setState(params)),
|
|
39
|
+
setImportJobState: (params) => withMongoError(() => setImportJobState(params)),
|
|
40
|
+
pushIds,
|
|
41
|
+
pushMessages,
|
|
42
|
+
setOperation: (params) => withMongoError(() => setOperation(params)),
|
|
43
|
+
setOperations: (params) => withMongoError(() => setOperations(params)),
|
|
44
|
+
addBlobSize: (params) => withMongoError(() => addBlobSize(params)),
|
|
45
|
+
setBlobSize: (params) => withMongoError(() => setBlobSize(params))
|
|
219
46
|
};
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
47
|
+
async function createPrio({ correlationId, cataloger, oCatalogerIn, operation, operationSettings }) {
|
|
48
|
+
debugDev('createPrio');
|
|
49
|
+
const time = new Date();
|
|
50
|
+
const newQueueItem = {
|
|
51
|
+
correlationId,
|
|
52
|
+
cataloger,
|
|
53
|
+
operation,
|
|
54
|
+
operations: [operation],
|
|
55
|
+
oCatalogerIn,
|
|
56
|
+
operationSettings: {
|
|
57
|
+
prio: true,
|
|
58
|
+
...operationSettings
|
|
59
|
+
},
|
|
60
|
+
queueItemState: QUEUE_ITEM_STATE.VALIDATOR.PENDING_VALIDATION,
|
|
61
|
+
blobSize: 1,
|
|
62
|
+
importJobState: {
|
|
63
|
+
CREATE: IMPORT_JOB_STATE.EMPTY,
|
|
64
|
+
UPDATE: IMPORT_JOB_STATE.EMPTY,
|
|
65
|
+
FIX: IMPORT_JOB_STATE.EMPTY
|
|
66
|
+
},
|
|
67
|
+
records: [],
|
|
68
|
+
errorMessage: '',
|
|
69
|
+
errorStatus: '',
|
|
70
|
+
creationTime: time,
|
|
71
|
+
modificationTime: time
|
|
72
|
+
};
|
|
73
|
+
try {
|
|
74
|
+
debugDev(`Inserting: ${JSON.stringify(newQueueItem)} to ${collection}}`);
|
|
75
|
+
const result = await operator.insertOne(newQueueItem);
|
|
76
|
+
debugDev(JSON.stringify(result));
|
|
77
|
+
if (result.acknowledged) {
|
|
78
|
+
logger.info(`New PRIO queue item for ${operation} ${correlationId} has been made in ${collection}`);
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
throw new ApiError(httpStatus.INTERNAL_SERVER_ERROR, undefined);
|
|
82
|
+
}
|
|
83
|
+
catch (error) {
|
|
84
|
+
const apiError = error;
|
|
85
|
+
const errorMessage = apiError.payload || apiError.message || '';
|
|
86
|
+
logError(error);
|
|
87
|
+
throw new ApiError(httpStatus.INTERNAL_SERVER_ERROR, `Mongo errored: ${errorMessage}`);
|
|
88
|
+
}
|
|
227
89
|
}
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
90
|
+
async function createBulk({ correlationId, cataloger, oCatalogerIn, operation, contentType, recordLoadParams, stream, operationSettings }) {
|
|
91
|
+
const time = new Date();
|
|
92
|
+
const newQueueItem = {
|
|
93
|
+
correlationId,
|
|
94
|
+
cataloger,
|
|
95
|
+
oCatalogerIn,
|
|
96
|
+
operation,
|
|
97
|
+
operations: [operation],
|
|
98
|
+
operationSettings: {
|
|
99
|
+
prio: false,
|
|
100
|
+
...operationSettings
|
|
101
|
+
},
|
|
102
|
+
contentType,
|
|
103
|
+
recordLoadParams,
|
|
104
|
+
queueItemState: stream ? QUEUE_ITEM_STATE.VALIDATOR.UPLOADING : QUEUE_ITEM_STATE.VALIDATOR.WAITING_FOR_RECORDS,
|
|
105
|
+
blobSize: 0,
|
|
106
|
+
importJobState: {
|
|
107
|
+
CREATE: IMPORT_JOB_STATE.EMPTY,
|
|
108
|
+
UPDATE: IMPORT_JOB_STATE.EMPTY,
|
|
109
|
+
FIX: IMPORT_JOB_STATE.EMPTY
|
|
110
|
+
},
|
|
111
|
+
records: [],
|
|
112
|
+
errorMessage: '',
|
|
113
|
+
errorStatus: '',
|
|
114
|
+
creationTime: time,
|
|
115
|
+
modificationTime: time
|
|
116
|
+
};
|
|
117
|
+
if (stream) {
|
|
118
|
+
debug(`Creating streamBulk`);
|
|
119
|
+
try {
|
|
120
|
+
await new Promise((resolve, reject) => {
|
|
121
|
+
const outputStream = gridFSBucket.openUploadStream(correlationId);
|
|
122
|
+
outputStream.on('error', (error) => {
|
|
123
|
+
outputStream.destroy();
|
|
124
|
+
reject(error);
|
|
125
|
+
});
|
|
126
|
+
stream
|
|
127
|
+
.on('error', (error) => {
|
|
128
|
+
outputStream.destroy();
|
|
129
|
+
reject(error);
|
|
130
|
+
})
|
|
131
|
+
.on('data', chunk => outputStream.write(chunk))
|
|
132
|
+
.on('end', () => outputStream.end(undefined, () => {
|
|
133
|
+
resolve(correlationId);
|
|
134
|
+
}));
|
|
135
|
+
});
|
|
136
|
+
logger.info(`New BULK queue item for ${operation} ${correlationId} has been made in ${collection}!`);
|
|
137
|
+
debug(`New BULK queue item for ${operation} ${correlationId} has been made in ${collection}!`);
|
|
138
|
+
const result = await operator.insertOne(newQueueItem);
|
|
139
|
+
if (!result.acknowledged) {
|
|
140
|
+
throw new ApiError(httpStatus.INTERNAL_SERVER_ERROR, undefined);
|
|
141
|
+
}
|
|
142
|
+
return { correlationId, acknowledged: result.acknowledged, queueItemState: newQueueItem.queueItemState };
|
|
143
|
+
}
|
|
144
|
+
catch (error) {
|
|
145
|
+
const apiError = error;
|
|
146
|
+
const errorMessage = apiError.payload || apiError.message || '';
|
|
147
|
+
logError(error);
|
|
148
|
+
await removePartialContent(correlationId);
|
|
149
|
+
throw new ApiError(httpStatus.INTERNAL_SERVER_ERROR, `Mongo errored: ${errorMessage}`);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
async function removePartialContent(correlationId) {
|
|
153
|
+
try {
|
|
154
|
+
const files = await gridFSBucket.find({ filename: correlationId }).toArray();
|
|
155
|
+
for (const file of files) {
|
|
156
|
+
await gridFSBucket.delete(file._id);
|
|
157
|
+
logger.debug(`mongo/createBulk: removed partial GridFS file for ${correlationId}`);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
catch (cleanupError) {
|
|
161
|
+
const apiError = cleanupError;
|
|
162
|
+
logger.debug(`mongo/createBulk: partial GridFS cleanup failed for ${correlationId}: ${apiError.message}`);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
logger.debug(`No stream`);
|
|
166
|
+
debug(`Creating noStreamBulk`);
|
|
167
|
+
if (!stream) {
|
|
168
|
+
try {
|
|
169
|
+
const result = await operator.insertOne(newQueueItem);
|
|
170
|
+
if (result.acknowledged) {
|
|
171
|
+
logger.info(`New noStream BULK queue item for ${operation} ${correlationId} has been made in ${collection}`);
|
|
172
|
+
debug(`New noStream BULK queue item for ${operation} ${correlationId} has been made in ${collection}`);
|
|
173
|
+
return { correlationId, acknowledged: result.acknowledged, queueItemState: QUEUE_ITEM_STATE.VALIDATOR.WAITING_FOR_RECORDS };
|
|
174
|
+
}
|
|
175
|
+
throw new ApiError(httpStatus.INTERNAL_SERVER_ERROR, undefined);
|
|
176
|
+
}
|
|
177
|
+
catch (error) {
|
|
178
|
+
const apiError = error;
|
|
179
|
+
const errorMessage = apiError.payload || apiError.message || '';
|
|
180
|
+
logError(error);
|
|
181
|
+
throw new ApiError(httpStatus.INTERNAL_SERVER_ERROR, `Mongo errored: ${errorMessage}`);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
return undefined;
|
|
237
185
|
}
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
186
|
+
async function checkAndSetState({ correlationId, state, errorMessage = undefined, errorStatus = undefined }) {
|
|
187
|
+
assertString(correlationId, 'correlationId');
|
|
188
|
+
const timeOut = await checkTimeOut({ correlationId });
|
|
189
|
+
if (timeOut) {
|
|
190
|
+
const queueItem = await setState({ correlationId, state, errorMessage, errorStatus });
|
|
191
|
+
return { status: 'updated', queueItem };
|
|
192
|
+
}
|
|
193
|
+
const queueItem = await operator.findOne({ correlationId }, { projection: { _id: 0 } });
|
|
194
|
+
const status = queueItem && queueItem.queueItemState === QUEUE_ITEM_STATE.ABORT ? 'timeout' : 'unchanged';
|
|
195
|
+
return { status, queueItem };
|
|
242
196
|
}
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
await
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
await setImportJobState({ correlationId, operation: "FIX", importJobState: IMPORT_JOB_STATE.ABORT });
|
|
255
|
-
}
|
|
256
|
-
await setState({ correlationId, state: QUEUE_ITEM_STATE.ABORT, errorStatus: httpStatus.REQUEST_TIMEOUT, errorMessage: `Timeout in ${oldState}` });
|
|
257
|
-
return false;
|
|
197
|
+
async function checkAndSetImportJobState({ correlationId, operation, importJobState }) {
|
|
198
|
+
assertString(correlationId, 'correlationId');
|
|
199
|
+
logger.debug(`${correlationId}, ${importJobState}, ${operation}`);
|
|
200
|
+
const timeOut = await checkTimeOut({ correlationId, operation, importJobState });
|
|
201
|
+
if (timeOut) {
|
|
202
|
+
const queueItem = await setImportJobState({ correlationId, operation, importJobState });
|
|
203
|
+
return { status: 'updated', queueItem };
|
|
204
|
+
}
|
|
205
|
+
const queueItem = await operator.findOne({ correlationId }, { projection: { _id: 0 } });
|
|
206
|
+
const status = queueItem && queueItem.queueItemState === QUEUE_ITEM_STATE.ABORT ? 'timeout' : 'unchanged';
|
|
207
|
+
return { status, queueItem };
|
|
258
208
|
}
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
debug(`deleteResult: ${JSON.stringify(deleteResult)}`);
|
|
272
|
-
return { removed: deleteResult.deletedCount > 0, deletedContent: removedContent };
|
|
273
|
-
} catch (err) {
|
|
274
|
-
if (err instanceof MongoDriverError) {
|
|
275
|
-
debug(err);
|
|
276
|
-
if (err.message.indexOf("File not found for id") !== -1) {
|
|
277
|
-
logger.silly(`mongo/remove: File not found, removing queueItem ${JSON.stringify(params.correlationId)} from ${collection}`);
|
|
278
|
-
debug(`mongo/remove: File not found, removing queueItem ${JSON.stringify(params.correlationId)} from ${collection}`);
|
|
279
|
-
const deleteResult = await operator.deleteOne({ correlationId: params.correlationId });
|
|
280
|
-
return { removed: deleteResult.deletedCount > 0, deletedContent: false };
|
|
281
|
-
}
|
|
282
|
-
logger.error(err.message);
|
|
283
|
-
throw new ApiError(httpStatus.INTERNAL_SERVER_ERROR, err.message);
|
|
284
|
-
}
|
|
285
|
-
throw new ApiError(httpStatus.INTERNAL_SERVER_ERROR, err.message);
|
|
209
|
+
async function query(params, showParams = {}) {
|
|
210
|
+
logger.debug(`Querying: ${JSON.stringify(params)}, ${JSON.stringify(showParams)}`);
|
|
211
|
+
debug(`Querying: ${JSON.stringify(params)}, ${JSON.stringify(showParams)}`);
|
|
212
|
+
const { limit = 1000, skip = 0, ...rest } = params;
|
|
213
|
+
const result = await operator.find(rest, { projection: createProjection(showParams) })
|
|
214
|
+
.limit(parseInt(String(limit), 10))
|
|
215
|
+
.skip(parseInt(String(skip), 10))
|
|
216
|
+
.toArray();
|
|
217
|
+
logger.debug(`Query result: ${result.length > 0 ? 'Found!' : 'Not found!'}`);
|
|
218
|
+
logger.silly(`${JSON.stringify(result)}`);
|
|
219
|
+
debug(`${JSON.stringify(result)}`);
|
|
220
|
+
return result;
|
|
286
221
|
}
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
222
|
+
function createProjection(showParams = {}) {
|
|
223
|
+
logger.silly(`Creating projection for query: ${JSON.stringify(showParams)}`);
|
|
224
|
+
debug(`Creating projection for query: ${JSON.stringify(showParams)}`);
|
|
225
|
+
const { showAll = 0, showOperations = 0, showOperationSettings = 0, showRecordLoadParams = 0, showImportJobState = 0 } = showParams;
|
|
226
|
+
const combinedShowParams = {
|
|
227
|
+
showAll,
|
|
228
|
+
showOperations,
|
|
229
|
+
showOperationSettings,
|
|
230
|
+
showRecordLoadParams,
|
|
231
|
+
showImportJobState
|
|
232
|
+
};
|
|
233
|
+
logger.debug(`showAll: ${showAll}, showOperations: ${showOperations}, showOperationSettings: ${showOperationSettings}, showRecordLoadParams: ${showRecordLoadParams}, showImportJobState: ${showImportJobState}`);
|
|
234
|
+
debug(`showAll: ${showAll}, showOperations: ${showOperations}, showOperationSettings: ${showOperationSettings}, showRecordLoadParams: ${showRecordLoadParams}, showImportJobState: ${showImportJobState}`);
|
|
235
|
+
debug(`${JSON.stringify(combinedShowParams)}`);
|
|
236
|
+
if (parseBoolean(showAll)) {
|
|
237
|
+
debug(`ShowAll: ${showAll}`);
|
|
238
|
+
return {
|
|
239
|
+
_id: 0
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
const showParamToField = {
|
|
243
|
+
'showOperations': 'operations',
|
|
244
|
+
'showOperationSettings': 'operationSettings',
|
|
245
|
+
'showRecordLoadParams': 'recordLoadParams',
|
|
246
|
+
'showImportJobState': 'importJobState'
|
|
247
|
+
};
|
|
248
|
+
const result = Object.keys(combinedShowParams)
|
|
249
|
+
.filter(param => param !== 'showAll' && !parseBoolean(combinedShowParams[param]))
|
|
250
|
+
.filter((param) => param in showParamToField)
|
|
251
|
+
.map(param => showParamToField[param]);
|
|
252
|
+
logger.silly(`We want to exclude from projection: ${JSON.stringify(result)}`);
|
|
253
|
+
debug(`We want to exclude from projection: ${JSON.stringify(result)}`);
|
|
254
|
+
const excludeObject = Object.fromEntries(result.map(param => [param, 0]));
|
|
255
|
+
logger.silly(`We want to exclude from projection: ${JSON.stringify(excludeObject)}`);
|
|
256
|
+
debug(`We want to exclude from projection: ${JSON.stringify(excludeObject)}`);
|
|
302
257
|
return {
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
// 'error' event on the returned stream (e.g. file deleted
|
|
306
|
-
// concurrently) is an unhandled event for the caller -> the
|
|
307
|
-
// contract is: the caller MUST attach an 'error' handler.
|
|
308
|
-
readStream: gridFSBucket.openDownloadStreamByName(correlationId)
|
|
258
|
+
_id: 0,
|
|
259
|
+
...excludeObject
|
|
309
260
|
};
|
|
310
|
-
}
|
|
311
|
-
logger.debug(`OperationSettings for ${correlationId}: ${JSON.stringify(operationSettings)}`);
|
|
312
|
-
debug(`OperationSettings for ${correlationId}: ${JSON.stringify(operationSettings)}`);
|
|
313
|
-
debug(`Content is only available for streamBulk jobs. ${correlationId} is not a streamBulk job.`);
|
|
314
|
-
throw new ApiError(httpStatus.BAD_REQUEST, { message: `Content is only available for streamBulk jobs. ${correlationId} is not a streamBulk job.` });
|
|
315
261
|
}
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
262
|
+
async function queryById({ correlationId, checkModTime = false }) {
|
|
263
|
+
assertString(correlationId, 'correlationId');
|
|
264
|
+
if (checkModTime) {
|
|
265
|
+
debug(`queryById checkModTime: ${checkModTime}`);
|
|
266
|
+
await checkTimeOut({ correlationId });
|
|
267
|
+
return queryById({ correlationId });
|
|
268
|
+
}
|
|
269
|
+
const result = await operator.findOne({ correlationId }, { projection: { _id: 0 } });
|
|
270
|
+
return result;
|
|
271
|
+
}
|
|
272
|
+
async function checkTimeOut({ correlationId }) {
|
|
273
|
+
assertString(correlationId, 'correlationId');
|
|
274
|
+
const queueItem = await operator.findOne({ correlationId });
|
|
275
|
+
if (!queueItem) {
|
|
276
|
+
logger.silly(`${correlationId} not found in ${collection} - not a timeout`);
|
|
277
|
+
return false;
|
|
278
|
+
}
|
|
279
|
+
const { modificationTime, queueItemState: oldState, importJobState = { CREATE: IMPORT_JOB_STATE.EMPTY, UPDATE: IMPORT_JOB_STATE.EMPTY, FIX: IMPORT_JOB_STATE.EMPTY } } = queueItem;
|
|
280
|
+
if (oldState === QUEUE_ITEM_STATE.ABORT || oldState === QUEUE_ITEM_STATE.ERROR || oldState === QUEUE_ITEM_STATE.DONE) {
|
|
281
|
+
logger.silly(`${correlationId} has already state: ${oldState}`);
|
|
282
|
+
return false;
|
|
283
|
+
}
|
|
284
|
+
const timeoutTime = new Date(new Date(modificationTime).getTime() + timeoutMinutes * 60000);
|
|
285
|
+
logger.silly(`${correlationId} timeOut @ ${timeoutTime}`);
|
|
286
|
+
if (timeoutTime < new Date()) {
|
|
287
|
+
const finalImportJobStates = [IMPORT_JOB_STATE.ABORT, IMPORT_JOB_STATE.DONE, IMPORT_JOB_STATE.EMPTY, IMPORT_JOB_STATE.ERROR];
|
|
288
|
+
if (!finalImportJobStates.includes(importJobState.CREATE)) {
|
|
289
|
+
await setImportJobState({ correlationId, operation: 'CREATE', importJobState: IMPORT_JOB_STATE.ABORT });
|
|
290
|
+
}
|
|
291
|
+
if (!finalImportJobStates.includes(importJobState.UPDATE)) {
|
|
292
|
+
await setImportJobState({ correlationId, operation: 'UPDATE', importJobState: IMPORT_JOB_STATE.ABORT });
|
|
293
|
+
}
|
|
294
|
+
if (!finalImportJobStates.includes(importJobState.FIX)) {
|
|
295
|
+
await setImportJobState({ correlationId, operation: 'FIX', importJobState: IMPORT_JOB_STATE.ABORT });
|
|
296
|
+
}
|
|
297
|
+
await setState({ correlationId, state: QUEUE_ITEM_STATE.ABORT, errorStatus: httpStatus.REQUEST_TIMEOUT, errorMessage: `Timeout in ${oldState}` });
|
|
298
|
+
return false;
|
|
299
|
+
}
|
|
300
|
+
return true;
|
|
329
301
|
}
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
302
|
+
async function remove(params) {
|
|
303
|
+
logger.silly(`${JSON.stringify(params)}`);
|
|
304
|
+
logger.info(`Removing from Mongo (${collection}) id: ${params.correlationId}`);
|
|
305
|
+
debug(`${JSON.stringify(params)}`);
|
|
306
|
+
debug(`Removing from Mongo (${collection}) id: ${params.correlationId}`);
|
|
307
|
+
assertString(params.correlationId, 'correlationId');
|
|
308
|
+
try {
|
|
309
|
+
const removedContent = await removeContent({ correlationId: params.correlationId });
|
|
310
|
+
debug(`removedContent (removeContent result): ${JSON.stringify(removedContent)}`);
|
|
311
|
+
const deleteResult = await operator.deleteOne({ correlationId: params.correlationId });
|
|
312
|
+
debug(`deleteResult: ${JSON.stringify(deleteResult)}`);
|
|
313
|
+
return { removed: deleteResult.deletedCount > 0, deletedContent: removedContent };
|
|
314
|
+
}
|
|
315
|
+
catch (err) {
|
|
316
|
+
if (err instanceof MongoDriverError) {
|
|
317
|
+
debug(err);
|
|
318
|
+
if (err.message.indexOf('File not found for id') !== -1) {
|
|
319
|
+
logger.silly(`mongo/remove: File not found, removing queueItem ${JSON.stringify(params.correlationId)} from ${collection}`);
|
|
320
|
+
debug(`mongo/remove: File not found, removing queueItem ${JSON.stringify(params.correlationId)} from ${collection}`);
|
|
321
|
+
const deleteResult = await operator.deleteOne({ correlationId: params.correlationId });
|
|
322
|
+
return { removed: deleteResult.deletedCount > 0, deletedContent: false };
|
|
323
|
+
}
|
|
324
|
+
logger.error(err.message);
|
|
325
|
+
throw new ApiError(httpStatus.INTERNAL_SERVER_ERROR, err.message);
|
|
326
|
+
}
|
|
327
|
+
const driverError = err;
|
|
328
|
+
throw new ApiError(httpStatus.INTERNAL_SERVER_ERROR, driverError.message);
|
|
329
|
+
}
|
|
337
330
|
}
|
|
338
|
-
|
|
339
|
-
|
|
331
|
+
async function readContent(correlationId) {
|
|
332
|
+
logger.info(`Reading content from mongo for id: ${correlationId} in ${collection}`);
|
|
333
|
+
debug(`Reading content from mongo for id: ${correlationId} in ${collection}`);
|
|
334
|
+
assertString(correlationId, 'correlationId');
|
|
335
|
+
const result = await operator.findOne({ correlationId });
|
|
336
|
+
debug(result);
|
|
337
|
+
if (result) {
|
|
338
|
+
const { operationSettings } = result;
|
|
339
|
+
let files = [];
|
|
340
|
+
if (operationSettings.prio === false) {
|
|
341
|
+
files = await gridFSBucket.find({ filename: correlationId }).toArray();
|
|
342
|
+
}
|
|
343
|
+
const hasContent = files.length > 0;
|
|
344
|
+
if (hasContent) {
|
|
345
|
+
return {
|
|
346
|
+
contentType: result.contentType,
|
|
347
|
+
readStream: gridFSBucket.openDownloadStreamByName(correlationId)
|
|
348
|
+
};
|
|
349
|
+
}
|
|
350
|
+
logger.debug(`OperationSettings for ${correlationId}: ${JSON.stringify(operationSettings)}`);
|
|
351
|
+
debug(`OperationSettings for ${correlationId}: ${JSON.stringify(operationSettings)}`);
|
|
352
|
+
debug(`Content is only available for streamBulk jobs. ${correlationId} is not a streamBulk job.`);
|
|
353
|
+
throw new ApiError(httpStatus.BAD_REQUEST, { message: `Content is only available for streamBulk jobs. ${correlationId} is not a streamBulk job.` });
|
|
354
|
+
}
|
|
355
|
+
throw new ApiError(httpStatus.NOT_FOUND, { message: `Job ${correlationId} not found.` });
|
|
340
356
|
}
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
logger.
|
|
354
|
-
return
|
|
355
|
-
}
|
|
356
|
-
logger.debug(`getOne not working!`);
|
|
357
|
-
throw new ApiError(httpStatus.INTERNAL_SERVER_ERROR, "Missing parameters, cannot get queueItem");
|
|
358
|
-
} catch (error) {
|
|
359
|
-
const errorMessage = error.payload || error.message || "";
|
|
360
|
-
logError(error);
|
|
361
|
-
throw new ApiError(httpStatus.INTERNAL_SERVER_ERROR, `Mongo errored: ${errorMessage}`);
|
|
357
|
+
async function removeContent(params) {
|
|
358
|
+
logger.info(`Removing content from mongo for id: ${params.correlationId} in ${collection}`);
|
|
359
|
+
debug(`Removing content from mongo for id: ${params.correlationId} in ${collection}`);
|
|
360
|
+
assertString(params.correlationId, 'correlationId');
|
|
361
|
+
const result = await gridFSBucket.find({ filename: params.correlationId }).toArray();
|
|
362
|
+
debug(`mongo/removeContent: find bucket result ${JSON.stringify(result)}`);
|
|
363
|
+
if (result.length > 0) {
|
|
364
|
+
const [firstBucketResult] = result;
|
|
365
|
+
const fileId = firstBucketResult._id;
|
|
366
|
+
await gridFSBucket.delete(fileId);
|
|
367
|
+
return true;
|
|
368
|
+
}
|
|
369
|
+
logger.debug(`No files for ${params.correlationId} to delete`);
|
|
370
|
+
return false;
|
|
362
371
|
}
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
372
|
+
async function getOne({ queueItemState, importJobState = undefined }) {
|
|
373
|
+
logger.silly(`queueItemState: ${queueItemState}, importJobState: ${JSON.stringify(importJobState)}`);
|
|
374
|
+
if (queueItemState) {
|
|
375
|
+
assertString(queueItemState, 'queueItemState');
|
|
376
|
+
}
|
|
377
|
+
if (importJobState) {
|
|
378
|
+
assertImportJobStateFilter(importJobState, 'importJobState');
|
|
379
|
+
}
|
|
380
|
+
const cleanQueueItemState = queueItemState ? { queueItemState } : undefined;
|
|
381
|
+
const options = { projection: { _id: 0 } };
|
|
382
|
+
try {
|
|
383
|
+
if (queueItemState && importJobState === undefined) {
|
|
384
|
+
logger.silly(`Checking DB ${collection} for just ${JSON.stringify(queueItemState)}`);
|
|
385
|
+
return operator.findOne({ queueItemState }, options);
|
|
386
|
+
}
|
|
387
|
+
if (importJobState && queueItemState === undefined) {
|
|
388
|
+
logger.silly(`Checking DB ${collection} for ${JSON.stringify(importJobState)}`);
|
|
389
|
+
return operator.findOne({ ...importJobState }, options);
|
|
390
|
+
}
|
|
391
|
+
if (importJobState && queueItemState) {
|
|
392
|
+
logger.silly(`Checking DB ${collection} for ${queueItemState} and ${JSON.stringify(importJobState)}`);
|
|
393
|
+
return operator.findOne({ ...cleanQueueItemState, ...importJobState }, options);
|
|
394
|
+
}
|
|
395
|
+
logger.debug(`getOne not working!`);
|
|
396
|
+
throw new ApiError(httpStatus.INTERNAL_SERVER_ERROR, 'Missing parameters, cannot get queueItem');
|
|
397
|
+
}
|
|
398
|
+
catch (error) {
|
|
399
|
+
const apiError = error;
|
|
400
|
+
const errorMessage = apiError.payload || apiError.message || '';
|
|
401
|
+
logError(error);
|
|
402
|
+
throw new ApiError(httpStatus.INTERNAL_SERVER_ERROR, `Mongo errored: ${errorMessage}`);
|
|
403
|
+
}
|
|
381
404
|
}
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
405
|
+
async function getStream(correlationId) {
|
|
406
|
+
logger.info(`Forming stream from mongo for ${correlationId} in ${collection}`);
|
|
407
|
+
assertString(correlationId, 'correlationId');
|
|
408
|
+
try {
|
|
409
|
+
const files = await gridFSBucket.find({ filename: correlationId }).toArray();
|
|
410
|
+
if (files.length === 0) {
|
|
411
|
+
logger.info(`getStream: file ${correlationId} not found in ${collection}`);
|
|
412
|
+
throw new ApiError(httpStatus.NOT_FOUND, { message: `File ${correlationId} not found in ${collection}` });
|
|
413
|
+
}
|
|
414
|
+
return gridFSBucket.openDownloadStreamByName(correlationId);
|
|
415
|
+
}
|
|
416
|
+
catch (error) {
|
|
417
|
+
if (error instanceof ApiError) {
|
|
418
|
+
throw error;
|
|
419
|
+
}
|
|
420
|
+
const apiError = error;
|
|
421
|
+
const errorMessage = apiError.payload || apiError.message || '';
|
|
422
|
+
logError(error);
|
|
423
|
+
throw new ApiError(httpStatus.INTERNAL_SERVER_ERROR, `Mongo errored: ${errorMessage}`);
|
|
424
|
+
}
|
|
386
425
|
}
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
correlationId
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
426
|
+
async function pushIds({ correlationId, handledIds, rejectedIds }) {
|
|
427
|
+
if (!Array.isArray(handledIds) || !Array.isArray(rejectedIds)) {
|
|
428
|
+
throw new ApiError(httpStatus.BAD_REQUEST, 'pushIds: handledIds and rejectedIds must be arrays');
|
|
429
|
+
}
|
|
430
|
+
assertString(correlationId, 'correlationId');
|
|
431
|
+
logger.verbose(`Push ids (${handledIds.length}) and rejectedIds (${rejectedIds.length}) ${correlationId} to ${collection}`);
|
|
432
|
+
logger.debug(`ids (${handledIds.length}): ${JSON.stringify(handledIds)}, rejectedIds ${rejectedIds.length}: ${JSON.stringify(rejectedIds)}`);
|
|
433
|
+
try {
|
|
434
|
+
await operator.updateOne({
|
|
435
|
+
correlationId
|
|
436
|
+
}, {
|
|
437
|
+
$set: {
|
|
438
|
+
modificationTime: new Date()
|
|
439
|
+
},
|
|
440
|
+
$push: {
|
|
441
|
+
handledIds: { $each: handledIds },
|
|
442
|
+
rejectedIds: { $each: rejectedIds }
|
|
443
|
+
}
|
|
444
|
+
});
|
|
445
|
+
}
|
|
446
|
+
catch (error) {
|
|
447
|
+
const apiError = error;
|
|
448
|
+
const errorMessage = apiError.payload || apiError.message || '';
|
|
449
|
+
logError(error);
|
|
450
|
+
throw new ApiError(httpStatus.INTERNAL_SERVER_ERROR, `Mongo errored: ${errorMessage}`);
|
|
451
|
+
}
|
|
406
452
|
}
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
453
|
+
async function pushMessages({ correlationId, messages, messageField = 'messages' }) {
|
|
454
|
+
if (!Array.isArray(messages)) {
|
|
455
|
+
throw new ApiError(httpStatus.BAD_REQUEST, 'pushMessages: messages must be an array');
|
|
456
|
+
}
|
|
457
|
+
assertString(correlationId, 'correlationId');
|
|
458
|
+
assertString(messageField, 'messageField');
|
|
459
|
+
logger.verbose(`Push messages (${messages.length}) to ${messageField} ${correlationId} to ${collection}`);
|
|
460
|
+
logger.silly(`Messages (${messages.length}): ${JSON.stringify(messages)}`);
|
|
461
|
+
try {
|
|
462
|
+
await operator.updateOne({
|
|
463
|
+
correlationId
|
|
464
|
+
}, {
|
|
465
|
+
$set: {
|
|
466
|
+
modificationTime: new Date()
|
|
467
|
+
},
|
|
468
|
+
$push: {
|
|
469
|
+
[messageField]: { $each: messages }
|
|
470
|
+
}
|
|
471
|
+
});
|
|
472
|
+
}
|
|
473
|
+
catch (error) {
|
|
474
|
+
const apiError = error;
|
|
475
|
+
const errorMessage = apiError.payload || apiError.message || '';
|
|
476
|
+
logError(error);
|
|
477
|
+
throw new ApiError(httpStatus.INTERNAL_SERVER_ERROR, `Mongo errored: ${errorMessage}`);
|
|
478
|
+
}
|
|
411
479
|
}
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
480
|
+
function setState({ correlationId, state, errorMessage = undefined, errorStatus = undefined }) {
|
|
481
|
+
assertString(correlationId, 'correlationId');
|
|
482
|
+
const errorString = errorMessage || errorStatus ? `, Error message: '${JSON.stringify(errorMessage) || ''}', Error status: '${errorStatus || ''}'` : '';
|
|
483
|
+
logger.info(`Setting queue-item state ${state} for ${correlationId}${errorString} to ${collection}`);
|
|
484
|
+
const allQueueItemStates = [
|
|
485
|
+
QUEUE_ITEM_STATE.DONE,
|
|
486
|
+
QUEUE_ITEM_STATE.ERROR,
|
|
487
|
+
QUEUE_ITEM_STATE.ABORT,
|
|
488
|
+
...Object.values(QUEUE_ITEM_STATE.VALIDATOR),
|
|
489
|
+
...Object.values(QUEUE_ITEM_STATE.IMPORTER)
|
|
490
|
+
];
|
|
491
|
+
if (!allQueueItemStates.includes(state)) {
|
|
492
|
+
debug(`Trying to set invalid state ${state}`);
|
|
493
|
+
throw new ApiError(httpStatus.BAD_REQUEST, 'Trying to set invalid state');
|
|
494
|
+
}
|
|
495
|
+
const updateValues = {
|
|
496
|
+
queueItemState: state,
|
|
497
|
+
modificationTime: new Date(),
|
|
498
|
+
errorMessage,
|
|
499
|
+
errorStatus
|
|
500
|
+
};
|
|
501
|
+
Object.keys(updateValues).forEach(key => updateValues[key] === undefined && delete updateValues[key]);
|
|
502
|
+
return operator
|
|
503
|
+
.findOneAndUpdate({ correlationId }, { $set: updateValues }, { projection: { _id: 0 }, returnDocument: 'after' });
|
|
431
504
|
}
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
505
|
+
function setImportJobState({ correlationId, operation, importJobState }) {
|
|
506
|
+
assertString(correlationId, 'correlationId');
|
|
507
|
+
logger.info(`Setting queue-item importJobState: {${operation}: ${importJobState}} for ${correlationId}`);
|
|
508
|
+
debug(`Setting queue-item importJobState: {${operation}: ${importJobState}} for ${correlationId}`);
|
|
509
|
+
if (!(operation in OPERATIONS)) {
|
|
510
|
+
debug(`Error: ${operation} is not in ${JSON.stringify(OPERATIONS)}`);
|
|
511
|
+
throw new ApiError(httpStatus.BAD_REQUEST, 'Invalid operation for import job state');
|
|
512
|
+
}
|
|
513
|
+
if (!(importJobState in IMPORT_JOB_STATE)) {
|
|
514
|
+
debug(`Error: ${importJobState} is not in ${JSON.stringify(IMPORT_JOB_STATE)}`);
|
|
515
|
+
throw new ApiError(httpStatus.BAD_REQUEST, 'Invalid import job state');
|
|
516
|
+
}
|
|
517
|
+
const newJobState = getNewJobState(operation, importJobState);
|
|
518
|
+
return operator.findOneAndUpdate({
|
|
519
|
+
correlationId
|
|
520
|
+
}, {
|
|
521
|
+
$set: {
|
|
522
|
+
...newJobState,
|
|
523
|
+
modificationTime: new Date()
|
|
524
|
+
}
|
|
525
|
+
}, { projection: { _id: 0 }, returnDocument: 'after' });
|
|
443
526
|
}
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
);
|
|
456
|
-
}
|
|
457
|
-
function setImportJobState({ correlationId, operation, importJobState }) {
|
|
458
|
-
assertString(correlationId, "correlationId");
|
|
459
|
-
logger.info(`Setting queue-item importJobState: {${operation}: ${importJobState}} for ${correlationId}`);
|
|
460
|
-
debug(`Setting queue-item importJobState: {${operation}: ${importJobState}} for ${correlationId}`);
|
|
461
|
-
if (!(operation in OPERATIONS)) {
|
|
462
|
-
debug(`Error: ${operation} is not in ${JSON.stringify(OPERATIONS)}`);
|
|
463
|
-
throw new ApiError(httpStatus.BAD_REQUEST, "Invalid operation for import job state");
|
|
527
|
+
function getNewJobState(operation, importJobState) {
|
|
528
|
+
if (operation === OPERATIONS.CREATE) {
|
|
529
|
+
return { 'importJobState.CREATE': importJobState };
|
|
530
|
+
}
|
|
531
|
+
if (operation === OPERATIONS.UPDATE) {
|
|
532
|
+
return { 'importJobState.UPDATE': importJobState };
|
|
533
|
+
}
|
|
534
|
+
if (operation === OPERATIONS.FIX) {
|
|
535
|
+
return { 'importJobState.FIX': importJobState };
|
|
536
|
+
}
|
|
537
|
+
throw new ApiError(httpStatus.BAD_REQUEST, 'Invalid operation for import job state');
|
|
464
538
|
}
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
539
|
+
function unwrapAndUpdateResult(result, correlationId) {
|
|
540
|
+
if (result === null || result === undefined) {
|
|
541
|
+
logger.info(`Queue item ${correlationId} not found in ${collection}`);
|
|
542
|
+
throw new ApiError(httpStatus.NOT_FOUND, { message: `Job ${correlationId} not found.` });
|
|
543
|
+
}
|
|
544
|
+
return result;
|
|
468
545
|
}
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
546
|
+
async function setOperation({ correlationId, operation }) {
|
|
547
|
+
assertString(correlationId, 'correlationId');
|
|
548
|
+
const newOperation = operation;
|
|
549
|
+
const existing = await operator.findOne({ correlationId });
|
|
550
|
+
if (!existing) {
|
|
551
|
+
logger.info(`Setting queue-item operation: queue item ${correlationId} not found in ${collection}`);
|
|
552
|
+
throw new ApiError(httpStatus.NOT_FOUND, { message: `Job ${correlationId} not found.` });
|
|
553
|
+
}
|
|
554
|
+
const { operation: oldOperation } = existing;
|
|
555
|
+
logger.info(`Setting queue-item operation from ${oldOperation} to ${newOperation} for ${correlationId} to ${collection}`);
|
|
556
|
+
const result = await operator.findOneAndUpdate({
|
|
557
|
+
correlationId
|
|
558
|
+
}, {
|
|
559
|
+
$set: {
|
|
560
|
+
operation: newOperation,
|
|
561
|
+
modificationTime: new Date()
|
|
562
|
+
}
|
|
563
|
+
}, { projection: { _id: 0 }, returnDocument: 'after' });
|
|
564
|
+
debugDev(`${JSON.stringify(result)}`);
|
|
565
|
+
return unwrapAndUpdateResult(result, correlationId);
|
|
481
566
|
}
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
567
|
+
async function setOperations({ correlationId, addOperation, removeOperation = undefined }) {
|
|
568
|
+
assertString(correlationId, 'correlationId');
|
|
569
|
+
const queueItem = await operator.findOne({ correlationId });
|
|
570
|
+
logger.silly(`We found a queueItem: ${JSON.stringify(queueItem)}`);
|
|
571
|
+
if (!queueItem) {
|
|
572
|
+
logger.info(`Setting queue-item operations: queue item ${correlationId} not found in ${collection}`);
|
|
573
|
+
throw new ApiError(httpStatus.NOT_FOUND, { message: `Job ${correlationId} not found.` });
|
|
574
|
+
}
|
|
575
|
+
const oldOperations = queueItem.operations;
|
|
576
|
+
logger.info(`Setting queue-item operations from ${JSON.stringify(oldOperations)} by adding ${addOperation} and removing ${removeOperation} for ${correlationId} to ${collection}`);
|
|
577
|
+
const operationsAfterRemove = oldOperations.filter(operation => operation !== removeOperation);
|
|
578
|
+
const operationsAfterRemoveAndAdd = operationsAfterRemove.includes(addOperation) ? operationsAfterRemove : [...operationsAfterRemove, addOperation];
|
|
579
|
+
logger.silly(`operationsAfterRemove: ${operationsAfterRemove}`);
|
|
580
|
+
logger.silly(`operationAfterRemoveAndAdd: ${operationsAfterRemoveAndAdd}`);
|
|
581
|
+
const result = await operator.findOneAndUpdate({
|
|
582
|
+
correlationId
|
|
583
|
+
}, {
|
|
584
|
+
$set: {
|
|
585
|
+
operations: operationsAfterRemoveAndAdd,
|
|
586
|
+
modificationTime: new Date()
|
|
587
|
+
}
|
|
588
|
+
}, { projection: { _id: 0 }, returnDocument: 'after' });
|
|
589
|
+
debugDev(`${JSON.stringify(result)}`);
|
|
590
|
+
return unwrapAndUpdateResult(result, correlationId);
|
|
495
591
|
}
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
592
|
+
async function addBlobSize({ correlationId }) {
|
|
593
|
+
assertString(correlationId, 'correlationId');
|
|
594
|
+
const result = await operator.findOneAndUpdate({
|
|
595
|
+
correlationId,
|
|
596
|
+
queueItemState: QUEUE_ITEM_STATE.VALIDATOR.WAITING_FOR_RECORDS
|
|
597
|
+
}, {
|
|
598
|
+
$inc: {
|
|
599
|
+
blobSize: 1
|
|
600
|
+
},
|
|
601
|
+
$set: {
|
|
602
|
+
modificationTime: new Date()
|
|
603
|
+
}
|
|
604
|
+
}, { projection: { _id: 0 }, returnDocument: 'after' });
|
|
605
|
+
logger.verbose(`AddBlobSizeResult in mongo: ${JSON.stringify(result)}`);
|
|
606
|
+
debugDev(`${JSON.stringify(result)}`);
|
|
607
|
+
return result;
|
|
505
608
|
}
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
assertString(correlationId, "correlationId");
|
|
521
|
-
const queueItem = await operator.findOne({ correlationId });
|
|
522
|
-
logger.silly(`We found a queueItem: ${JSON.stringify(queueItem)}`);
|
|
523
|
-
if (!queueItem) {
|
|
524
|
-
logger.info(`Setting queue-item operations: queue item ${correlationId} not found in ${collection}`);
|
|
525
|
-
throw new ApiError(httpStatus.NOT_FOUND, { message: `Job ${correlationId} not found.` });
|
|
609
|
+
async function setBlobSize({ correlationId, blobSize }) {
|
|
610
|
+
assertString(correlationId, 'correlationId');
|
|
611
|
+
assertNumber(blobSize, 'blobSize');
|
|
612
|
+
const result = await operator.findOneAndUpdate({
|
|
613
|
+
correlationId
|
|
614
|
+
}, {
|
|
615
|
+
$set: {
|
|
616
|
+
blobSize,
|
|
617
|
+
modificationTime: new Date()
|
|
618
|
+
}
|
|
619
|
+
}, { projection: { _id: 0 }, returnDocument: 'after' });
|
|
620
|
+
logger.verbose(`SetBlobSizeResult in mongo: ${JSON.stringify(result)}`);
|
|
621
|
+
debugDev(`${JSON.stringify(result)}`);
|
|
622
|
+
return result;
|
|
526
623
|
}
|
|
527
|
-
const oldOperations = queueItem.operations;
|
|
528
|
-
logger.info(`Setting queue-item operations from ${JSON.stringify(oldOperations)} by adding ${addOperation} and removing ${removeOperation} for ${correlationId} to ${collection}`);
|
|
529
|
-
const operationsAfterRemove = oldOperations.filter((operation) => operation !== removeOperation);
|
|
530
|
-
const operationsAfterRemoveAndAdd = operationsAfterRemove.includes(addOperation) ? operationsAfterRemove : [...operationsAfterRemove, addOperation];
|
|
531
|
-
logger.silly(`operationsAfterRemove: ${operationsAfterRemove}`);
|
|
532
|
-
logger.silly(`operationAfterRemoveAndAdd: ${operationsAfterRemoveAndAdd}`);
|
|
533
|
-
const result = await operator.findOneAndUpdate({
|
|
534
|
-
correlationId
|
|
535
|
-
}, {
|
|
536
|
-
$set: {
|
|
537
|
-
operations: operationsAfterRemoveAndAdd,
|
|
538
|
-
modificationTime: /* @__PURE__ */ new Date()
|
|
539
|
-
}
|
|
540
|
-
}, { projection: { _id: 0 }, returnDocument: "after" });
|
|
541
|
-
debugDev(`${JSON.stringify(result)}`);
|
|
542
|
-
return unwrapAndUpdateResult(result, correlationId);
|
|
543
|
-
}
|
|
544
|
-
async function addBlobSize({ correlationId }) {
|
|
545
|
-
assertString(correlationId, "correlationId");
|
|
546
|
-
const result = await operator.findOneAndUpdate({
|
|
547
|
-
correlationId,
|
|
548
|
-
queueItemState: QUEUE_ITEM_STATE.VALIDATOR.WAITING_FOR_RECORDS
|
|
549
|
-
}, {
|
|
550
|
-
$inc: {
|
|
551
|
-
blobSize: 1
|
|
552
|
-
},
|
|
553
|
-
$set: {
|
|
554
|
-
modificationTime: /* @__PURE__ */ new Date()
|
|
555
|
-
}
|
|
556
|
-
}, { projection: { _id: 0 }, returnDocument: "after" });
|
|
557
|
-
logger.verbose(`AddBlobSizeResult in mongo: ${JSON.stringify(result)}`);
|
|
558
|
-
debugDev(`${JSON.stringify(result)}`);
|
|
559
|
-
return result;
|
|
560
|
-
}
|
|
561
|
-
async function setBlobSize({ correlationId, blobSize }) {
|
|
562
|
-
assertString(correlationId, "correlationId");
|
|
563
|
-
assertNumber(blobSize, "blobSize");
|
|
564
|
-
const result = await operator.findOneAndUpdate({
|
|
565
|
-
correlationId
|
|
566
|
-
}, {
|
|
567
|
-
$set: {
|
|
568
|
-
blobSize,
|
|
569
|
-
modificationTime: /* @__PURE__ */ new Date()
|
|
570
|
-
}
|
|
571
|
-
}, { projection: { _id: 0 }, returnDocument: "after" });
|
|
572
|
-
logger.verbose(`SetBlobSizeResult in mongo: ${JSON.stringify(result)}`);
|
|
573
|
-
debugDev(`${JSON.stringify(result)}`);
|
|
574
|
-
return result;
|
|
575
|
-
}
|
|
576
624
|
}
|
|
577
|
-
//# sourceMappingURL=mongo.js.map
|
|
625
|
+
//# sourceMappingURL=mongo.js.map
|