2025-09-22 15:56:41 -07:00
|
|
|
import mongoose, { Schema, Document, Types } from "mongoose";
|
|
|
|
|
|
2025-04-25 12:46:35 -07:00
|
|
|
import bcrypt from "bcryptjs";
|
2025-09-22 15:56:41 -07:00
|
|
|
import logger from "../../../utils/logger.js";
|
|
|
|
|
import Monitor from "../Monitor.js";
|
|
|
|
|
import Team from "../Team.js";
|
|
|
|
|
import Notification from "../Notification.js";
|
|
|
|
|
import { subscribe } from "diagnostics_channel";
|
|
|
|
|
|
|
|
|
|
export const RoleTypes = ["user", "admin", "superadmin", "demo"] as const;
|
|
|
|
|
export type RoleType = (typeof RoleTypes)[number];
|
|
|
|
|
|
|
|
|
|
export interface ITokenizedUser {
|
|
|
|
|
sub: string;
|
|
|
|
|
roles: string[];
|
|
|
|
|
}
|
2025-04-20 11:29:53 -07:00
|
|
|
|
2025-09-22 15:56:41 -07:00
|
|
|
export interface IUser extends Document {
|
|
|
|
|
// V1
|
|
|
|
|
_id: Types.ObjectId;
|
|
|
|
|
firstName: string;
|
|
|
|
|
lastName: string;
|
|
|
|
|
email: string;
|
|
|
|
|
password: string;
|
|
|
|
|
avatarImage: string;
|
|
|
|
|
profileImage: {
|
|
|
|
|
data: Buffer;
|
|
|
|
|
contentType: string;
|
|
|
|
|
};
|
|
|
|
|
isActive: boolean;
|
|
|
|
|
isVerified: boolean;
|
|
|
|
|
role?: RoleType[];
|
|
|
|
|
teamId: Types.ObjectId;
|
|
|
|
|
checkTTL: number;
|
|
|
|
|
createdAt: Date;
|
|
|
|
|
updatedAt: Date;
|
|
|
|
|
comparePassword: (submittedPassword: string) => Promise<boolean>;
|
|
|
|
|
// V2
|
|
|
|
|
roles: Types.ObjectId[];
|
|
|
|
|
lastLoginAt?: Date;
|
|
|
|
|
version: number;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const UserSchema = new Schema<IUser>(
|
2025-04-20 11:29:53 -07:00
|
|
|
{
|
|
|
|
|
firstName: {
|
|
|
|
|
type: String,
|
|
|
|
|
required: true,
|
|
|
|
|
},
|
|
|
|
|
lastName: {
|
|
|
|
|
type: String,
|
|
|
|
|
required: true,
|
|
|
|
|
},
|
|
|
|
|
email: {
|
|
|
|
|
type: String,
|
|
|
|
|
required: true,
|
|
|
|
|
unique: true,
|
|
|
|
|
},
|
|
|
|
|
password: {
|
|
|
|
|
type: String,
|
2025-09-22 15:56:41 -07:00
|
|
|
required: false,
|
2025-04-20 11:29:53 -07:00
|
|
|
},
|
|
|
|
|
avatarImage: {
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
|
|
|
|
profileImage: {
|
|
|
|
|
data: Buffer,
|
|
|
|
|
contentType: String,
|
|
|
|
|
},
|
|
|
|
|
isActive: {
|
|
|
|
|
type: Boolean,
|
|
|
|
|
default: true,
|
|
|
|
|
},
|
|
|
|
|
isVerified: {
|
|
|
|
|
type: Boolean,
|
|
|
|
|
default: false,
|
|
|
|
|
},
|
|
|
|
|
role: {
|
|
|
|
|
type: [String],
|
2025-09-22 15:56:41 -07:00
|
|
|
default: ["user"],
|
|
|
|
|
enum: RoleTypes,
|
2025-04-20 11:29:53 -07:00
|
|
|
},
|
|
|
|
|
teamId: {
|
|
|
|
|
type: mongoose.Schema.Types.ObjectId,
|
|
|
|
|
ref: "Team",
|
|
|
|
|
immutable: true,
|
|
|
|
|
},
|
|
|
|
|
checkTTL: {
|
|
|
|
|
type: Number,
|
|
|
|
|
},
|
2025-09-22 15:56:41 -07:00
|
|
|
|
|
|
|
|
// v2
|
|
|
|
|
roles: [
|
|
|
|
|
{
|
|
|
|
|
type: Schema.Types.ObjectId,
|
|
|
|
|
ref: "Role",
|
|
|
|
|
default: [],
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
lastLoginAt: {
|
|
|
|
|
type: Date,
|
|
|
|
|
},
|
|
|
|
|
version: {
|
|
|
|
|
type: Number,
|
|
|
|
|
default: 1,
|
|
|
|
|
},
|
2025-04-20 11:29:53 -07:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
timestamps: true,
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
2025-04-25 12:46:35 -07:00
|
|
|
UserSchema.pre("save", function (next) {
|
2025-04-20 11:29:53 -07:00
|
|
|
if (!this.isModified("password")) {
|
2025-04-25 12:46:35 -07:00
|
|
|
return next();
|
2025-04-20 11:29:53 -07:00
|
|
|
}
|
2025-04-25 12:46:35 -07:00
|
|
|
const salt = bcrypt.genSaltSync(10);
|
|
|
|
|
this.password = bcrypt.hashSync(this.password, salt);
|
2025-04-20 11:29:53 -07:00
|
|
|
next();
|
|
|
|
|
});
|
|
|
|
|
|
2025-04-25 12:46:35 -07:00
|
|
|
UserSchema.pre("findOneAndUpdate", function (next) {
|
2025-04-20 11:29:53 -07:00
|
|
|
const update = this.getUpdate();
|
2025-09-22 15:56:41 -07:00
|
|
|
if (update && "password" in update) {
|
2025-04-25 12:46:35 -07:00
|
|
|
const salt = bcrypt.genSaltSync(10);
|
|
|
|
|
update.password = bcrypt.hashSync(update.password, salt);
|
2025-04-20 11:29:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
next();
|
|
|
|
|
});
|
|
|
|
|
|
2025-06-24 12:07:02 +08:00
|
|
|
UserSchema.pre("findOneAndDelete", async function (next) {
|
|
|
|
|
try {
|
|
|
|
|
const userToDelete = await this.model.findOne(this.getFilter());
|
2025-06-24 13:25:10 +08:00
|
|
|
if (!userToDelete) return next();
|
2025-06-24 12:07:02 +08:00
|
|
|
if (userToDelete.role.includes("superadmin")) {
|
|
|
|
|
await Team.deleteOne({ _id: userToDelete.teamId });
|
|
|
|
|
await Monitor.deleteMany({ userId: userToDelete._id });
|
|
|
|
|
await this.model.deleteMany({
|
|
|
|
|
teamId: userToDelete.teamId,
|
|
|
|
|
_id: { $ne: userToDelete._id },
|
|
|
|
|
});
|
|
|
|
|
await Notification.deleteMany({ teamId: userToDelete.teamId });
|
|
|
|
|
}
|
|
|
|
|
next();
|
|
|
|
|
} catch (error) {
|
2025-09-22 15:56:41 -07:00
|
|
|
next(error as Error);
|
2025-06-24 12:07:02 +08:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2025-09-22 15:56:41 -07:00
|
|
|
UserSchema.methods.comparePassword = async function (submittedPassword: string) {
|
2025-04-20 11:29:53 -07:00
|
|
|
const res = await bcrypt.compare(submittedPassword, this.password);
|
|
|
|
|
return res;
|
|
|
|
|
};
|
|
|
|
|
|
2025-09-22 15:56:41 -07:00
|
|
|
export const User = mongoose.model<IUser>("User", UserSchema);
|