Update isPaid telemetry accounting to be tier-based instead of via slug

This commit is contained in:
Tuan Dang
2023-05-25 12:59:18 +03:00
parent ca41c65fe0
commit 7812061e66
4 changed files with 23 additions and 19 deletions
@@ -51,7 +51,7 @@ export const batchSecrets = async (req: Request, res: Response) => {
if (!workspace) throw WorkspaceNotFoundError(); if (!workspace) throw WorkspaceNotFoundError();
const orgPlan = await EELicenseService.getOrganizationPlan(workspace.organization.toString()); const orgPlan = await EELicenseService.getOrganizationPlan(workspace.organization.toString());
const isPaid = orgPlan.tier < 1; const isPaid = orgPlan.tier >= 1;
const createSecrets: BatchSecret[] = []; const createSecrets: BatchSecret[] = [];
const updateSecrets: BatchSecret[] = []; const updateSecrets: BatchSecret[] = [];
@@ -387,7 +387,7 @@ export const createSecrets = async (req: Request, res: Response) => {
if (!workspace) throw WorkspaceNotFoundError(); if (!workspace) throw WorkspaceNotFoundError();
const orgPlan = await EELicenseService.getOrganizationPlan(workspace.organization.toString()); const orgPlan = await EELicenseService.getOrganizationPlan(workspace.organization.toString());
const isPaid = orgPlan.tier < 1; const isPaid = orgPlan.tier >= 1;
let listOfSecretsToCreate; let listOfSecretsToCreate;
if (Array.isArray(req.body.secrets)) { if (Array.isArray(req.body.secrets)) {
@@ -613,7 +613,7 @@ export const getSecrets = async (req: Request, res: Response) => {
if (!workspace) throw WorkspaceNotFoundError(); if (!workspace) throw WorkspaceNotFoundError();
const orgPlan = await EELicenseService.getOrganizationPlan(workspace.organization.toString()); const orgPlan = await EELicenseService.getOrganizationPlan(workspace.organization.toString());
const isPaid = orgPlan.tier < 1; const isPaid = orgPlan.tier >= 1;
// secrets to return // secrets to return
let secrets: ISecret[] = []; let secrets: ISecret[] = [];
@@ -963,7 +963,7 @@ export const updateSecrets = async (req: Request, res: Response) => {
if (!workspace) throw WorkspaceNotFoundError(); if (!workspace) throw WorkspaceNotFoundError();
const orgPlan = await EELicenseService.getOrganizationPlan(workspace.organization.toString()); const orgPlan = await EELicenseService.getOrganizationPlan(workspace.organization.toString());
const isPaid = orgPlan.tier < 1; const isPaid = orgPlan.tier >= 1;
const postHogClient = await TelemetryService.getPostHogClient(); const postHogClient = await TelemetryService.getPostHogClient();
if (postHogClient) { if (postHogClient) {
@@ -1100,13 +1100,11 @@ export const deleteSecrets = async (req: Request, res: Response) => {
workspaceId: new Types.ObjectId(key) workspaceId: new Types.ObjectId(key)
}); });
const organizationId = ( const workspace = await Workspace.findById(key);
await Workspace.findOne({ if (!workspace) throw WorkspaceNotFoundError();
_id: key
}) const orgPlan = await EELicenseService.getOrganizationPlan(workspace.organization.toString());
)?.organization?.toString(); const isPaid = orgPlan.tier >= 1;
const orgPlan = await EELicenseService.getOrganizationPlan(organizationId || '');
const isPaid = orgPlan.slug != 'starter';
const postHogClient = await TelemetryService.getPostHogClient(); const postHogClient = await TelemetryService.getPostHogClient();
if (postHogClient) { if (postHogClient) {
@@ -7,13 +7,12 @@ 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) => {
const plan = await EELicenseService.getOrganizationPlan(req.organization._id.toString()); const { organizationId } = req.params;
// cache fetched plan for organization const plan = await EELicenseService.getOrganizationPlan(organizationId);
EELicenseService.localFeatureSet.set(req.organization._id.toString(), plan);
return res.status(200).send({ return res.status(200).send({
plan plan,
}); });
} }
+9 -4
View File
@@ -17,7 +17,7 @@ import { OrganizationNotFoundError } from '../../utils/errors';
interface FeatureSet { interface FeatureSet {
_id: string | null; _id: string | null;
slug: 'starter' | 'team' | 'pro' | 'enterprise' | null; slug: 'starter' | 'team' | 'pro' | 'enterprise' | null;
tier: number | null; tier: number;
projectLimit: number | null; projectLimit: number | null;
memberLimit: number | null; memberLimit: number | null;
secretVersioning: boolean; secretVersioning: boolean;
@@ -63,11 +63,13 @@ class EELicenseService {
}); });
} }
public async getOrganizationPlan(organizationId: string) { public async getOrganizationPlan(organizationId: string): Promise<FeatureSet> {
try { try {
if (this.instanceType === 'cloud') { if (this.instanceType === 'cloud') {
const cachedPlan = this.localFeatureSet.get(organizationId); const cachedPlan = this.localFeatureSet.get<FeatureSet>(organizationId);
if (cachedPlan) return cachedPlan; if (cachedPlan) {
return cachedPlan;
}
const organization = await Organization.findById(organizationId); const organization = await Organization.findById(organizationId);
if (!organization) throw OrganizationNotFoundError(); if (!organization) throw OrganizationNotFoundError();
@@ -76,6 +78,9 @@ class EELicenseService {
`${await getLicenseServerUrl()}/api/license-server/v1/customers/${organization.customerId}/cloud-plan` `${await getLicenseServerUrl()}/api/license-server/v1/customers/${organization.customerId}/cloud-plan`
); );
// cache fetched plan for organization
this.localFeatureSet.set(organizationId, currentPlan);
return currentPlan; return currentPlan;
} }
} catch (err) { } catch (err) {
+2
View File
@@ -251,6 +251,8 @@ const updateSubscriptionOrgQuantity = async ({
quantity quantity
} }
); );
EELicenseService.localFeatureSet.del(organizationId);
} }
if (EELicenseService.instanceType === 'enterprise-self-hosted') { if (EELicenseService.instanceType === 'enterprise-self-hosted') {