Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
+395
-172
@@ -1,4 +1,5 @@
|
||||
import { getDbClient, type DbClient } from "./client.server.js";
|
||||
import { getRlsExecutionContext, runWithRlsContext } from "./rls-context.server.js";
|
||||
import type {
|
||||
Prisma,
|
||||
SubscriptionFeature,
|
||||
@@ -10,10 +11,13 @@ import type {
|
||||
TenantSubscriptionProduct,
|
||||
TenantSubscriptionProductPrice,
|
||||
TenantSubscriptionUsageRecord,
|
||||
TenantType,
|
||||
TenantProductAcquisition,
|
||||
CheckoutSessionStatus,
|
||||
} from "./prisma-browser.js";
|
||||
|
||||
export const subscriptionProductKindValues = ["base_plan", "recurring_add_on", "one_time_setup", "contract_override"] as const;
|
||||
export type SubscriptionProductKindValue = (typeof subscriptionProductKindValues)[number];
|
||||
|
||||
export type SubscriptionPriceWithProduct = SubscriptionPrice & {
|
||||
subscriptionProduct: SubscriptionProduct;
|
||||
};
|
||||
@@ -38,7 +42,7 @@ export type SubscriptionProductCreateInput = Pick<
|
||||
| "hasQuantity"
|
||||
| "canBuyAgain"
|
||||
> &
|
||||
Partial<Pick<SubscriptionProduct, "code" | "lagoPlanCode">>;
|
||||
Partial<Pick<SubscriptionProduct, "code" | "lagoPlanCode">> & { kind?: SubscriptionProductKindValue };
|
||||
|
||||
export type SubscriptionProductUpdateInput = Partial<
|
||||
Pick<
|
||||
@@ -58,16 +62,36 @@ export type SubscriptionProductUpdateInput = Partial<
|
||||
| "hasQuantity"
|
||||
| "canBuyAgain"
|
||||
>
|
||||
>;
|
||||
> & { kind?: SubscriptionProductKindValue };
|
||||
|
||||
export type SubscriptionPriceCreateInput = Omit<
|
||||
Pick<SubscriptionPrice, "subscriptionProductId" | "billingProviderId" | "type" | "billingPeriod" | "price" | "currency" | "trialDays" | "active">,
|
||||
"price"
|
||||
> &
|
||||
Partial<Pick<SubscriptionPrice, "lagoFeeCode" | "molliePriceReference">> & { price: SubscriptionPrice["price"] | number | string };
|
||||
Partial<Pick<SubscriptionPrice, "lagoPlanCode" | "lagoFeeCode" | "molliePriceReference" | "billingCadence" | "contractMonths" | "requiredPaymentMethod">> & {
|
||||
price: SubscriptionPrice["price"] | number | string;
|
||||
};
|
||||
|
||||
export type SubscriptionPriceUpdateInput = Partial<
|
||||
Omit<Pick<SubscriptionPrice, "billingProviderId" | "lagoFeeCode" | "molliePriceReference" | "type" | "billingPeriod" | "price" | "currency" | "trialDays" | "active">, "price"> & {
|
||||
Omit<
|
||||
Pick<
|
||||
SubscriptionPrice,
|
||||
| "billingProviderId"
|
||||
| "lagoPlanCode"
|
||||
| "lagoFeeCode"
|
||||
| "molliePriceReference"
|
||||
| "billingCadence"
|
||||
| "contractMonths"
|
||||
| "requiredPaymentMethod"
|
||||
| "type"
|
||||
| "billingPeriod"
|
||||
| "price"
|
||||
| "currency"
|
||||
| "trialDays"
|
||||
| "active"
|
||||
>,
|
||||
"price"
|
||||
> & {
|
||||
price: SubscriptionPrice["price"] | number | string;
|
||||
}
|
||||
>;
|
||||
@@ -95,7 +119,10 @@ export type SubscriptionUsageBasedTierCreateInput = Omit<
|
||||
flatFeePrice?: SubscriptionUsageBasedTier["flatFeePrice"] | number | string | null;
|
||||
};
|
||||
|
||||
export type SubscriptionFeatureCreateInput = Omit<SubscriptionFeature, "id" | "subscriptionProductId" | "href" | "badge" | "accumulate" | "code" | "quotaValue"> & {
|
||||
export type SubscriptionFeatureCreateInput = Omit<
|
||||
SubscriptionFeature,
|
||||
"id" | "subscriptionProductId" | "href" | "badge" | "accumulate" | "code" | "quotaValue"
|
||||
> & {
|
||||
href?: string | null;
|
||||
badge?: string | null;
|
||||
accumulate?: boolean;
|
||||
@@ -109,11 +136,11 @@ export type SubscriptionProductPriceWithDetails = Omit<SubscriptionPrice, "id">
|
||||
|
||||
export type SubscriptionProductWithDetails = Omit<SubscriptionProduct, "id"> & {
|
||||
id?: string;
|
||||
kind?: SubscriptionProductKindValue;
|
||||
translatedTitle?: string;
|
||||
tenantProducts: TenantSubscriptionProduct[];
|
||||
usageBasedPrices: (SubscriptionUsageBasedPrice & { tiers: SubscriptionUsageBasedTier[] })[];
|
||||
prices: SubscriptionProductPriceWithDetails[];
|
||||
assignsTenantTypes: TenantType[];
|
||||
features: SubscriptionFeature[];
|
||||
};
|
||||
|
||||
@@ -129,6 +156,7 @@ export type SubscriptionFeatureSummary = {
|
||||
|
||||
export type TenantSubscriptionProductWithDetails = TenantSubscriptionProduct & {
|
||||
subscriptionProduct: SubscriptionProduct & { features: SubscriptionFeature[] };
|
||||
acquisition?: TenantProductAcquisition | null;
|
||||
prices: (TenantSubscriptionProductPrice & {
|
||||
subscriptionPrice: SubscriptionPrice | null;
|
||||
subscriptionUsageBasedPrice: (SubscriptionUsageBasedPrice & { tiers: SubscriptionUsageBasedTier[] }) | null;
|
||||
@@ -179,21 +207,55 @@ export type TenantSubscriptionProductPage = {
|
||||
};
|
||||
|
||||
export type TenantSubscriptionCreateInput = Pick<TenantSubscription, "tenantId"> &
|
||||
Partial<Pick<TenantSubscription, "billingProviderCustomerId" | "lagoCustomerId" | "mollieCustomerId" | "planCode" | "seatQuantity">>;
|
||||
Partial<
|
||||
Pick<
|
||||
TenantSubscription,
|
||||
| "billingProviderCustomerId"
|
||||
| "lagoCustomerId"
|
||||
| "mollieCustomerId"
|
||||
| "planCode"
|
||||
| "seatQuantity"
|
||||
| "officeSeatQuantity"
|
||||
| "fieldSeatQuantity"
|
||||
| "billingCadence"
|
||||
| "commitmentEndsAt"
|
||||
>
|
||||
>;
|
||||
|
||||
export type TenantSubscriptionCustomerIdUpdateInput = Partial<
|
||||
Pick<TenantSubscription, "billingProviderCustomerId" | "lagoCustomerId" | "mollieCustomerId" | "planCode" | "seatQuantity" | "lastInvoiceExternalId" | "lastPaymentExternalId">
|
||||
Pick<
|
||||
TenantSubscription,
|
||||
| "billingProviderCustomerId"
|
||||
| "lagoCustomerId"
|
||||
| "mollieCustomerId"
|
||||
| "planCode"
|
||||
| "seatQuantity"
|
||||
| "officeSeatQuantity"
|
||||
| "fieldSeatQuantity"
|
||||
| "billingCadence"
|
||||
| "commitmentEndsAt"
|
||||
| "lastInvoiceExternalId"
|
||||
| "lastPaymentExternalId"
|
||||
>
|
||||
>;
|
||||
|
||||
export type TenantSubscriptionProductCreateInput = Pick<
|
||||
TenantSubscriptionProduct,
|
||||
| "tenantSubscriptionId"
|
||||
| "subscriptionProductId"
|
||||
| "billingProviderSubscriptionId"
|
||||
| "quantity"
|
||||
| "fromCheckoutSessionId"
|
||||
"tenantSubscriptionId" | "subscriptionProductId" | "billingProviderSubscriptionId" | "quantity" | "fromCheckoutSessionId"
|
||||
> &
|
||||
Partial<Pick<TenantSubscriptionProduct, "customEntitlementPlanId" | "lagoSubscriptionId" | "planCode" | "seatQuantity">> & {
|
||||
Partial<
|
||||
Pick<
|
||||
TenantSubscriptionProduct,
|
||||
| "customEntitlementPlanId"
|
||||
| "lagoSubscriptionId"
|
||||
| "planCode"
|
||||
| "seatQuantity"
|
||||
| "officeSeatQuantity"
|
||||
| "fieldSeatQuantity"
|
||||
| "billingCadence"
|
||||
| "commitmentEndsAt"
|
||||
>
|
||||
> & {
|
||||
prices: {
|
||||
subscriptionPriceId?: string;
|
||||
subscriptionUsageBasedPriceId?: string;
|
||||
@@ -203,7 +265,19 @@ export type TenantSubscriptionProductCreateInput = Pick<
|
||||
export type TenantSubscriptionProductUpdateInput = Partial<
|
||||
Pick<
|
||||
TenantSubscriptionProduct,
|
||||
"quantity" | "seatQuantity" | "planCode" | "lagoSubscriptionId" | "cancelledAt" | "endsAt" | "currentPeriodStart" | "currentPeriodEnd" | "customEntitlementPlanId"
|
||||
| "quantity"
|
||||
| "seatQuantity"
|
||||
| "officeSeatQuantity"
|
||||
| "fieldSeatQuantity"
|
||||
| "billingCadence"
|
||||
| "commitmentEndsAt"
|
||||
| "planCode"
|
||||
| "lagoSubscriptionId"
|
||||
| "cancelledAt"
|
||||
| "endsAt"
|
||||
| "currentPeriodStart"
|
||||
| "currentPeriodEnd"
|
||||
| "customEntitlementPlanId"
|
||||
>
|
||||
>;
|
||||
|
||||
@@ -217,12 +291,14 @@ export type TenantSubscriptionUsageRecordCreateInput = Pick<
|
||||
> &
|
||||
Partial<Pick<TenantSubscriptionUsageRecord, "idempotencyKey">>;
|
||||
|
||||
export type TenantCheckoutSessionStatus = CheckoutSessionStatus & { tenantId: string };
|
||||
|
||||
export type CheckoutSessionStatusCreateInput = Pick<CheckoutSessionStatus, "id" | "email" | "fromUrl"> &
|
||||
Pick<TenantCheckoutSessionStatus, "tenantId"> &
|
||||
Partial<
|
||||
Pick<
|
||||
CheckoutSessionStatus,
|
||||
| "fromUserId"
|
||||
| "fromTenantId"
|
||||
| "provider"
|
||||
| "providerPaymentId"
|
||||
| "invoiceExternalId"
|
||||
@@ -231,6 +307,11 @@ export type CheckoutSessionStatusCreateInput = Pick<CheckoutSessionStatus, "id"
|
||||
| "lagoSubscriptionId"
|
||||
| "planCode"
|
||||
| "quantity"
|
||||
| "officeSeatQuantity"
|
||||
| "fieldSeatQuantity"
|
||||
| "billingCadence"
|
||||
| "commitmentEndsAt"
|
||||
| "storageAddOnCodes"
|
||||
| "amountCents"
|
||||
| "currency"
|
||||
| "checkoutUrl"
|
||||
@@ -242,16 +323,27 @@ export type CheckoutSessionStatusUpdateInput = Pick<CheckoutSessionStatus, "pend
|
||||
Partial<
|
||||
Pick<
|
||||
CheckoutSessionStatus,
|
||||
| "createdUserId"
|
||||
| "createdTenantId"
|
||||
| "providerPaymentId"
|
||||
| "invoiceExternalId"
|
||||
| "lagoSubscriptionId"
|
||||
| "checkoutUrl"
|
||||
| "status"
|
||||
"createdUserId" | "createdTenantId" | "providerPaymentId" | "invoiceExternalId" | "lagoSubscriptionId" | "checkoutUrl" | "status" | "fromUrl"
|
||||
>
|
||||
>;
|
||||
|
||||
export type CheckoutSessionStatusClaimInput = {
|
||||
id: string;
|
||||
tenantId: string;
|
||||
expectedStatus: string;
|
||||
claimedStatus: string;
|
||||
updatedBefore?: Date;
|
||||
};
|
||||
|
||||
export type CheckoutSessionInvoiceCorrelationInput = {
|
||||
id: string;
|
||||
tenantId: string;
|
||||
providerPaymentId: string;
|
||||
lagoSubscriptionId: string;
|
||||
sourceInvoiceExternalId: string;
|
||||
providerInvoiceExternalId: string;
|
||||
};
|
||||
|
||||
export type TenantIdResult = { tenantId: string };
|
||||
|
||||
export type BillingProviderTenantSubscriptionProduct = Pick<TenantSubscriptionProduct, "id" | "endsAt" | "cancelledAt" | "customEntitlementPlanId"> & {
|
||||
@@ -275,7 +367,6 @@ const subscriptionProductDetailsInclude = {
|
||||
tenantProducts: true,
|
||||
usageBasedPrices: { include: { tiers: { orderBy: { from: "asc" } } } },
|
||||
prices: true,
|
||||
assignsTenantTypes: true,
|
||||
features: {
|
||||
orderBy: {
|
||||
order: "asc",
|
||||
@@ -297,6 +388,7 @@ const tenantSimpleSelect = {
|
||||
|
||||
const tenantSubscriptionProductDetailsInclude = {
|
||||
subscriptionProduct: { include: { features: true } },
|
||||
acquisition: true,
|
||||
prices: { include: { subscriptionPrice: true, subscriptionUsageBasedPrice: { include: { tiers: true } } } },
|
||||
} as const;
|
||||
|
||||
@@ -353,10 +445,7 @@ export type SubscriptionCatalogClient = {
|
||||
include: typeof subscriptionProductDetailsInclude;
|
||||
orderBy: { order: "asc" } | [{ public: "desc" }, { order: "asc" }];
|
||||
}): Promise<SubscriptionProductWithDetails[]>;
|
||||
findUnique(args: {
|
||||
where: { id: string };
|
||||
include: typeof subscriptionProductDetailsInclude;
|
||||
}): Promise<SubscriptionProductWithDetails | null>;
|
||||
findUnique(args: { where: { id: string }; include: typeof subscriptionProductDetailsInclude }): Promise<SubscriptionProductWithDetails | null>;
|
||||
findFirst(args: { where: { billingProviderId: string } }): Promise<SubscriptionProduct | null>;
|
||||
create(args: { data: SubscriptionProductCreateInput }): Promise<SubscriptionProduct>;
|
||||
update(args: { where: { id: string }; data: SubscriptionProductUpdateInput | { billingProviderId: string } }): Promise<SubscriptionProduct>;
|
||||
@@ -393,6 +482,18 @@ export type SubscriptionCatalogClient = {
|
||||
|
||||
export type SubscriptionProductReaderClient = SubscriptionCatalogClient;
|
||||
|
||||
export type SubscriptionProductUsageClient = {
|
||||
adminUser: {
|
||||
findUnique(args: { where: { id: string }; select: { id: true } }): Promise<{ id: string } | null>;
|
||||
};
|
||||
tenant: {
|
||||
findMany(args: { select: { id: true }; orderBy: { id: "asc" } }): Promise<Array<{ id: string }>>;
|
||||
};
|
||||
tenantSubscriptionProduct: {
|
||||
findMany(args: { select: { subscriptionProductId: true } }): Promise<Array<{ subscriptionProductId: string }>>;
|
||||
};
|
||||
};
|
||||
|
||||
type TenantSubscriptionProductWhereInput = {
|
||||
subscriptionProductId?: { equals: string };
|
||||
tenantSubscription?: { tenantId?: { equals: string } };
|
||||
@@ -403,10 +504,7 @@ type TenantSubscriptionProductWhereInput = {
|
||||
|
||||
export type TenantSubscriptionReaderClient = {
|
||||
tenantSubscription: {
|
||||
findUnique(args: {
|
||||
where: { tenantId: string };
|
||||
include: typeof tenantSubscriptionDetailsInclude;
|
||||
}): Promise<TenantSubscriptionWithDetails | null>;
|
||||
findUnique(args: { where: { tenantId: string }; include: typeof tenantSubscriptionDetailsInclude }): Promise<TenantSubscriptionWithDetails | null>;
|
||||
findMany(args: { include: typeof tenantSubscriptionDetailsInclude }): Promise<TenantSubscriptionWithDetails[]>;
|
||||
};
|
||||
};
|
||||
@@ -431,10 +529,16 @@ export type TenantBillingClient = TenantSubscriptionReaderClient & {
|
||||
}): Promise<TenantSubscriptionProductWithTenant[]>;
|
||||
count(args: { where: TenantSubscriptionProductWhereInput }): Promise<number>;
|
||||
findFirst(args: {
|
||||
where: { tenantSubscription: { tenantId: string }; subscriptionProductId: string } | { id: string; tenantSubscription: { tenantId: string } } | { billingProviderSubscriptionId: string };
|
||||
where:
|
||||
| { tenantSubscription: { tenantId: string }; subscriptionProductId: string }
|
||||
| { id: string; tenantSubscription: { tenantId: string } }
|
||||
| { billingProviderSubscriptionId: string };
|
||||
include: typeof tenantSubscriptionProductWithTenantInclude;
|
||||
}): Promise<TenantSubscriptionProductWithTenant | null>;
|
||||
findUnique(args: {
|
||||
where: { id: string };
|
||||
include: typeof tenantSubscriptionProductWithTenantInclude;
|
||||
}): Promise<TenantSubscriptionProductWithTenant | null>;
|
||||
findUnique(args: { where: { id: string }; include: typeof tenantSubscriptionProductWithTenantInclude }): Promise<TenantSubscriptionProductWithTenant | null>;
|
||||
create(args: {
|
||||
data: Omit<TenantSubscriptionProductCreateInput, "prices"> & {
|
||||
endsAt: null;
|
||||
@@ -455,12 +559,13 @@ export type TenantBillingClient = TenantSubscriptionReaderClient & {
|
||||
}): Promise<{ tenantSubscription: { tenantId: string } }>;
|
||||
};
|
||||
checkoutSessionStatus: {
|
||||
findUnique(args: { where: { id: string } }): Promise<CheckoutSessionStatus | null>;
|
||||
create(args: { data: CheckoutSessionStatusCreateInput & { pending: true; fromUserId: string | null; fromTenantId: string | null } }): Promise<CheckoutSessionStatus>;
|
||||
findFirst(args: { where: { id: string; tenantId: string } }): Promise<TenantCheckoutSessionStatus | null>;
|
||||
create(args: { data: CheckoutSessionStatusCreateInput & { pending: true; fromUserId: string | null } }): Promise<TenantCheckoutSessionStatus>;
|
||||
update(args: {
|
||||
where: { id: string };
|
||||
where: { id: string; tenantId: string };
|
||||
data: CheckoutSessionStatusUpdateInput & { createdUserId: string | null; createdTenantId: string | null };
|
||||
}): Promise<CheckoutSessionStatus>;
|
||||
}): Promise<TenantCheckoutSessionStatus>;
|
||||
updateMany(args: { where: Record<string, unknown>; data: Record<string, unknown> }): Promise<{ count: number }>;
|
||||
};
|
||||
tenantSubscriptionUsageRecord: {
|
||||
create(args: { data: TenantSubscriptionUsageRecordCreateInput }): Promise<TenantSubscriptionUsageRecord>;
|
||||
@@ -482,7 +587,7 @@ export type BillingProviderTenantSubscriptionProductClient = {
|
||||
};
|
||||
|
||||
export async function getAllSubscriptionProductsWithTenants(
|
||||
client: SubscriptionProductReaderClient = getDbClient(),
|
||||
client: SubscriptionProductReaderClient = getDbClient()
|
||||
): Promise<SubscriptionProductWithDetails[]> {
|
||||
return await client.subscriptionProduct
|
||||
.findMany({
|
||||
@@ -494,45 +599,84 @@ export async function getAllSubscriptionProductsWithTenants(
|
||||
});
|
||||
}
|
||||
|
||||
export async function getAllSubscriptionProducts(
|
||||
isPublic?: boolean,
|
||||
client: SubscriptionProductReaderClient = getDbClient(),
|
||||
): Promise<SubscriptionProductWithDetails[]> {
|
||||
if (isPublic) {
|
||||
return await client.subscriptionProduct
|
||||
.findMany({
|
||||
where: {
|
||||
active: true,
|
||||
public: true,
|
||||
},
|
||||
include: subscriptionProductDetailsInclude,
|
||||
orderBy: {
|
||||
order: "asc",
|
||||
},
|
||||
})
|
||||
.catch(() => {
|
||||
return [];
|
||||
});
|
||||
/**
|
||||
* Returns only aggregate catalog usage. Raw tenant subscription rows remain
|
||||
* behind each tenant's RLS boundary and are never exposed to the admin UI.
|
||||
*/
|
||||
export async function getSubscriptionProductTenantUsageCounts(client: SubscriptionProductUsageClient = getDbClient()): Promise<Record<string, number>> {
|
||||
const context = getRlsExecutionContext();
|
||||
if (!context?.userId || context.tenantId) {
|
||||
throw new Error("Subscription catalog usage requires a root platform-operator context");
|
||||
}
|
||||
|
||||
return await client.subscriptionProduct
|
||||
.findMany({
|
||||
const operator = await client.adminUser.findUnique({
|
||||
where: { id: context.userId },
|
||||
select: { id: true },
|
||||
});
|
||||
if (!operator) throw new Error("Subscription catalog usage requires a platform operator");
|
||||
|
||||
const tenants = await client.tenant.findMany({ select: { id: true }, orderBy: { id: "asc" } });
|
||||
const counts: Record<string, number> = {};
|
||||
const concurrency = 8;
|
||||
for (let index = 0; index < tenants.length; index += concurrency) {
|
||||
const batch = tenants.slice(index, index + concurrency);
|
||||
const rowsByTenant = await Promise.all(
|
||||
batch.map(({ id: tenantId }) =>
|
||||
runWithRlsContext({ tenantId }, () => client.tenantSubscriptionProduct.findMany({ select: { subscriptionProductId: true } }))
|
||||
)
|
||||
);
|
||||
for (const rows of rowsByTenant) {
|
||||
for (const row of rows) counts[row.subscriptionProductId] = (counts[row.subscriptionProductId] ?? 0) + 1;
|
||||
}
|
||||
}
|
||||
return counts;
|
||||
}
|
||||
|
||||
export async function getAllSubscriptionProducts(
|
||||
isPublic?: boolean,
|
||||
client: SubscriptionProductReaderClient = getDbClient()
|
||||
): Promise<SubscriptionProductWithDetails[]> {
|
||||
return await getAllSubscriptionProductsStrict(isPublic, client).catch(() => {
|
||||
return [];
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads the active subscription catalog without the legacy empty-list fallback.
|
||||
* Bootstrap callers use this variant so an unavailable catalog cannot be
|
||||
* mistaken for a valid catalog with no eligible plan.
|
||||
*/
|
||||
export async function getAllSubscriptionProductsStrict(
|
||||
isPublic?: boolean,
|
||||
client: SubscriptionProductReaderClient = getDbClient()
|
||||
): Promise<SubscriptionProductWithDetails[]> {
|
||||
if (isPublic) {
|
||||
return await client.subscriptionProduct.findMany({
|
||||
where: {
|
||||
active: true,
|
||||
public: true,
|
||||
},
|
||||
include: subscriptionProductDetailsInclude,
|
||||
orderBy: {
|
||||
order: "asc",
|
||||
},
|
||||
})
|
||||
.catch(() => {
|
||||
return [];
|
||||
});
|
||||
}
|
||||
|
||||
return await client.subscriptionProduct.findMany({
|
||||
where: {
|
||||
active: true,
|
||||
},
|
||||
include: subscriptionProductDetailsInclude,
|
||||
orderBy: {
|
||||
order: "asc",
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function getSubscriptionProductsInIds(
|
||||
ids: string[],
|
||||
client: SubscriptionProductReaderClient = getDbClient(),
|
||||
client: SubscriptionProductReaderClient = getDbClient()
|
||||
): Promise<SubscriptionProductWithDetails[]> {
|
||||
return await client.subscriptionProduct.findMany({
|
||||
where: {
|
||||
@@ -543,9 +687,7 @@ export async function getSubscriptionProductsInIds(
|
||||
});
|
||||
}
|
||||
|
||||
export async function getAllSubscriptionFeatures(
|
||||
client: SubscriptionProductReaderClient = getDbClient(),
|
||||
): Promise<SubscriptionFeatureSummary[]> {
|
||||
export async function getAllSubscriptionFeatures(client: SubscriptionProductReaderClient = getDbClient()): Promise<SubscriptionFeatureSummary[]> {
|
||||
return await client.subscriptionFeature.findMany({
|
||||
select: { name: true, code: true, order: true, title: true, type: true, value: true, accumulate: true },
|
||||
});
|
||||
@@ -553,7 +695,7 @@ export async function getAllSubscriptionFeatures(
|
||||
|
||||
export async function getSubscriptionProduct(
|
||||
id: string,
|
||||
client: SubscriptionProductReaderClient = getDbClient(),
|
||||
client: SubscriptionProductReaderClient = getDbClient()
|
||||
): Promise<SubscriptionProductWithDetails | null> {
|
||||
return await client.subscriptionProduct.findUnique({
|
||||
where: {
|
||||
@@ -565,7 +707,7 @@ export async function getSubscriptionProduct(
|
||||
|
||||
export async function getSubscriptionProductByBillingProviderId(
|
||||
billingProviderId: string,
|
||||
client: SubscriptionCatalogClient = getDbClient(),
|
||||
client: SubscriptionCatalogClient = getDbClient()
|
||||
): Promise<SubscriptionProduct | null> {
|
||||
return await client.subscriptionProduct.findFirst({
|
||||
where: {
|
||||
@@ -574,9 +716,7 @@ export async function getSubscriptionProductByBillingProviderId(
|
||||
});
|
||||
}
|
||||
|
||||
export async function getSubscriptionPrices(
|
||||
client: SubscriptionCatalogClient = getDbClient(),
|
||||
): Promise<SubscriptionPriceWithProduct[]> {
|
||||
export async function getSubscriptionPrices(client: SubscriptionCatalogClient = getDbClient()): Promise<SubscriptionPriceWithProduct[]> {
|
||||
return await client.subscriptionPrice
|
||||
.findMany({
|
||||
include: {
|
||||
@@ -598,10 +738,7 @@ export async function getSubscriptionPrices(
|
||||
});
|
||||
}
|
||||
|
||||
export async function getSubscriptionPrice(
|
||||
id: string,
|
||||
client: SubscriptionCatalogClient = getDbClient(),
|
||||
): Promise<SubscriptionPriceWithProduct | null> {
|
||||
export async function getSubscriptionPrice(id: string, client: SubscriptionCatalogClient = getDbClient()): Promise<SubscriptionPriceWithProduct | null> {
|
||||
return await client.subscriptionPrice
|
||||
.findUnique({
|
||||
where: { id },
|
||||
@@ -616,7 +753,7 @@ export async function getSubscriptionPrice(
|
||||
|
||||
export async function getSubscriptionPriceByBillingProviderId(
|
||||
billingProviderId: string,
|
||||
client: SubscriptionCatalogClient = getDbClient(),
|
||||
client: SubscriptionCatalogClient = getDbClient()
|
||||
): Promise<SubscriptionPriceWithProduct | null> {
|
||||
return await client.subscriptionPrice
|
||||
.findFirst({
|
||||
@@ -632,7 +769,7 @@ export async function getSubscriptionPriceByBillingProviderId(
|
||||
|
||||
export async function getSubscriptionUsageBasedPriceByBillingProviderId(
|
||||
billingProviderId: string,
|
||||
client: SubscriptionCatalogClient = getDbClient(),
|
||||
client: SubscriptionCatalogClient = getDbClient()
|
||||
): Promise<SubscriptionUsageBasedPriceWithProduct | null> {
|
||||
return await client.subscriptionUsageBasedPrice
|
||||
.findFirst({
|
||||
@@ -648,7 +785,7 @@ export async function getSubscriptionUsageBasedPriceByBillingProviderId(
|
||||
|
||||
export async function createSubscriptionProduct(
|
||||
data: SubscriptionProductCreateInput,
|
||||
client: SubscriptionCatalogClient = getDbClient(),
|
||||
client: SubscriptionCatalogClient = getDbClient()
|
||||
): Promise<SubscriptionProduct> {
|
||||
return await client.subscriptionProduct.create({ data });
|
||||
}
|
||||
@@ -656,7 +793,7 @@ export async function createSubscriptionProduct(
|
||||
export async function updateSubscriptionProduct(
|
||||
id: string,
|
||||
data: SubscriptionProductUpdateInput,
|
||||
client: SubscriptionCatalogClient = getDbClient(),
|
||||
client: SubscriptionCatalogClient = getDbClient()
|
||||
): Promise<SubscriptionProduct> {
|
||||
return await client.subscriptionProduct.update({
|
||||
where: { id },
|
||||
@@ -667,7 +804,7 @@ export async function updateSubscriptionProduct(
|
||||
export async function updateSubscriptionProductBillingProviderId(
|
||||
id: string,
|
||||
data: { billingProviderId: string },
|
||||
client: SubscriptionCatalogClient = getDbClient(),
|
||||
client: SubscriptionCatalogClient = getDbClient()
|
||||
): Promise<SubscriptionProduct> {
|
||||
return await client.subscriptionProduct.update({
|
||||
where: { id },
|
||||
@@ -678,7 +815,7 @@ export async function updateSubscriptionProductBillingProviderId(
|
||||
export async function updateSubscriptionPriceBillingProviderId(
|
||||
id: string,
|
||||
data: { billingProviderId: string },
|
||||
client: SubscriptionCatalogClient = getDbClient(),
|
||||
client: SubscriptionCatalogClient = getDbClient()
|
||||
): Promise<SubscriptionPrice> {
|
||||
return await client.subscriptionPrice.update({
|
||||
where: { id },
|
||||
@@ -688,7 +825,7 @@ export async function updateSubscriptionPriceBillingProviderId(
|
||||
|
||||
export async function createSubscriptionPrice(
|
||||
data: SubscriptionPriceCreateInput,
|
||||
client: SubscriptionCatalogClient = getDbClient(),
|
||||
client: SubscriptionCatalogClient = getDbClient()
|
||||
): Promise<SubscriptionPrice> {
|
||||
return await client.subscriptionPrice.create({ data });
|
||||
}
|
||||
@@ -696,7 +833,7 @@ export async function createSubscriptionPrice(
|
||||
export async function updateSubscriptionPrice(
|
||||
id: string,
|
||||
data: SubscriptionPriceUpdateInput,
|
||||
client: SubscriptionCatalogClient = getDbClient(),
|
||||
client: SubscriptionCatalogClient = getDbClient()
|
||||
): Promise<SubscriptionPrice> {
|
||||
return await client.subscriptionPrice.update({
|
||||
where: { id },
|
||||
@@ -706,14 +843,14 @@ export async function updateSubscriptionPrice(
|
||||
|
||||
export async function createSubscriptionUsageBasedPrice(
|
||||
data: SubscriptionUsageBasedPriceCreateInput,
|
||||
client: SubscriptionCatalogClient = getDbClient(),
|
||||
client: SubscriptionCatalogClient = getDbClient()
|
||||
): Promise<SubscriptionUsageBasedPrice> {
|
||||
return await client.subscriptionUsageBasedPrice.create({ data });
|
||||
}
|
||||
|
||||
export async function createSubscriptionUsageBasedTier(
|
||||
data: SubscriptionUsageBasedTierCreateInput,
|
||||
client: SubscriptionCatalogClient = getDbClient(),
|
||||
client: SubscriptionCatalogClient = getDbClient()
|
||||
): Promise<SubscriptionUsageBasedTier> {
|
||||
return await client.subscriptionUsageBasedTier.create({ data });
|
||||
}
|
||||
@@ -721,7 +858,7 @@ export async function createSubscriptionUsageBasedTier(
|
||||
export async function createSubscriptionFeature(
|
||||
subscriptionProductId: string,
|
||||
data: SubscriptionFeatureCreateInput,
|
||||
client: SubscriptionCatalogClient = getDbClient(),
|
||||
client: SubscriptionCatalogClient = getDbClient()
|
||||
): Promise<SubscriptionFeature> {
|
||||
return await client.subscriptionFeature.create({
|
||||
data: {
|
||||
@@ -731,46 +868,31 @@ export async function createSubscriptionFeature(
|
||||
});
|
||||
}
|
||||
|
||||
export async function deleteSubscriptionProduct(
|
||||
id: string,
|
||||
client: SubscriptionCatalogClient = getDbClient(),
|
||||
): Promise<SubscriptionProduct> {
|
||||
export async function deleteSubscriptionProduct(id: string, client: SubscriptionCatalogClient = getDbClient()): Promise<SubscriptionProduct> {
|
||||
return await client.subscriptionProduct.delete({
|
||||
where: { id },
|
||||
});
|
||||
}
|
||||
|
||||
export async function deleteSubscriptionPrice(
|
||||
id: string,
|
||||
client: SubscriptionCatalogClient = getDbClient(),
|
||||
): Promise<SubscriptionPrice> {
|
||||
export async function deleteSubscriptionPrice(id: string, client: SubscriptionCatalogClient = getDbClient()): Promise<SubscriptionPrice> {
|
||||
return await client.subscriptionPrice.delete({
|
||||
where: { id },
|
||||
});
|
||||
}
|
||||
|
||||
export async function deleteSubscriptionUsageBasedTier(
|
||||
id: string,
|
||||
client: SubscriptionCatalogClient = getDbClient(),
|
||||
): Promise<SubscriptionUsageBasedTier> {
|
||||
export async function deleteSubscriptionUsageBasedTier(id: string, client: SubscriptionCatalogClient = getDbClient()): Promise<SubscriptionUsageBasedTier> {
|
||||
return await client.subscriptionUsageBasedTier.delete({
|
||||
where: { id },
|
||||
});
|
||||
}
|
||||
|
||||
export async function deleteSubscriptionUsageBasedPrice(
|
||||
id: string,
|
||||
client: SubscriptionCatalogClient = getDbClient(),
|
||||
): Promise<SubscriptionUsageBasedPrice> {
|
||||
export async function deleteSubscriptionUsageBasedPrice(id: string, client: SubscriptionCatalogClient = getDbClient()): Promise<SubscriptionUsageBasedPrice> {
|
||||
return await client.subscriptionUsageBasedPrice.delete({
|
||||
where: { id },
|
||||
});
|
||||
}
|
||||
|
||||
export async function deleteSubscriptionFeatures(
|
||||
subscriptionProductId: string,
|
||||
client: SubscriptionCatalogClient = getDbClient(),
|
||||
): Promise<{ count: number }> {
|
||||
export async function deleteSubscriptionFeatures(subscriptionProductId: string, client: SubscriptionCatalogClient = getDbClient()): Promise<{ count: number }> {
|
||||
return await client.subscriptionFeature.deleteMany({
|
||||
where: { subscriptionProductId },
|
||||
});
|
||||
@@ -778,7 +900,7 @@ export async function deleteSubscriptionFeatures(
|
||||
|
||||
export async function getTenantSubscription(
|
||||
tenantId: string,
|
||||
client: TenantSubscriptionReaderClient = getDbClient(),
|
||||
client: TenantSubscriptionReaderClient = getDbClient()
|
||||
): Promise<TenantSubscriptionWithDetails | null> {
|
||||
return await client.tenantSubscription.findUnique({
|
||||
where: {
|
||||
@@ -788,9 +910,7 @@ export async function getTenantSubscription(
|
||||
});
|
||||
}
|
||||
|
||||
export async function getTenantSubscriptions(
|
||||
client: TenantSubscriptionReaderClient = getDbClient(),
|
||||
): Promise<TenantSubscriptionWithDetails[]> {
|
||||
export async function getTenantSubscriptions(client: TenantSubscriptionReaderClient = getDbClient()): Promise<TenantSubscriptionWithDetails[]> {
|
||||
return await client.tenantSubscription.findMany({
|
||||
include: tenantSubscriptionDetailsInclude,
|
||||
});
|
||||
@@ -798,7 +918,7 @@ export async function getTenantSubscriptions(
|
||||
|
||||
export async function createTenantSubscription(
|
||||
data: TenantSubscriptionCreateInput,
|
||||
client: TenantBillingClient = getTenantBillingClient(),
|
||||
client: TenantBillingClient = getTenantBillingClient()
|
||||
): Promise<TenantSubscriptionWithDetails> {
|
||||
return await client.tenantSubscription.create({
|
||||
data,
|
||||
@@ -809,7 +929,7 @@ export async function createTenantSubscription(
|
||||
export async function updateTenantSubscriptionCustomerId(
|
||||
tenantId: string,
|
||||
data: TenantSubscriptionCustomerIdUpdateInput,
|
||||
client: TenantBillingClient = getTenantBillingClient(),
|
||||
client: TenantBillingClient = getTenantBillingClient()
|
||||
): Promise<TenantSubscription> {
|
||||
return await client.tenantSubscription.update({
|
||||
where: { tenantId },
|
||||
@@ -862,7 +982,7 @@ export async function getAllTenantSubscriptionProducts(
|
||||
filters?: TenantSubscriptionProductFilters,
|
||||
pagination?: TenantSubscriptionProductPagination,
|
||||
client: TenantBillingClient = getTenantBillingClient(),
|
||||
now: Date = new Date(),
|
||||
now: Date = new Date()
|
||||
): Promise<TenantSubscriptionProductPage> {
|
||||
const where = buildTenantSubscriptionProductWhere(filters, now);
|
||||
const items = await client.tenantSubscriptionProduct.findMany({
|
||||
@@ -890,7 +1010,7 @@ export async function getAllTenantSubscriptionProducts(
|
||||
export async function getTenantSubscriptionProduct(
|
||||
tenantId: string,
|
||||
subscriptionProductId: string,
|
||||
client: TenantBillingClient = getTenantBillingClient(),
|
||||
client: TenantBillingClient = getTenantBillingClient()
|
||||
): Promise<TenantSubscriptionProductWithTenant | null> {
|
||||
return await client.tenantSubscriptionProduct.findFirst({
|
||||
where: {
|
||||
@@ -903,7 +1023,7 @@ export async function getTenantSubscriptionProduct(
|
||||
|
||||
export async function adminGetTenantSubscriptionProductById(
|
||||
id: string,
|
||||
client: TenantBillingClient = getTenantBillingClient(),
|
||||
client: TenantBillingClient = getTenantBillingClient()
|
||||
): Promise<TenantSubscriptionProductWithTenant | null> {
|
||||
return await client.tenantSubscriptionProduct.findUnique({
|
||||
where: { id },
|
||||
@@ -913,7 +1033,7 @@ export async function adminGetTenantSubscriptionProductById(
|
||||
|
||||
export async function getTenantSubscriptionProductById(
|
||||
{ id, tenantId }: { id: string; tenantId: string },
|
||||
client: TenantBillingClient = getTenantBillingClient(),
|
||||
client: TenantBillingClient = getTenantBillingClient()
|
||||
): Promise<TenantSubscriptionProductWithTenant | null> {
|
||||
return await client.tenantSubscriptionProduct.findFirst({
|
||||
where: { id, tenantSubscription: { tenantId } },
|
||||
@@ -923,7 +1043,7 @@ export async function getTenantSubscriptionProductById(
|
||||
|
||||
export async function getTenantSubscriptionProductByBillingProviderSubscriptionId(
|
||||
billingProviderSubscriptionId: string,
|
||||
client: TenantBillingClient = getTenantBillingClient(),
|
||||
client: TenantBillingClient = getTenantBillingClient()
|
||||
): Promise<TenantSubscriptionProductWithTenant | null> {
|
||||
return await client.tenantSubscriptionProduct.findFirst({
|
||||
where: { billingProviderSubscriptionId },
|
||||
@@ -933,7 +1053,7 @@ export async function getTenantSubscriptionProductByBillingProviderSubscriptionI
|
||||
|
||||
export async function addTenantSubscriptionProduct(
|
||||
data: TenantSubscriptionProductCreateInput,
|
||||
client: TenantBillingClient = getTenantBillingClient(),
|
||||
client: TenantBillingClient = getTenantBillingClient()
|
||||
): Promise<TenantSubscriptionProduct & { tenantSubscription: TenantSubscription }> {
|
||||
return await client.tenantSubscriptionProduct.create({
|
||||
data: {
|
||||
@@ -943,6 +1063,10 @@ export async function addTenantSubscriptionProduct(
|
||||
lagoSubscriptionId: data.lagoSubscriptionId,
|
||||
planCode: data.planCode,
|
||||
seatQuantity: data.seatQuantity,
|
||||
officeSeatQuantity: data.officeSeatQuantity,
|
||||
fieldSeatQuantity: data.fieldSeatQuantity,
|
||||
billingCadence: data.billingCadence,
|
||||
commitmentEndsAt: data.commitmentEndsAt,
|
||||
quantity: data.quantity,
|
||||
fromCheckoutSessionId: data.fromCheckoutSessionId,
|
||||
customEntitlementPlanId: data.customEntitlementPlanId,
|
||||
@@ -959,7 +1083,7 @@ export async function addTenantSubscriptionProduct(
|
||||
export async function updateTenantSubscriptionProduct(
|
||||
{ id, tenantId }: { id: string; tenantId: string },
|
||||
data: TenantSubscriptionProductUpdateInput,
|
||||
client: TenantBillingClient = getTenantBillingClient(),
|
||||
client: TenantBillingClient = getTenantBillingClient()
|
||||
): Promise<TenantSubscriptionProduct & { tenantSubscription: TenantSubscription }> {
|
||||
const existing = await getTenantSubscriptionProductById({ id, tenantId }, client);
|
||||
if (!existing) {
|
||||
@@ -972,16 +1096,14 @@ export async function updateTenantSubscriptionProduct(
|
||||
});
|
||||
}
|
||||
|
||||
export function getEffectiveTenantEntitlementPlanId(
|
||||
item: Pick<TenantSubscriptionProduct, "subscriptionProductId" | "customEntitlementPlanId">,
|
||||
): string {
|
||||
export function getEffectiveTenantEntitlementPlanId(item: Pick<TenantSubscriptionProduct, "subscriptionProductId" | "customEntitlementPlanId">): string {
|
||||
return item.customEntitlementPlanId ?? item.subscriptionProductId;
|
||||
}
|
||||
|
||||
export async function updateTenantSubscriptionProductCustomEntitlementPlan(
|
||||
{ id, tenantId }: { id: string; tenantId: string },
|
||||
data: TenantSubscriptionProductCustomEntitlementPlanUpdateInput,
|
||||
client: TenantBillingClient = getTenantBillingClient(),
|
||||
client: TenantBillingClient = getTenantBillingClient()
|
||||
): Promise<TenantIdResult> {
|
||||
const existing = await getTenantSubscriptionProductById({ id, tenantId }, client);
|
||||
if (!existing) {
|
||||
@@ -1005,7 +1127,7 @@ export async function updateTenantSubscriptionProductCustomEntitlementPlan(
|
||||
export async function cancelTenantSubscriptionProduct(
|
||||
{ id, tenantId }: { id: string; tenantId: string },
|
||||
data: TenantSubscriptionProductCancelInput,
|
||||
client: TenantBillingClient = getTenantBillingClient(),
|
||||
client: TenantBillingClient = getTenantBillingClient()
|
||||
): Promise<TenantIdResult> {
|
||||
const existing = await getTenantSubscriptionProductById({ id, tenantId }, client);
|
||||
if (!existing) {
|
||||
@@ -1023,70 +1145,171 @@ export async function cancelTenantSubscriptionProduct(
|
||||
return { tenantId: item.tenantSubscription.tenantId };
|
||||
}
|
||||
|
||||
export async function getCheckoutSessionStatus(id: string, client: TenantBillingClient = getTenantBillingClient()): Promise<CheckoutSessionStatus | null> {
|
||||
return await client.checkoutSessionStatus.findUnique({
|
||||
where: { id },
|
||||
});
|
||||
export async function getCheckoutSessionStatus(
|
||||
{ id, tenantId }: { id: string; tenantId: string },
|
||||
client: TenantBillingClient = getTenantBillingClient()
|
||||
): Promise<TenantCheckoutSessionStatus | null> {
|
||||
return runWithRlsContext({ tenantId }, () =>
|
||||
client.checkoutSessionStatus.findFirst({
|
||||
where: { id, tenantId },
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
export async function createCheckoutSessionStatus(
|
||||
data: CheckoutSessionStatusCreateInput,
|
||||
client: TenantBillingClient = getTenantBillingClient(),
|
||||
): Promise<CheckoutSessionStatus> {
|
||||
return await client.checkoutSessionStatus.create({
|
||||
data: {
|
||||
id: data.id,
|
||||
pending: true,
|
||||
email: data.email,
|
||||
fromUrl: data.fromUrl,
|
||||
fromUserId: data.fromUserId ?? null,
|
||||
fromTenantId: data.fromTenantId ?? null,
|
||||
provider: data.provider ?? null,
|
||||
providerPaymentId: data.providerPaymentId ?? null,
|
||||
invoiceExternalId: data.invoiceExternalId ?? null,
|
||||
subscriptionProductId: data.subscriptionProductId ?? null,
|
||||
subscriptionPriceId: data.subscriptionPriceId ?? null,
|
||||
lagoSubscriptionId: data.lagoSubscriptionId ?? null,
|
||||
planCode: data.planCode ?? null,
|
||||
quantity: data.quantity ?? 1,
|
||||
amountCents: data.amountCents ?? null,
|
||||
currency: data.currency ?? null,
|
||||
checkoutUrl: data.checkoutUrl ?? null,
|
||||
status: data.status ?? "open",
|
||||
},
|
||||
});
|
||||
client: TenantBillingClient = getTenantBillingClient()
|
||||
): Promise<TenantCheckoutSessionStatus> {
|
||||
return runWithRlsContext({ tenantId: data.tenantId }, () =>
|
||||
client.checkoutSessionStatus.create({
|
||||
data: {
|
||||
id: data.id,
|
||||
tenantId: data.tenantId,
|
||||
pending: true,
|
||||
email: data.email,
|
||||
fromUrl: data.fromUrl,
|
||||
fromUserId: data.fromUserId ?? null,
|
||||
provider: data.provider ?? null,
|
||||
providerPaymentId: data.providerPaymentId ?? null,
|
||||
invoiceExternalId: data.invoiceExternalId ?? null,
|
||||
subscriptionProductId: data.subscriptionProductId ?? null,
|
||||
subscriptionPriceId: data.subscriptionPriceId ?? null,
|
||||
lagoSubscriptionId: data.lagoSubscriptionId ?? null,
|
||||
planCode: data.planCode ?? null,
|
||||
quantity: data.quantity ?? 1,
|
||||
officeSeatQuantity: data.officeSeatQuantity ?? 0,
|
||||
fieldSeatQuantity: data.fieldSeatQuantity ?? 0,
|
||||
billingCadence: data.billingCadence ?? "monthly",
|
||||
commitmentEndsAt: data.commitmentEndsAt ?? null,
|
||||
storageAddOnCodes: data.storageAddOnCodes ?? [],
|
||||
amountCents: data.amountCents ?? null,
|
||||
currency: data.currency ?? null,
|
||||
checkoutUrl: data.checkoutUrl ?? null,
|
||||
status: data.status ?? "open",
|
||||
},
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
export async function updateCheckoutSessionStatus(
|
||||
id: string,
|
||||
{ id, tenantId }: { id: string; tenantId: string },
|
||||
data: CheckoutSessionStatusUpdateInput,
|
||||
client: TenantBillingClient = getTenantBillingClient(),
|
||||
): Promise<CheckoutSessionStatus> {
|
||||
return await client.checkoutSessionStatus.update({
|
||||
where: { id },
|
||||
data: {
|
||||
pending: data.pending,
|
||||
createdUserId: data.createdUserId ?? null,
|
||||
createdTenantId: data.createdTenantId ?? null,
|
||||
providerPaymentId: data.providerPaymentId ?? undefined,
|
||||
invoiceExternalId: data.invoiceExternalId ?? undefined,
|
||||
lagoSubscriptionId: data.lagoSubscriptionId ?? undefined,
|
||||
checkoutUrl: data.checkoutUrl ?? undefined,
|
||||
status: data.status ?? undefined,
|
||||
},
|
||||
client: TenantBillingClient = getTenantBillingClient()
|
||||
): Promise<TenantCheckoutSessionStatus> {
|
||||
return runWithRlsContext({ tenantId }, () =>
|
||||
client.checkoutSessionStatus.update({
|
||||
where: { id, tenantId },
|
||||
data: {
|
||||
pending: data.pending,
|
||||
createdUserId: data.createdUserId ?? null,
|
||||
createdTenantId: data.createdTenantId ?? null,
|
||||
providerPaymentId: data.providerPaymentId ?? undefined,
|
||||
invoiceExternalId: data.invoiceExternalId ?? undefined,
|
||||
lagoSubscriptionId: data.lagoSubscriptionId ?? undefined,
|
||||
checkoutUrl: data.checkoutUrl ?? undefined,
|
||||
status: data.status ?? undefined,
|
||||
fromUrl: data.fromUrl ?? undefined,
|
||||
},
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
export async function claimCheckoutSessionStatus(
|
||||
input: CheckoutSessionStatusClaimInput,
|
||||
client: TenantBillingClient = getTenantBillingClient()
|
||||
): Promise<TenantCheckoutSessionStatus | null> {
|
||||
return runWithRlsContext({ tenantId: input.tenantId }, async () => {
|
||||
const claimed = await client.checkoutSessionStatus.updateMany({
|
||||
where: {
|
||||
id: input.id,
|
||||
tenantId: input.tenantId,
|
||||
pending: true,
|
||||
status: input.expectedStatus,
|
||||
...(input.updatedBefore ? { updatedAt: { lte: input.updatedBefore } } : {}),
|
||||
},
|
||||
data: { status: input.claimedStatus },
|
||||
});
|
||||
if (claimed.count !== 1) return null;
|
||||
const session = await client.checkoutSessionStatus.findFirst({ where: { id: input.id, tenantId: input.tenantId } });
|
||||
return session?.status === input.claimedStatus ? session : null;
|
||||
});
|
||||
}
|
||||
|
||||
export async function transitionClaimedCheckoutSessionStatus(
|
||||
input: { id: string; tenantId: string; claimedStatus: string; data: CheckoutSessionStatusUpdateInput },
|
||||
client: TenantBillingClient = getTenantBillingClient()
|
||||
): Promise<TenantCheckoutSessionStatus | null> {
|
||||
return runWithRlsContext({ tenantId: input.tenantId }, async () => {
|
||||
const transitioned = await client.checkoutSessionStatus.updateMany({
|
||||
where: { id: input.id, tenantId: input.tenantId, pending: true, status: input.claimedStatus },
|
||||
data: {
|
||||
pending: input.data.pending,
|
||||
providerPaymentId: input.data.providerPaymentId ?? undefined,
|
||||
invoiceExternalId: input.data.invoiceExternalId ?? undefined,
|
||||
lagoSubscriptionId: input.data.lagoSubscriptionId ?? undefined,
|
||||
checkoutUrl: input.data.checkoutUrl ?? undefined,
|
||||
status: input.data.status ?? undefined,
|
||||
fromUrl: input.data.fromUrl ?? undefined,
|
||||
},
|
||||
});
|
||||
if (transitioned.count !== 1) return null;
|
||||
return client.checkoutSessionStatus.findFirst({ where: { id: input.id, tenantId: input.tenantId } });
|
||||
});
|
||||
}
|
||||
|
||||
export async function correlateCheckoutSessionProviderInvoice(
|
||||
input: CheckoutSessionInvoiceCorrelationInput,
|
||||
client: TenantBillingClient = getTenantBillingClient()
|
||||
): Promise<void> {
|
||||
const normalized = {
|
||||
id: input.id.trim(),
|
||||
tenantId: input.tenantId.trim(),
|
||||
providerPaymentId: input.providerPaymentId.trim(),
|
||||
lagoSubscriptionId: input.lagoSubscriptionId.trim(),
|
||||
sourceInvoiceExternalId: input.sourceInvoiceExternalId.trim(),
|
||||
providerInvoiceExternalId: input.providerInvoiceExternalId.trim(),
|
||||
};
|
||||
if (Object.values(normalized).some((value) => value.length === 0)) {
|
||||
throw new Error("Checkout invoice correlation requires complete provider references");
|
||||
}
|
||||
|
||||
await runWithRlsContext({ tenantId: normalized.tenantId }, async () => {
|
||||
const updated = await client.checkoutSessionStatus.updateMany({
|
||||
where: {
|
||||
id: normalized.id,
|
||||
tenantId: normalized.tenantId,
|
||||
providerPaymentId: normalized.providerPaymentId,
|
||||
lagoSubscriptionId: normalized.lagoSubscriptionId,
|
||||
invoiceExternalId: normalized.sourceInvoiceExternalId,
|
||||
},
|
||||
data: { invoiceExternalId: normalized.providerInvoiceExternalId },
|
||||
});
|
||||
if (updated.count === 1) return;
|
||||
|
||||
const replay = await client.checkoutSessionStatus.findFirst({
|
||||
where: { id: normalized.id, tenantId: normalized.tenantId },
|
||||
});
|
||||
if (
|
||||
replay?.providerPaymentId === normalized.providerPaymentId &&
|
||||
replay.lagoSubscriptionId === normalized.lagoSubscriptionId &&
|
||||
replay.invoiceExternalId === normalized.providerInvoiceExternalId
|
||||
) {
|
||||
return;
|
||||
}
|
||||
throw new Error("Checkout invoice correlation did not match the exact payment ledger row");
|
||||
});
|
||||
}
|
||||
|
||||
export async function createTenantSubscriptionUsageRecord(
|
||||
data: TenantSubscriptionUsageRecordCreateInput,
|
||||
client: TenantBillingClient = getTenantBillingClient(),
|
||||
client: TenantBillingClient = getTenantBillingClient()
|
||||
): Promise<TenantSubscriptionUsageRecord> {
|
||||
return await client.tenantSubscriptionUsageRecord.create({ data });
|
||||
}
|
||||
|
||||
export async function findTenantSubscriptionProductByBillingProviderSubscriptionId(
|
||||
billingProviderSubscriptionId: string,
|
||||
client: BillingProviderTenantSubscriptionProductClient = getDbClient() as unknown as BillingProviderTenantSubscriptionProductClient,
|
||||
client: BillingProviderTenantSubscriptionProductClient = getDbClient() as unknown as BillingProviderTenantSubscriptionProductClient
|
||||
): Promise<BillingProviderTenantSubscriptionProduct | null> {
|
||||
return client.tenantSubscriptionProduct.findFirst({
|
||||
where: {
|
||||
@@ -1099,7 +1322,7 @@ export async function findTenantSubscriptionProductByBillingProviderSubscription
|
||||
export async function updateTenantSubscriptionProductFromBillingProvider(
|
||||
id: string,
|
||||
data: BillingProviderTenantSubscriptionProductUpdate,
|
||||
client: BillingProviderTenantSubscriptionProductClient = getDbClient() as unknown as BillingProviderTenantSubscriptionProductClient,
|
||||
client: BillingProviderTenantSubscriptionProductClient = getDbClient() as unknown as BillingProviderTenantSubscriptionProductClient
|
||||
): Promise<{ tenantId: string }> {
|
||||
const item = await client.tenantSubscriptionProduct.update({
|
||||
where: { id },
|
||||
|
||||
Reference in new issue
Block a user