Modularize getOrganizationPlan function

This commit is contained in:
Tuan Dang
2023-05-23 23:54:54 +03:00
parent a8502377c7
commit 96607153dc
2 changed files with 29 additions and 23 deletions
@@ -1,4 +1,3 @@
import * as Sentry from '@sentry/node';
import { Request, Response } from 'express'; import { Request, Response } from 'express';
import { getLicenseServerUrl } from '../../../config'; import { getLicenseServerUrl } from '../../../config';
import { licenseServerKeyRequest } from '../../../config/request'; import { licenseServerKeyRequest } from '../../../config/request';
@@ -8,31 +7,13 @@ import { EELicenseService } from '../../services';
* Return the organization's current plan and allowed feature set * Return the organization's current plan and allowed feature set
*/ */
export const getOrganizationPlan = async (req: Request, res: Response) => { export const getOrganizationPlan = async (req: Request, res: Response) => {
try { const plan = await EELicenseService.getOrganizationPlan(req.organization._id.toString());
if (EELicenseService.instanceType === 'cloud') {
// instance of Infisical is a cloud instance
const cachedPlan = EELicenseService.localFeatureSet.get(req.organization._id.toString()); // cache fetched plan for organization
if (cachedPlan) return cachedPlan; EELicenseService.localFeatureSet.set(req.organization._id.toString(), plan);
const { data: { currentPlan } } = await licenseServerKeyRequest.get(
`${await getLicenseServerUrl()}/api/license-server/v1/customers/${req.organization.customerId}/cloud-plan`
);
// cache fetched plan for organization
EELicenseService.localFeatureSet.set(req.organization._id.toString(), currentPlan);
return res.status(200).send({
plan: currentPlan
});
}
} catch (err) {
Sentry.setUser({ email: req.user.email });
Sentry.captureException(err);
}
return res.status(200).send({ return res.status(200).send({
plan: EELicenseService.globalFeatureSet plan
}); });
} }
@@ -7,9 +7,12 @@ import {
} from '../../config'; } from '../../config';
import { import {
licenseKeyRequest, licenseKeyRequest,
licenseServerKeyRequest,
refreshLicenseServerKeyToken, refreshLicenseServerKeyToken,
refreshLicenseKeyToken refreshLicenseKeyToken
} from '../../config/request'; } from '../../config/request';
import { Organization } from '../../models';
import { OrganizationNotFoundError } from '../../utils/errors';
interface FeatureSet { interface FeatureSet {
_id: string | null; _id: string | null;
@@ -59,6 +62,28 @@ class EELicenseService {
stdTTL: 300 stdTTL: 300
}); });
} }
public async getOrganizationPlan(organizationId: string) {
try {
if (this.instanceType === 'cloud') {
const cachedPlan = this.localFeatureSet.get(organizationId);
if (cachedPlan) return cachedPlan;
const organization = await Organization.findById(organizationId);
if (!organization) throw OrganizationNotFoundError();
const { data: { currentPlan } } = await licenseServerKeyRequest.get(
`${await getLicenseServerUrl()}/api/license-server/v1/customers/${organization.customerId}/cloud-plan`
);
return currentPlan;
}
} catch (err) {
return this.globalFeatureSet;
}
return this.globalFeatureSet;
}
public async initGlobalFeatureSet() { public async initGlobalFeatureSet() {
const licenseServerKey = await getLicenseServerKey(); const licenseServerKey = await getLicenseServerKey();