diff --git a/src/database/database.js b/src/database/database.js index da9334a..1803fcc 100644 --- a/src/database/database.js +++ b/src/database/database.js @@ -846,38 +846,25 @@ export const editObject = async ({ model, id, updateData, user, populate, recalc } } - // Audit log before update - await editAuditLog( - previousExpandedObject, - { ...previousExpandedObject, ...expandObjectIds(updateData) }, - id, - parentType, - user + const updatedObject = mergeObjectUpdates( + _.cloneDeep(previousExpandedObject), + expandObjectIds(updateData) ); + // Audit log before update + await editAuditLog(previousExpandedObject, updatedObject, id, parentType, user); + if ( parentType !== 'notification' && parentType !== 'auditLog' && parentType !== 'userNotifier' ) { - await editNotification( - previousExpandedObject, - { ...previousExpandedObject, ...expandObjectIds(updateData) }, - id, - parentType, - user - ); + await editNotification(previousExpandedObject, updatedObject, id, parentType, user); } // Distribute update await distributeUpdate(updateData, id, parentType); // Call childUpdate event for any child objects - await distributeChildUpdate( - previousExpandedObject, - { ...previousExpandedObject, ...expandObjectIds(updateData) }, - id, - model - ); - const updatedObject = { ...previousExpandedObject, ...expandObjectIds(updateData) }; + await distributeChildUpdate(previousExpandedObject, updatedObject, id, model); // Update cache with the new version await updateObjectCache({ diff --git a/src/database/schemas/production/printer.schema.js b/src/database/schemas/production/printer.schema.js index 56efd2c..750d003 100644 --- a/src/database/schemas/production/printer.schema.js +++ b/src/database/schemas/production/printer.schema.js @@ -19,7 +19,8 @@ const alertSchema = new Schema( type: { type: String, required: true }, // error, info, message message: { type: String, required: false }, actions: [{ type: String, required: false, default: [] }], - _id: { type: String, required: true }, + _id: { type: String, required: true, default: () => generateId()() }, + code: { type: String, required: false }, canDismiss: { type: Boolean, required: true, default: true }, }, { timestamps: true, _id: false } diff --git a/src/services/production/printers.js b/src/services/production/printers.js index d890e73..fd3167a 100644 --- a/src/services/production/printers.js +++ b/src/services/production/printers.js @@ -128,6 +128,7 @@ export const editPrinterRouteHandler = async (req, res) => { host: req.body.host, pendingSlicerUploads: req.body.pendingSlicerUploads, active: req.body.active, + alerts: req.body.alerts, }; // Create audit log before updating const result = await editObject({ @@ -140,7 +141,7 @@ export const editPrinterRouteHandler = async (req, res) => { if (result.error) { logger.error('Error editing printer:', result.error); - res.status(result).send(result); + res.status(result.code).send(result); return; } diff --git a/src/utils.js b/src/utils.js index 2eae32f..4539240 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1056,7 +1056,7 @@ function expandObjectIds(input) { for (const [key, val] of Object.entries(value)) { if (excludedFields.includes(key)) { // Do not expand keys that are excluded - result[key] = val.toString(); + result[key] = val == null ? val : val.toString(); } else if (isObjectId(val)) { result[key] = { _id: val }; } else if (Array.isArray(val)) {