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
All checks were successful
farmcontrol/farmcontrol-api/pipeline/head This commit looks good
This commit is contained in:
parent
3a79526eda
commit
5a4fecbae0
@ -846,38 +846,25 @@ export const editObject = async ({ model, id, updateData, user, populate, recalc
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Audit log before update
|
const updatedObject = mergeObjectUpdates(
|
||||||
await editAuditLog(
|
_.cloneDeep(previousExpandedObject),
|
||||||
previousExpandedObject,
|
expandObjectIds(updateData)
|
||||||
{ ...previousExpandedObject, ...expandObjectIds(updateData) },
|
|
||||||
id,
|
|
||||||
parentType,
|
|
||||||
user
|
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Audit log before update
|
||||||
|
await editAuditLog(previousExpandedObject, updatedObject, id, parentType, user);
|
||||||
|
|
||||||
if (
|
if (
|
||||||
parentType !== 'notification' &&
|
parentType !== 'notification' &&
|
||||||
parentType !== 'auditLog' &&
|
parentType !== 'auditLog' &&
|
||||||
parentType !== 'userNotifier'
|
parentType !== 'userNotifier'
|
||||||
) {
|
) {
|
||||||
await editNotification(
|
await editNotification(previousExpandedObject, updatedObject, id, parentType, user);
|
||||||
previousExpandedObject,
|
|
||||||
{ ...previousExpandedObject, ...expandObjectIds(updateData) },
|
|
||||||
id,
|
|
||||||
parentType,
|
|
||||||
user
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
// Distribute update
|
// Distribute update
|
||||||
await distributeUpdate(updateData, id, parentType);
|
await distributeUpdate(updateData, id, parentType);
|
||||||
// Call childUpdate event for any child objects
|
// Call childUpdate event for any child objects
|
||||||
await distributeChildUpdate(
|
await distributeChildUpdate(previousExpandedObject, updatedObject, id, model);
|
||||||
previousExpandedObject,
|
|
||||||
{ ...previousExpandedObject, ...expandObjectIds(updateData) },
|
|
||||||
id,
|
|
||||||
model
|
|
||||||
);
|
|
||||||
const updatedObject = { ...previousExpandedObject, ...expandObjectIds(updateData) };
|
|
||||||
|
|
||||||
// Update cache with the new version
|
// Update cache with the new version
|
||||||
await updateObjectCache({
|
await updateObjectCache({
|
||||||
|
|||||||
@ -19,7 +19,8 @@ const alertSchema = new Schema(
|
|||||||
type: { type: String, required: true }, // error, info, message
|
type: { type: String, required: true }, // error, info, message
|
||||||
message: { type: String, required: false },
|
message: { type: String, required: false },
|
||||||
actions: [{ type: String, required: false, default: [] }],
|
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 },
|
canDismiss: { type: Boolean, required: true, default: true },
|
||||||
},
|
},
|
||||||
{ timestamps: true, _id: false }
|
{ timestamps: true, _id: false }
|
||||||
|
|||||||
@ -128,6 +128,7 @@ export const editPrinterRouteHandler = async (req, res) => {
|
|||||||
host: req.body.host,
|
host: req.body.host,
|
||||||
pendingSlicerUploads: req.body.pendingSlicerUploads,
|
pendingSlicerUploads: req.body.pendingSlicerUploads,
|
||||||
active: req.body.active,
|
active: req.body.active,
|
||||||
|
alerts: req.body.alerts,
|
||||||
};
|
};
|
||||||
// Create audit log before updating
|
// Create audit log before updating
|
||||||
const result = await editObject({
|
const result = await editObject({
|
||||||
@ -140,7 +141,7 @@ export const editPrinterRouteHandler = async (req, res) => {
|
|||||||
|
|
||||||
if (result.error) {
|
if (result.error) {
|
||||||
logger.error('Error editing printer:', result.error);
|
logger.error('Error editing printer:', result.error);
|
||||||
res.status(result).send(result);
|
res.status(result.code).send(result);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1056,7 +1056,7 @@ function expandObjectIds(input) {
|
|||||||
for (const [key, val] of Object.entries(value)) {
|
for (const [key, val] of Object.entries(value)) {
|
||||||
if (excludedFields.includes(key)) {
|
if (excludedFields.includes(key)) {
|
||||||
// Do not expand keys that are excluded
|
// Do not expand keys that are excluded
|
||||||
result[key] = val.toString();
|
result[key] = val == null ? val : val.toString();
|
||||||
} else if (isObjectId(val)) {
|
} else if (isObjectId(val)) {
|
||||||
result[key] = { _id: val };
|
result[key] = { _id: val };
|
||||||
} else if (Array.isArray(val)) {
|
} else if (Array.isArray(val)) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user