feat: pay invoice of the free support plan
This commit is contained in:
@@ -518,12 +518,15 @@ export class InvoicesService {
|
||||
|
||||
//*********************************** */
|
||||
|
||||
async payInvoice(invoiceId: string, userId: string) {
|
||||
const queryRunner = this.dataSource.createQueryRunner();
|
||||
async payInvoice(invoiceId: string, userId: string, queryRunner = this.dataSource.createQueryRunner()) {
|
||||
let transactionStarted = false;
|
||||
|
||||
try {
|
||||
await queryRunner.connect();
|
||||
await queryRunner.startTransaction();
|
||||
if (!queryRunner.isTransactionActive) {
|
||||
await queryRunner.connect();
|
||||
await queryRunner.startTransaction();
|
||||
transactionStarted = true;
|
||||
}
|
||||
|
||||
const user = await this.usersService.findOneByIdWithQueryRunner(userId, queryRunner);
|
||||
|
||||
@@ -573,16 +576,17 @@ export class InvoicesService {
|
||||
paidAt: invoice.paidAt,
|
||||
});
|
||||
|
||||
await queryRunner.commitTransaction();
|
||||
if (transactionStarted) await queryRunner.commitTransaction();
|
||||
|
||||
return {
|
||||
message: InvoiceMessage.INVOICE_PAID,
|
||||
invoice,
|
||||
};
|
||||
} catch (error) {
|
||||
await queryRunner.rollbackTransaction();
|
||||
if (transactionStarted) await queryRunner.rollbackTransaction();
|
||||
throw error;
|
||||
} finally {
|
||||
await queryRunner.release();
|
||||
if (transactionStarted) await queryRunner.release();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -152,6 +152,12 @@ export class SupportPlansService {
|
||||
|
||||
await queryRunner.manager.save(this.userSupportPlanRepo.target, userSupportPlan);
|
||||
|
||||
if (supportPlan.isFree) {
|
||||
userSupportPlan.status = UserSupportPlanStatus.ACTIVE;
|
||||
await queryRunner.manager.save(this.userSupportPlanRepo.target, userSupportPlan);
|
||||
await this.invoicesService.payInvoice(invoice.id, user.id, queryRunner);
|
||||
}
|
||||
|
||||
await queryRunner.commitTransaction();
|
||||
return {
|
||||
message: SupportPlanMessage.SUPPORT_PLAN_SUBSCRIBED,
|
||||
@@ -249,6 +255,12 @@ export class SupportPlansService {
|
||||
currentUserSupportPlan.status = UserSupportPlanStatus.CANCELLED;
|
||||
await queryRunner.manager.save(this.userSupportPlanRepo.target, currentUserSupportPlan);
|
||||
|
||||
if (newSupportPlan.isFree) {
|
||||
currentUserSupportPlan.status = UserSupportPlanStatus.ACTIVE;
|
||||
await queryRunner.manager.save(this.userSupportPlanRepo.target, currentUserSupportPlan);
|
||||
await this.invoicesService.payInvoice(invoice.id, user.id, queryRunner);
|
||||
}
|
||||
|
||||
await queryRunner.commitTransaction();
|
||||
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user