Skip to content

Commit

Permalink
feat: make sure every preventActions contains a value (#30708)
Browse files Browse the repository at this point in the history
  • Loading branch information
ggazzo authored Oct 21, 2023
1 parent bba3c9d commit b98b99e
Showing 1 changed file with 38 additions and 6 deletions.
44 changes: 38 additions & 6 deletions ee/packages/license/src/license.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'];

Expand Down Expand Up @@ -303,12 +304,43 @@ export class LicenseManager extends Emitter<LicenseEvents> {
}
}

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<T extends LicenseLimitKind>(
Expand Down Expand Up @@ -408,7 +440,7 @@ export class LicenseManager extends Emitter<LicenseEvents> {
return {
license: (includeLicense && license) || undefined,
activeModules,
preventedActions: this.shouldPreventActionResultsMap,
preventedActions: await this.shouldPreventActionResultsMap(),
limits: limits as Record<LicenseLimitKind, { max: number; value: number }>,
tags: license?.information.tags || [],
trial: Boolean(license?.information.trial),
Expand Down

0 comments on commit b98b99e

Please sign in to comment.