feat: implement invoice processing and discount management
This commit is contained in:
@@ -8,6 +8,9 @@ import { WorkerProcessor } from "../../../common/queues/worker.processor";
|
||||
import { LoggerService } from "../../logger/logger.service";
|
||||
import { NotificationQueue } from "../../notifications/queue/notification.queue";
|
||||
import { SubscriptionStatus } from "../../subscriptions/enums/subscription-status.enum";
|
||||
import { SupportPlan } from "../../support-plans/entities/support-plan.entity";
|
||||
import { UserSupportPlan } from "../../support-plans/entities/user-support-plan.entity";
|
||||
import { UserSupportPlanStatus } from "../../support-plans/enums/user-support-plan-status.enum";
|
||||
import { User } from "../../users/entities/user.entity";
|
||||
import { RoleEnum } from "../../users/enums/role.enum";
|
||||
import { INVOICE } from "../constants";
|
||||
@@ -210,6 +213,16 @@ export class InvoiceProcessor extends WorkerProcessor {
|
||||
invoiceId: invoice.numericId.toString(),
|
||||
});
|
||||
}
|
||||
if (invoice.items[0]?.supportPlan) {
|
||||
this.logger.log(
|
||||
`Invoice ${invoice.id} has been cancelled as it is more than 10 days overdue. Support Plan ${invoice.items[0].supportPlan.id} has been cancelled.`,
|
||||
);
|
||||
const userSupportPlan = invoice.items[0].supportPlan;
|
||||
userSupportPlan.status = UserSupportPlanStatus.INACTIVE;
|
||||
await queryRunner.manager.save(UserSupportPlan, userSupportPlan);
|
||||
|
||||
await this.applyFreeSupportPlan(invoice, queryRunner);
|
||||
}
|
||||
this.logger.log(`Invoice ${invoice.id} has been cancelled as it is more than 10 days overdue.`);
|
||||
}
|
||||
//********************************** */
|
||||
@@ -348,4 +361,39 @@ export class InvoiceProcessor extends WorkerProcessor {
|
||||
await queryRunner.release();
|
||||
}
|
||||
}
|
||||
|
||||
//********************************** */
|
||||
private async applyFreeSupportPlan(invoice: Invoice, queryRunner: QueryRunner) {
|
||||
const freeSupportPlan = await queryRunner.manager.findOne(SupportPlan, {
|
||||
where: { isFree: true },
|
||||
});
|
||||
|
||||
if (!freeSupportPlan) throw new Error(`No free support plan found`);
|
||||
|
||||
const startDate = dayjs().toDate();
|
||||
const endDate = dayjs().add(freeSupportPlan.duration, "day").toDate();
|
||||
|
||||
const userSupportPlan = queryRunner.manager.create(UserSupportPlan, {
|
||||
user: invoice.user,
|
||||
supportPlan: freeSupportPlan,
|
||||
startDate,
|
||||
endDate,
|
||||
});
|
||||
|
||||
await queryRunner.manager.save(UserSupportPlan, userSupportPlan);
|
||||
|
||||
const invoiceDueDate = dayjs(userSupportPlan.startDate).add(INVOICE.DUEDATE, "day").toDate();
|
||||
|
||||
const newInvoice = await this.invoicesService.createInvoiceForSupportPlan(
|
||||
invoice.user,
|
||||
freeSupportPlan,
|
||||
userSupportPlan,
|
||||
invoiceDueDate,
|
||||
queryRunner,
|
||||
);
|
||||
|
||||
userSupportPlan.status = UserSupportPlanStatus.ACTIVE;
|
||||
await queryRunner.manager.save(UserSupportPlan, userSupportPlan);
|
||||
await this.invoicesService.payInvoice(newInvoice.id, invoice.user.id, queryRunner);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user