Refresh HiveGrid Cloud reference snapshot

This commit is contained in:
david committed 2026-08-03 15:31:14 +02:00
1 parent 5e99e14772
commit 7f88026f79
5955 files changed
+1177325 -188362

No files matched your search

@@ -7,6 +7,7 @@ const dbMock = {
},
tenantFeatureOverride: {
findUnique: vi.fn(),
findMany: vi.fn(),
},
};
const getPermissionsByUserMock = vi.fn();
@@ -20,6 +21,7 @@ describe("resolveTenantProductAccess", () => {
vi.doMock("~/utils/loaders.middleware", () => ({ getIsSuperAdminByUserId: vi.fn().mockResolvedValue(false) }));
dbMock.tenantProductConfiguration.findUnique.mockResolvedValue({ blueprintId: "crm-saas" });
dbMock.tenantFeatureOverride.findUnique.mockResolvedValue({ override: "inherit" });
dbMock.tenantFeatureOverride.findMany.mockResolvedValue([]);
getPermissionsByUserMock.mockResolvedValue(["app.settings.roles.set"]);
});
@@ -37,7 +39,7 @@ describe("resolveTenantProductAccess", () => {
products: [{ customEntitlementPlanId: "business", features: [] }],
},
}),
).resolves.toMatchObject({ allowed: true, reason: "monthly-remaining", limit: 10000 });
).resolves.toMatchObject({ allowed: true, reason: "unlimited" });
expect(dbMock.tenantProductConfiguration.findUnique).toHaveBeenCalledWith({
where: { tenantId: "tenant_1" },
@@ -49,7 +51,7 @@ describe("resolveTenantProductAccess", () => {
});
expect(getPermissionsByUserMock).toHaveBeenCalledWith("user_1", "tenant_1");
},
15000,
60000,
);
it("lets tenant overrides disable toggleable features even when the plan allows them", async () => {
@@ -67,4 +69,55 @@ describe("resolveTenantProductAccess", () => {
}),
).resolves.toMatchObject({ allowed: false, reason: "tenant-override-disabled" });
});
it("resolves many features with one configuration query and one override query", async () => {
const { resolveTenantProductAccessBatch } = await import("./productAccess.server");
dbMock.tenantFeatureOverride.findMany.mockResolvedValue([{ featureKey: featureKeys.workflowEngine, override: "disabled" }]);
const decisions = await resolveTenantProductAccessBatch({
tenantId: "tenant_1",
userId: "user_1",
featureKeys: [featureKeys.workflowEngine, featureKeys.files, featureKeys.workflowEngine],
subscription: { products: [{ customEntitlementPlanId: "business", features: [] }] },
tenantRolePermissionNames: ["app.settings.roles.set"],
bypassTenantRolePermission: false,
});
expect(dbMock.tenantProductConfiguration.findUnique).toHaveBeenCalledTimes(1);
expect(dbMock.tenantFeatureOverride.findMany).toHaveBeenCalledTimes(1);
expect(dbMock.tenantFeatureOverride.findMany).toHaveBeenCalledWith({
where: { tenantId: "tenant_1", featureKey: { in: [featureKeys.workflowEngine, featureKeys.files] } },
select: { featureKey: true, override: true },
});
expect(decisions).toHaveLength(2);
expect(Object.fromEntries(decisions)[featureKeys.workflowEngine]).toMatchObject({ allowed: false, reason: "tenant-override-disabled" });
expect(getPermissionsByUserMock).not.toHaveBeenCalled();
});
it("does not convert frozen billing entitlements into an active feature subscription", async () => {
const { tenantBillingEntitlementsToSubscription } = await import("./productAccess.server");
expect(
tenantBillingEntitlementsToSubscription({
tenantId: "tenant_1",
status: "frozen",
readOnly: true,
planCode: "business",
seatQuantity: 10,
features: {
"email-marketing": {
featureKey: "email-marketing",
title: "Email Marketing",
enabled: true,
limitType: 2,
limit: 50_000,
quotaValue: null,
accumulate: false,
sourceProductId: "product_business",
sourcePlanCode: "business",
},
},
}),
).toBeNull();
});
});
@@ -27,6 +27,10 @@ export type ResolveTenantProductAccessInput = {
bypassTenantRolePermission?: boolean;
};
export type ResolveTenantProductAccessBatchInput = Omit<ResolveTenantProductAccessInput, "featureKey"> & {
featureKeys: readonly string[];
};
function normalizeTenantOverride(value: string | null | undefined): TenantFeatureOverride {
if (value === "enabled" || value === "disabled" || value === "inherit") {
return value;
@@ -35,6 +39,7 @@ function normalizeTenantOverride(value: string | null | undefined): TenantFeatur
}
export function tenantBillingEntitlementsToSubscription(entitlements: TenantBillingEntitlements): SubscriptionEntitlementSubscription | null {
if (entitlements.readOnly) return null;
const features = Object.values(entitlements.features).map((feature, index) => ({
order: index,
title: feature.title,
@@ -52,20 +57,13 @@ export async function resolveTenantEntitlementSubscription(tenantId: string): Pr
return tenantBillingEntitlementsToSubscription(await resolveTenantEntitlements(tenantId));
}
export async function resolveTenantProductAccess(input: ResolveTenantProductAccessInput): Promise<ProductAccessDecision> {
const [configuration, override, tenantRolePermissionNames, isSuperAdmin] = await Promise.all([
db.tenantProductConfiguration.findUnique({
where: { tenantId: input.tenantId },
select: { blueprintId: true },
}),
db.tenantFeatureOverride.findUnique({
where: { tenantId_featureKey: { tenantId: input.tenantId, featureKey: input.featureKey } },
select: { override: true },
}),
input.tenantRolePermissionNames ? Promise.resolve([...input.tenantRolePermissionNames]) : getPermissionsByUser(input.userId, input.tenantId),
input.bypassTenantRolePermission === undefined ? getIsSuperAdminByUserId(input.userId) : Promise.resolve(input.bypassTenantRolePermission),
]);
const blueprintId = (configuration?.blueprintId as ProductBlueprintId | undefined) ?? getDefaultProductBlueprint().id;
function evaluateResolvedProductAccess(
input: ResolveTenantProductAccessInput,
blueprintId: ProductBlueprintId,
tenantOverride: string | null | undefined,
tenantRolePermissionNames: readonly string[],
isSuperAdmin: boolean,
): ProductAccessDecision {
const rolePermissionNames = [...tenantRolePermissionNames];
if (isSuperAdmin || input.bypassTenantRolePermission) {
for (const permissionName of getProductBlueprint(blueprintId)
@@ -83,11 +81,61 @@ export async function resolveTenantProductAccess(input: ResolveTenantProductAcce
subscription: input.subscription,
used: input.used,
period: input.period,
tenantOverride: normalizeTenantOverride(override?.override),
tenantOverride: normalizeTenantOverride(tenantOverride),
tenantRolePermissionNames: rolePermissionNames,
});
}
export async function resolveTenantProductAccess(input: ResolveTenantProductAccessInput): Promise<ProductAccessDecision> {
const [configuration, override, tenantRolePermissionNames, isSuperAdmin] = await Promise.all([
db.tenantProductConfiguration.findUnique({
where: { tenantId: input.tenantId },
select: { blueprintId: true },
}),
db.tenantFeatureOverride.findUnique({
where: { tenantId_featureKey: { tenantId: input.tenantId, featureKey: input.featureKey } },
select: { override: true },
}),
input.tenantRolePermissionNames ? Promise.resolve([...input.tenantRolePermissionNames]) : getPermissionsByUser(input.userId, input.tenantId),
input.bypassTenantRolePermission === undefined ? getIsSuperAdminByUserId(input.userId) : Promise.resolve(input.bypassTenantRolePermission),
]);
const blueprintId = (configuration?.blueprintId as ProductBlueprintId | undefined) ?? getDefaultProductBlueprint().id;
return evaluateResolvedProductAccess(input, blueprintId, override?.override, tenantRolePermissionNames, isSuperAdmin);
}
export async function resolveTenantProductAccessBatch(
input: ResolveTenantProductAccessBatchInput,
): Promise<Array<readonly [string, ProductAccessDecision]>> {
const featureKeys = [...new Set(input.featureKeys.filter(Boolean))];
if (featureKeys.length === 0) return [];
const [configuration, overrides, tenantRolePermissionNames, isSuperAdmin] = await Promise.all([
db.tenantProductConfiguration.findUnique({
where: { tenantId: input.tenantId },
select: { blueprintId: true },
}),
db.tenantFeatureOverride.findMany({
where: { tenantId: input.tenantId, featureKey: { in: featureKeys } },
select: { featureKey: true, override: true },
}),
input.tenantRolePermissionNames ? Promise.resolve([...input.tenantRolePermissionNames]) : getPermissionsByUser(input.userId, input.tenantId),
input.bypassTenantRolePermission === undefined ? getIsSuperAdminByUserId(input.userId) : Promise.resolve(input.bypassTenantRolePermission),
]);
const blueprintId = (configuration?.blueprintId as ProductBlueprintId | undefined) ?? getDefaultProductBlueprint().id;
const overrideByFeatureKey = new Map(overrides.map((override) => [override.featureKey, override.override]));
return featureKeys.map((featureKey) => [
featureKey,
evaluateResolvedProductAccess(
{ ...input, featureKey },
blueprintId,
overrideByFeatureKey.get(featureKey),
tenantRolePermissionNames,
isSuperAdmin,
),
] as const);
}
export async function requireTenantProductAccess({
request,
params,
@@ -218,7 +218,7 @@ describe("evaluateSubscriptionEntitlement", () => {
},
tenantRolePermissionNames: ["app.settings.roles.set"],
})
).toMatchObject({ allowed: true, reason: "monthly-remaining", limit: 10000 });
).toMatchObject({ allowed: true, reason: "unlimited" });
expect(
evaluateProductSubscriptionAccess({
@@ -15,7 +15,7 @@ import { DefaultFeatures } from "~/application/dtos/shared/DefaultFeatures";
import type { SubscriptionFeatureDto } from "~/application/dtos/subscriptions/SubscriptionFeatureDto";
import type { SubscriptionProductDto } from "~/application/dtos/subscriptions/SubscriptionProductDto";
import { SubscriptionFeatureLimitType } from "~/application/enums/subscriptions/SubscriptionFeatureLimitType";
import { t as i18nT } from "i18next";
import { requestScopedT as i18nT } from "~/locale/request-scoped-t";
export interface SubscriptionEntitlementTrialState {
readonly active: boolean;