import { Document, Model, Schema, model, models } from "mongoose"; import { FindClientCompanyOverrideParams, IClientCompanyNotificationOverride, IClientCompanyNotificationOverrideAdapter, } from "./ClientCompanyNotificationOverride.Interface"; export interface IClientCompanyNotificationOverrideDocument extends Omit, Document {} export class ClientCompanyNotificationOverrideAdapterMongoose implements IClientCompanyNotificationOverrideAdapter { schema: Schema; overrideList: Model; constructor() { this.schema = new Schema( { clientId: { type: Schema.Types.ObjectId, required: true, ref: "Client", }, companyId: { type: Schema.Types.ObjectId, required: true, ref: "Company", }, preferredChannels: { type: [String], required: true, default: ["whatsapp", "email", "system"], enum: ["whatsapp", "email", "system"], }, mutedChannels: { type: [String], required: true, default: [], enum: ["whatsapp", "email", "system"], }, }, { timestamps: true, } ); this.schema.index({ clientId: 1, companyId: 1 }, { unique: true }); this.overrideList = models.ClientCompanyNotificationOverride || model( "ClientCompanyNotificationOverride", this.schema ); } public async findOne( filters: FindClientCompanyOverrideParams ): Promise { const doc = await this.overrideList.findOne(filters).exec(); return doc ? (doc.toObject() as IClientCompanyNotificationOverride) : null; } public async upsert( clientId: string, companyId: string, data: Partial< Omit< IClientCompanyNotificationOverride, "id" | "clientId" | "companyId" | "createdAt" | "updatedAt" > > ): Promise { const doc = await this.overrideList .findOneAndUpdate( { clientId, companyId }, { $set: data }, { upsert: true, new: true } ) .exec(); return doc.toObject() as IClientCompanyNotificationOverride; } }