notification queue

This commit is contained in:
2025-12-13 23:26:07 +03:30
parent 835f2a3853
commit befa311e98
20 changed files with 176 additions and 405 deletions
@@ -2,7 +2,7 @@ import { Injectable, Logger } from '@nestjs/common';
import { InjectQueue } from '@nestjs/bullmq';
import { Queue } from 'bullmq';
import { NotificationQueueNameEnum } from '../constants/queue';
import { NotificationQueueJob } from '../interfaces/notification-queue.interface';
import { PushNotificationQueueJob, SmsNotificationQueueJob } from '../interfaces/jobs-queue.interface';
@Injectable()
export class NotificationQueueService {
@@ -13,9 +13,9 @@ export class NotificationQueueService {
@InjectQueue(NotificationQueueNameEnum.PUSH) private readonly pushQueue: Queue,
) {}
async addSmsNotification(job: NotificationQueueJob): Promise<void> {
async addSmsNotification(job: SmsNotificationQueueJob): Promise<void> {
try {
const jobId = job.idempotencyKey || `${job.restaurantId}-${job.title}-${Date.now()}`;
const jobId = `${job.templateId}-${JSON.stringify(job.parameters)}-${Date.now()}`;
await this.smsQueue.add('sms-notification', job, {
jobId,
@@ -40,9 +40,9 @@ export class NotificationQueueService {
}
}
async addPushNotification(job: NotificationQueueJob): Promise<void> {
async addPushNotification(job: PushNotificationQueueJob): Promise<void> {
try {
const jobId = job.idempotencyKey || `${job.restaurantId}-${job.title}-${Date.now()}`;
const jobId = `${job.title}-${JSON.stringify(job.action)}-${Date.now()}`;
await this.pushQueue.add('push-notification', job, {
jobId,
@@ -67,26 +67,49 @@ export class NotificationQueueService {
}
}
// async addBulkNotifications(jobs: NotificationQueueJob[]): Promise<void> {
// try {
// const queueJobs = jobs.map(job => ({
// name: 'send-notification',
// data: job,
// opts: {
// jobId: job.idempotencyKey || `${job.restaurantId}-${job.title}-${Date.now()}`,
// attempts: 3,
// backoff: {
// type: 'exponential',
// delay: 2000,
// },
// },
// }));
async addBulkSmsNotifications(jobs: SmsNotificationQueueJob[]): Promise<void> {
try {
const queueJobs = jobs.map(job => ({
name: 'sms-notification',
data: job,
opts: {
jobId: `${job.templateId}-${JSON.stringify(job.parameters)}-${Date.now()}`,
attempts: 3,
backoff: {
type: 'exponential',
delay: 2000,
},
},
}));
// await this.notificationQueue.addBulk(queueJobs);
// this.logger.log(`Added ${jobs.length} notification jobs to queue`);
// } catch (error) {
// this.logger.error(`Failed to add bulk notifications to queue:`, error);
// throw error;
// }
// }
await this.smsQueue.addBulk(queueJobs);
this.logger.log(`Added ${jobs.length} SMS notification jobs to queue`);
} catch (error) {
this.logger.error(`Failed to add bulk notifications to queue:`, error);
throw error;
}
}
async addBulkPushNotifications(jobs: PushNotificationQueueJob[]): Promise<void> {
try {
const queueJobs = jobs.map(job => ({
name: 'push-notification',
data: job,
opts: {
jobId: `${job.title}-${JSON.stringify(job.action)}-${Date.now()}`,
attempts: 3,
backoff: {
type: 'exponential',
delay: 2000,
},
},
}));
await this.pushQueue.addBulk(queueJobs);
this.logger.log(`Added ${jobs.length} Push notification jobs to queue`);
} catch (error) {
this.logger.error(`Failed to add bulk notifications to queue:`, error);
throw error;
}
}
}