chore: add recurring invoice proccesor logic
This commit is contained in:
@@ -24,6 +24,7 @@ import { INVOICE } from "../constants";
|
||||
import { CreateInvoiceDto } from "../DTO/create-invoice.dto";
|
||||
import { InvoicesSearchQueryDto, UserInvoicesSearchQueryDto } from "../DTO/invoices-search-query.dto";
|
||||
import { Invoice } from "../entities/invoice.entity";
|
||||
import { RecurringPeriodEnum } from "../enums/invoice-recurring-period.enum";
|
||||
import { InvoiceStatus } from "../enums/invoice-status.enum";
|
||||
import { InvoicesRepository } from "../repositories/invoices.repository";
|
||||
|
||||
@@ -62,8 +63,7 @@ export class InvoicesService {
|
||||
const totalPrice = invoiceItems.reduce((sum, item) => new Decimal(item.totalPrice).add(sum), new Decimal(0));
|
||||
const tax = totalPrice.mul(0.1);
|
||||
|
||||
// const dueDate = dayjs().add(INVOICE.DUEDATE, "day").toDate();
|
||||
const dueDate = dayjs().add(1, "minute").toDate();
|
||||
const dueDate = dayjs().add(INVOICE.DUEDATE, "day").toDate();
|
||||
|
||||
const invoice = queryRunner.manager.create(Invoice, {
|
||||
user: { id: createDto.userId },
|
||||
@@ -91,17 +91,34 @@ export class InvoicesService {
|
||||
},
|
||||
queryRunner,
|
||||
);
|
||||
|
||||
//add this 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()),
|
||||
// delay: 1.5 * 60 * 1000, // 5 minutes in milliseconds
|
||||
attempts: INVOICE.INVOICE_REMINDER_JOB_ATTEMPTS,
|
||||
backoff: INVOICE.INVOICE_REMINDER_JOB_BACKOFF,
|
||||
backoff: { type: "exponential", delay: INVOICE.INVOICE_REMINDER_JOB_BACKOFF },
|
||||
},
|
||||
);
|
||||
//add to queue for recurring invoice
|
||||
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}`);
|
||||
}
|
||||
await queryRunner.commitTransaction();
|
||||
|
||||
return {
|
||||
@@ -264,8 +281,7 @@ export class InvoicesService {
|
||||
{
|
||||
delay: dayjs(invoice.dueDate).subtract(INVOICE.DAYS_BEFORE_OVERDUE, "day").diff(dayjs()),
|
||||
attempts: INVOICE.INVOICE_REMINDER_JOB_ATTEMPTS,
|
||||
backoff: INVOICE.INVOICE_REMINDER_JOB_BACKOFF,
|
||||
repeat: { every: 300000 },
|
||||
backoff: { type: "exponential", delay: INVOICE.INVOICE_REMINDER_JOB_BACKOFF },
|
||||
},
|
||||
);
|
||||
|
||||
@@ -453,7 +469,33 @@ export class InvoicesService {
|
||||
return invoiceCount;
|
||||
}
|
||||
|
||||
//*********************************** */
|
||||
//********************************** */
|
||||
|
||||
private async calculateRecurringDelay(recurringPeriod: RecurringPeriodEnum) {
|
||||
let delayMs: number;
|
||||
switch (recurringPeriod) {
|
||||
case RecurringPeriodEnum.WEEKLY:
|
||||
delayMs = dayjs().add(1, "week").subtract(7, "days").diff(dayjs());
|
||||
break;
|
||||
case RecurringPeriodEnum.MONTHLY:
|
||||
delayMs = dayjs().add(1, "month").subtract(7, "days").diff(dayjs());
|
||||
break;
|
||||
case RecurringPeriodEnum.QUARTERLY:
|
||||
delayMs = dayjs().add(3, "month").subtract(7, "days").diff(dayjs());
|
||||
break;
|
||||
case RecurringPeriodEnum.BIANNUALLY:
|
||||
delayMs = dayjs().add(6, "month").subtract(7, "days").diff(dayjs());
|
||||
break;
|
||||
case RecurringPeriodEnum.ANNUALLY:
|
||||
delayMs = dayjs().add(1, "year").subtract(7, "days").diff(dayjs());
|
||||
break;
|
||||
default:
|
||||
delayMs = dayjs().add(1, "month").subtract(7, "days").diff(dayjs());
|
||||
}
|
||||
|
||||
this.logger.log(`Calculated recurring delay: ${delayMs}ms for period: ${recurringPeriod}`);
|
||||
return delayMs;
|
||||
}
|
||||
|
||||
// async applyDiscount(invoiceId: string, applyDiscountDto: ApplyDiscountDto, userId: string) {
|
||||
// const invoice = await this.invoiceRepository.findOneBy({ id: invoiceId, status: InvoiceStatus.PENDING });
|
||||
|
||||
Reference in New Issue
Block a user