import { Prop, Schema, SchemaFactory } from "@nestjs/mongoose"; import { Types } from "mongoose"; import { RoleEnum } from "src/Types&Enums/role.enum"; @Schema({ collection: "financial-expert", versionKey: false, timestamps: true, }) export class FinancialExpertModel { _id?: Types.ObjectId; @Prop({ required: true }) firstName: string; @Prop({ required: true }) lastName: string; @Prop({ type: String, unique: true, sparse: true, required: false }) email?: string; @Prop({ type: String }) username?: string; @Prop({ type: String, index: true, sparse: true }) nationalCode?: string; @Prop({ type: Types.ObjectId, index: true }) clientKey?: Types.ObjectId; @Prop({ type: Types.ObjectId, index: true }) branchId?: Types.ObjectId; @Prop({ required: true }) password: string; @Prop({ required: false }) mobile?: string; @Prop({ required: false }) phone?: string; @Prop({ default: RoleEnum.FINANCIAL_EXPERT }) role: RoleEnum; @Prop({ type: "string", default: "" }) otp: string; @Prop({ type: "string", required: false }) expertCode?: string; createdAt: Date; } export const FinancialExpertDbSchema = SchemaFactory.createForClass( FinancialExpertModel, ); FinancialExpertDbSchema.index( { clientKey: 1, nationalCode: 1 }, { unique: true, sparse: true }, ); FinancialExpertDbSchema.pre("save", function (next) { if (!this.username) { this.username = (this as any).email || (this as any).nationalCode; } next(); });