Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update tenant selector dropdown data source #6438

Merged
merged 1 commit into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/console/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"devDependencies": {
"@fontsource/roboto-mono": "^5.0.0",
"@jest/types": "^29.5.0",
"@logto/cloud": "0.2.5-3452c56",
"@logto/cloud": "0.2.5-a6cff75",
"@logto/connector-kit": "workspace:^4.0.0",
"@logto/core-kit": "workspace:^2.5.0",
"@logto/elements": "workspace:^0.0.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,18 @@
import { conditional } from '@silverhand/essentials';

import {
type TenantResponse,
type NewSubscriptionUsage,
type NewSubscriptionQuota,
} from '@/cloud/types/router';
import { type TenantResponse } from '@/cloud/types/router';
import { isDevFeaturesEnabled } from '@/consts/env';
import DynamicT from '@/ds-components/DynamicT';
import Tag from '@/ds-components/Tag';
import { type SubscriptionPlan } from '@/types/subscriptions';

type Props = {
readonly tenantData: TenantResponse;
/** @deprecated */
readonly tenantSubscriptionPlan: SubscriptionPlan;
readonly tenantStatus: {
usage: NewSubscriptionUsage;
quota: NewSubscriptionQuota;
};
readonly className?: string;
};

function TenantStatusTag({ tenantData, tenantSubscriptionPlan, tenantStatus, className }: Props) {
const { usage, openInvoices, isSuspended } = tenantData;
function TenantStatusTag({ tenantData, tenantSubscriptionPlan, className }: Props) {
const { usage, quota, openInvoices, isSuspended } = tenantData;

/**
* Tenant status priority:
Expand All @@ -46,21 +37,11 @@ function TenantStatusTag({ tenantData, tenantSubscriptionPlan, tenantStatus, cla
);
}

const { usage: tenantUsage, quota: tenantQuota } = tenantStatus;

const { activeUsers } = usage;

const {
quota: { mauLimit },
} = tenantSubscriptionPlan;
const mauLimit = isDevFeaturesEnabled ? quota.mauLimit : tenantSubscriptionPlan.quota.mauLimit;

const isMauExceeded =
conditional(
isDevFeaturesEnabled &&
tenantQuota.mauLimit !== null &&
tenantUsage.mauLimit >= tenantQuota.mauLimit
) ??
(mauLimit !== null && activeUsers >= mauLimit);
const isMauExceeded = mauLimit !== null && activeUsers >= mauLimit;

if (isMauExceeded) {
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,7 @@ function TenantDropdownItem({ tenantData, isSelected, onClick }: Props) {
subscription: { planId },
} = tenantData;

const {
currentPlan,
subscriptionPlans,
currentSubscriptionUsage: usage,
currentSubscriptionQuota: quota,
} = useContext(SubscriptionDataContext);
const { currentPlan, subscriptionPlans } = useContext(SubscriptionDataContext);
const tenantSubscriptionPlan = useMemo(
() => subscriptionPlans.find((plan) => plan.id === planId),
[subscriptionPlans, planId]
Expand All @@ -51,7 +46,6 @@ function TenantDropdownItem({ tenantData, isSelected, onClick }: Props) {
<TenantEnvTag tag={tag} />
<TenantStatusTag
tenantData={tenantData}
tenantStatus={{ usage, quota }}
tenantSubscriptionPlan={tenantSubscriptionPlan}
className={styles.statusTag}
/>
Expand Down
5 changes: 3 additions & 2 deletions packages/console/src/consts/tenants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ export const defaultTenantResponse: TenantResponse = {
},
usage: {
activeUsers: 0,
cost: 0,
tokenUsage: 0,
},
quota: {
mauLimit: null,
},
openInvoices: [],
isSuspended: false,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
"zod": "^3.23.8"
},
"devDependencies": {
"@logto/cloud": "0.2.5-3452c56",
"@logto/cloud": "0.2.5-a6cff75",
"@silverhand/eslint-config": "6.0.1",
"@silverhand/ts-config": "6.0.0",
"@types/adm-zip": "^0.5.5",
Expand Down
17 changes: 6 additions & 11 deletions packages/core/src/libraries/quota.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import {
getTenantSubscriptionPlan,
getTenantSubscriptionData,
getTenantSubscriptionScopeUsage,
reportSubscriptionUpdates,
isReportSubscriptionUpdatesUsageKey,
} from '#src/utils/subscription/index.js';
Expand Down Expand Up @@ -235,16 +234,12 @@
return;
}

const [
{
quota: { scopesPerResourceLimit, scopesPerRoleLimit },
},
scopeUsages,
] = await Promise.all([
getTenantSubscriptionData(cloudConnection),
getTenantSubscriptionScopeUsage(cloudConnection, entityName),
]);
const usage = scopeUsages[entityId] ?? 0;
const {
quota: { scopesPerResourceLimit, scopesPerRoleLimit },
resources,
roles,
} = await getTenantSubscriptionData(cloudConnection);

Check warning on line 241 in packages/core/src/libraries/quota.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/libraries/quota.ts#L237-L241

Added lines #L237 - L241 were not covered by tests
const usage = (entityName === 'resources' ? resources[entityId] : roles[entityId]) ?? 0;

if (entityName === 'resources') {
assertThat(
Expand Down
22 changes: 5 additions & 17 deletions packages/core/src/utils/subscription/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,16 @@
planId: string;
quota: SubscriptionQuota;
usage: SubscriptionUsage;
resources: Record<string, number>;
roles: Record<string, number>;

Check warning on line 47 in packages/core/src/utils/subscription/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/utils/subscription/index.ts#L46-L47

Added lines #L46 - L47 were not covered by tests
}> => {
const client = await cloudConnection.getClient();
const [{ planId }, quota, usage] = await Promise.all([
const [{ planId }, { quota, usage, resources, roles }] = await Promise.all([

Check warning on line 50 in packages/core/src/utils/subscription/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/utils/subscription/index.ts#L50

Added line #L50 was not covered by tests
client.get('/api/tenants/my/subscription'),
client.get('/api/tenants/my/subscription/quota'),
client.get('/api/tenants/my/subscription/usage'),
client.get('/api/tenants/my/subscription-usage'),

Check warning on line 52 in packages/core/src/utils/subscription/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/utils/subscription/index.ts#L52

Added line #L52 was not covered by tests
]);

return { planId, quota, usage };
};

export const getTenantSubscriptionScopeUsage = async (
cloudConnection: CloudConnectionLibrary,
entityName: 'resources' | 'roles'
): Promise<Record<string, number>> => {
const client = await cloudConnection.getClient();
const scopeUsages = await client.get('/api/tenants/my/subscription/usage/:entityName/scopes', {
params: { entityName },
search: {},
});

return scopeUsages;
return { planId, quota, usage, resources, roles };

Check warning on line 55 in packages/core/src/utils/subscription/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/utils/subscription/index.ts#L55

Added line #L55 was not covered by tests
};

export const reportSubscriptionUpdates = async (
Expand Down
37 changes: 24 additions & 13 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading