-
Notifications
You must be signed in to change notification settings - Fork 245
Expand file tree
/
Copy pathupdateVariants.ts
More file actions
313 lines (289 loc) · 7.78 KB
/
Copy pathupdateVariants.ts
File metadata and controls
313 lines (289 loc) · 7.78 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import {
type ApiPlanV1,
type DiffedCustomizePlanV1,
ErrCode,
type FullProduct,
products,
RecaseError,
type UpdateVariantParams,
} from "@autumn/shared";
import { inArray } from "drizzle-orm";
import type { AutumnContext } from "@/honoUtils/HonoEnv.js";
import {
applyDiffToVariantPlan,
fullProductToApiPlanV1,
getApiPlanDiff,
getVariantSettingsPatch,
omitVariantOwnedSettings,
variantSettingsPatchHasValues,
} from "../common/planTransformUtils.js";
import { createVariant } from "../createVariant/createVariant.js";
import { hasPlanCustomers } from "../previewUpdatePlan/hasPlanCustomers.js";
import { getVariantPropagationTargets } from "./getVariantPropagationTargets.js";
import { updateVariant } from "./updateVariant.js";
type VariantUpdateJob = {
variant: FullProduct;
diff?: DiffedCustomizePlanV1;
targetPlan?: ApiPlanV1;
wasCreated?: boolean;
};
const validateVariantUpdates = (variantUpdates: UpdateVariantParams[]) => {
for (const variantUpdate of variantUpdates) {
if (variantUpdate.force_version && variantUpdate.disable_version) {
throw new RecaseError({
message: "Cannot use both force_version and disable_version",
code: ErrCode.ConflictingVersionFlags,
statusCode: 400,
});
}
}
};
const buildVariantUpdateIndex = (variantUpdates: UpdateVariantParams[]) =>
new Map(
variantUpdates.map((variantUpdate) => [
variantUpdate.variant_plan_id,
variantUpdate,
]),
);
// The base plan's rows belong to a different product; carrying their ids into
// the variant would insert entitlements/prices that collide with the base's.
const stripBasePlanRowIds = (plan: ApiPlanV1): ApiPlanV1 => ({
...plan,
price: plan.price
? (({ entitlement_id: _entitlementId, price_id: _priceId, ...rest }) =>
rest)(plan.price)
: plan.price,
items: plan.items.map(
({ entitlement_id: _entitlementId, price_id: _priceId, ...rest }) => rest,
),
});
const buildVariantTargetPlan = ({
incomingBasePlan,
variant,
variantUpdate,
}: {
incomingBasePlan: ApiPlanV1;
variant: FullProduct;
variantUpdate: UpdateVariantParams;
}): ApiPlanV1 => ({
...stripBasePlanRowIds(
applyDiffToVariantPlan({
plan: incomingBasePlan,
diff: variantUpdate.customize,
}),
),
id: variant.id,
name: variantUpdate.name ?? variant.name,
});
const ensureMissingVariantName = ({
missingVariantId,
variantUpdate,
}: {
missingVariantId: string;
variantUpdate?: UpdateVariantParams;
}): string => {
if (variantUpdate?.name) return variantUpdate.name;
throw new RecaseError({
message: `Variant ${missingVariantId} does not exist. Provide name to create it.`,
code: ErrCode.InvalidPropagationTarget,
statusCode: 400,
});
};
const buildSelectedVariantJobs = ({
baseDiff,
incomingBasePlan,
selectedVariants,
variantUpdateById,
}: {
baseDiff: DiffedCustomizePlanV1;
incomingBasePlan: ApiPlanV1;
selectedVariants: FullProduct[];
variantUpdateById: Map<string, UpdateVariantParams>;
}): Map<string, VariantUpdateJob> => {
const jobs = new Map<string, VariantUpdateJob>();
for (const variant of selectedVariants) {
const variantUpdate = variantUpdateById.get(variant.id);
jobs.set(variant.id, {
variant,
...(variantUpdate
? {
targetPlan: buildVariantTargetPlan({
incomingBasePlan,
variant,
variantUpdate,
}),
}
: { diff: baseDiff }),
});
}
return jobs;
};
const addSettingsPropagationJobs = ({
allVariants,
jobs,
}: {
allVariants: FullProduct[];
jobs: Map<string, VariantUpdateJob>;
}) => {
for (const variant of allVariants) {
if (variant.archived || jobs.has(variant.id)) continue;
jobs.set(variant.id, { variant, diff: {} });
}
};
const moveLatestVariantsToBaseVersion = async ({
ctx,
variants,
newBase,
skipVariantIds,
}: {
ctx: AutumnContext;
variants: FullProduct[];
newBase: FullProduct;
skipVariantIds: Set<string>;
}) => {
const variantInternalIds: string[] = [];
for (const variant of variants) {
if (variant.archived) continue;
if (skipVariantIds.has(variant.id)) continue;
if (variant.base_internal_product_id === newBase.internal_id) continue;
variantInternalIds.push(variant.internal_id);
}
if (variantInternalIds.length === 0) return;
await ctx.db
.update(products)
.set({ base_internal_product_id: newBase.internal_id })
.where(inArray(products.internal_id, variantInternalIds));
};
export const updateVariants = async ({
ctx,
oldBase,
newBase,
propagateToVariants,
variantUpdates = [],
disableVersion,
forceVersion,
allVersions,
}: {
ctx: AutumnContext;
oldBase: FullProduct;
newBase: FullProduct;
propagateToVariants: string[];
variantUpdates?: UpdateVariantParams[];
disableVersion?: boolean;
forceVersion?: boolean;
allVersions?: boolean;
}) => {
validateVariantUpdates(variantUpdates);
const variantUpdateById = buildVariantUpdateIndex(variantUpdates);
const targetVariantIds = [
...new Set([...propagateToVariants, ...variantUpdateById.keys()]),
];
const [currentBasePlan, incomingBasePlan, variants] = await Promise.all([
fullProductToApiPlanV1({ ctx, product: oldBase }),
fullProductToApiPlanV1({ ctx, product: newBase }),
getVariantPropagationTargets({
ctx,
oldBase,
propagateToVariants: targetVariantIds,
missingAllowedIds: new Set(variantUpdateById.keys()),
}),
]);
const baseDiff = getApiPlanDiff({
from: currentBasePlan,
to: incomingBasePlan,
});
const settingsPatch = omitVariantOwnedSettings(
getVariantSettingsPatch({
from: currentBasePlan,
to: incomingBasePlan,
}),
);
const hasSettingsPatch = variantSettingsPatchHasValues(settingsPatch);
const baseWasVersioned = oldBase.internal_id !== newBase.internal_id;
if (targetVariantIds.length === 0 && !hasSettingsPatch) {
if (baseWasVersioned) {
await moveLatestVariantsToBaseVersion({
ctx,
variants: variants.allVariants,
newBase,
skipVariantIds: new Set(),
});
}
return;
}
const resolveShouldVersion = async (
variant: FullProduct,
variantUpdate?: UpdateVariantParams,
): Promise<boolean> => {
if (variantUpdate?.force_version) return true;
if (variantUpdate?.disable_version) return false;
if (forceVersion) return true;
if (disableVersion || allVersions) return false;
return hasPlanCustomers({ ctx, product: variant });
};
const jobs = buildSelectedVariantJobs({
baseDiff,
incomingBasePlan,
selectedVariants: variants.variants,
variantUpdateById,
});
if (hasSettingsPatch) {
addSettingsPropagationJobs({
allVariants: variants.allVariants,
jobs,
});
}
for (const missingVariantId of variants.missingVariantIds) {
const variantUpdate = variantUpdateById.get(missingVariantId);
const name = ensureMissingVariantName({ missingVariantId, variantUpdate });
const variant = await createVariant({
ctx,
data: {
base_plan_id: newBase.id,
variant_plan_id: missingVariantId,
name,
},
initialBaseProduct: newBase,
});
jobs.set(variant.id, {
variant,
targetPlan: variantUpdate
? buildVariantTargetPlan({
incomingBasePlan,
variant,
variantUpdate,
})
: undefined,
diff: variantUpdate ? undefined : baseDiff,
wasCreated: true,
});
}
const versionedVariantIds = new Set<string>();
for (const { variant, diff, targetPlan, wasCreated } of jobs.values()) {
const variantUpdate = variantUpdateById.get(variant.id);
const shouldVersion =
wasCreated && !variantUpdate?.force_version
? false
: await resolveShouldVersion(variant, variantUpdate);
if (shouldVersion) versionedVariantIds.add(variant.id);
await updateVariant({
ctx,
variant,
diff,
targetPlan,
settingsPatch,
shouldVersion,
baseInternalProductId: baseWasVersioned ? newBase.internal_id : undefined,
allVersions,
variantUpdate,
});
}
if (baseWasVersioned) {
await moveLatestVariantsToBaseVersion({
ctx,
variants: variants.allVariants,
newBase,
skipVariantIds: versionedVariantIds,
});
}
};