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