-
Notifications
You must be signed in to change notification settings - Fork 245
Expand file tree
/
Copy pathpreviewCatalogPlanUpdate.ts
More file actions
145 lines (139 loc) · 3.31 KB
/
Copy pathpreviewCatalogPlanUpdate.ts
File metadata and controls
145 lines (139 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import {
apiPlan,
type CatalogPlanParams,
expandPathIncludes,
type FullProduct,
type PlanUpdatePreview,
PlanUpdatePreviewSchema,
PreviewUpdatePlanExpand,
type PreviewUpdatePlanParamsV2,
type ProductV2,
productV2ToApiPlanV1,
} from "@autumn/shared";
import type { AutumnContext } from "@/honoUtils/HonoEnv.js";
import { buildIncomingProductV2 } from "@/internal/product/actions/previewUpdatePlan/buildIncomingProductV2.js";
import { buildPlanUpdatePreview } from "@/internal/product/actions/previewUpdatePlan/previewUpdatePlan.js";
const previewNewPlan = ({
ctx,
planParams,
currency,
}: {
ctx: AutumnContext;
planParams: CatalogPlanParams;
currency: string;
}): PlanUpdatePreview => {
const {
plan_id,
new_plan_id,
version,
variants: _variants,
include_versions: _includeVersions,
include_variants: _includeVariants,
...rest
} = planParams;
const resolved = apiPlan.map.paramsV1ToProductV2({
ctx,
params: {
id: new_plan_id ?? plan_id,
...rest,
add_on: rest.add_on ?? false,
auto_enable: rest.auto_enable ?? false,
},
});
const product = {
env: ctx.env,
version: version ?? 1,
created_at: Date.now(),
archived: false,
...resolved,
id: new_plan_id ?? plan_id,
name: resolved.name || new_plan_id || plan_id,
items: resolved.items ?? [],
is_add_on: resolved.is_add_on ?? false,
is_default: resolved.is_default ?? false,
group: resolved.group ?? "",
} as ProductV2;
const plan = productV2ToApiPlanV1({
product,
features: ctx.features,
currency,
});
const shouldExpandPlan = expandPathIncludes({
expand: ctx.expand,
includes: [PreviewUpdatePlanExpand.Plan],
});
return PlanUpdatePreviewSchema.parse({
plan_id,
...(shouldExpandPlan ? { plan } : {}),
has_customers: false,
versionable: false,
customize: null,
previous_attributes: null,
item_changes: [],
variants: [],
other_versions: [],
});
};
export const previewCatalogPlanUpdate = async ({
ctx,
planParams,
current,
hasCustomers,
customerCount,
currency,
}: {
ctx: AutumnContext;
planParams: CatalogPlanParams;
current: FullProduct | null;
hasCustomers: boolean;
customerCount: number;
currency: string;
}): Promise<PlanUpdatePreview> => {
const { variants, ...basePlanParams } = planParams;
if (!current) {
return previewNewPlan({
ctx,
planParams,
currency,
});
}
const shouldExpandPlan = expandPathIncludes({
expand: ctx.expand,
includes: [PreviewUpdatePlanExpand.Plan],
});
const data: PreviewUpdatePlanParamsV2 = {
...basePlanParams,
variants,
expand: shouldExpandPlan ? [PreviewUpdatePlanExpand.Plan] : [],
include_versions: Boolean(
planParams.include_versions || planParams.all_versions,
),
include_variants: Boolean(
planParams.include_variants ||
(planParams.all_versions &&
((planParams.update_variant_ids?.length ?? 0) > 0 ||
(variants?.length ?? 0) > 0)),
),
};
// Catalog licenses are resolved together against the virtual post-update
// catalog after ordinary plan previews are built.
const previewData = {
...data,
licenses: undefined,
};
const incoming = buildIncomingProductV2({
ctx,
base: current,
data: previewData,
});
return buildPlanUpdatePreview({
ctx,
currentFullProduct: current,
incomingProductV2: incoming,
data: previewData,
variantUpdates: variants,
hasCustomers,
customerCount,
currency,
});
};