import { Document, Model, Schema, model, models } from "mongoose"; import { FindCompanyPolicyParams, ICompanyNotificationPolicy, ICompanyNotificationPolicyAdapter, } from "./CompanyNotificationPolicy.Interface"; export interface ICompanyNotificationPolicyDocument extends Omit, Document {} export class CompanyNotificationPolicyAdapterMongoose implements ICompanyNotificationPolicyAdapter { schema: Schema; policyList: Model; constructor() { this.schema = new Schema( { companyId: { type: Schema.Types.ObjectId, required: true, unique: true, ref: "Company", }, defaultChannels: { type: [String], required: true, default: ["whatsapp", "email", "system"], enum: ["whatsapp", "email", "system"], }, mutedChannels: { type: [String], required: false, default: [], enum: ["whatsapp", "email", "system"], }, timezone: { type: String, required: true, default: "America/Argentina/Buenos_Aires", }, quietHours: { from: { type: String, required: false }, to: { type: String, required: false }, }, reminderRules: [ { offset: { type: Number, required: true }, enabled: { type: Boolean, required: true, default: true }, }, ], }, { timestamps: true, } ); this.policyList = models.CompanyNotificationPolicy || model( "CompanyNotificationPolicy", this.schema ); } public async findOne( filters: FindCompanyPolicyParams ): Promise { const doc = await this.policyList.findOne(filters).exec(); return doc ? (doc.toObject() as ICompanyNotificationPolicy) : null; } public async upsert( companyId: string, data: Partial> ): Promise { const doc = await this.policyList .findOneAndUpdate({ companyId }, { $set: data }, { upsert: true, new: true }) .exec(); return doc.toObject() as ICompanyNotificationPolicy; } }