mirror of
https://github.com/logto-io/logto.git
synced 2025-04-14 23:11:31 -05:00
refactor: refactor code
This commit is contained in:
parent
78d73fdcb9
commit
3ba5558feb
2 changed files with 35 additions and 44 deletions
|
@ -127,12 +127,10 @@ export class QuotaLibrary {
|
|||
quota: { scopesPerResourceLimit, scopesPerRoleLimit },
|
||||
} = await this.subscription.getSubscriptionData();
|
||||
|
||||
const usage =
|
||||
(entityName === 'resources'
|
||||
? // eslint-disable-next-line unicorn/no-await-expression-member
|
||||
(await this.getScopesForResourcesTenantUsage())[entityId]
|
||||
: // eslint-disable-next-line unicorn/no-await-expression-member
|
||||
(await this.getScopesForRolesTenantUsage())[entityId]) ?? 0;
|
||||
const { [entityName]: usage = 0 } =
|
||||
entityName === 'resources'
|
||||
? await this.getScopesForResourcesTenantUsage()
|
||||
: await this.getScopesForRolesTenantUsage();
|
||||
|
||||
if (entityName === 'resources') {
|
||||
assertThat(
|
||||
|
@ -193,25 +191,25 @@ export class QuotaLibrary {
|
|||
);
|
||||
|
||||
const unparsedUsage: SelfComputedTenantUsage = {
|
||||
applicationsLimit: Number(rawUsage.applicationsLimit),
|
||||
thirdPartyApplicationsLimit: Number(rawUsage.thirdPartyApplicationsLimit),
|
||||
scopesPerResourceLimit: Number(rawUsage.scopesPerResourceLimit), // Max scopes per resource
|
||||
userRolesLimit: Number(rawUsage.userRolesLimit),
|
||||
machineToMachineRolesLimit: Number(rawUsage.machineToMachineRolesLimit),
|
||||
scopesPerRoleLimit: Number(rawUsage.scopesPerRoleLimit), // Max scopes per role
|
||||
hooksLimit: Number(rawUsage.hooksLimit),
|
||||
applicationsLimit: rawUsage.applicationsLimit,
|
||||
thirdPartyApplicationsLimit: rawUsage.thirdPartyApplicationsLimit,
|
||||
scopesPerResourceLimit: rawUsage.scopesPerResourceLimit, // Max scopes per resource
|
||||
userRolesLimit: rawUsage.userRolesLimit,
|
||||
machineToMachineRolesLimit: rawUsage.machineToMachineRolesLimit,
|
||||
scopesPerRoleLimit: rawUsage.scopesPerRoleLimit, // Max scopes per role
|
||||
hooksLimit: rawUsage.hooksLimit,
|
||||
customJwtEnabled: rawUsage.customJwtEnabled,
|
||||
bringYourUiEnabled: rawUsage.bringYourUiEnabled,
|
||||
/** Add-on quotas start */
|
||||
machineToMachineLimit: Number(rawUsage.machineToMachineLimit),
|
||||
resourcesLimit: Number(rawUsage.resourcesLimit),
|
||||
enterpriseSsoLimit: Number(rawUsage.enterpriseSsoLimit),
|
||||
machineToMachineLimit: rawUsage.machineToMachineLimit,
|
||||
resourcesLimit: rawUsage.resourcesLimit,
|
||||
enterpriseSsoLimit: rawUsage.enterpriseSsoLimit,
|
||||
mfaEnabled: rawUsage.mfaEnabled,
|
||||
/** Enterprise only add-on quotas */
|
||||
idpInitiatedSsoEnabled: rawUsage.idpInitiatedSsoEnabled,
|
||||
samlApplicationsLimit: Number(rawUsage.samlApplicationsLimit),
|
||||
samlApplicationsLimit: rawUsage.samlApplicationsLimit,
|
||||
socialConnectorsLimit: socialConnectors.length,
|
||||
organizationsLimit: Number(rawUsage.organizationsLimit),
|
||||
organizationsLimit: rawUsage.organizationsLimit,
|
||||
/**
|
||||
* We can not calculate the quota usage since there is no related DB configuration for such feature.
|
||||
* Whether the feature is enabled depends on the `quota` defined for each plan/SKU.
|
||||
|
|
|
@ -4,47 +4,40 @@ import { z } from 'zod';
|
|||
import { type SubscriptionUsage } from '#src/utils/subscription/types.js';
|
||||
|
||||
/**
|
||||
* A generic type that converts all number properties in T to type V
|
||||
*/
|
||||
type ConvertNumberProperties<T, V> = {
|
||||
[K in keyof T]: T[K] extends number ? V : T[K];
|
||||
};
|
||||
|
||||
/**
|
||||
* TenantUsage type that converts all number properties in SubscriptionUsage to string
|
||||
* TenantUsage type that is based on SubscriptionUsage but with specific fields omitted
|
||||
* Note: Although we use z.string().transform(Number) in the guard, the actual type is still number
|
||||
* because the transform function converts the string input to number output
|
||||
*/
|
||||
export type TenantUsage = Omit<
|
||||
ConvertNumberProperties<SubscriptionUsage, string>,
|
||||
SubscriptionUsage,
|
||||
'socialConnectorsLimit' | 'subjectTokenEnabled' | 'tenantMembersLimit'
|
||||
>;
|
||||
|
||||
/**
|
||||
* Guard for TenantUsage that uses string validators for all number properties
|
||||
* Guard for TenantUsage that accepts string inputs and transforms them to numbers
|
||||
* This is why we use z.string().transform(Number) instead of z.number()
|
||||
*/
|
||||
export const tenantUsageGuard = z.object({
|
||||
applicationsLimit: z.string(),
|
||||
thirdPartyApplicationsLimit: z.string(),
|
||||
scopesPerResourceLimit: z.string(),
|
||||
// SocialConnectorsLimit: z.string(),
|
||||
userRolesLimit: z.string(),
|
||||
machineToMachineRolesLimit: z.string(),
|
||||
scopesPerRoleLimit: z.string(),
|
||||
hooksLimit: z.string(),
|
||||
applicationsLimit: z.string().transform(Number),
|
||||
thirdPartyApplicationsLimit: z.string().transform(Number),
|
||||
scopesPerResourceLimit: z.string().transform(Number),
|
||||
userRolesLimit: z.string().transform(Number),
|
||||
machineToMachineRolesLimit: z.string().transform(Number),
|
||||
scopesPerRoleLimit: z.string().transform(Number),
|
||||
hooksLimit: z.string().transform(Number),
|
||||
customJwtEnabled: z.boolean(),
|
||||
// SubjectTokenEnabled: z.boolean(),
|
||||
bringYourUiEnabled: z.boolean(),
|
||||
/** Add-on quotas start */
|
||||
machineToMachineLimit: z.string(),
|
||||
resourcesLimit: z.string(),
|
||||
enterpriseSsoLimit: z.string(),
|
||||
// TenantMembersLimit: z.string(),
|
||||
machineToMachineLimit: z.string().transform(Number),
|
||||
resourcesLimit: z.string().transform(Number),
|
||||
enterpriseSsoLimit: z.string().transform(Number),
|
||||
mfaEnabled: z.boolean(),
|
||||
organizationsLimit: z.string(),
|
||||
organizationsLimit: z.string().transform(Number),
|
||||
/** Enterprise only add-on quotas */
|
||||
idpInitiatedSsoEnabled: z.boolean(),
|
||||
samlApplicationsLimit: z.string(),
|
||||
samlApplicationsLimit: z.string().transform(Number),
|
||||
/** Add-on quotas end */
|
||||
}) satisfies ToZodObject<TenantUsage>;
|
||||
}); // Type assertion to specify input type as string
|
||||
|
||||
export type SelfComputedTenantUsage = Omit<SubscriptionUsage, 'tenantMembersLimit'>;
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue