initial commit
This commit is contained in:
1 parent
ec9c79fc94
commit
a0ebf7d224
8 files changed
+81
-76
No files matched your search
@@ -1,74 +0,0 @@
|
|||||||
import mongoose from "mongoose";
|
|
||||||
|
|
||||||
const NotificationSchema = mongoose.Schema(
|
|
||||||
{
|
|
||||||
userId: {
|
|
||||||
type: mongoose.Schema.Types.ObjectId,
|
|
||||||
ref: "User",
|
|
||||||
immutable: true,
|
|
||||||
required: true,
|
|
||||||
},
|
|
||||||
teamId: {
|
|
||||||
type: mongoose.Schema.Types.ObjectId,
|
|
||||||
ref: "Team",
|
|
||||||
immutable: true,
|
|
||||||
required: true,
|
|
||||||
},
|
|
||||||
type: {
|
|
||||||
type: String,
|
|
||||||
enum: ["email", "slack", "discord", "webhook", "pager_duty", "matrix"],
|
|
||||||
},
|
|
||||||
notificationName: {
|
|
||||||
type: String,
|
|
||||||
required: true,
|
|
||||||
},
|
|
||||||
address: {
|
|
||||||
type: String,
|
|
||||||
},
|
|
||||||
phone: {
|
|
||||||
type: String,
|
|
||||||
},
|
|
||||||
// Matrix-specific fields
|
|
||||||
homeserverUrl: {
|
|
||||||
type: String,
|
|
||||||
},
|
|
||||||
roomId: {
|
|
||||||
type: String,
|
|
||||||
},
|
|
||||||
accessToken: {
|
|
||||||
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);
|
|
||||||
Executable
+50
@@ -0,0 +1,50 @@
|
|||||||
|
import { Schema, model, type Types } from "mongoose";
|
||||||
|
import type { Notification, NotificationChannel } from "@/types/notification.js";
|
||||||
|
|
||||||
|
interface NotificationDocument extends Omit<Notification, "id" | "userId" | "teamId" | "createdAt" | "updatedAt"> {
|
||||||
|
_id: Types.ObjectId;
|
||||||
|
userId: Types.ObjectId;
|
||||||
|
teamId: Types.ObjectId;
|
||||||
|
createdAt: Date;
|
||||||
|
updatedAt: Date;
|
||||||
|
}
|
||||||
|
|
||||||
|
const NotificationSchema = new Schema<NotificationDocument>(
|
||||||
|
{
|
||||||
|
userId: {
|
||||||
|
type: Schema.Types.ObjectId,
|
||||||
|
ref: "User",
|
||||||
|
immutable: true,
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
teamId: {
|
||||||
|
type: Schema.Types.ObjectId,
|
||||||
|
ref: "Team",
|
||||||
|
immutable: true,
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
type: {
|
||||||
|
type: String,
|
||||||
|
enum: ["email", "slack", "discord", "webhook", "pager_duty", "matrix"] as NotificationChannel[],
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
notificationName: {
|
||||||
|
type: String,
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
address: { type: String },
|
||||||
|
phone: { type: String },
|
||||||
|
homeserverUrl: { type: String },
|
||||||
|
roomId: { type: String },
|
||||||
|
accessToken: { type: String },
|
||||||
|
},
|
||||||
|
{
|
||||||
|
timestamps: true,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
const NotificationModel = model<NotificationDocument>("Notification", NotificationSchema);
|
||||||
|
|
||||||
|
export type { NotificationDocument };
|
||||||
|
export { NotificationModel };
|
||||||
|
export default NotificationModel;
|
||||||
@@ -3,7 +3,7 @@ import bcrypt from "bcryptjs";
|
|||||||
import type { User, UserProfileImage, UserRole } from "@/types/index.js";
|
import type { User, UserProfileImage, UserRole } from "@/types/index.js";
|
||||||
import { MonitorModel } from "@/db/models/index.js";
|
import { MonitorModel } from "@/db/models/index.js";
|
||||||
import Team from "./Team.js";
|
import Team from "./Team.js";
|
||||||
import Notification from "./Notification.js";
|
import NotificationModel from "./Notification.js";
|
||||||
|
|
||||||
type UserDocumentBase = Omit<User, "id" | "teamId" | "createdAt" | "updatedAt"> & {
|
type UserDocumentBase = Omit<User, "id" | "teamId" | "createdAt" | "updatedAt"> & {
|
||||||
teamId?: Types.ObjectId;
|
teamId?: Types.ObjectId;
|
||||||
@@ -76,7 +76,7 @@ UserSchema.pre("findOneAndDelete", async function (next) {
|
|||||||
await Team.deleteOne({ _id: userToDelete.teamId });
|
await Team.deleteOne({ _id: userToDelete.teamId });
|
||||||
await MonitorModel.deleteMany({ userId: userToDelete._id });
|
await MonitorModel.deleteMany({ userId: userToDelete._id });
|
||||||
await this.model.deleteMany({ teamId: userToDelete.teamId, _id: { $ne: userToDelete._id } });
|
await this.model.deleteMany({ teamId: userToDelete.teamId, _id: { $ne: userToDelete._id } });
|
||||||
await Notification.deleteMany({ teamId: userToDelete.teamId });
|
await NotificationModel.deleteMany({ teamId: userToDelete.teamId });
|
||||||
}
|
}
|
||||||
next();
|
next();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
@@ -21,3 +21,6 @@ export { default as AppSettingsModel } from "@/db/models/AppSettings.js";
|
|||||||
|
|
||||||
export * from "@/db/models/RecoveryToken.js";
|
export * from "@/db/models/RecoveryToken.js";
|
||||||
export { default as RecoveryTokenModel } from "@/db/models/RecoveryToken.js";
|
export { default as RecoveryTokenModel } from "@/db/models/RecoveryToken.js";
|
||||||
|
|
||||||
|
export * from "@/db/models/Notification.js";
|
||||||
|
export { default as NotificationModel } from "@/db/models/Notification.js";
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
import type { Monitor } from "@/types/index.js";
|
||||||
|
|
||||||
|
export interface IAlert {}
|
||||||
|
export interface IMessageProvider {}
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
import type { Monitor } from "@/types/index.js";
|
||||||
|
interface INotificationSerivce {
|
||||||
|
handleNotifications: (moniotr: Monitor) => Promise<void>;
|
||||||
|
}
|
||||||
@@ -7,3 +7,4 @@ export * from "@/types/user.js";
|
|||||||
export * from "@/types/invite.js";
|
export * from "@/types/invite.js";
|
||||||
export * from "@/types/recoveryToken.js";
|
export * from "@/types/recoveryToken.js";
|
||||||
export * from "@/types/settings.js";
|
export * from "@/types/settings.js";
|
||||||
|
export * from "@/types/notification.js";
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
export const NotificationChannels = ["email", "slack", "discord", "webhook", "pager_duty", "matrix"] as const;
|
||||||
|
export type NotificationChannel = (typeof NotificationChannels)[number];
|
||||||
|
|
||||||
|
export interface Notification {
|
||||||
|
id: string;
|
||||||
|
userId: string;
|
||||||
|
teamId: string;
|
||||||
|
type: NotificationChannel;
|
||||||
|
notificationName: string;
|
||||||
|
address?: string;
|
||||||
|
phone?: string;
|
||||||
|
homeserverUrl?: string;
|
||||||
|
roomId?: string;
|
||||||
|
accessToken?: string;
|
||||||
|
createdAt: string;
|
||||||
|
updatedAt: string;
|
||||||
|
}
|
||||||
Reference in new issue
Block a user