import { createTenantSubscription as persistTenantSubscription, getTenantSubscription as readTenantSubscription, updateTenantSubscriptionCustomerId as persistTenantSubscriptionCustomerId, type TenantSubscriptionWithDetails, } from "@toolctrl/db/subscriptions.server"; import { clearTenantSubscriptionCache } from "~/utils/services/.server/subscriptionService"; export { getTenantSubscription, getTenantSubscriptions, type TenantSubscriptionWithDetails } from "@toolctrl/db/subscriptions.server"; export async function getOrPersistTenantSubscription(tenantId: string): Promise { const subscription = await readTenantSubscription(tenantId); if (!subscription) { return await createTenantSubscription(tenantId, ""); } return subscription; } export async function createTenantSubscription(tenantId: string, stripeCustomerId: string) { const item = await persistTenantSubscription({ tenantId, stripeCustomerId }); // Cross-replica purge: clearTenantSubscriptionCache publishes on the // subscription pub/sub channel so peer web replicas drop their L1 too. clearTenantSubscriptionCache(tenantId); return item; } export async function updateTenantSubscriptionCustomerId(tenantId: string, data: { stripeCustomerId: string }) { const item = await persistTenantSubscriptionCustomerId(tenantId, data); clearTenantSubscriptionCache(tenantId); return item; }