Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added fix to remove children when type changed #3311

Merged
merged 1 commit into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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