chore: add notif service for new criticisms ticket and subscribtion

This commit is contained in:
mahyargdz
2025-05-04 14:48:39 +03:30
parent 9d50578cc2
commit 170bb867d0
14 changed files with 236 additions and 93 deletions
@@ -15,10 +15,10 @@ import { Invoice } from "../entities/invoice.entity";
import { InvoiceStatus } from "../enums/invoice-status.enum";
import { InvoicesService } from "../providers/invoices.service";
@Processor(INVOICE.INVOICE_QUEUE_NAME, { concurrency: 5 })
@Processor(INVOICE.QUEUE_NAME, { concurrency: 5 })
export class InvoiceProcessor extends WorkerProcessor {
constructor(
@InjectQueue(INVOICE.INVOICE_QUEUE_NAME) private readonly invoiceQueue: Queue,
@InjectQueue(INVOICE.QUEUE_NAME) private readonly invoiceQueue: Queue,
private readonly invoicesService: InvoicesService,
private readonly dataSource: DataSource,
private readonly notificationService: NotificationsService,
@@ -30,10 +30,12 @@ export class InvoiceProcessor extends WorkerProcessor {
async process(job: Job, token?: string) {
this.logger.log(job);
switch (job.name) {
case INVOICE.INVOICE_REMINDER_JOB_NAME:
case INVOICE.REMINDER_JOB_NAME:
return this.sendBillInvoiceReminder(job, token);
case INVOICE.INVOICE_RECURRING_JOB_NAME:
case INVOICE.RECURRING_JOB_NAME:
return this.createRecurringInvoice(job, token);
case INVOICE.SUBSCRIPTION_ADMIN_NOTIFICATION_JOB_NAME:
return this.notifyAdminForSubscriptionInvoice(job);
default:
this.logger.error(`Unknown job name: ${job.name}`);
return;
@@ -176,7 +178,7 @@ export class InvoiceProcessor extends WorkerProcessor {
private async fetchInvoice(invoiceId: string, queryRunner: QueryRunner) {
const invoice = await queryRunner.manager.findOne(Invoice, {
where: { id: invoiceId },
relations: { user: true, items: { subscriptionPlan: { plan: true } } },
relations: { user: true, items: { subscriptionPlan: { plan: { service: true } } } },
});
if (!invoice) throw new Error(`Invoice not found: ${invoiceId}`);
@@ -291,12 +293,12 @@ export class InvoiceProcessor extends WorkerProcessor {
private async scheduleNextReminder(invoiceId: string) {
this.logger.log(`Invoice ${invoiceId} is still unpaid. Scheduling a new reminder.`);
await this.invoiceQueue.add(
INVOICE.INVOICE_REMINDER_JOB_NAME,
INVOICE.REMINDER_JOB_NAME,
{ invoiceId },
{
delay: INVOICE.INVOICE_REMINDER_REPEAT_DELAY, // 1 day delay in milliseconds
attempts: INVOICE.INVOICE_REMINDER_JOB_ATTEMPTS,
backoff: { type: "exponential", delay: INVOICE.INVOICE_REMINDER_JOB_BACKOFF },
delay: INVOICE.REMINDER_REPEAT_DELAY, // 1 day delay in milliseconds
attempts: INVOICE.REMINDER_JOB_ATTEMPTS,
backoff: { type: "exponential", delay: INVOICE.REMINDER_JOB_BACKOFF },
},
);
}
@@ -326,4 +328,43 @@ export class InvoiceProcessor extends WorkerProcessor {
admins.map((admin) => this.notificationService.createRecurringInvoiceNotification(admin.id, notificationPayload, queryRunner)),
);
}
//********************************** */
private async notifyAdminForSubscriptionInvoice(job: Job<{ invoiceId: string }>) {
const queryRunner = this.dataSource.createQueryRunner();
try {
await queryRunner.connect();
await queryRunner.startTransaction();
const { invoiceId } = job.data;
const invoice = await this.fetchInvoice(invoiceId, queryRunner);
const superAdmins = await this.fetchAdmin(queryRunner);
for (const admin of superAdmins) {
await this.notificationService.createNewSubscriptionNotification(
admin.id,
{
date: invoice.dueDate,
userPhone: invoice.user.phone,
userEmail: invoice.user.email,
serviceName: invoice.items[0]?.subscriptionPlan?.plan.service.name ?? "",
fullName: invoice.user.firstName + " " + invoice.user.lastName,
},
queryRunner,
);
}
await queryRunner.commitTransaction();
return true;
} catch (error) {
this.logger.error(`Failed to notify admins for subscription invoice:`, error);
await queryRunner.rollbackTransaction();
throw error;
} finally {
await queryRunner.release();
}
}
}