feat: add appointment notification sharing functionality via WhatsApp, email, and system clipboard
This commit is contained in:
@@ -1,11 +1,21 @@
|
||||
import { Document, Model, Schema, model } from "mongoose";
|
||||
import {
|
||||
CreateJobParams,
|
||||
DeleteOldJobsParams,
|
||||
DeleteOldJobsResult,
|
||||
INotificationJob,
|
||||
INotificationJobAdapter,
|
||||
NotificationJobStatus,
|
||||
} from "./NotificationJobs.Interface";
|
||||
|
||||
export const NOTIFICATION_JOB_CLEANUP_RETENTION_DAYS = 7;
|
||||
export const NOTIFICATION_JOB_CLEANUP_INTERVAL_MS = 60 * 60 * 1000;
|
||||
export const NOTIFICATION_JOB_CLEANUP_STATUSES = [
|
||||
NotificationJobStatus.SENT,
|
||||
NotificationJobStatus.FAILED,
|
||||
NotificationJobStatus.CANCELLED,
|
||||
];
|
||||
|
||||
export interface INotificationJobDocument
|
||||
extends Omit<INotificationJob, "id">,
|
||||
Document {}
|
||||
@@ -13,6 +23,7 @@ export interface INotificationJobDocument
|
||||
export class NotificationJobsAdapterMongoose implements INotificationJobAdapter {
|
||||
schema: Schema;
|
||||
notificationJobList: Model<INotificationJobDocument>;
|
||||
private lastCleanupAt?: number;
|
||||
|
||||
constructor() {
|
||||
this.schema = new Schema(
|
||||
@@ -73,6 +84,7 @@ export class NotificationJobsAdapterMongoose implements INotificationJobAdapter
|
||||
}
|
||||
|
||||
public async create(data: CreateJobParams): Promise<INotificationJob> {
|
||||
await this.cleanupOldJobsIfDueSafely();
|
||||
const doc = await this.notificationJobList.create({
|
||||
...data,
|
||||
status: NotificationJobStatus.PENDING,
|
||||
@@ -111,6 +123,7 @@ export class NotificationJobsAdapterMongoose implements INotificationJobAdapter
|
||||
}
|
||||
|
||||
public async cancelByAppointment(appointmentId: string): Promise<void> {
|
||||
await this.cleanupOldJobsIfDueSafely();
|
||||
await this.notificationJobList
|
||||
.updateMany(
|
||||
{
|
||||
@@ -123,9 +136,52 @@ export class NotificationJobsAdapterMongoose implements INotificationJobAdapter
|
||||
}
|
||||
|
||||
public async findByAppointment(appointmentId: string): Promise<INotificationJob[]> {
|
||||
await this.cleanupOldJobsIfDueSafely();
|
||||
const docs = await this.notificationJobList
|
||||
.find({ appointmentId })
|
||||
.exec();
|
||||
return docs.map((d) => d.toObject() as INotificationJob);
|
||||
}
|
||||
|
||||
public async deleteOldJobs(
|
||||
data: DeleteOldJobsParams
|
||||
): Promise<DeleteOldJobsResult> {
|
||||
const result = await this.notificationJobList
|
||||
.deleteMany({
|
||||
scheduledAt: { $lt: data.scheduledBefore },
|
||||
status: { $in: data.statuses },
|
||||
})
|
||||
.exec();
|
||||
|
||||
return { deletedCount: result.deletedCount || 0 };
|
||||
}
|
||||
|
||||
public async cleanupOldJobsIfDue(now = new Date()): Promise<void> {
|
||||
if (
|
||||
this.lastCleanupAt !== undefined &&
|
||||
now.getTime() - this.lastCleanupAt < NOTIFICATION_JOB_CLEANUP_INTERVAL_MS
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
const scheduledBefore = new Date(now.getTime());
|
||||
scheduledBefore.setDate(
|
||||
scheduledBefore.getDate() - NOTIFICATION_JOB_CLEANUP_RETENTION_DAYS
|
||||
);
|
||||
|
||||
await this.deleteOldJobs({
|
||||
scheduledBefore,
|
||||
statuses: NOTIFICATION_JOB_CLEANUP_STATUSES,
|
||||
});
|
||||
this.lastCleanupAt = now.getTime();
|
||||
}
|
||||
|
||||
private async cleanupOldJobsIfDueSafely(): Promise<void> {
|
||||
try {
|
||||
await this.cleanupOldJobsIfDue();
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : String(error);
|
||||
console.warn(`Notification jobs cleanup failed: ${message}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user