84 lines
2.8 KiB
TypeScript
84 lines
2.8 KiB
TypeScript
import { Document, Model, Schema, model, models } from "mongoose";
|
|
import {
|
|
FindCompanyPolicyParams,
|
|
ICompanyNotificationPolicy,
|
|
ICompanyNotificationPolicyAdapter,
|
|
} from "./CompanyNotificationPolicy.Interface";
|
|
|
|
export interface ICompanyNotificationPolicyDocument
|
|
extends Omit<ICompanyNotificationPolicy, "id">,
|
|
Document {}
|
|
|
|
export class CompanyNotificationPolicyAdapterMongoose
|
|
implements ICompanyNotificationPolicyAdapter
|
|
{
|
|
schema: Schema;
|
|
policyList: Model<ICompanyNotificationPolicyDocument>;
|
|
|
|
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<ICompanyNotificationPolicyDocument>(
|
|
"CompanyNotificationPolicy",
|
|
this.schema
|
|
);
|
|
}
|
|
|
|
public async findOne(
|
|
filters: FindCompanyPolicyParams
|
|
): Promise<ICompanyNotificationPolicy | null> {
|
|
const doc = await this.policyList.findOne(filters).exec();
|
|
return doc ? (doc.toObject() as ICompanyNotificationPolicy) : null;
|
|
}
|
|
|
|
public async upsert(
|
|
companyId: string,
|
|
data: Partial<Omit<ICompanyNotificationPolicy, "id" | "companyId" | "createdAt" | "updatedAt">>
|
|
): Promise<ICompanyNotificationPolicy> {
|
|
const doc = await this.policyList
|
|
.findOneAndUpdate({ companyId }, { $set: data }, { upsert: true, new: true })
|
|
.exec();
|
|
return doc.toObject() as ICompanyNotificationPolicy;
|
|
}
|
|
}
|