Skip to content

Commit

Permalink
Merge pull request #3311 from tarun7singh/monitor-group-fix
Browse files Browse the repository at this point in the history
Added fix to remove children when type changed
  • Loading branch information
louislam committed Jun 26, 2023
2 parents 149f8c3 + 7a34103 commit 9fb8f94
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
11 changes: 11 additions & 0 deletions server/model/monitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -1463,6 +1463,17 @@ class Monitor extends BeanModel {
return childrenIDs;
}

/**
* Unlinks all children of the the group monitor
* @param {number} groupID ID of group to remove children of
* @returns {Promise<void>}
*/
static async unlinkAllChildren(groupID) {
return await R.exec("UPDATE `monitor` SET parent = ? WHERE parent = ? ", [
null, groupID
]);
}

/**
* Checks recursive if parent (ancestors) are active
* @param {number} monitorID ID of the monitor to get
Expand Down
12 changes: 11 additions & 1 deletion server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,7 @@ let needSetup = false;
// Edit a monitor
socket.on("editMonitor", async (monitor, callback) => {
try {
let removeGroupChildren = false;
checkLogin(socket);

let bean = await R.findOne("monitor", " id = ? ", [ monitor.id ]);
Expand All @@ -684,14 +685,19 @@ let needSetup = false;
throw new Error("Permission denied.");
}

// Check if Parent is Decendant (would cause endless loop)
// Check if Parent is Descendant (would cause endless loop)
if (monitor.parent !== null) {
const childIDs = await Monitor.getAllChildrenIDs(monitor.id);
if (childIDs.includes(monitor.parent)) {
throw new Error("Invalid Monitor Group");
}
}

// Remove children if monitor type has changed (from group to non-group)
if (bean.type === "group" && monitor.type !== bean.type) {
removeGroupChildren = true;
}

bean.name = monitor.name;
bean.description = monitor.description;
bean.parent = monitor.parent;
Expand Down Expand Up @@ -752,6 +758,10 @@ let needSetup = false;

await R.store(bean);

if (removeGroupChildren) {
await Monitor.unlinkAllChildren(monitor.id);
}

await updateMonitorNotification(bean.id, monitor.notificationIDList);

if (bean.isActive()) {
Expand Down

0 comments on commit 9fb8f94

Please sign in to comment.