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
@@ -35,7 +35,7 @@ export class InvoicesService {
private readonly logger = new Logger(InvoicesService.name);
constructor(
@InjectQueue(INVOICE.INVOICE_QUEUE_NAME) private readonly invoiceQueue: Queue,
@InjectQueue(INVOICE.QUEUE_NAME) private readonly invoiceQueue: Queue,
private readonly notificationsService: NotificationsService,
private readonly invoiceRepository: InvoicesRepository,
private readonly invoiceItemsRepository: InvoiceItemsRepository,
@@ -380,12 +380,13 @@ export class InvoicesService {
);
await this.invoiceQueue.add(
INVOICE.INVOICE_REMINDER_JOB_NAME,
INVOICE.REMINDER_JOB_NAME,
{ invoiceId: invoice.id },
{
delay: dayjs(invoice.dueDate).subtract(INVOICE.DAYS_BEFORE_OVERDUE, "day").diff(dayjs()),
attempts: INVOICE.INVOICE_REMINDER_JOB_ATTEMPTS,
backoff: { type: "exponential", delay: INVOICE.INVOICE_REMINDER_JOB_BACKOFF },
attempts: INVOICE.REMINDER_JOB_ATTEMPTS,
backoff: { type: "exponential", delay: INVOICE.REMINDER_JOB_BACKOFF },
priority: INVOICE.REMINDER_JOB_PRIORITY,
},
);
@@ -470,9 +471,11 @@ export class InvoicesService {
async payInvoice(invoiceId: string, userId: string) {
const queryRunner = this.dataSource.createQueryRunner();
await queryRunner.connect();
await queryRunner.startTransaction();
try {
await queryRunner.connect();
await queryRunner.startTransaction();
const user = await this.usersService.findOneByIdWithQueryRunner(userId, queryRunner);
const invoice = await this.getPendingInvoiceByIdWithQueryRunner(invoiceId, user.id, queryRunner);
@@ -494,6 +497,7 @@ export class InvoicesService {
//
await queryRunner.manager.save(UserSubscription, userSubscription);
await this.walletsService.createSubscriptionTransaction(invoice.totalPrice, userWallet.id, queryRunner);
await this.addNotifyAdminForSubscriptionPaymentToQueue(invoice);
//
} else {
await this.walletsService.createInvoiceTransaction(invoice.totalPrice, userWallet.id, queryRunner);
@@ -603,40 +607,6 @@ export class InvoicesService {
return delayMs;
}
//*********************************** */
private async scheduleInvoiceJobs(invoice: Invoice, createDto?: CreateInvoiceDto) {
// Add to queue for billing reminder
await this.invoiceQueue.add(
INVOICE.INVOICE_REMINDER_JOB_NAME,
{ invoiceId: invoice.id },
{
delay: dayjs(invoice.dueDate).subtract(INVOICE.DAYS_BEFORE_OVERDUE, "day").diff(dayjs()),
attempts: INVOICE.INVOICE_REMINDER_JOB_ATTEMPTS,
backoff: { type: "exponential", delay: INVOICE.INVOICE_REMINDER_JOB_BACKOFF },
},
);
if (createDto?.isRecurring && !createDto?.recurringPeriod) throw new BadRequestException(InvoiceMessage.RECURRING_PERIOD_REQUIRED);
// Add to queue for recurring invoice if applicable
if (createDto?.isRecurring && createDto?.recurringPeriod) {
await this.invoiceQueue.add(
INVOICE.INVOICE_RECURRING_JOB_NAME,
{
invoiceId: invoice.id,
adminCreated: true,
},
{
delay: await this.calculateRecurringDelay(createDto.recurringPeriod),
attempts: INVOICE.INVOICE_RECURRING_JOB_ATTEMPTS,
backoff: { type: "exponential", delay: INVOICE.INVOICE_REMINDER_JOB_BACKOFF },
},
);
this.logger.log(`Scheduled recurring invoice for user ${createDto.userId} with interval ${createDto.recurringPeriod}`);
}
}
//*********************************** */
async applyDiscount(invoiceId: string, discountCode: string, userId: string) {
const queryRunner = this.dataSource.createQueryRunner();
@@ -721,4 +691,53 @@ export class InvoicesService {
if (!invoice) throw new BadRequestException(InvoiceMessage.NOT_FOUND_BY_ID_OR_NOT_BELONG_TO_USER);
return invoice;
}
//*********************************** */
private async scheduleInvoiceJobs(invoice: Invoice, createDto?: CreateInvoiceDto) {
// Add to queue for billing reminder
await this.invoiceQueue.add(
INVOICE.REMINDER_JOB_NAME,
{ invoiceId: invoice.id },
{
delay: dayjs(invoice.dueDate).subtract(INVOICE.DAYS_BEFORE_OVERDUE, "day").diff(dayjs()),
attempts: INVOICE.REMINDER_JOB_ATTEMPTS,
backoff: { type: "exponential", delay: INVOICE.REMINDER_JOB_BACKOFF },
priority: INVOICE.REMINDER_JOB_PRIORITY,
},
);
if (createDto?.isRecurring && !createDto?.recurringPeriod) throw new BadRequestException(InvoiceMessage.RECURRING_PERIOD_REQUIRED);
// Add to queue for recurring invoice if applicable
if (createDto?.isRecurring && createDto?.recurringPeriod) {
await this.invoiceQueue.add(
INVOICE.RECURRING_JOB_NAME,
{
invoiceId: invoice.id,
adminCreated: true,
},
{
delay: await this.calculateRecurringDelay(createDto.recurringPeriod),
attempts: INVOICE.RECURRING_JOB_ATTEMPTS,
backoff: { type: "exponential", delay: INVOICE.RECURRING_JOB_BACKOFF },
priority: INVOICE.RECURRING_JOB_PRIORITY,
},
);
this.logger.log(`Scheduled recurring invoice for user ${createDto.userId} with interval ${createDto.recurringPeriod}`);
}
}
//*********************************** */
private async addNotifyAdminForSubscriptionPaymentToQueue(invoice: Invoice) {
await this.invoiceQueue.add(
INVOICE.SUBSCRIPTION_ADMIN_NOTIFICATION_JOB_NAME,
{ invoiceId: invoice.id },
{
delay: INVOICE.SUBSCRIPTION_ADMIN_NOTIFICATION_JOB_DELAY,
attempts: INVOICE.SUBSCRIPTION_ADMIN_NOTIFICATION_JOB_ATTEMPTS,
backoff: { type: "exponential", delay: INVOICE.SUBSCRIPTION_ADMIN_NOTIFICATION_JOB_BACKOFF },
priority: INVOICE.SUBSCRIPTION_ADMIN_NOTIFICATION_JOB_PRIORITY,
},
);
}
}