Update hivegrid-cloud reference to latest main

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
davidandClaude Fable 5 committed 2026-07-08 16:15:37 +02:00
1 parent e98d38e716
commit 7b21524c86
3781 files changed
+373484 -225607

No files matched your search

@@ -0,0 +1,254 @@
import { getDbClient } from "./client.server.js";
export const billingSubscriptionStatuses = ["trial", "active", "past_due", "cancelled", "frozen"] as const;
export type BillingSubscriptionStatus = (typeof billingSubscriptionStatuses)[number];
export type TenantBillingEntitlement = {
featureKey: string;
title: string;
enabled: boolean;
limitType: number;
limit: number;
quotaValue: unknown | null;
accumulate: boolean;
sourceProductId: string;
sourcePlanCode: string | null;
};
export type TenantBillingState = {
tenantId: string;
status: BillingSubscriptionStatus;
readOnly: boolean;
planCode: string | null;
seatQuantity: number;
lagoCustomerId: string | null;
mollieCustomerId: string | null;
lastInvoiceExternalId: string | null;
lastPaymentExternalId: string | null;
trialStartsAt: Date | null;
trialEndsAt: Date | null;
pastDueSince: Date | null;
frozenAt: Date | null;
cancelledAt: Date | null;
currentPeriodStart: Date | null;
currentPeriodEnd: Date | null;
entitlements: TenantBillingEntitlement[];
};
export type TenantBillingEntitlements = {
tenantId: string;
planCode: string | null;
seatQuantity: number;
features: Record<string, TenantBillingEntitlement>;
};
type TenantBillingFeatureRow = {
code?: string | null;
name: string;
order: number;
title: string;
type: number;
value: number;
quotaValue?: unknown | null;
accumulate: boolean;
};
type TenantBillingProductRow = {
id: string;
createdAt: Date;
customEntitlementPlanId?: string | null;
cancelledAt?: Date | null;
endsAt?: Date | null;
planCode?: string | null;
seatQuantity?: number | null;
quantity?: number | null;
currentPeriodStart?: Date | null;
currentPeriodEnd?: Date | null;
subscriptionProduct: {
id: string;
code?: string | null;
lagoPlanCode?: string | null;
title: string;
features: TenantBillingFeatureRow[];
};
};
type TenantBillingSubscriptionRow = {
tenantId: string;
status?: string | null;
planCode?: string | null;
seatQuantity?: number | null;
lagoCustomerId?: string | null;
mollieCustomerId?: string | null;
lastInvoiceExternalId?: string | null;
lastPaymentExternalId?: string | null;
trialStartsAt?: Date | null;
trialEndsAt?: Date | null;
pastDueSince?: Date | null;
frozenAt?: Date | null;
cancelledAt?: Date | null;
products: TenantBillingProductRow[];
};
export const tenantBillingStateInclude = {
products: {
include: {
subscriptionProduct: {
include: {
features: true,
},
},
},
},
} as const;
export type TenantBillingStateClient = {
tenantSubscription: {
findUnique(args: { where: { tenantId: string }; include: typeof tenantBillingStateInclude }): Promise<TenantBillingSubscriptionRow | null>;
};
};
const NOT_INCLUDED_LIMIT_TYPE = 0;
function getTenantBillingStateClient(): TenantBillingStateClient {
return getDbClient() as unknown as TenantBillingStateClient;
}
function isBillingSubscriptionStatus(value: string | null | undefined): value is BillingSubscriptionStatus {
return billingSubscriptionStatuses.includes(value as BillingSubscriptionStatus);
}
export function normalizeBillingSubscriptionStatus(
value: string | null | undefined,
markers: { frozenAt?: Date | null; cancelledAt?: Date | null } = {},
): BillingSubscriptionStatus {
if (markers.cancelledAt) return "cancelled";
if (markers.frozenAt) return "frozen";
return isBillingSubscriptionStatus(value) ? value : "trial";
}
function isReadOnlyStatus(status: BillingSubscriptionStatus): boolean {
return status === "past_due" || status === "cancelled" || status === "frozen";
}
function positiveInteger(value: number | null | undefined): number | null {
if (typeof value !== "number" || !Number.isFinite(value)) return null;
const normalized = Math.trunc(value);
return normalized > 0 ? normalized : null;
}
function activeProducts(products: TenantBillingProductRow[], now: Date): TenantBillingProductRow[] {
return products
.filter((product) => !product.endsAt || product.endsAt > now)
.sort((left, right) => right.createdAt.getTime() - left.createdAt.getTime());
}
function resolveProductPlanCode(product: TenantBillingProductRow): string | null {
return (
product.planCode ??
product.customEntitlementPlanId ??
product.subscriptionProduct.code ??
product.subscriptionProduct.lagoPlanCode ??
product.subscriptionProduct.id ??
null
);
}
function resolvePlanCode(subscription: TenantBillingSubscriptionRow | null, products: TenantBillingProductRow[]): string | null {
return subscription?.planCode ?? products.map(resolveProductPlanCode).find((value): value is string => Boolean(value)) ?? null;
}
function resolveSeatQuantity(subscription: TenantBillingSubscriptionRow | null, products: TenantBillingProductRow[]): number {
return subscription?.seatQuantity && subscription.seatQuantity > 0
? subscription.seatQuantity
: products.map((product) => positiveInteger(product.seatQuantity) ?? positiveInteger(product.quantity)).find((value): value is number => value !== null) ?? 1;
}
function resolvePeriod(products: TenantBillingProductRow[]): Pick<TenantBillingState, "currentPeriodStart" | "currentPeriodEnd"> {
const product = products.find((item) => item.currentPeriodStart || item.currentPeriodEnd);
return {
currentPeriodStart: product?.currentPeriodStart ?? null,
currentPeriodEnd: product?.currentPeriodEnd ?? null,
};
}
function resolveEntitlements(products: TenantBillingProductRow[]): TenantBillingEntitlement[] {
const entitlements = new Map<string, TenantBillingEntitlement>();
for (const product of products) {
const sourcePlanCode = resolveProductPlanCode(product);
for (const feature of product.subscriptionProduct.features) {
const featureKey = feature.code ?? feature.name;
if (!featureKey) continue;
const current = entitlements.get(featureKey);
const next: TenantBillingEntitlement = {
featureKey,
title: feature.title,
enabled: feature.type !== NOT_INCLUDED_LIMIT_TYPE,
limitType: feature.type,
limit: feature.value,
quotaValue: feature.quotaValue ?? null,
accumulate: feature.accumulate,
sourceProductId: product.subscriptionProduct.id,
sourcePlanCode,
};
if (!current || next.limitType > current.limitType || (next.limitType === current.limitType && next.limit > current.limit)) {
entitlements.set(featureKey, next);
}
}
}
return [...entitlements.values()].sort((left, right) => left.featureKey.localeCompare(right.featureKey));
}
export async function resolveTenantBillingState(
tenantId: string,
options: { client?: TenantBillingStateClient; now?: Date } = {},
): Promise<TenantBillingState> {
const subscription = await (options.client ?? getTenantBillingStateClient()).tenantSubscription.findUnique({
where: { tenantId },
include: tenantBillingStateInclude,
});
const products = activeProducts(subscription?.products ?? [], options.now ?? new Date());
const status = normalizeBillingSubscriptionStatus(subscription?.status, {
frozenAt: subscription?.frozenAt,
cancelledAt: subscription?.cancelledAt,
});
const period = resolvePeriod(products);
return {
tenantId,
status,
readOnly: isReadOnlyStatus(status),
planCode: resolvePlanCode(subscription, products),
seatQuantity: resolveSeatQuantity(subscription, products),
lagoCustomerId: subscription?.lagoCustomerId ?? null,
mollieCustomerId: subscription?.mollieCustomerId ?? null,
lastInvoiceExternalId: subscription?.lastInvoiceExternalId ?? null,
lastPaymentExternalId: subscription?.lastPaymentExternalId ?? null,
trialStartsAt: subscription?.trialStartsAt ?? null,
trialEndsAt: subscription?.trialEndsAt ?? null,
pastDueSince: subscription?.pastDueSince ?? null,
frozenAt: subscription?.frozenAt ?? null,
cancelledAt: subscription?.cancelledAt ?? null,
currentPeriodStart: period.currentPeriodStart,
currentPeriodEnd: period.currentPeriodEnd,
entitlements: resolveEntitlements(products),
};
}
export async function resolveTenantEntitlements(
tenantId: string,
options: { client?: TenantBillingStateClient; now?: Date } = {},
): Promise<TenantBillingEntitlements> {
const state = await resolveTenantBillingState(tenantId, options);
return {
tenantId: state.tenantId,
planCode: state.planCode,
seatQuantity: state.seatQuantity,
features: Object.fromEntries(state.entitlements.map((entitlement) => [entitlement.featureKey, entitlement])),
};
}