feat: implement comprehensive notification engine with policies, preferences, and automated dispatching services
This commit is contained in:
+70
@@ -0,0 +1,70 @@
|
||||
import { Document, Model, Schema, model, models } from "mongoose";
|
||||
import {
|
||||
FindClientPreferencesParams,
|
||||
IClientNotificationPreferences,
|
||||
IClientNotificationPreferencesAdapter,
|
||||
} from "./ClientNotificationPreferences.Interface";
|
||||
|
||||
export interface IClientNotificationPreferencesDocument
|
||||
extends Omit<IClientNotificationPreferences, "id">,
|
||||
Document {}
|
||||
|
||||
export class ClientNotificationPreferencesAdapterMongoose
|
||||
implements IClientNotificationPreferencesAdapter
|
||||
{
|
||||
schema: Schema;
|
||||
preferencesList: Model<IClientNotificationPreferencesDocument>;
|
||||
|
||||
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<IClientNotificationPreferencesDocument>(
|
||||
"ClientNotificationPreferences",
|
||||
this.schema
|
||||
);
|
||||
}
|
||||
|
||||
public async findOne(
|
||||
filters: FindClientPreferencesParams
|
||||
): Promise<IClientNotificationPreferences | null> {
|
||||
const doc = await this.preferencesList.findOne(filters).exec();
|
||||
return doc ? (doc.toObject() as IClientNotificationPreferences) : null;
|
||||
}
|
||||
|
||||
public async upsert(
|
||||
userId: string,
|
||||
data: Partial<
|
||||
Omit<IClientNotificationPreferences, "id" | "userId" | "createdAt" | "updatedAt">
|
||||
>
|
||||
): Promise<IClientNotificationPreferences> {
|
||||
const doc = await this.preferencesList
|
||||
.findOneAndUpdate({ userId }, { $set: data }, { upsert: true, new: true })
|
||||
.exec();
|
||||
return doc.toObject() as IClientNotificationPreferences;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user