mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-06 18:27:19 +00:00
Merge pull request #4497 from Infisical/ENG-3594
Improveme getOrgPlanTable and add EnterpriseOnPrem fetch plan table
This commit is contained in:
@@ -442,6 +442,62 @@ export const licenseServiceFactory = ({
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const calculateUsageValue = (
|
||||||
|
rowName: string,
|
||||||
|
field: string,
|
||||||
|
projectCount: number,
|
||||||
|
totalIdentities: number
|
||||||
|
): string => {
|
||||||
|
if (rowName === BillingPlanRows.WorkspaceLimit.name || field === BillingPlanRows.WorkspaceLimit.field) {
|
||||||
|
return projectCount.toString();
|
||||||
|
}
|
||||||
|
if (rowName === BillingPlanRows.IdentityLimit.name || field === BillingPlanRows.IdentityLimit.field) {
|
||||||
|
return totalIdentities.toString();
|
||||||
|
}
|
||||||
|
return "-";
|
||||||
|
};
|
||||||
|
|
||||||
|
const fetchPlanTableFromServer = async (customerId: string | null | undefined) => {
|
||||||
|
if (!customerId) {
|
||||||
|
throw new NotFoundError({ message: "Organization customer ID is required for plan table retrieval" });
|
||||||
|
}
|
||||||
|
|
||||||
|
const baseUrl = `/api/license-server/v1/customers/${customerId}`;
|
||||||
|
|
||||||
|
if (instanceType === InstanceType.Cloud) {
|
||||||
|
const { data } = await licenseServerCloudApi.request.get<{
|
||||||
|
head: { name: string }[];
|
||||||
|
rows: { name: string; allowed: boolean }[];
|
||||||
|
}>(`${baseUrl}/cloud-plan/table`);
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (instanceType === InstanceType.EnterpriseOnPrem) {
|
||||||
|
const { data } = await licenseServerOnPremApi.request.get<{
|
||||||
|
head: { name: string }[];
|
||||||
|
rows: { name: string; allowed: boolean }[];
|
||||||
|
}>(`${baseUrl}/on-prem-plan/table`);
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new Error(`Unsupported instance type for server-based plan table: ${instanceType}`);
|
||||||
|
};
|
||||||
|
|
||||||
|
const getUsageMetrics = async (orgId: string) => {
|
||||||
|
const [orgMembersUsed, identityUsed, projectCount] = await Promise.all([
|
||||||
|
orgDAL.countAllOrgMembers(orgId),
|
||||||
|
identityOrgMembershipDAL.countAllOrgIdentities({ orgId }),
|
||||||
|
projectDAL.countOfOrgProjects(orgId)
|
||||||
|
]);
|
||||||
|
|
||||||
|
return {
|
||||||
|
orgMembersUsed,
|
||||||
|
identityUsed,
|
||||||
|
projectCount,
|
||||||
|
totalIdentities: identityUsed + orgMembersUsed
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
// returns org current plan feature table
|
// returns org current plan feature table
|
||||||
const getOrgPlanTable = async ({ orgId, actor, actorId, actorAuthMethod, actorOrgId }: TGetOrgBillInfoDTO) => {
|
const getOrgPlanTable = async ({ orgId, actor, actorId, actorAuthMethod, actorOrgId }: TGetOrgBillInfoDTO) => {
|
||||||
const { permission } = await permissionService.getOrgPermission(actor, actorId, orgId, actorAuthMethod, actorOrgId);
|
const { permission } = await permissionService.getOrgPermission(actor, actorId, orgId, actorAuthMethod, actorOrgId);
|
||||||
@@ -454,55 +510,25 @@ export const licenseServiceFactory = ({
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const orgMembersUsed = await orgDAL.countAllOrgMembers(orgId);
|
const { projectCount, totalIdentities } = await getUsageMetrics(orgId);
|
||||||
const identityUsed = await identityOrgMembershipDAL.countAllOrgIdentities({ orgId });
|
|
||||||
const projects = await projectDAL.find({ orgId });
|
|
||||||
const projectCount = projects.length;
|
|
||||||
|
|
||||||
if (instanceType === InstanceType.Cloud) {
|
if (instanceType === InstanceType.Cloud || instanceType === InstanceType.EnterpriseOnPrem) {
|
||||||
const { data } = await licenseServerCloudApi.request.get<{
|
const tableResponse = await fetchPlanTableFromServer(organization.customerId);
|
||||||
head: { name: string }[];
|
|
||||||
rows: { name: string; allowed: boolean }[];
|
|
||||||
}>(`/api/license-server/v1/customers/${organization.customerId}/cloud-plan/table`);
|
|
||||||
|
|
||||||
const formattedData = {
|
return {
|
||||||
head: data.head,
|
head: tableResponse.head,
|
||||||
rows: data.rows.map((el) => {
|
rows: tableResponse.rows.map((row) => ({
|
||||||
let used = "-";
|
...row,
|
||||||
|
used: calculateUsageValue(row.name, "", projectCount, totalIdentities)
|
||||||
if (el.name === BillingPlanRows.WorkspaceLimit.name) {
|
}))
|
||||||
used = projectCount.toString();
|
|
||||||
} else if (el.name === BillingPlanRows.IdentityLimit.name) {
|
|
||||||
used = (identityUsed + orgMembersUsed).toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
...el,
|
|
||||||
used
|
|
||||||
};
|
|
||||||
})
|
|
||||||
};
|
};
|
||||||
return formattedData;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const mappedRows = await Promise.all(
|
const mappedRows = Object.values(BillingPlanRows).map(({ name, field }) => ({
|
||||||
Object.values(BillingPlanRows).map(async ({ name, field }: { name: string; field: string }) => {
|
name,
|
||||||
const allowed = onPremFeatures[field as keyof TFeatureSet];
|
allowed: onPremFeatures[field as keyof TFeatureSet] || false,
|
||||||
let used = "-";
|
used: calculateUsageValue(name, field, projectCount, totalIdentities)
|
||||||
|
}));
|
||||||
if (field === BillingPlanRows.WorkspaceLimit.field) {
|
|
||||||
used = projectCount.toString();
|
|
||||||
} else if (field === BillingPlanRows.IdentityLimit.field) {
|
|
||||||
used = (identityUsed + orgMembersUsed).toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
name,
|
|
||||||
allowed,
|
|
||||||
used
|
|
||||||
};
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
head: Object.values(BillingPlanTableHead),
|
head: Object.values(BillingPlanTableHead),
|
||||||
|
|||||||
Reference in New Issue
Block a user