Files
shop-api/src/modules/admin/models/admin.model.ts
T
morteza fa1a029298
Build and Deploy / build_and_deploy (push) Has been cancelled
use bycryptjs
2026-06-23 00:03:28 +03:30

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 };