Add pre save and preupdate functinality to set thresholds on alertThreshold change
This commit is contained in:
1 parent
d1d19177bd
commit
24d5a38527
1 file changed
+27
@@ -49,4 +49,31 @@ const NotificationSchema = mongoose.Schema(
|
|||||||
timestamps: true,
|
timestamps: true,
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
NotificationSchema.pre("save", function (next) {
|
||||||
|
if (!this.cpuAlertThreshold || this.isModified("alertThreshold")) {
|
||||||
|
this.cpuAlertThreshold = this.alertThreshold;
|
||||||
|
}
|
||||||
|
if (!this.memoryAlertThreshold || this.isModified("alertThreshold")) {
|
||||||
|
this.memoryAlertThreshold = this.alertThreshold;
|
||||||
|
}
|
||||||
|
if (!this.diskAlertThreshold || this.isModified("alertThreshold")) {
|
||||||
|
this.diskAlertThreshold = this.alertThreshold;
|
||||||
|
}
|
||||||
|
if (!this.tempAlertThreshold || this.isModified("alertThreshold")) {
|
||||||
|
this.tempAlertThreshold = this.alertThreshold;
|
||||||
|
}
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
|
||||||
|
NotificationSchema.pre("findOneAndUpdate", function (next) {
|
||||||
|
const update = this.getUpdate();
|
||||||
|
if (update.alertThreshold) {
|
||||||
|
update.cpuAlertThreshold = update.alertThreshold;
|
||||||
|
update.memoryAlertThreshold = update.alertThreshold;
|
||||||
|
update.diskAlertThreshold = update.alertThreshold;
|
||||||
|
update.tempAlertThreshold = update.alertThreshold;
|
||||||
|
}
|
||||||
|
next();
|
||||||
|
});
|
||||||
export default mongoose.model("Notification", NotificationSchema);
|
export default mongoose.model("Notification", NotificationSchema);
|
||||||
Reference in new issue
Block a user