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

64 lines
1.5 KiB
JavaScript
Raw Normal View History

2025-04-20 11:29:53 -07:00
import mongoose from "mongoose";
const NotificationSchema = mongoose.Schema(
{
2025-06-09 10:24:06 +08:00
userId: {
type: mongoose.Schema.Types.ObjectId,
ref: "User",
immutable: true,
required: true,
},
teamId: {
2025-04-20 11:29:53 -07:00
type: mongoose.Schema.Types.ObjectId,
2025-06-09 10:24:06 +08:00
ref: "Team",
2025-04-20 11:29:53 -07:00
immutable: true,
2025-06-09 10:24:06 +08:00
required: true,
2025-04-20 11:29:53 -07:00
},
type: {
type: String,
2025-06-18 14:42:59 +08:00
enum: ["email", "slack", "discord", "webhook", "pager_duty"],
2025-04-20 11:29:53 -07:00
},
2025-06-09 10:49:52 +08:00
notificationName: {
type: String,
required: true,
},
2025-04-20 11:29:53 -07:00
address: {
type: String,
},
phone: {
type: String,
},
},
{
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);