85 lines
2.7 KiB
TypeScript
85 lines
2.7 KiB
TypeScript
import { Document, Model, Schema, model, models } from "mongoose";
|
|
import {
|
|
FindClientCompanyOverrideParams,
|
|
IClientCompanyNotificationOverride,
|
|
IClientCompanyNotificationOverrideAdapter,
|
|
} from "./ClientCompanyNotificationOverride.Interface";
|
|
|
|
export interface IClientCompanyNotificationOverrideDocument
|
|
extends Omit<IClientCompanyNotificationOverride, "id">,
|
|
Document {}
|
|
|
|
export class ClientCompanyNotificationOverrideAdapterMongoose
|
|
implements IClientCompanyNotificationOverrideAdapter
|
|
{
|
|
schema: Schema;
|
|
overrideList: Model<IClientCompanyNotificationOverrideDocument>;
|
|
|
|
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<IClientCompanyNotificationOverrideDocument>(
|
|
"ClientCompanyNotificationOverride",
|
|
this.schema
|
|
);
|
|
}
|
|
|
|
public async findOne(
|
|
filters: FindClientCompanyOverrideParams
|
|
): Promise<IClientCompanyNotificationOverride | null> {
|
|
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<IClientCompanyNotificationOverride> {
|
|
const doc = await this.overrideList
|
|
.findOneAndUpdate(
|
|
{ clientId, companyId },
|
|
{ $set: data },
|
|
{ upsert: true, new: true }
|
|
)
|
|
.exec();
|
|
return doc.toObject() as IClientCompanyNotificationOverride;
|
|
}
|
|
}
|