Skip to content

Commit

Permalink
Merge branch 'feat/limit-files-upload' into feat/limit-files-upload-ci
Browse files Browse the repository at this point in the history
  • Loading branch information
apsantiso committed Mar 4, 2024
2 parents 53ec3ea + cada49e commit b0f7201
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
5 changes: 4 additions & 1 deletion src/app/middleware/feature-limits.middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const build = (Service: {
}
} catch (err) {
if (err instanceof MissingValuesForFeatureLimit) {
return res.status(400).send('You reached the limit for your tier!');
return res.status(400).send(err.message);
}

if (err instanceof NoLimitFoundForUserTierAndLabel) {
Expand Down Expand Up @@ -74,6 +74,9 @@ const extractDataFromRequest = (request: any, dataSources: DataSource[]) => {
const extractedData = {} as any;
for (const { sourceKey, fieldName } of dataSources) {
const value = request[sourceKey][fieldName];
if (value === null || value === undefined) {
throw new MissingValuesForFeatureLimit(`Missing required value to check user limits, ${fieldName} is missing`);
}
extractedData[fieldName] = value;
}
return extractedData;
Expand Down
12 changes: 8 additions & 4 deletions src/app/routes/gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 } });
});
Expand Down
6 changes: 6 additions & 0 deletions src/app/services/featureLimit.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { LimitLabels } = require('../middleware/feature-limits.middleware');
const { NoLimitFoundForUserTierAndLabel } = require('./errors/FeatureLimitsErrors');
const { INDIVIDUAL_FREE_TIER_PLAN_ID } = require('../constants');

const LimitTypes = {
Counter: 'counter',
Expand All @@ -14,6 +15,10 @@ module.exports = (Model, App) => {
});
};

const getIndividualFreeTier = async () => {
return getTierByPlanId(INDIVIDUAL_FREE_TIER_PLAN_ID);
};

const findLimitByLabelAndTier = async (tierId, label) => {
return Model.limits.findOne({
where: {
Expand Down Expand Up @@ -69,5 +74,6 @@ module.exports = (Model, App) => {
Name: 'FeatureLimits',
getTierByPlanId,
shouldLimitBeEnforced,
getIndividualFreeTier,
};
};

0 comments on commit b0f7201

Please sign in to comment.