33 lines
1.0 KiB
TypeScript
33 lines
1.0 KiB
TypeScript
import { hash } from "bcryptjs";
|
|
import { CallbackError, Schema, model } from "mongoose";
|
|
|
|
import { IAdmin } from "./Abstraction/IAdmin";
|
|
|
|
const adminSchema = new Schema<IAdmin>(
|
|
{
|
|
fullName: { type: String, required: true },
|
|
userName: { type: String, required: true },
|
|
email: { type: String, unique: true },
|
|
password: { type: String, minlength: 6 },
|
|
phoneNumber: { type: String, unique: true },
|
|
role: { type: Schema.Types.ObjectId, ref: "Role", required: true },
|
|
permissions: { type: [Schema.Types.ObjectId], ref: "Permission" },
|
|
createdBy: { type: Schema.Types.ObjectId, ref: "Admin" },
|
|
},
|
|
{ timestamps: true, toJSON: { versionKey: false }, id: false },
|
|
);
|
|
|
|
adminSchema.pre("save", async function (next) {
|
|
if (!this.isModified("password")) return next();
|
|
try {
|
|
this.password = await hash(this.password, 10);
|
|
next();
|
|
} catch (error) {
|
|
next(error as CallbackError);
|
|
}
|
|
});
|
|
|
|
const adminModel = model<IAdmin>("Admin", adminSchema);
|
|
|
|
export { adminModel as AdminModel };
|