feat: pay the free subscriptiuon if present in subscribe to plan
This commit is contained in:
@@ -210,6 +210,8 @@ export class SubscriptionsService {
|
||||
|
||||
async subscribeToPlan(serviceId: string, subscribeDto: SubscribeServiceDto, userId: string) {
|
||||
const queryRunner = this.dataSource.createQueryRunner();
|
||||
|
||||
let isFree = false;
|
||||
try {
|
||||
await queryRunner.connect();
|
||||
await queryRunner.startTransaction();
|
||||
@@ -226,6 +228,7 @@ export class SubscriptionsService {
|
||||
if (!plan) throw new BadRequestException(SubscriptionMessage.NOT_FOUND);
|
||||
|
||||
if (plan.isFree) {
|
||||
isFree = true;
|
||||
const oneMonthAgo = dayjs().subtract(1, "month").toDate();
|
||||
const existingFreePlanSubscription = await queryRunner.manager.findOne(UserSubscription, {
|
||||
where: {
|
||||
@@ -279,6 +282,10 @@ export class SubscriptionsService {
|
||||
|
||||
await this.addProvisioningJob(userSubscription, plan, user);
|
||||
|
||||
if (isFree) {
|
||||
await this.invoicesService.payInvoice(invoice.id, user.id, queryRunner);
|
||||
}
|
||||
|
||||
await queryRunner.commitTransaction();
|
||||
return {
|
||||
message: SubscriptionMessage.SUBSCRIBED,
|
||||
|
||||
Reference in New Issue
Block a user