Update hivegrid-cloud reference to latest main
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e98d38e716
commit
7b21524c86
3781 files changed
+373484
-225607
No files matched your search
+112
-10
@@ -5,14 +5,29 @@ import {
|
||||
type EntitlementLimitType,
|
||||
type EntitlementPlanStub,
|
||||
type FeatureKey,
|
||||
} from "@toolctrl/entitlements";
|
||||
} from "@hivegrid/entitlements";
|
||||
|
||||
import { getDbClient } from "./client.server.js";
|
||||
import type { SubscriptionFeature, SubscriptionProduct } from "./prisma-browser.js";
|
||||
import type { SubscriptionFeatureCreateInput, SubscriptionProductCreateInput, SubscriptionProductUpdateInput } from "./subscriptions.server.js";
|
||||
import type { SubscriptionFeature, SubscriptionPrice, SubscriptionProduct } from "./prisma-browser.js";
|
||||
import type {
|
||||
SubscriptionFeatureCreateInput,
|
||||
SubscriptionPriceCreateInput,
|
||||
SubscriptionPriceUpdateInput,
|
||||
SubscriptionProductCreateInput,
|
||||
SubscriptionProductUpdateInput,
|
||||
} from "./subscriptions.server.js";
|
||||
|
||||
const SEEDED_STRIPE_ID_PREFIX = "toolctrl_entitlement_plan";
|
||||
const SEEDED_BILLING_PROVIDER_ID_PREFIX = "hivegrid_entitlement_plan";
|
||||
const SEEDED_PRICE_PROVIDER_ID_PREFIX = "hivegrid_entitlement_price";
|
||||
const REDACTED = "[redacted]";
|
||||
const PRICE_TYPE_RECURRING = 1;
|
||||
const BILLING_PERIOD_MONTHLY = 3;
|
||||
const BILLING_PERIOD_YEARLY = 4;
|
||||
const STANDARD_PLAN_PRICES_EUR: Record<string, { monthly: string; yearly: string }> = {
|
||||
starter: { monthly: "199", yearly: "179" },
|
||||
pro: { monthly: "499", yearly: "449" },
|
||||
business: { monthly: "999", yearly: "899" },
|
||||
};
|
||||
|
||||
export type EntitlementsSeedPhase = "start" | "plan-seeded" | "success" | "failure";
|
||||
|
||||
@@ -22,6 +37,8 @@ export type EntitlementsSeedDiagnosticEvent = {
|
||||
featureCount: number;
|
||||
createdProducts: number;
|
||||
updatedProducts: number;
|
||||
createdPrices: number;
|
||||
updatedPrices: number;
|
||||
deletedFeatures: number;
|
||||
createdFeatures: number;
|
||||
planId?: string;
|
||||
@@ -38,12 +55,14 @@ export type EntitlementsSeedResult = {
|
||||
featureCount: number;
|
||||
createdProducts: number;
|
||||
updatedProducts: number;
|
||||
createdPrices: number;
|
||||
updatedPrices: number;
|
||||
deletedFeatures: number;
|
||||
createdFeatures: number;
|
||||
};
|
||||
|
||||
export type EntitlementsSeedSubscriptionProductClient = {
|
||||
findFirst(args: { where: { OR: [{ stripeId: string }, { title: string }] } }): Promise<Pick<SubscriptionProduct, "id"> | null>;
|
||||
findFirst(args: { where: { OR: [{ billingProviderId: string }, { title: string }] } }): Promise<Pick<SubscriptionProduct, "id"> | null>;
|
||||
create(args: { data: SubscriptionProductCreateInput }): Promise<Pick<SubscriptionProduct, "id">>;
|
||||
update(args: { where: { id: string }; data: SubscriptionProductUpdateInput }): Promise<Pick<SubscriptionProduct, "id">>;
|
||||
};
|
||||
@@ -53,9 +72,16 @@ export type EntitlementsSeedSubscriptionFeatureClient = {
|
||||
create(args: { data: SubscriptionFeatureCreateInput & { subscriptionProductId: string } }): Promise<SubscriptionFeature>;
|
||||
};
|
||||
|
||||
export type EntitlementsSeedSubscriptionPriceClient = {
|
||||
findFirst(args: { where: { subscriptionProductId: string; currency: string; billingPeriod: number } }): Promise<Pick<SubscriptionPrice, "id"> | null>;
|
||||
create(args: { data: SubscriptionPriceCreateInput }): Promise<SubscriptionPrice>;
|
||||
update(args: { where: { id: string }; data: SubscriptionPriceUpdateInput }): Promise<SubscriptionPrice>;
|
||||
};
|
||||
|
||||
export type EntitlementsSeedClient = {
|
||||
subscriptionProduct: EntitlementsSeedSubscriptionProductClient;
|
||||
subscriptionFeature: EntitlementsSeedSubscriptionFeatureClient;
|
||||
subscriptionPrice: EntitlementsSeedSubscriptionPriceClient;
|
||||
};
|
||||
|
||||
export type SeedStandardEntitlementsOptions = {
|
||||
@@ -70,6 +96,8 @@ function getInitialResult(plans: readonly EntitlementPlanStub[]): EntitlementsSe
|
||||
featureCount: plans.reduce((total, plan) => total + plan.features.length, 0),
|
||||
createdProducts: 0,
|
||||
updatedProducts: 0,
|
||||
createdPrices: 0,
|
||||
updatedPrices: 0,
|
||||
deletedFeatures: 0,
|
||||
createdFeatures: 0,
|
||||
};
|
||||
@@ -82,6 +110,8 @@ function diagnosticFromResult(phase: EntitlementsSeedPhase, result: Entitlements
|
||||
featureCount: result.featureCount,
|
||||
createdProducts: result.createdProducts,
|
||||
updatedProducts: result.updatedProducts,
|
||||
createdPrices: result.createdPrices,
|
||||
updatedPrices: result.updatedPrices,
|
||||
deletedFeatures: result.deletedFeatures,
|
||||
createdFeatures: result.createdFeatures,
|
||||
...(planId ? { planId } : {}),
|
||||
@@ -92,13 +122,15 @@ async function emitDiagnostic(diagnostics: EntitlementsSeedDiagnostics | undefin
|
||||
await diagnostics?.(event);
|
||||
}
|
||||
|
||||
export function getEntitlementPlanStripeId(plan: Pick<EntitlementPlanStub, "id">): string {
|
||||
return `${SEEDED_STRIPE_ID_PREFIX}_${plan.id}`;
|
||||
export function getEntitlementPlanBillingProviderId(plan: Pick<EntitlementPlanStub, "id">): string {
|
||||
return `${SEEDED_BILLING_PROVIDER_ID_PREFIX}_${plan.id}`;
|
||||
}
|
||||
|
||||
function mapPlanToProductCreateInput(plan: EntitlementPlanStub): SubscriptionProductCreateInput {
|
||||
return {
|
||||
stripeId: getEntitlementPlanStripeId(plan),
|
||||
billingProviderId: getEntitlementPlanBillingProviderId(plan),
|
||||
code: plan.id,
|
||||
lagoPlanCode: plan.id,
|
||||
order: plan.metadata.sortOrder,
|
||||
title: plan.metadata.publicName,
|
||||
model: 0,
|
||||
@@ -117,7 +149,9 @@ function mapPlanToProductCreateInput(plan: EntitlementPlanStub): SubscriptionPro
|
||||
function mapPlanToProductUpdateInput(plan: EntitlementPlanStub): SubscriptionProductUpdateInput {
|
||||
const product = mapPlanToProductCreateInput(plan);
|
||||
return {
|
||||
stripeId: product.stripeId,
|
||||
billingProviderId: product.billingProviderId,
|
||||
code: product.code,
|
||||
lagoPlanCode: product.lagoPlanCode,
|
||||
order: product.order,
|
||||
title: product.title,
|
||||
model: product.model,
|
||||
@@ -132,6 +166,60 @@ function mapPlanToProductUpdateInput(plan: EntitlementPlanStub): SubscriptionPro
|
||||
};
|
||||
}
|
||||
|
||||
function getPriceBillingProviderId(plan: Pick<EntitlementPlanStub, "id">, billingPeriod: number): string {
|
||||
return `${SEEDED_PRICE_PROVIDER_ID_PREFIX}_${plan.id}_${billingPeriod === BILLING_PERIOD_YEARLY ? "yearly" : "monthly"}`;
|
||||
}
|
||||
|
||||
function getLagoFeeCode(plan: Pick<EntitlementPlanStub, "id">, billingPeriod: number): string {
|
||||
return `${plan.id}_${billingPeriod === BILLING_PERIOD_YEARLY ? "yearly" : "monthly"}`;
|
||||
}
|
||||
|
||||
function mapPlanPriceToInput(plan: EntitlementPlanStub, subscriptionProductId: string, billingPeriod: number, price: string): SubscriptionPriceCreateInput {
|
||||
const billingProviderId = getPriceBillingProviderId(plan, billingPeriod);
|
||||
return {
|
||||
subscriptionProductId,
|
||||
billingProviderId,
|
||||
lagoFeeCode: getLagoFeeCode(plan, billingPeriod),
|
||||
molliePriceReference: billingProviderId,
|
||||
type: PRICE_TYPE_RECURRING,
|
||||
billingPeriod,
|
||||
price,
|
||||
currency: "eur",
|
||||
trialDays: 0,
|
||||
active: true,
|
||||
};
|
||||
}
|
||||
|
||||
async function seedPlanPrice(client: EntitlementsSeedClient, plan: EntitlementPlanStub, productId: string, billingPeriod: number, price: string) {
|
||||
const priceData = mapPlanPriceToInput(plan, productId, billingPeriod, price);
|
||||
const existingPrice = await client.subscriptionPrice.findFirst({
|
||||
where: {
|
||||
subscriptionProductId: productId,
|
||||
currency: priceData.currency,
|
||||
billingPeriod: priceData.billingPeriod,
|
||||
},
|
||||
});
|
||||
|
||||
if (existingPrice) {
|
||||
await client.subscriptionPrice.update({
|
||||
where: { id: existingPrice.id },
|
||||
data: {
|
||||
billingProviderId: priceData.billingProviderId,
|
||||
lagoFeeCode: priceData.lagoFeeCode,
|
||||
molliePriceReference: priceData.molliePriceReference,
|
||||
type: priceData.type,
|
||||
price: priceData.price,
|
||||
trialDays: priceData.trialDays,
|
||||
active: priceData.active,
|
||||
},
|
||||
});
|
||||
return { createdPrices: 0, updatedPrices: 1 };
|
||||
}
|
||||
|
||||
await client.subscriptionPrice.create({ data: priceData });
|
||||
return { createdPrices: 1, updatedPrices: 0 };
|
||||
}
|
||||
|
||||
function getFeatureValue(limitType: EntitlementLimitType, limit: number | null): number {
|
||||
return limit ?? limitType;
|
||||
}
|
||||
@@ -179,7 +267,7 @@ export function redactSeedError(error: unknown): { name: string; message: string
|
||||
async function seedOnePlan(client: EntitlementsSeedClient, plan: EntitlementPlanStub): Promise<Omit<EntitlementsSeedResult, "planCount" | "featureCount">> {
|
||||
const productData = mapPlanToProductCreateInput(plan);
|
||||
const existingProduct = await client.subscriptionProduct.findFirst({
|
||||
where: { OR: [{ stripeId: productData.stripeId }, { title: productData.title }] },
|
||||
where: { OR: [{ billingProviderId: productData.billingProviderId }, { title: productData.title }] },
|
||||
});
|
||||
|
||||
const product = existingProduct
|
||||
@@ -190,6 +278,16 @@ async function seedOnePlan(client: EntitlementsSeedClient, plan: EntitlementPlan
|
||||
: await client.subscriptionProduct.create({ data: productData });
|
||||
|
||||
const deletedFeatures = await client.subscriptionFeature.deleteMany({ where: { subscriptionProductId: product.id } });
|
||||
let createdPrices = 0;
|
||||
let updatedPrices = 0;
|
||||
const standardPrices = STANDARD_PLAN_PRICES_EUR[plan.id];
|
||||
if (standardPrices) {
|
||||
const monthly = await seedPlanPrice(client, plan, product.id, BILLING_PERIOD_MONTHLY, standardPrices.monthly);
|
||||
const yearly = await seedPlanPrice(client, plan, product.id, BILLING_PERIOD_YEARLY, standardPrices.yearly);
|
||||
createdPrices += monthly.createdPrices + yearly.createdPrices;
|
||||
updatedPrices += monthly.updatedPrices + yearly.updatedPrices;
|
||||
}
|
||||
|
||||
for (const feature of plan.features) {
|
||||
await client.subscriptionFeature.create({
|
||||
data: {
|
||||
@@ -202,6 +300,8 @@ async function seedOnePlan(client: EntitlementsSeedClient, plan: EntitlementPlan
|
||||
return {
|
||||
createdProducts: existingProduct ? 0 : 1,
|
||||
updatedProducts: existingProduct ? 1 : 0,
|
||||
createdPrices,
|
||||
updatedPrices,
|
||||
deletedFeatures: deletedFeatures.count,
|
||||
createdFeatures: plan.features.length,
|
||||
};
|
||||
@@ -220,6 +320,8 @@ export async function seedStandardEntitlements({
|
||||
const planResult = await seedOnePlan(client, plan);
|
||||
result.createdProducts += planResult.createdProducts;
|
||||
result.updatedProducts += planResult.updatedProducts;
|
||||
result.createdPrices += planResult.createdPrices;
|
||||
result.updatedPrices += planResult.updatedPrices;
|
||||
result.deletedFeatures += planResult.deletedFeatures;
|
||||
result.createdFeatures += planResult.createdFeatures;
|
||||
await emitDiagnostic(diagnostics, diagnosticFromResult("plan-seeded", result, plan.id));
|
||||
|
||||
Reference in new issue
Block a user