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
+197
-20
@@ -1,7 +1,15 @@
|
||||
import {
|
||||
assertKnownFeatureKey,
|
||||
billingCadences,
|
||||
customerPortalCommercialProducts,
|
||||
emailMarketingCommercialProducts,
|
||||
entitlementLimitTypes,
|
||||
getBillingCatalogVariantForSelection,
|
||||
getCommercialProductFeatureLimit,
|
||||
getFeatureGate,
|
||||
standardEntitlementPlans,
|
||||
type BillingCatalogPlanVariant,
|
||||
type CommercialProductDefinition,
|
||||
type EntitlementLimitType,
|
||||
type EntitlementPlanStub,
|
||||
type FeatureKey,
|
||||
@@ -23,11 +31,9 @@ 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" },
|
||||
};
|
||||
const BILLING_PERIOD_ONE_TIME = 0;
|
||||
const SEEDED_COMMERCIAL_PRODUCT_PREFIX = "hivegrid_commercial_product";
|
||||
const SEEDED_COMMERCIAL_PRICE_PREFIX = "hivegrid_commercial_price";
|
||||
|
||||
export type EntitlementsSeedPhase = "start" | "plan-seeded" | "success" | "failure";
|
||||
|
||||
@@ -87,13 +93,16 @@ export type EntitlementsSeedClient = {
|
||||
export type SeedStandardEntitlementsOptions = {
|
||||
client?: EntitlementsSeedClient;
|
||||
plans?: readonly EntitlementPlanStub[];
|
||||
includeCustomerPortalCommercialProducts?: boolean;
|
||||
includeEmailMarketingCommercialProducts?: boolean;
|
||||
diagnostics?: EntitlementsSeedDiagnostics;
|
||||
};
|
||||
|
||||
function getInitialResult(plans: readonly EntitlementPlanStub[]): EntitlementsSeedResult {
|
||||
function getInitialResult(plans: readonly EntitlementPlanStub[], commercialProducts: readonly CommercialProductDefinition[]): EntitlementsSeedResult {
|
||||
return {
|
||||
planCount: plans.length,
|
||||
featureCount: plans.reduce((total, plan) => total + plan.features.length, 0),
|
||||
planCount: plans.length + commercialProducts.length,
|
||||
featureCount:
|
||||
plans.reduce((total, plan) => total + plan.features.length, 0) + commercialProducts.reduce((total, product) => total + product.featureKeys.length, 0),
|
||||
createdProducts: 0,
|
||||
updatedProducts: 0,
|
||||
createdPrices: 0,
|
||||
@@ -131,6 +140,7 @@ function mapPlanToProductCreateInput(plan: EntitlementPlanStub): SubscriptionPro
|
||||
billingProviderId: getEntitlementPlanBillingProviderId(plan),
|
||||
code: plan.id,
|
||||
lagoPlanCode: plan.id,
|
||||
kind: "base_plan",
|
||||
order: plan.metadata.sortOrder,
|
||||
title: plan.metadata.publicName,
|
||||
model: 0,
|
||||
@@ -152,6 +162,7 @@ function mapPlanToProductUpdateInput(plan: EntitlementPlanStub): SubscriptionPro
|
||||
billingProviderId: product.billingProviderId,
|
||||
code: product.code,
|
||||
lagoPlanCode: product.lagoPlanCode,
|
||||
kind: product.kind,
|
||||
order: product.order,
|
||||
title: product.title,
|
||||
model: product.model,
|
||||
@@ -166,32 +177,176 @@ function mapPlanToProductUpdateInput(plan: EntitlementPlanStub): SubscriptionPro
|
||||
};
|
||||
}
|
||||
|
||||
function commercialProductBillingProviderId(product: CommercialProductDefinition): string {
|
||||
return `${SEEDED_COMMERCIAL_PRODUCT_PREFIX}_${product.code}`;
|
||||
}
|
||||
|
||||
function commercialPriceBillingProviderId(product: CommercialProductDefinition): string {
|
||||
return `${SEEDED_COMMERCIAL_PRICE_PREFIX}_${product.code}`;
|
||||
}
|
||||
|
||||
function commercialAmountDecimal(amountCents: number): string {
|
||||
if (!Number.isSafeInteger(amountCents) || amountCents <= 0) throw new RangeError("Commercial product amount must be a positive cent integer.");
|
||||
return (amountCents / 100).toFixed(2);
|
||||
}
|
||||
|
||||
function mapCommercialProductCreateInput(product: CommercialProductDefinition): SubscriptionProductCreateInput {
|
||||
return {
|
||||
billingProviderId: commercialProductBillingProviderId(product),
|
||||
code: product.code,
|
||||
lagoPlanCode: product.source === "recurring_add_on" ? product.code : null,
|
||||
kind: product.source,
|
||||
order: product.catalogOrder,
|
||||
title: product.title,
|
||||
model: 0,
|
||||
public: product.public,
|
||||
active: true,
|
||||
description: product.description,
|
||||
badge: null,
|
||||
groupTitle: product.groupTitle,
|
||||
groupDescription: null,
|
||||
billingAddressCollection: "auto",
|
||||
hasQuantity: false,
|
||||
canBuyAgain: product.source === "one_time_setup",
|
||||
};
|
||||
}
|
||||
|
||||
function mapCommercialProductUpdateInput(product: CommercialProductDefinition): SubscriptionProductUpdateInput {
|
||||
return mapCommercialProductCreateInput(product);
|
||||
}
|
||||
|
||||
async function seedCommercialProduct(
|
||||
client: EntitlementsSeedClient,
|
||||
productDefinition: CommercialProductDefinition
|
||||
): Promise<Omit<EntitlementsSeedResult, "planCount" | "featureCount">> {
|
||||
const productData = mapCommercialProductCreateInput(productDefinition);
|
||||
const existingProduct = await client.subscriptionProduct.findFirst({
|
||||
where: { OR: [{ billingProviderId: productData.billingProviderId }, { title: productData.title }] },
|
||||
});
|
||||
const product = existingProduct
|
||||
? await client.subscriptionProduct.update({ where: { id: existingProduct.id }, data: mapCommercialProductUpdateInput(productDefinition) })
|
||||
: await client.subscriptionProduct.create({ data: productData });
|
||||
|
||||
const deletedFeatures = await client.subscriptionFeature.deleteMany({ where: { subscriptionProductId: product.id } });
|
||||
for (const [index, featureKey] of productDefinition.featureKeys.entries()) {
|
||||
assertKnownFeatureKey(featureKey);
|
||||
const registryItem = getFeatureGate(featureKey);
|
||||
if (!registryItem) throw new RangeError(`Unknown feature key: ${featureKey}`);
|
||||
const featureLimit = getCommercialProductFeatureLimit(productDefinition, featureKey);
|
||||
await client.subscriptionFeature.create({
|
||||
data: {
|
||||
subscriptionProductId: product.id,
|
||||
order: index + 1,
|
||||
title: registryItem.title,
|
||||
name: featureKey,
|
||||
type: featureLimit.limitType,
|
||||
value: featureLimit.limit ?? featureLimit.limitType,
|
||||
href: null,
|
||||
badge: registryItem.title,
|
||||
accumulate: featureLimit.limitType === entitlementLimitTypes.monthly,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
let createdPrices = 0;
|
||||
let updatedPrices = 0;
|
||||
if (productDefinition.amountCents !== null && productDefinition.currency !== null) {
|
||||
const billingPeriod = productDefinition.source === "recurring_add_on" ? BILLING_PERIOD_MONTHLY : BILLING_PERIOD_ONE_TIME;
|
||||
const billingProviderId = commercialPriceBillingProviderId(productDefinition);
|
||||
const priceData: SubscriptionPriceCreateInput = {
|
||||
subscriptionProductId: product.id,
|
||||
billingProviderId,
|
||||
lagoPlanCode: productDefinition.source === "recurring_add_on" ? productDefinition.code : null,
|
||||
lagoFeeCode: productDefinition.code,
|
||||
molliePriceReference: billingProviderId,
|
||||
billingCadence: "monthly",
|
||||
contractMonths: 1,
|
||||
requiredPaymentMethod: "any",
|
||||
type: productDefinition.source === "recurring_add_on" ? PRICE_TYPE_RECURRING : 0,
|
||||
billingPeriod,
|
||||
price: commercialAmountDecimal(productDefinition.amountCents),
|
||||
currency: productDefinition.currency.toLowerCase(),
|
||||
trialDays: 0,
|
||||
active: true,
|
||||
};
|
||||
const existingPrice = await client.subscriptionPrice.findFirst({
|
||||
where: { subscriptionProductId: product.id, currency: priceData.currency, billingPeriod },
|
||||
});
|
||||
if (existingPrice) {
|
||||
await client.subscriptionPrice.update({
|
||||
where: { id: existingPrice.id },
|
||||
data: {
|
||||
billingProviderId: priceData.billingProviderId,
|
||||
lagoPlanCode: priceData.lagoPlanCode,
|
||||
lagoFeeCode: priceData.lagoFeeCode,
|
||||
molliePriceReference: priceData.molliePriceReference,
|
||||
billingCadence: priceData.billingCadence,
|
||||
contractMonths: priceData.contractMonths,
|
||||
requiredPaymentMethod: priceData.requiredPaymentMethod,
|
||||
type: priceData.type,
|
||||
price: priceData.price,
|
||||
trialDays: priceData.trialDays,
|
||||
active: priceData.active,
|
||||
},
|
||||
});
|
||||
updatedPrices = 1;
|
||||
} else {
|
||||
await client.subscriptionPrice.create({ data: priceData });
|
||||
createdPrices = 1;
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
createdProducts: existingProduct ? 0 : 1,
|
||||
updatedProducts: existingProduct ? 1 : 0,
|
||||
createdPrices,
|
||||
updatedPrices,
|
||||
deletedFeatures: deletedFeatures.count,
|
||||
createdFeatures: productDefinition.featureKeys.length,
|
||||
};
|
||||
}
|
||||
|
||||
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 getLagoFeeCode(variant: BillingCatalogPlanVariant): string {
|
||||
return `${variant.code}_base`;
|
||||
}
|
||||
|
||||
function mapPlanPriceToInput(plan: EntitlementPlanStub, subscriptionProductId: string, billingPeriod: number, price: string): SubscriptionPriceCreateInput {
|
||||
function mapPlanPriceToInput(
|
||||
plan: EntitlementPlanStub,
|
||||
subscriptionProductId: string,
|
||||
billingPeriod: number,
|
||||
variant: BillingCatalogPlanVariant
|
||||
): SubscriptionPriceCreateInput {
|
||||
const billingProviderId = getPriceBillingProviderId(plan, billingPeriod);
|
||||
return {
|
||||
subscriptionProductId,
|
||||
billingProviderId,
|
||||
lagoFeeCode: getLagoFeeCode(plan, billingPeriod),
|
||||
lagoPlanCode: variant.code,
|
||||
lagoFeeCode: getLagoFeeCode(variant),
|
||||
molliePriceReference: billingProviderId,
|
||||
billingCadence: variant.cadence,
|
||||
contractMonths: variant.contractMonths,
|
||||
requiredPaymentMethod: variant.requiredPaymentMethod,
|
||||
type: PRICE_TYPE_RECURRING,
|
||||
billingPeriod,
|
||||
price,
|
||||
price: (variant.baseAmountCents / 100).toFixed(2),
|
||||
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);
|
||||
async function seedPlanPrice(
|
||||
client: EntitlementsSeedClient,
|
||||
plan: EntitlementPlanStub,
|
||||
productId: string,
|
||||
billingPeriod: number,
|
||||
variant: BillingCatalogPlanVariant
|
||||
) {
|
||||
const priceData = mapPlanPriceToInput(plan, productId, billingPeriod, variant);
|
||||
const existingPrice = await client.subscriptionPrice.findFirst({
|
||||
where: {
|
||||
subscriptionProductId: productId,
|
||||
@@ -205,8 +360,12 @@ async function seedPlanPrice(client: EntitlementsSeedClient, plan: EntitlementPl
|
||||
where: { id: existingPrice.id },
|
||||
data: {
|
||||
billingProviderId: priceData.billingProviderId,
|
||||
lagoPlanCode: priceData.lagoPlanCode,
|
||||
lagoFeeCode: priceData.lagoFeeCode,
|
||||
molliePriceReference: priceData.molliePriceReference,
|
||||
billingCadence: priceData.billingCadence,
|
||||
contractMonths: priceData.contractMonths,
|
||||
requiredPaymentMethod: priceData.requiredPaymentMethod,
|
||||
type: priceData.type,
|
||||
price: priceData.price,
|
||||
trialDays: priceData.trialDays,
|
||||
@@ -280,10 +439,11 @@ async function seedOnePlan(client: EntitlementsSeedClient, plan: EntitlementPlan
|
||||
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);
|
||||
const monthlyVariant = getBillingCatalogVariantForSelection(plan.id, billingCadences.monthly);
|
||||
const annualSepaVariant = getBillingCatalogVariantForSelection(plan.id, billingCadences.annualSepa);
|
||||
if (monthlyVariant && annualSepaVariant) {
|
||||
const monthly = await seedPlanPrice(client, plan, product.id, BILLING_PERIOD_MONTHLY, monthlyVariant);
|
||||
const yearly = await seedPlanPrice(client, plan, product.id, BILLING_PERIOD_YEARLY, annualSepaVariant);
|
||||
createdPrices += monthly.createdPrices + yearly.createdPrices;
|
||||
updatedPrices += monthly.updatedPrices + yearly.updatedPrices;
|
||||
}
|
||||
@@ -310,9 +470,15 @@ async function seedOnePlan(client: EntitlementsSeedClient, plan: EntitlementPlan
|
||||
export async function seedStandardEntitlements({
|
||||
client = getDbClient(),
|
||||
plans = standardEntitlementPlans,
|
||||
includeCustomerPortalCommercialProducts = false,
|
||||
includeEmailMarketingCommercialProducts = false,
|
||||
diagnostics,
|
||||
}: SeedStandardEntitlementsOptions = {}): Promise<EntitlementsSeedResult> {
|
||||
const result = getInitialResult(plans);
|
||||
const commercialProducts = [
|
||||
...(includeCustomerPortalCommercialProducts ? Object.values(customerPortalCommercialProducts) : []),
|
||||
...(includeEmailMarketingCommercialProducts ? emailMarketingCommercialProducts : []),
|
||||
];
|
||||
const result = getInitialResult(plans, commercialProducts);
|
||||
await emitDiagnostic(diagnostics, diagnosticFromResult("start", result));
|
||||
|
||||
try {
|
||||
@@ -327,6 +493,17 @@ export async function seedStandardEntitlements({
|
||||
await emitDiagnostic(diagnostics, diagnosticFromResult("plan-seeded", result, plan.id));
|
||||
}
|
||||
|
||||
for (const product of commercialProducts) {
|
||||
const productResult = await seedCommercialProduct(client, product);
|
||||
result.createdProducts += productResult.createdProducts;
|
||||
result.updatedProducts += productResult.updatedProducts;
|
||||
result.createdPrices += productResult.createdPrices;
|
||||
result.updatedPrices += productResult.updatedPrices;
|
||||
result.deletedFeatures += productResult.deletedFeatures;
|
||||
result.createdFeatures += productResult.createdFeatures;
|
||||
await emitDiagnostic(diagnostics, diagnosticFromResult("plan-seeded", result, product.code));
|
||||
}
|
||||
|
||||
await emitDiagnostic(diagnostics, diagnosticFromResult("success", result));
|
||||
return result;
|
||||
} catch (error) {
|
||||
|
||||
Reference in new issue
Block a user