From 410c4ea3e215746dd5e02488c3ee87719c30dfb0 Mon Sep 17 00:00:00 2001 From: Ahmed Bouhuolia Date: Fri, 30 Aug 2024 17:52:53 +0200 Subject: [PATCH] fix: Subscription active detarminer --- .../src/api/middleware/SubscriptionMiddleware.ts | 6 ++++-- .../system/models/Subscriptions/PlanSubscription.ts | 13 ++++++------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/packages/server/src/api/middleware/SubscriptionMiddleware.ts b/packages/server/src/api/middleware/SubscriptionMiddleware.ts index 16b758478..8c764f47e 100644 --- a/packages/server/src/api/middleware/SubscriptionMiddleware.ts +++ b/packages/server/src/api/middleware/SubscriptionMiddleware.ts @@ -2,7 +2,6 @@ import { Container } from 'typedi'; import { Request, Response, NextFunction } from 'express'; const SupportedMethods = ['POST', 'PUT']; -const Excluded = []; export default (subscriptionSlug = 'main') => async (req: Request, res: Response, next: NextFunction) => { @@ -22,7 +21,10 @@ export default (subscriptionSlug = 'main') => errors: [{ type: 'TENANT.HAS.NO.SUBSCRIPTION' }], }); } - if (SupportedMethods.includes(req.method) && subscription.inactive()) { + const isMethodSupported = SupportedMethods.includes(req.method); + const isSubscriptionInactive = subscription.inactive(); + + if (isMethodSupported && isSubscriptionInactive) { return res.boom.badRequest(null, { errors: [{ type: 'ORGANIZATION.SUBSCRIPTION.INACTIVE' }], }); diff --git a/packages/server/src/system/models/Subscriptions/PlanSubscription.ts b/packages/server/src/system/models/Subscriptions/PlanSubscription.ts index ff1f7d4fd..13c09bceb 100644 --- a/packages/server/src/system/models/Subscriptions/PlanSubscription.ts +++ b/packages/server/src/system/models/Subscriptions/PlanSubscription.ts @@ -84,7 +84,7 @@ export default class PlanSubscription extends mixin(SystemModel) { /** * Filter the failed payment. - * @param builder + * @param builder */ failedPayment(builder) { builder.where('payment_status', SubscriptionPaymentStatus.Failed); @@ -92,7 +92,7 @@ export default class PlanSubscription extends mixin(SystemModel) { /** * Filter the succeed payment. - * @param builder + * @param builder */ succeedPayment(builder) { builder.where('payment_status', SubscriptionPaymentStatus.Succeed); @@ -136,14 +136,13 @@ export default class PlanSubscription extends mixin(SystemModel) { /** * Check if the subscription is active. - * Crtiria should be: - * - During the trial period and NOT canceled. - * - Not ended. - * + * Crtiria should be active: + * - During the trial period should NOT be canceled. + * - Out of trial period should NOT be ended. * @return {Boolean} */ public active() { - return this.onTrial() || !this.ended(); + return this.onTrial() ? !this.canceled() : !this.ended(); } /**