1120 lines
36 KiB
TypeScript
1120 lines
36 KiB
TypeScript
import { getDbClient, type DbClient } from "./client.server.js";
|
|
import type {
|
|
Prisma,
|
|
SubscriptionFeature,
|
|
SubscriptionPrice,
|
|
SubscriptionProduct,
|
|
SubscriptionUsageBasedPrice,
|
|
SubscriptionUsageBasedTier,
|
|
TenantSubscription,
|
|
TenantSubscriptionProduct,
|
|
TenantSubscriptionProductPrice,
|
|
TenantSubscriptionUsageRecord,
|
|
TenantType,
|
|
CheckoutSessionStatus,
|
|
} from "./prisma-browser.js";
|
|
|
|
export type SubscriptionPriceWithProduct = SubscriptionPrice & {
|
|
subscriptionProduct: SubscriptionProduct;
|
|
};
|
|
|
|
export type SubscriptionUsageBasedPriceWithProduct = SubscriptionUsageBasedPrice & {
|
|
subscriptionProduct: SubscriptionProduct;
|
|
};
|
|
|
|
export type SubscriptionProductCreateInput = Pick<
|
|
SubscriptionProduct,
|
|
| "billingProviderId"
|
|
| "order"
|
|
| "title"
|
|
| "model"
|
|
| "description"
|
|
| "badge"
|
|
| "groupTitle"
|
|
| "groupDescription"
|
|
| "active"
|
|
| "public"
|
|
| "billingAddressCollection"
|
|
| "hasQuantity"
|
|
| "canBuyAgain"
|
|
> &
|
|
Partial<Pick<SubscriptionProduct, "code" | "lagoPlanCode">>;
|
|
|
|
export type SubscriptionProductUpdateInput = Partial<
|
|
Pick<
|
|
SubscriptionProduct,
|
|
| "billingProviderId"
|
|
| "code"
|
|
| "lagoPlanCode"
|
|
| "order"
|
|
| "title"
|
|
| "model"
|
|
| "description"
|
|
| "badge"
|
|
| "groupTitle"
|
|
| "groupDescription"
|
|
| "public"
|
|
| "billingAddressCollection"
|
|
| "hasQuantity"
|
|
| "canBuyAgain"
|
|
>
|
|
>;
|
|
|
|
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 };
|
|
|
|
export type SubscriptionPriceUpdateInput = Partial<
|
|
Omit<Pick<SubscriptionPrice, "billingProviderId" | "lagoFeeCode" | "molliePriceReference" | "type" | "billingPeriod" | "price" | "currency" | "trialDays" | "active">, "price"> & {
|
|
price: SubscriptionPrice["price"] | number | string;
|
|
}
|
|
>;
|
|
|
|
export type SubscriptionUsageBasedPriceCreateInput = Pick<
|
|
SubscriptionUsageBasedPrice,
|
|
| "subscriptionProductId"
|
|
| "billingProviderId"
|
|
| "currency"
|
|
| "billingPeriod"
|
|
| "unit"
|
|
| "unitTitle"
|
|
| "unitTitlePlural"
|
|
| "usageType"
|
|
| "aggregateUsage"
|
|
| "tiersMode"
|
|
| "billingScheme"
|
|
>;
|
|
|
|
export type SubscriptionUsageBasedTierCreateInput = Omit<
|
|
Pick<SubscriptionUsageBasedTier, "subscriptionUsageBasedPriceId" | "from" | "to" | "perUnitPrice" | "flatFeePrice">,
|
|
"perUnitPrice" | "flatFeePrice"
|
|
> & {
|
|
perUnitPrice?: SubscriptionUsageBasedTier["perUnitPrice"] | number | string | null;
|
|
flatFeePrice?: SubscriptionUsageBasedTier["flatFeePrice"] | number | string | null;
|
|
};
|
|
|
|
export type SubscriptionFeatureCreateInput = Omit<SubscriptionFeature, "id" | "subscriptionProductId" | "href" | "badge" | "accumulate" | "code" | "quotaValue"> & {
|
|
href?: string | null;
|
|
badge?: string | null;
|
|
accumulate?: boolean;
|
|
code?: string | null;
|
|
quotaValue?: Prisma.NullableJsonNullValueInput | Prisma.InputJsonValue;
|
|
};
|
|
|
|
export type SubscriptionProductPriceWithDetails = Omit<SubscriptionPrice, "id"> & {
|
|
id?: string;
|
|
};
|
|
|
|
export type SubscriptionProductWithDetails = Omit<SubscriptionProduct, "id"> & {
|
|
id?: string;
|
|
translatedTitle?: string;
|
|
tenantProducts: TenantSubscriptionProduct[];
|
|
usageBasedPrices: (SubscriptionUsageBasedPrice & { tiers: SubscriptionUsageBasedTier[] })[];
|
|
prices: SubscriptionProductPriceWithDetails[];
|
|
assignsTenantTypes: TenantType[];
|
|
features: SubscriptionFeature[];
|
|
};
|
|
|
|
export type SubscriptionFeatureSummary = {
|
|
name: string;
|
|
code?: string | null;
|
|
order: number;
|
|
title: string;
|
|
type: number;
|
|
value: number;
|
|
accumulate: boolean;
|
|
};
|
|
|
|
export type TenantSubscriptionProductWithDetails = TenantSubscriptionProduct & {
|
|
subscriptionProduct: SubscriptionProduct & { features: SubscriptionFeature[] };
|
|
prices: (TenantSubscriptionProductPrice & {
|
|
subscriptionPrice: SubscriptionPrice | null;
|
|
subscriptionUsageBasedPrice: (SubscriptionUsageBasedPrice & { tiers: SubscriptionUsageBasedTier[] }) | null;
|
|
})[];
|
|
};
|
|
|
|
export type TenantSimple = {
|
|
id: string;
|
|
name: string;
|
|
slug: string;
|
|
icon: string | null;
|
|
logo: string | null;
|
|
deactivatedReason: string | null;
|
|
active: boolean;
|
|
theme: string | null;
|
|
themeConfig: string | null;
|
|
};
|
|
|
|
export type TenantSubscriptionProductWithTenant = TenantSubscriptionProductWithDetails & {
|
|
tenantSubscription: TenantSubscription & { tenant: TenantSimple };
|
|
};
|
|
|
|
export type TenantSubscriptionWithDetails = TenantSubscription & {
|
|
products: TenantSubscriptionProductWithDetails[];
|
|
};
|
|
|
|
export type TenantSubscriptionProductStatusFilter = "active" | "ended" | "active-cancelled" | "active-not-cancelled";
|
|
|
|
export type TenantSubscriptionProductFilters = {
|
|
tenantId?: string;
|
|
subscriptionProductId?: string;
|
|
status?: TenantSubscriptionProductStatusFilter | string;
|
|
};
|
|
|
|
export type TenantSubscriptionProductPagination = {
|
|
page: number;
|
|
pageSize: number;
|
|
};
|
|
|
|
export type TenantSubscriptionProductPage = {
|
|
items: TenantSubscriptionProductWithTenant[];
|
|
pagination: {
|
|
page: number;
|
|
pageSize: number;
|
|
totalItems: number;
|
|
totalPages: number;
|
|
};
|
|
};
|
|
|
|
export type TenantSubscriptionCreateInput = Pick<TenantSubscription, "tenantId"> &
|
|
Partial<Pick<TenantSubscription, "billingProviderCustomerId" | "lagoCustomerId" | "mollieCustomerId" | "planCode" | "seatQuantity">>;
|
|
|
|
export type TenantSubscriptionCustomerIdUpdateInput = Partial<
|
|
Pick<TenantSubscription, "billingProviderCustomerId" | "lagoCustomerId" | "mollieCustomerId" | "planCode" | "seatQuantity" | "lastInvoiceExternalId" | "lastPaymentExternalId">
|
|
>;
|
|
|
|
export type TenantSubscriptionProductCreateInput = Pick<
|
|
TenantSubscriptionProduct,
|
|
| "tenantSubscriptionId"
|
|
| "subscriptionProductId"
|
|
| "billingProviderSubscriptionId"
|
|
| "quantity"
|
|
| "fromCheckoutSessionId"
|
|
> &
|
|
Partial<Pick<TenantSubscriptionProduct, "customEntitlementPlanId" | "lagoSubscriptionId" | "planCode" | "seatQuantity">> & {
|
|
prices: {
|
|
subscriptionPriceId?: string;
|
|
subscriptionUsageBasedPriceId?: string;
|
|
}[];
|
|
};
|
|
|
|
export type TenantSubscriptionProductUpdateInput = Partial<
|
|
Pick<
|
|
TenantSubscriptionProduct,
|
|
"quantity" | "seatQuantity" | "planCode" | "lagoSubscriptionId" | "cancelledAt" | "endsAt" | "currentPeriodStart" | "currentPeriodEnd" | "customEntitlementPlanId"
|
|
>
|
|
>;
|
|
|
|
export type TenantSubscriptionProductCustomEntitlementPlanUpdateInput = Pick<TenantSubscriptionProduct, "customEntitlementPlanId">;
|
|
|
|
export type TenantSubscriptionProductCancelInput = Pick<TenantSubscriptionProduct, "cancelledAt" | "endsAt">;
|
|
|
|
export type TenantSubscriptionUsageRecordCreateInput = Pick<
|
|
TenantSubscriptionUsageRecord,
|
|
"tenantId" | "tenantSubscriptionId" | "tenantSubscriptionProductPriceId" | "metricCode" | "timestamp" | "quantity" | "billingProviderSubscriptionItemId"
|
|
> &
|
|
Partial<Pick<TenantSubscriptionUsageRecord, "idempotencyKey">>;
|
|
|
|
export type CheckoutSessionStatusCreateInput = Pick<CheckoutSessionStatus, "id" | "email" | "fromUrl"> &
|
|
Partial<
|
|
Pick<
|
|
CheckoutSessionStatus,
|
|
| "fromUserId"
|
|
| "fromTenantId"
|
|
| "provider"
|
|
| "providerPaymentId"
|
|
| "invoiceExternalId"
|
|
| "subscriptionProductId"
|
|
| "subscriptionPriceId"
|
|
| "lagoSubscriptionId"
|
|
| "planCode"
|
|
| "quantity"
|
|
| "amountCents"
|
|
| "currency"
|
|
| "checkoutUrl"
|
|
| "status"
|
|
>
|
|
>;
|
|
|
|
export type CheckoutSessionStatusUpdateInput = Pick<CheckoutSessionStatus, "pending"> &
|
|
Partial<
|
|
Pick<
|
|
CheckoutSessionStatus,
|
|
| "createdUserId"
|
|
| "createdTenantId"
|
|
| "providerPaymentId"
|
|
| "invoiceExternalId"
|
|
| "lagoSubscriptionId"
|
|
| "checkoutUrl"
|
|
| "status"
|
|
>
|
|
>;
|
|
|
|
export type TenantIdResult = { tenantId: string };
|
|
|
|
export type BillingProviderTenantSubscriptionProduct = Pick<TenantSubscriptionProduct, "id" | "endsAt" | "cancelledAt" | "customEntitlementPlanId"> & {
|
|
tenantSubscription: Pick<TenantSubscription, "tenantId"> & {
|
|
tenant: {
|
|
id: string;
|
|
name: string;
|
|
};
|
|
};
|
|
subscriptionProduct: Pick<SubscriptionProduct, "id" | "title">;
|
|
};
|
|
|
|
export type BillingProviderTenantSubscriptionProductUpdate = {
|
|
cancelledAt?: Date | null;
|
|
currentPeriodStart?: Date;
|
|
currentPeriodEnd?: Date;
|
|
endsAt?: Date | null;
|
|
};
|
|
|
|
const subscriptionProductDetailsInclude = {
|
|
tenantProducts: true,
|
|
usageBasedPrices: { include: { tiers: { orderBy: { from: "asc" } } } },
|
|
prices: true,
|
|
assignsTenantTypes: true,
|
|
features: {
|
|
orderBy: {
|
|
order: "asc",
|
|
},
|
|
},
|
|
} as const;
|
|
|
|
const tenantSimpleSelect = {
|
|
id: true,
|
|
name: true,
|
|
slug: true,
|
|
icon: true,
|
|
logo: true,
|
|
deactivatedReason: true,
|
|
active: true,
|
|
theme: true,
|
|
themeConfig: true,
|
|
} as const;
|
|
|
|
const tenantSubscriptionProductDetailsInclude = {
|
|
subscriptionProduct: { include: { features: true } },
|
|
prices: { include: { subscriptionPrice: true, subscriptionUsageBasedPrice: { include: { tiers: true } } } },
|
|
} as const;
|
|
|
|
const tenantSubscriptionProductWithTenantInclude = {
|
|
...tenantSubscriptionProductDetailsInclude,
|
|
tenantSubscription: {
|
|
include: { tenant: { select: tenantSimpleSelect } },
|
|
},
|
|
} as const;
|
|
|
|
const tenantSubscriptionDetailsInclude = {
|
|
products: {
|
|
include: tenantSubscriptionProductDetailsInclude,
|
|
orderBy: {
|
|
subscriptionProduct: {
|
|
order: "desc",
|
|
},
|
|
},
|
|
},
|
|
} as const;
|
|
|
|
const billingProviderTenantSubscriptionProductSelect = {
|
|
id: true,
|
|
endsAt: true,
|
|
cancelledAt: true,
|
|
customEntitlementPlanId: true,
|
|
tenantSubscription: {
|
|
select: {
|
|
tenantId: true,
|
|
tenant: {
|
|
select: {
|
|
id: true,
|
|
name: true,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
subscriptionProduct: {
|
|
select: {
|
|
id: true,
|
|
title: true,
|
|
},
|
|
},
|
|
} as const;
|
|
|
|
export type SubscriptionCatalogClient = {
|
|
subscriptionProduct: {
|
|
findMany(args: {
|
|
where?: {
|
|
active?: boolean;
|
|
public?: boolean;
|
|
id?: { in: string[] };
|
|
};
|
|
include: typeof subscriptionProductDetailsInclude;
|
|
orderBy: { order: "asc" } | [{ public: "desc" }, { order: "asc" }];
|
|
}): Promise<SubscriptionProductWithDetails[]>;
|
|
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>;
|
|
delete(args: { where: { id: string } }): Promise<SubscriptionProduct>;
|
|
};
|
|
subscriptionPrice: {
|
|
findMany(args: {
|
|
include: { subscriptionProduct: true };
|
|
orderBy: [{ subscriptionProduct: { order: "asc" } }, { price: "asc" }];
|
|
}): Promise<SubscriptionPriceWithProduct[]>;
|
|
findUnique(args: { where: { id: string }; include: { subscriptionProduct: true } }): Promise<SubscriptionPriceWithProduct | null>;
|
|
findFirst(args: { where: { billingProviderId: string }; include: { subscriptionProduct: true } }): Promise<SubscriptionPriceWithProduct | null>;
|
|
create(args: { data: SubscriptionPriceCreateInput }): Promise<SubscriptionPrice>;
|
|
update(args: { where: { id: string }; data: SubscriptionPriceUpdateInput | { billingProviderId: string } }): Promise<SubscriptionPrice>;
|
|
delete(args: { where: { id: string } }): Promise<SubscriptionPrice>;
|
|
};
|
|
subscriptionUsageBasedPrice: {
|
|
findFirst(args: { where: { billingProviderId: string }; include: { subscriptionProduct: true } }): Promise<SubscriptionUsageBasedPriceWithProduct | null>;
|
|
create(args: { data: SubscriptionUsageBasedPriceCreateInput }): Promise<SubscriptionUsageBasedPrice>;
|
|
delete(args: { where: { id: string } }): Promise<SubscriptionUsageBasedPrice>;
|
|
};
|
|
subscriptionUsageBasedTier: {
|
|
create(args: { data: SubscriptionUsageBasedTierCreateInput }): Promise<SubscriptionUsageBasedTier>;
|
|
delete(args: { where: { id: string } }): Promise<SubscriptionUsageBasedTier>;
|
|
};
|
|
subscriptionFeature: {
|
|
findMany(args: {
|
|
select: { name: true; code: true; order: true; title: true; type: true; value: true; accumulate: true };
|
|
}): Promise<SubscriptionFeatureSummary[]>;
|
|
create(args: { data: SubscriptionFeatureCreateInput & { subscriptionProductId: string } }): Promise<SubscriptionFeature>;
|
|
deleteMany(args: { where: { subscriptionProductId: string } }): Promise<{ count: number }>;
|
|
};
|
|
};
|
|
|
|
export type SubscriptionProductReaderClient = SubscriptionCatalogClient;
|
|
|
|
type TenantSubscriptionProductWhereInput = {
|
|
subscriptionProductId?: { equals: string };
|
|
tenantSubscription?: { tenantId?: { equals: string } };
|
|
OR?: [{ endsAt: { gte: Date } }, { endsAt: null }];
|
|
endsAt?: { lt: Date; not: null };
|
|
cancelledAt?: { not: null } | null;
|
|
};
|
|
|
|
export type TenantSubscriptionReaderClient = {
|
|
tenantSubscription: {
|
|
findUnique(args: {
|
|
where: { tenantId: string };
|
|
include: typeof tenantSubscriptionDetailsInclude;
|
|
}): Promise<TenantSubscriptionWithDetails | null>;
|
|
findMany(args: { include: typeof tenantSubscriptionDetailsInclude }): Promise<TenantSubscriptionWithDetails[]>;
|
|
};
|
|
};
|
|
|
|
type DbCompatibleDelegate<TDelegateName extends keyof DbClient> = Pick<DbClient, TDelegateName>[TDelegateName];
|
|
|
|
const getTenantBillingClient = (): TenantBillingClient => getDbClient() as unknown as TenantBillingClient;
|
|
|
|
export type TenantBillingClient = TenantSubscriptionReaderClient & {
|
|
tenantSubscription: TenantSubscriptionReaderClient["tenantSubscription"] & {
|
|
create(args: { data: TenantSubscriptionCreateInput; include: typeof tenantSubscriptionDetailsInclude }): Promise<TenantSubscriptionWithDetails>;
|
|
update(args: { where: { tenantId: string }; data: TenantSubscriptionCustomerIdUpdateInput }): Promise<TenantSubscription>;
|
|
findMany(args: { select: { tenantId: true } }): Promise<TenantIdResult[]>;
|
|
};
|
|
tenantSubscriptionProduct: DbCompatibleDelegate<"tenantSubscriptionProduct"> & {
|
|
findMany(args: {
|
|
skip?: number;
|
|
take?: number;
|
|
where: TenantSubscriptionProductWhereInput;
|
|
include: typeof tenantSubscriptionProductWithTenantInclude;
|
|
orderBy: { createdAt: "desc" };
|
|
}): Promise<TenantSubscriptionProductWithTenant[]>;
|
|
count(args: { where: TenantSubscriptionProductWhereInput }): Promise<number>;
|
|
findFirst(args: {
|
|
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>;
|
|
create(args: {
|
|
data: Omit<TenantSubscriptionProductCreateInput, "prices"> & {
|
|
endsAt: null;
|
|
cancelledAt: null;
|
|
prices: { create: TenantSubscriptionProductCreateInput["prices"] };
|
|
};
|
|
include: { tenantSubscription: true };
|
|
}): Promise<TenantSubscriptionProduct & { tenantSubscription: TenantSubscription }>;
|
|
update(args: {
|
|
where: { id: string };
|
|
data: TenantSubscriptionProductUpdateInput | TenantSubscriptionProductCancelInput;
|
|
include: { tenantSubscription: true };
|
|
}): Promise<TenantSubscriptionProduct & { tenantSubscription: TenantSubscription }>;
|
|
update(args: {
|
|
where: { id: string };
|
|
data: TenantSubscriptionProductCustomEntitlementPlanUpdateInput;
|
|
select: { tenantSubscription: { select: { tenantId: true } } };
|
|
}): 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>;
|
|
update(args: {
|
|
where: { id: string };
|
|
data: CheckoutSessionStatusUpdateInput & { createdUserId: string | null; createdTenantId: string | null };
|
|
}): Promise<CheckoutSessionStatus>;
|
|
};
|
|
tenantSubscriptionUsageRecord: {
|
|
create(args: { data: TenantSubscriptionUsageRecordCreateInput }): Promise<TenantSubscriptionUsageRecord>;
|
|
};
|
|
};
|
|
|
|
export type BillingProviderTenantSubscriptionProductClient = {
|
|
tenantSubscriptionProduct: {
|
|
findFirst(args: {
|
|
where: { billingProviderSubscriptionId: string };
|
|
select: typeof billingProviderTenantSubscriptionProductSelect;
|
|
}): Promise<BillingProviderTenantSubscriptionProduct | null>;
|
|
update(args: {
|
|
where: { id: string };
|
|
data: BillingProviderTenantSubscriptionProductUpdate;
|
|
select: { tenantSubscription: { select: { tenantId: true } } };
|
|
}): Promise<{ tenantSubscription: { tenantId: string } }>;
|
|
};
|
|
};
|
|
|
|
export async function getAllSubscriptionProductsWithTenants(
|
|
client: SubscriptionProductReaderClient = getDbClient(),
|
|
): Promise<SubscriptionProductWithDetails[]> {
|
|
return await client.subscriptionProduct
|
|
.findMany({
|
|
include: subscriptionProductDetailsInclude,
|
|
orderBy: [{ public: "desc" }, { order: "asc" }],
|
|
})
|
|
.catch(() => {
|
|
return [];
|
|
});
|
|
}
|
|
|
|
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 [];
|
|
});
|
|
}
|
|
|
|
return await client.subscriptionProduct
|
|
.findMany({
|
|
where: {
|
|
active: true,
|
|
},
|
|
include: subscriptionProductDetailsInclude,
|
|
orderBy: {
|
|
order: "asc",
|
|
},
|
|
})
|
|
.catch(() => {
|
|
return [];
|
|
});
|
|
}
|
|
|
|
export async function getSubscriptionProductsInIds(
|
|
ids: string[],
|
|
client: SubscriptionProductReaderClient = getDbClient(),
|
|
): Promise<SubscriptionProductWithDetails[]> {
|
|
return await client.subscriptionProduct.findMany({
|
|
where: {
|
|
id: { in: ids },
|
|
},
|
|
include: subscriptionProductDetailsInclude,
|
|
orderBy: { order: "asc" },
|
|
});
|
|
}
|
|
|
|
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 },
|
|
});
|
|
}
|
|
|
|
export async function getSubscriptionProduct(
|
|
id: string,
|
|
client: SubscriptionProductReaderClient = getDbClient(),
|
|
): Promise<SubscriptionProductWithDetails | null> {
|
|
return await client.subscriptionProduct.findUnique({
|
|
where: {
|
|
id,
|
|
},
|
|
include: subscriptionProductDetailsInclude,
|
|
});
|
|
}
|
|
|
|
export async function getSubscriptionProductByBillingProviderId(
|
|
billingProviderId: string,
|
|
client: SubscriptionCatalogClient = getDbClient(),
|
|
): Promise<SubscriptionProduct | null> {
|
|
return await client.subscriptionProduct.findFirst({
|
|
where: {
|
|
billingProviderId,
|
|
},
|
|
});
|
|
}
|
|
|
|
export async function getSubscriptionPrices(
|
|
client: SubscriptionCatalogClient = getDbClient(),
|
|
): Promise<SubscriptionPriceWithProduct[]> {
|
|
return await client.subscriptionPrice
|
|
.findMany({
|
|
include: {
|
|
subscriptionProduct: true,
|
|
},
|
|
orderBy: [
|
|
{
|
|
subscriptionProduct: {
|
|
order: "asc",
|
|
},
|
|
},
|
|
{
|
|
price: "asc",
|
|
},
|
|
],
|
|
})
|
|
.catch(() => {
|
|
return [];
|
|
});
|
|
}
|
|
|
|
export async function getSubscriptionPrice(
|
|
id: string,
|
|
client: SubscriptionCatalogClient = getDbClient(),
|
|
): Promise<SubscriptionPriceWithProduct | null> {
|
|
return await client.subscriptionPrice
|
|
.findUnique({
|
|
where: { id },
|
|
include: {
|
|
subscriptionProduct: true,
|
|
},
|
|
})
|
|
.catch(() => {
|
|
return null;
|
|
});
|
|
}
|
|
|
|
export async function getSubscriptionPriceByBillingProviderId(
|
|
billingProviderId: string,
|
|
client: SubscriptionCatalogClient = getDbClient(),
|
|
): Promise<SubscriptionPriceWithProduct | null> {
|
|
return await client.subscriptionPrice
|
|
.findFirst({
|
|
where: { billingProviderId },
|
|
include: {
|
|
subscriptionProduct: true,
|
|
},
|
|
})
|
|
.catch(() => {
|
|
return null;
|
|
});
|
|
}
|
|
|
|
export async function getSubscriptionUsageBasedPriceByBillingProviderId(
|
|
billingProviderId: string,
|
|
client: SubscriptionCatalogClient = getDbClient(),
|
|
): Promise<SubscriptionUsageBasedPriceWithProduct | null> {
|
|
return await client.subscriptionUsageBasedPrice
|
|
.findFirst({
|
|
where: { billingProviderId },
|
|
include: {
|
|
subscriptionProduct: true,
|
|
},
|
|
})
|
|
.catch(() => {
|
|
return null;
|
|
});
|
|
}
|
|
|
|
export async function createSubscriptionProduct(
|
|
data: SubscriptionProductCreateInput,
|
|
client: SubscriptionCatalogClient = getDbClient(),
|
|
): Promise<SubscriptionProduct> {
|
|
return await client.subscriptionProduct.create({ data });
|
|
}
|
|
|
|
export async function updateSubscriptionProduct(
|
|
id: string,
|
|
data: SubscriptionProductUpdateInput,
|
|
client: SubscriptionCatalogClient = getDbClient(),
|
|
): Promise<SubscriptionProduct> {
|
|
return await client.subscriptionProduct.update({
|
|
where: { id },
|
|
data,
|
|
});
|
|
}
|
|
|
|
export async function updateSubscriptionProductBillingProviderId(
|
|
id: string,
|
|
data: { billingProviderId: string },
|
|
client: SubscriptionCatalogClient = getDbClient(),
|
|
): Promise<SubscriptionProduct> {
|
|
return await client.subscriptionProduct.update({
|
|
where: { id },
|
|
data,
|
|
});
|
|
}
|
|
|
|
export async function updateSubscriptionPriceBillingProviderId(
|
|
id: string,
|
|
data: { billingProviderId: string },
|
|
client: SubscriptionCatalogClient = getDbClient(),
|
|
): Promise<SubscriptionPrice> {
|
|
return await client.subscriptionPrice.update({
|
|
where: { id },
|
|
data,
|
|
});
|
|
}
|
|
|
|
export async function createSubscriptionPrice(
|
|
data: SubscriptionPriceCreateInput,
|
|
client: SubscriptionCatalogClient = getDbClient(),
|
|
): Promise<SubscriptionPrice> {
|
|
return await client.subscriptionPrice.create({ data });
|
|
}
|
|
|
|
export async function updateSubscriptionPrice(
|
|
id: string,
|
|
data: SubscriptionPriceUpdateInput,
|
|
client: SubscriptionCatalogClient = getDbClient(),
|
|
): Promise<SubscriptionPrice> {
|
|
return await client.subscriptionPrice.update({
|
|
where: { id },
|
|
data,
|
|
});
|
|
}
|
|
|
|
export async function createSubscriptionUsageBasedPrice(
|
|
data: SubscriptionUsageBasedPriceCreateInput,
|
|
client: SubscriptionCatalogClient = getDbClient(),
|
|
): Promise<SubscriptionUsageBasedPrice> {
|
|
return await client.subscriptionUsageBasedPrice.create({ data });
|
|
}
|
|
|
|
export async function createSubscriptionUsageBasedTier(
|
|
data: SubscriptionUsageBasedTierCreateInput,
|
|
client: SubscriptionCatalogClient = getDbClient(),
|
|
): Promise<SubscriptionUsageBasedTier> {
|
|
return await client.subscriptionUsageBasedTier.create({ data });
|
|
}
|
|
|
|
export async function createSubscriptionFeature(
|
|
subscriptionProductId: string,
|
|
data: SubscriptionFeatureCreateInput,
|
|
client: SubscriptionCatalogClient = getDbClient(),
|
|
): Promise<SubscriptionFeature> {
|
|
return await client.subscriptionFeature.create({
|
|
data: {
|
|
subscriptionProductId,
|
|
...data,
|
|
},
|
|
});
|
|
}
|
|
|
|
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> {
|
|
return await client.subscriptionPrice.delete({
|
|
where: { id },
|
|
});
|
|
}
|
|
|
|
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> {
|
|
return await client.subscriptionUsageBasedPrice.delete({
|
|
where: { id },
|
|
});
|
|
}
|
|
|
|
export async function deleteSubscriptionFeatures(
|
|
subscriptionProductId: string,
|
|
client: SubscriptionCatalogClient = getDbClient(),
|
|
): Promise<{ count: number }> {
|
|
return await client.subscriptionFeature.deleteMany({
|
|
where: { subscriptionProductId },
|
|
});
|
|
}
|
|
|
|
export async function getTenantSubscription(
|
|
tenantId: string,
|
|
client: TenantSubscriptionReaderClient = getDbClient(),
|
|
): Promise<TenantSubscriptionWithDetails | null> {
|
|
return await client.tenantSubscription.findUnique({
|
|
where: {
|
|
tenantId,
|
|
},
|
|
include: tenantSubscriptionDetailsInclude,
|
|
});
|
|
}
|
|
|
|
export async function getTenantSubscriptions(
|
|
client: TenantSubscriptionReaderClient = getDbClient(),
|
|
): Promise<TenantSubscriptionWithDetails[]> {
|
|
return await client.tenantSubscription.findMany({
|
|
include: tenantSubscriptionDetailsInclude,
|
|
});
|
|
}
|
|
|
|
export async function createTenantSubscription(
|
|
data: TenantSubscriptionCreateInput,
|
|
client: TenantBillingClient = getTenantBillingClient(),
|
|
): Promise<TenantSubscriptionWithDetails> {
|
|
return await client.tenantSubscription.create({
|
|
data,
|
|
include: tenantSubscriptionDetailsInclude,
|
|
});
|
|
}
|
|
|
|
export async function updateTenantSubscriptionCustomerId(
|
|
tenantId: string,
|
|
data: TenantSubscriptionCustomerIdUpdateInput,
|
|
client: TenantBillingClient = getTenantBillingClient(),
|
|
): Promise<TenantSubscription> {
|
|
return await client.tenantSubscription.update({
|
|
where: { tenantId },
|
|
data,
|
|
});
|
|
}
|
|
|
|
export async function listTenantSubscriptionIds(client: TenantBillingClient = getTenantBillingClient()): Promise<TenantIdResult[]> {
|
|
return await client.tenantSubscription.findMany({
|
|
select: { tenantId: true },
|
|
});
|
|
}
|
|
|
|
function buildTenantSubscriptionProductWhere(filters?: TenantSubscriptionProductFilters, now: Date = new Date()): TenantSubscriptionProductWhereInput {
|
|
let where: TenantSubscriptionProductWhereInput = {
|
|
subscriptionProductId: filters?.subscriptionProductId ? { equals: filters.subscriptionProductId } : undefined,
|
|
tenantSubscription: {
|
|
tenantId: filters?.tenantId ? { equals: filters.tenantId } : undefined,
|
|
},
|
|
};
|
|
|
|
if (filters?.status === "active") {
|
|
where = {
|
|
...where,
|
|
OR: [{ endsAt: { gte: now } }, { endsAt: null }],
|
|
};
|
|
} else if (filters?.status === "ended") {
|
|
where = {
|
|
...where,
|
|
endsAt: { lt: now, not: null },
|
|
};
|
|
} else if (filters?.status === "active-cancelled") {
|
|
where = {
|
|
...where,
|
|
OR: [{ endsAt: { gte: now } }, { endsAt: null }],
|
|
cancelledAt: { not: null },
|
|
};
|
|
} else if (filters?.status === "active-not-cancelled") {
|
|
where = {
|
|
...where,
|
|
OR: [{ endsAt: { gte: now } }, { endsAt: null }],
|
|
cancelledAt: null,
|
|
};
|
|
}
|
|
|
|
return where;
|
|
}
|
|
|
|
export async function getAllTenantSubscriptionProducts(
|
|
filters?: TenantSubscriptionProductFilters,
|
|
pagination?: TenantSubscriptionProductPagination,
|
|
client: TenantBillingClient = getTenantBillingClient(),
|
|
now: Date = new Date(),
|
|
): Promise<TenantSubscriptionProductPage> {
|
|
const where = buildTenantSubscriptionProductWhere(filters, now);
|
|
const items = await client.tenantSubscriptionProduct.findMany({
|
|
skip: pagination ? pagination.pageSize * (pagination.page - 1) : undefined,
|
|
take: pagination ? pagination.pageSize : undefined,
|
|
where,
|
|
include: tenantSubscriptionProductWithTenantInclude,
|
|
orderBy: { createdAt: "desc" },
|
|
});
|
|
const totalItems = await client.tenantSubscriptionProduct.count({ where });
|
|
const page = pagination?.page ?? 1;
|
|
const pageSize = pagination?.pageSize ?? 10;
|
|
|
|
return {
|
|
items,
|
|
pagination: {
|
|
page,
|
|
pageSize,
|
|
totalItems,
|
|
totalPages: Math.ceil(totalItems / pageSize),
|
|
},
|
|
};
|
|
}
|
|
|
|
export async function getTenantSubscriptionProduct(
|
|
tenantId: string,
|
|
subscriptionProductId: string,
|
|
client: TenantBillingClient = getTenantBillingClient(),
|
|
): Promise<TenantSubscriptionProductWithTenant | null> {
|
|
return await client.tenantSubscriptionProduct.findFirst({
|
|
where: {
|
|
tenantSubscription: { tenantId },
|
|
subscriptionProductId,
|
|
},
|
|
include: tenantSubscriptionProductWithTenantInclude,
|
|
});
|
|
}
|
|
|
|
export async function adminGetTenantSubscriptionProductById(
|
|
id: string,
|
|
client: TenantBillingClient = getTenantBillingClient(),
|
|
): Promise<TenantSubscriptionProductWithTenant | null> {
|
|
return await client.tenantSubscriptionProduct.findUnique({
|
|
where: { id },
|
|
include: tenantSubscriptionProductWithTenantInclude,
|
|
});
|
|
}
|
|
|
|
export async function getTenantSubscriptionProductById(
|
|
{ id, tenantId }: { id: string; tenantId: string },
|
|
client: TenantBillingClient = getTenantBillingClient(),
|
|
): Promise<TenantSubscriptionProductWithTenant | null> {
|
|
return await client.tenantSubscriptionProduct.findFirst({
|
|
where: { id, tenantSubscription: { tenantId } },
|
|
include: tenantSubscriptionProductWithTenantInclude,
|
|
});
|
|
}
|
|
|
|
export async function getTenantSubscriptionProductByBillingProviderSubscriptionId(
|
|
billingProviderSubscriptionId: string,
|
|
client: TenantBillingClient = getTenantBillingClient(),
|
|
): Promise<TenantSubscriptionProductWithTenant | null> {
|
|
return await client.tenantSubscriptionProduct.findFirst({
|
|
where: { billingProviderSubscriptionId },
|
|
include: tenantSubscriptionProductWithTenantInclude,
|
|
});
|
|
}
|
|
|
|
export async function addTenantSubscriptionProduct(
|
|
data: TenantSubscriptionProductCreateInput,
|
|
client: TenantBillingClient = getTenantBillingClient(),
|
|
): Promise<TenantSubscriptionProduct & { tenantSubscription: TenantSubscription }> {
|
|
return await client.tenantSubscriptionProduct.create({
|
|
data: {
|
|
tenantSubscriptionId: data.tenantSubscriptionId,
|
|
subscriptionProductId: data.subscriptionProductId,
|
|
billingProviderSubscriptionId: data.billingProviderSubscriptionId,
|
|
lagoSubscriptionId: data.lagoSubscriptionId,
|
|
planCode: data.planCode,
|
|
seatQuantity: data.seatQuantity,
|
|
quantity: data.quantity,
|
|
fromCheckoutSessionId: data.fromCheckoutSessionId,
|
|
customEntitlementPlanId: data.customEntitlementPlanId,
|
|
endsAt: null,
|
|
cancelledAt: null,
|
|
prices: {
|
|
create: data.prices,
|
|
},
|
|
},
|
|
include: { tenantSubscription: true },
|
|
});
|
|
}
|
|
|
|
export async function updateTenantSubscriptionProduct(
|
|
{ id, tenantId }: { id: string; tenantId: string },
|
|
data: TenantSubscriptionProductUpdateInput,
|
|
client: TenantBillingClient = getTenantBillingClient(),
|
|
): Promise<TenantSubscriptionProduct & { tenantSubscription: TenantSubscription }> {
|
|
const existing = await getTenantSubscriptionProductById({ id, tenantId }, client);
|
|
if (!existing) {
|
|
throw new Error(`Tenant subscription product ${id} not found`);
|
|
}
|
|
return await client.tenantSubscriptionProduct.update({
|
|
where: { id },
|
|
data,
|
|
include: { tenantSubscription: true },
|
|
});
|
|
}
|
|
|
|
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(),
|
|
): Promise<TenantIdResult> {
|
|
const existing = await getTenantSubscriptionProductById({ id, tenantId }, client);
|
|
if (!existing) {
|
|
throw new Error(`Tenant subscription product ${id} not found`);
|
|
}
|
|
const item = await client.tenantSubscriptionProduct.update({
|
|
where: { id },
|
|
data,
|
|
select: {
|
|
tenantSubscription: {
|
|
select: {
|
|
tenantId: true,
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
return { tenantId: item.tenantSubscription.tenantId };
|
|
}
|
|
|
|
export async function cancelTenantSubscriptionProduct(
|
|
{ id, tenantId }: { id: string; tenantId: string },
|
|
data: TenantSubscriptionProductCancelInput,
|
|
client: TenantBillingClient = getTenantBillingClient(),
|
|
): Promise<TenantIdResult> {
|
|
const existing = await getTenantSubscriptionProductById({ id, tenantId }, client);
|
|
if (!existing) {
|
|
throw new Error(`Tenant subscription product ${id} not found`);
|
|
}
|
|
const item = await client.tenantSubscriptionProduct.update({
|
|
where: { id },
|
|
data: {
|
|
cancelledAt: data.cancelledAt,
|
|
endsAt: data.endsAt,
|
|
},
|
|
include: { tenantSubscription: true },
|
|
});
|
|
|
|
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 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",
|
|
},
|
|
});
|
|
}
|
|
|
|
export async function updateCheckoutSessionStatus(
|
|
id: 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,
|
|
},
|
|
});
|
|
}
|
|
|
|
export async function createTenantSubscriptionUsageRecord(
|
|
data: TenantSubscriptionUsageRecordCreateInput,
|
|
client: TenantBillingClient = getTenantBillingClient(),
|
|
): Promise<TenantSubscriptionUsageRecord> {
|
|
return await client.tenantSubscriptionUsageRecord.create({ data });
|
|
}
|
|
|
|
export async function findTenantSubscriptionProductByBillingProviderSubscriptionId(
|
|
billingProviderSubscriptionId: string,
|
|
client: BillingProviderTenantSubscriptionProductClient = getDbClient() as unknown as BillingProviderTenantSubscriptionProductClient,
|
|
): Promise<BillingProviderTenantSubscriptionProduct | null> {
|
|
return client.tenantSubscriptionProduct.findFirst({
|
|
where: {
|
|
billingProviderSubscriptionId,
|
|
},
|
|
select: billingProviderTenantSubscriptionProductSelect,
|
|
});
|
|
}
|
|
|
|
export async function updateTenantSubscriptionProductFromBillingProvider(
|
|
id: string,
|
|
data: BillingProviderTenantSubscriptionProductUpdate,
|
|
client: BillingProviderTenantSubscriptionProductClient = getDbClient() as unknown as BillingProviderTenantSubscriptionProductClient,
|
|
): Promise<{ tenantId: string }> {
|
|
const item = await client.tenantSubscriptionProduct.update({
|
|
where: { id },
|
|
data,
|
|
select: {
|
|
tenantSubscription: {
|
|
select: {
|
|
tenantId: true,
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
return {
|
|
tenantId: item.tenantSubscription.tenantId,
|
|
};
|
|
}
|