Files
cfw-autumn/shared/api/billing/attachV2/requestChanges/V1.2_AttachParamsChange.ts

69 lines
2.1 KiB
TypeScript

import { billingParamsV0ToInvoiceModeParams } from "@api/billing/common/mappers/billingParamsV0ToInvoiceModeParams";
import { freeTrialParamsV0ToV1 } from "@api/common/freeTrial/mappers/freeTrialParamsV0ToV1";
import { ApiVersion } from "@api/versionUtils/ApiVersion";
import {
AffectedResource,
defineVersionChange,
} from "@api/versionUtils/versionChangeUtils/VersionChange";
import { productItemsToCustomizePlanV1 } from "@utils/productV2Utils/productItemUtils/convertProductItem/productItemsToCustomizePlanV1";
import type { z } from "zod/v4";
import type { SharedContext } from "../../../../types/sharedContext";
import { AttachParamsV0Schema } from "../attachParamsV0";
import { AttachParamsV1Schema } from "../attachParamsV1";
export const V1_2_AttachParamsChange = defineVersionChange({
name: "V1.2 Attach Params Change",
newVersion: ApiVersion.V2_0,
oldVersion: ApiVersion.V1_Beta,
description: [
"Maps free_trial {length,duration} to {duration_length,duration_type}",
"Maps top-level items to customize.items",
],
affectedResources: [AffectedResource.Attach],
newSchema: AttachParamsV1Schema,
oldSchema: AttachParamsV0Schema,
affectsRequest: true,
affectsResponse: false,
transformRequest: ({
ctx,
input,
}: {
ctx: SharedContext;
input: z.infer<typeof AttachParamsV0Schema>;
}): z.infer<typeof AttachParamsV1Schema> => {
const itemsCustomize = input.items
? productItemsToCustomizePlanV1({
ctx,
items: input.items,
})
: undefined;
const freeTrialV1 = freeTrialParamsV0ToV1({
freeTrialParamsV0: input.free_trial,
});
const customizeV1 =
itemsCustomize || freeTrialV1 !== undefined
? {
...itemsCustomize,
free_trial: freeTrialV1,
}
: undefined;
const newPlanId = input.product_id ?? undefined;
const featureQuantities = input.options;
const invoiceMode = billingParamsV0ToInvoiceModeParams({ input });
return {
...input,
plan_id: newPlanId,
feature_quantities: featureQuantities,
invoice_mode: invoiceMode,
enable_plan_immediately: input.enable_product_immediately,
customize: customizeV1,
proration_behavior: input.billing_behavior,
};
},
});