Files
Checkmate/Server/db/models/Notification.js
T

79 lines
1.8 KiB
JavaScript
Raw Normal View History

2024-10-15 11:57:37 +08:00
import mongoose from "mongoose";
2024-07-30 12:54:35 -07:00
const NotificationSchema = mongoose.Schema(
2024-10-17 12:07:30 +08:00
{
monitorId: {
type: mongoose.Schema.Types.ObjectId,
ref: "Monitor",
immutable: true,
},
type: {
type: String,
enum: ["email", "sms"],
},
address: {
type: String,
},
phone: {
type: String,
},
alertThreshold: {
type: Number,
default: 5,
},
cpuAlertThreshold: {
type: Number,
default: function () {
return this.alertThreshold;
},
},
memoryAlertThreshold: {
type: Number,
default: function () {
return this.alertThreshold;
},
},
diskAlertThreshold: {
type: Number,
default: function () {
return this.alertThreshold;
},
},
2024-11-25 11:04:18 +08:00
tempAlertThreshold: {
type: Number,
default: function () {
return this.alertThreshold;
},
},
2024-10-17 12:07:30 +08:00
},
{
timestamps: true,
}
2024-07-30 12:54:35 -07:00
);
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();
});
2024-10-15 11:57:37 +08:00
export default mongoose.model("Notification", NotificationSchema);