Refactored editObject function in database.js to utilize a merged object for updates, improving code clarity and reducing redundancy. Updated printer schema to include a default ID generation method and added alerts handling in editPrinterRouteHandler for enhanced printer management.
All checks were successful
farmcontrol/farmcontrol-api/pipeline/head This commit looks good

This commit is contained in:
Tom Butcher 2026-07-26 21:11:43 +01:00
parent 3a79526eda
commit 5a4fecbae0
4 changed files with 13 additions and 24 deletions

View File

@ -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({

View File

@ -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 }

View File

@ -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;
}

View File

@ -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)) {