feat: new attach / update subscription params
This commit is contained in:
26
shared/api/products/components/basePrice/basePrice.ts
Normal file
26
shared/api/products/components/basePrice/basePrice.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { BillingInterval } from "@models/productModels/intervals/billingInterval";
|
||||
import { z } from "zod/v4";
|
||||
import { DisplaySchema } from "../display";
|
||||
|
||||
export const BasePriceSchema = z.object({
|
||||
amount: z.number(),
|
||||
interval: z.enum(BillingInterval),
|
||||
interval_count: z.number().optional(),
|
||||
display: DisplaySchema.optional(),
|
||||
});
|
||||
|
||||
export const BasePriceParamsSchema = BasePriceSchema.omit({
|
||||
display: true,
|
||||
}).extend({
|
||||
interval_count: z.number().optional(),
|
||||
|
||||
entitlement_id: z.string().optional().meta({
|
||||
internal: true,
|
||||
}),
|
||||
price_id: z.string().optional().meta({
|
||||
internal: true,
|
||||
}),
|
||||
});
|
||||
|
||||
export type BasePrice = z.infer<typeof BasePriceSchema>;
|
||||
export type BasePriceParams = z.infer<typeof BasePriceParamsSchema>;
|
||||
@@ -0,0 +1,53 @@
|
||||
import type {
|
||||
BasePrice,
|
||||
BasePriceParams,
|
||||
} from "@api/products/components/basePrice/basePrice";
|
||||
import { BillingInterval } from "@models/productModels/intervals/billingInterval";
|
||||
import {
|
||||
type ProductItem,
|
||||
ProductItemType,
|
||||
} from "@models/productV2Models/productItemModels/productItemModels";
|
||||
import { getProductItemDisplay } from "@utils/productDisplayUtils";
|
||||
import { billingToItemInterval } from "@utils/productV2Utils/productItemUtils/itemIntervalUtils";
|
||||
import type { SharedContext } from "../../../../types/sharedContext";
|
||||
|
||||
export const basePriceToProductItem = ({
|
||||
ctx,
|
||||
basePrice,
|
||||
}: {
|
||||
ctx: SharedContext;
|
||||
basePrice: BasePrice | BasePriceParams;
|
||||
}): ProductItem => {
|
||||
const basePriceDisplay =
|
||||
"display" in basePrice ? basePrice.display : undefined;
|
||||
|
||||
const entitlementId =
|
||||
"entitlement_id" in basePrice
|
||||
? (basePrice.entitlement_id ?? undefined)
|
||||
: undefined;
|
||||
const priceId =
|
||||
"price_id" in basePrice ? (basePrice.price_id ?? undefined) : undefined;
|
||||
|
||||
const item = {
|
||||
type: ProductItemType.Price,
|
||||
feature_id: null,
|
||||
feature: null,
|
||||
interval: billingToItemInterval({
|
||||
billingInterval: basePrice.interval ?? BillingInterval.Month,
|
||||
}),
|
||||
interval_count: basePrice.interval_count ?? 1,
|
||||
price: basePrice.amount ?? 0,
|
||||
|
||||
entitlement_id: entitlementId,
|
||||
price_id: priceId,
|
||||
} satisfies ProductItem;
|
||||
|
||||
const display = basePriceDisplay
|
||||
? getProductItemDisplay({ item, features: ctx.features })
|
||||
: undefined;
|
||||
|
||||
return {
|
||||
...item,
|
||||
display,
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user