From 61b7c4993fb3004a90206291f130025886aad677 Mon Sep 17 00:00:00 2001 From: Andres Pinto Date: Thu, 7 Mar 2024 11:39:23 -0400 Subject: [PATCH] fix: gateway update tier does not fail anymore --- src/app/routes/gateway.js | 12 ++++++++---- src/app/services/featureLimit.js | 6 ++++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/app/routes/gateway.js b/src/app/routes/gateway.js index 34b3b234..2f4771a2 100644 --- a/src/app/routes/gateway.js +++ b/src/app/routes/gateway.js @@ -79,17 +79,21 @@ module.exports = (Router, Service) => { let user = await Service.User.FindUserByUuid(uuid); if (!user) { - Logger.error('[Gateway]: Failed to get user :%s', uuid); + Logger.error('[GATEWAY/TIER]: Failed to get user :%s', uuid); return res.status(500).send(); } - const paidPlanTier = await Service.FeatureLimits.getTierByPlanId( + let paidPlanTier = await Service.FeatureLimits.getTierByPlanId( planId === 'free_individual_tier' ? INDIVIDUAL_FREE_TIER_PLAN_ID : planId, ); + if (!paidPlanTier) { - Logger.error(`[GATEWAY]: Plan id not found id: ${planId} email: ${user.email}`); - return res.status(500).send({ error: 'Plan was not found' }); + Logger.error( + `[GATEWAY/TIER]: Plan id not found, assigning free tier by default. id: ${planId}, email: ${user.email}`, + ); + paidPlanTier = await Service.FeatureLimits.getIndividualFreeTier(); } + await Service.User.updateTier(user, paidPlanTier.tierId); return res.status(200).send({ error: null, user: { ...user.dataValues, tierId: paidPlanTier.tierId } }); }); diff --git a/src/app/services/featureLimit.js b/src/app/services/featureLimit.js index ba18fa55..d2899bcb 100644 --- a/src/app/services/featureLimit.js +++ b/src/app/services/featureLimit.js @@ -1,3 +1,4 @@ +const { INDIVIDUAL_FREE_TIER_PLAN_ID } = require('../constants'); module.exports = (Model, App) => { const getTierByPlanId = async (planId) => { @@ -7,8 +8,13 @@ module.exports = (Model, App) => { }); }; + const getIndividualFreeTier = async () => { + return getTierByPlanId(INDIVIDUAL_FREE_TIER_PLAN_ID); + }; + return { Name: 'FeatureLimits', getTierByPlanId, + getIndividualFreeTier, }; };