From b98b99e7a29a2673bbdd9cbeb8f448e3c053072d Mon Sep 17 00:00:00 2001 From: Guilherme Gazzo Date: Fri, 20 Oct 2023 21:04:37 -0300 Subject: [PATCH] feat: make sure every preventActions contains a value (#30708) --- ee/packages/license/src/license.ts | 44 ++++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 6 deletions(-) diff --git a/ee/packages/license/src/license.ts b/ee/packages/license/src/license.ts index e8be1df85eac..fb290d541cfb 100644 --- a/ee/packages/license/src/license.ts +++ b/ee/packages/license/src/license.ts @@ -27,6 +27,7 @@ import { isBehaviorsInResult } from './validation/isBehaviorsInResult'; import { isReadyForValidation } from './validation/isReadyForValidation'; import { runValidation } from './validation/runValidation'; import { validateFormat } from './validation/validateFormat'; +import { validateLicenseLimits } from './validation/validateLicenseLimits'; const globalLimitKinds: LicenseLimitKind[] = ['activeUsers', 'guestUsers', 'privateApps', 'marketplaceApps', 'monthlyActiveContacts']; @@ -303,12 +304,43 @@ export class LicenseManager extends Emitter { } } - public get shouldPreventActionResultsMap(): { + public async shouldPreventActionResultsMap(): Promise<{ [key in LicenseLimitKind]: boolean; - } { - return Object.fromEntries(this.shouldPreventActionResults.entries()) as { - [key in LicenseLimitKind]: boolean; - }; + }> { + const keys: LicenseLimitKind[] = [ + 'activeUsers', + 'guestUsers', + 'roomsPerGuest', + 'privateApps', + 'marketplaceApps', + 'monthlyActiveContacts', + ]; + + const items = await Promise.all( + keys.map(async (limit) => { + const cached = this.shouldPreventActionResults.get(limit as LicenseLimitKind); + + if (cached !== undefined) { + return [limit as LicenseLimitKind, cached]; + } + + const fresh = this._license + ? isBehaviorsInResult( + await validateLicenseLimits.call(this, this._license, { + behaviors: ['prevent_action'], + limits: [limit], + }), + ['prevent_action'], + ) + : false; + + this.shouldPreventActionResults.set(limit as LicenseLimitKind, fresh); + + return [limit as LicenseLimitKind, fresh]; + }), + ); + + return Object.fromEntries(items); } public async shouldPreventAction( @@ -408,7 +440,7 @@ export class LicenseManager extends Emitter { return { license: (includeLicense && license) || undefined, activeModules, - preventedActions: this.shouldPreventActionResultsMap, + preventedActions: await this.shouldPreventActionResultsMap(), limits: limits as Record, tags: license?.information.tags || [], trial: Boolean(license?.information.trial),