import { Document, Model, Schema, model, models } from "mongoose"; import { FindClientPreferencesParams, IClientNotificationPreferences, IClientNotificationPreferencesAdapter, } from "./ClientNotificationPreferences.Interface"; export interface IClientNotificationPreferencesDocument extends Omit, Document {} export class ClientNotificationPreferencesAdapterMongoose implements IClientNotificationPreferencesAdapter { schema: Schema; preferencesList: Model; constructor() { this.schema = new Schema( { userId: { type: Schema.Types.ObjectId, required: true, unique: true, ref: "User", }, 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.preferencesList = models.ClientNotificationPreferences || model( "ClientNotificationPreferences", this.schema ); } public async findOne( filters: FindClientPreferencesParams ): Promise { const doc = await this.preferencesList.findOne(filters).exec(); return doc ? (doc.toObject() as IClientNotificationPreferences) : null; } public async upsert( userId: string, data: Partial< Omit > ): Promise { const doc = await this.preferencesList .findOneAndUpdate({ userId }, { $set: data }, { upsert: true, new: true }) .exec(); return doc.toObject() as IClientNotificationPreferences; } }