Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const { productConfiguration, shopId } = update;
bulkOperations.push({
updateOne: {
filter: {
"productConfiguration.productVariantId": productConfiguration.productVariantId,
shopId
},
update: modifier,
upsert: true
}
});
});
let res;
try {
Logger.trace({ ...logCtx, bulkOperations }, "Running bulk op");
res = await SimpleInventory.bulkWrite(bulkOperations, { ordered: false });
} catch (error) {
Logger.error({ ...logCtx, error }, "One or more of the bulk update failed");
res = error; // error object has details about failed & successful operations
}
const upsertedIds = res.result.upserted.map((each) => each._id);
const failedUpdates = validUpdates.filter((update) => {
const match = res.result.writeErrors.find((writeError) => {
const errVariantId = writeError.op.q["productConfiguration.productVariantId"];
const updateVariantId = update.productConfiguration.productVariantId;
return updateVariantId === errVariantId;
});
if (match) return true;
return false;
});
await context.mutations.transformAndValidateCart(context, cart);
return {
replaceOne: {
filter: { _id: cart._id },
replacement: cart,
upsert: true
}
};
});
const bulkWrites = await Promise.all(bulkWritePromises);
let writeErrors;
try {
Logger.trace({ ...logCtx, bulkWrites }, "Running bulk op");
const bulkWriteResult = await Cart.bulkWrite(bulkWrites, { ordered: false });
({ writeErrors } = bulkWriteResult.result);
} catch (error) {
if (!error.result || typeof error.result.getWriteErrors !== "function") throw error;
// This happens only if all writes fail. `error` object has the result on it.
writeErrors = error.result.getWriteErrors();
}
// Figure out which failed and which succeeded. Emit "after update" or log error
const cartIds = [];
await Promise.all(carts.map(async (cart, index) => {
// If updating this cart failed, log the error details and stop
const writeError = writeErrors.find((writeErr) => writeErr.index === index);
if (writeError) {
Logger.error({
...logCtx,
export default async function executeBulkOperation(collection, operations, totalProducts) {
let response;
try {
Logger.trace({ ...logCtx, operations }, "Running bulk operation");
response = await collection.bulkWrite(operations, { ordered: false });
} catch (error) {
Logger.error({ ...logCtx, error }, "One or more of the bulk update failed");
response = error; // error object has details about failed & successful operations
}
const { nMatched, nModified, result: { writeErrors } } = response;
const notFoundCount = totalProducts - nMatched;
const cleanedErrors = writeErrors.map((error) => ({
documentId: error.op._id,
errorMsg: error.errmsg
}));
return {
foundCount: nMatched,
upsertedDocsOpData.forEach((operationData) => {
const arg = {
productConfiguration: {
productId: operationData.updateOne.update.$setOnInsert["productConfiguration.productId"],
productVariantId: operationData.updateOne.filter["productConfiguration.productVariantId"]
},
shopId: operationData.updateOne.filter.shopId
};
Logger.trace({ ...logCtx, arg }, "Calling recalculateReservedSimpleInventory");
context.mutations.recalculateReservedSimpleInventory(context, arg);
});
}