import { Prop, Schema, SchemaFactory } from "@nestjs/mongoose"; import { Types } from "mongoose"; import { RoleEnum } from "src/Types&Enums/role.enum"; @Schema({ _id: false, versionKey: false }) export class FileMakerLocationModel { @Prop({ type: String, required: true }) id: string; @Prop({ type: String, required: true }) name: string; } export const FileMakerLocationDbSchema = SchemaFactory.createForClass( FileMakerLocationModel, ); @Schema({ collection: "file-maker", versionKey: false, timestamps: true, }) export class FileMakerModel { _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({ type: [FileMakerLocationDbSchema], default: [] }) locations?: FileMakerLocationModel[]; @Prop({ required: true }) password: string; @Prop({ required: false }) mobile?: string; @Prop({ required: false }) phone?: string; @Prop({ type: String }) city?: string; @Prop({ type: String }) state?: string; @Prop({ type: String }) address?: string; @Prop({ default: RoleEnum.FILE_MAKER }) role: RoleEnum; @Prop({ type: "string", default: "" }) otp: string; @Prop({ type: "string", required: false }) expertCode?: string; @Prop({ type: String, required: false }) ThirdPartyClaimExpertId?: string; @Prop({ type: String, required: false }) CarBodyClaimExpertId?: string; createdAt: Date; } export const FileMakerDbSchema = SchemaFactory.createForClass(FileMakerModel); FileMakerDbSchema.index( { clientKey: 1, nationalCode: 1 }, { unique: true, sparse: true }, ); FileMakerDbSchema.pre("save", function (next) { if (!this.username) { this.username = (this as any).email || (this as any).nationalCode; } next(); });