feat: new attach / update subscription params
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
import { ProductItemSchema } from "@models/productV2Models/productItemModels/productItemModels";
|
||||
import { z } from "zod/v4";
|
||||
|
||||
export const CustomizePlanV0Schema = z.array(ProductItemSchema);
|
||||
|
||||
export type CustomizePlanV0 = z.infer<typeof CustomizePlanV0Schema>;
|
||||
14
shared/api/billing/common/customizePlan/customizePlanV1.ts
Normal file
14
shared/api/billing/common/customizePlan/customizePlanV1.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { BasePriceParamsSchema } from "@api/products/components/basePrice/basePrice";
|
||||
import { CreatePlanItemParamsV1Schema } from "@api/products/items/crud/createPlanItemParamsV1";
|
||||
import { z } from "zod/v4";
|
||||
|
||||
export const CustomizePlanV1Schema = z
|
||||
.object({
|
||||
price: BasePriceParamsSchema.nullable().optional(), // null to remove base price
|
||||
items: z.array(CreatePlanItemParamsV1Schema).optional(),
|
||||
})
|
||||
.refine((data) => data.items !== undefined || data.price !== undefined, {
|
||||
message: "When using customize, either items or price must be provided",
|
||||
});
|
||||
|
||||
export type CustomizePlanV1 = z.infer<typeof CustomizePlanV1Schema>;
|
||||
@@ -0,0 +1,62 @@
|
||||
import { basePriceToProductItem } from "@api/products/components/basePrice/basePriceToProductItem";
|
||||
import { planV1ToProductItems } from "@api/products/mappers/planV1ToProductItems";
|
||||
import type { FullProduct } from "@models/productModels/productModels";
|
||||
import { isPriceItem, mapToProductItems } from "@utils/index";
|
||||
import type { SharedContext } from "../../../../../types/sharedContext";
|
||||
import type { CustomizePlanV0 } from "../customizePlanV0";
|
||||
import type { CustomizePlanV1 } from "../customizePlanV1";
|
||||
|
||||
export const customizePlanV1ToV0 = ({
|
||||
ctx,
|
||||
customizePlanV1,
|
||||
fullProduct,
|
||||
}: {
|
||||
ctx: SharedContext;
|
||||
customizePlanV1: CustomizePlanV1;
|
||||
fullProduct: FullProduct;
|
||||
}): CustomizePlanV0 => {
|
||||
const currentProductItems = mapToProductItems({
|
||||
prices: fullProduct.prices,
|
||||
entitlements: fullProduct.entitlements,
|
||||
features: ctx.features,
|
||||
});
|
||||
|
||||
if (
|
||||
customizePlanV1.price !== undefined &&
|
||||
customizePlanV1.items !== undefined
|
||||
) {
|
||||
// 1. If price AND items provided, return full items array
|
||||
return planV1ToProductItems({
|
||||
ctx,
|
||||
plan: { price: customizePlanV1.price, items: customizePlanV1.items },
|
||||
});
|
||||
} else if (
|
||||
customizePlanV1.price !== undefined &&
|
||||
customizePlanV1.items === undefined
|
||||
) {
|
||||
// 2. If price provided, but no items, then customize base price and carry over feature items
|
||||
const featureItems = currentProductItems.filter(
|
||||
(item) => !isPriceItem(item),
|
||||
);
|
||||
|
||||
const basePriceItem = customizePlanV1.price
|
||||
? basePriceToProductItem({
|
||||
ctx,
|
||||
basePrice: customizePlanV1.price,
|
||||
})
|
||||
: undefined;
|
||||
|
||||
return basePriceItem ? [basePriceItem, ...featureItems] : featureItems;
|
||||
} else {
|
||||
// 3. If no price provided, then carry over base price
|
||||
const basePriceItem = currentProductItems.filter((item) =>
|
||||
isPriceItem(item),
|
||||
);
|
||||
const featureItems = planV1ToProductItems({
|
||||
ctx,
|
||||
plan: { price: null, items: customizePlanV1.items ?? [] },
|
||||
});
|
||||
|
||||
return [...basePriceItem, ...featureItems];
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user