71 lines
2.3 KiB
TypeScript
71 lines
2.3 KiB
TypeScript
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;
|
|
}
|
|
}
|