-
Notifications
You must be signed in to change notification settings - Fork 245
Expand file tree
/
Copy pathexecuteCustomerEntitlementOperations.ts
More file actions
213 lines (202 loc) · 5.59 KB
/
Copy pathexecuteCustomerEntitlementOperations.ts
File metadata and controls
213 lines (202 loc) · 5.59 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
import pLimit from "p-limit";
import type { AutumnContext } from "@/honoUtils/HonoEnv";
import { generateId } from "@/utils/genUtils";
import type {
AddEntitlementPriceOperation,
EntitlementPriceOperation,
RemoveEntitlementPriceOperation,
ReplaceEntitlementPriceOperation,
} from "../types/entitlementPriceOperationTypes";
import type { CustomerEntitlementBatchTransition } from "../types/types";
import { BATCH_TRANSITION_OPERATION_CONCURRENCY } from "../utils/batchTransitionConstants";
import { executeBatchedMutation } from "./executeBatchedMutation";
import { addCustomerEntitlementsBatch } from "./sql/addCustomerEntitlementsBatch";
import { deleteCustomerEntitlementsBatch } from "./sql/deleteCustomerEntitlementsBatch";
import { insertPooledBalanceGraph } from "./sql/insertPooledBalanceGraph";
import { replaceCustomerEntitlementsBatch } from "./sql/replaceCustomerEntitlementsBatch";
const executeReplacement = async ({
ctx,
customerLicenseLinkId,
operation,
}: {
ctx: AutumnContext;
customerLicenseLinkId: string;
operation: ReplaceEntitlementPriceOperation;
}) => {
if (operation.fromEntitlementIds.length === 0) return 0;
if (operation.fromEntitlementIds.includes(operation.toEntitlementId)) {
throw new Error(
"Batch replacement requires different outgoing and incoming entitlement IDs",
);
}
return executeBatchedMutation({
db: ctx.db,
operationName: "Customer entitlement replacement",
executeBatch: ({ db, batchSize }) =>
replaceCustomerEntitlementsBatch({
db,
customerLicenseLinkId,
operation,
batchSize,
}),
});
};
const executeAddition = async ({
ctx,
batchTransition,
operation,
}: {
ctx: AutumnContext;
batchTransition: CustomerEntitlementBatchTransition;
operation: AddEntitlementPriceOperation;
}) => {
return executeBatchedMutation({
db: ctx.db,
operationName: "Customer entitlement addition",
executeBatch: async ({ db, batchSize }) => {
const pooledBalanceId = operation.pooledAdd
? await insertPooledBalanceGraph({
db,
pooledAdd: operation.pooledAdd,
customerId: operation.customerEntitlement.customer_id ?? null,
orgId: ctx.org.id,
env: ctx.env,
now: Date.now(),
})
: undefined;
return addCustomerEntitlementsBatch({
db,
customerLicenseLinkId: batchTransition.customerLicenseLinkId,
assignmentCutoffMs: batchTransition.assignmentCutoffMs,
customerEntitlementIds: Array.from({ length: batchSize }, () =>
generateId("cus_ent"),
),
operation,
batchSize,
pooledBalanceId,
contributionIds: pooledBalanceId
? Array.from({ length: batchSize }, () =>
generateId("pool_contribution"),
)
: undefined,
});
},
});
};
const executeRemoval = async ({
ctx,
customerLicenseLinkId,
operation,
}: {
ctx: AutumnContext;
customerLicenseLinkId: string;
operation: RemoveEntitlementPriceOperation;
}) => {
if (operation.fromEntitlementIds.length === 0) return 0;
return executeBatchedMutation({
db: ctx.db,
operationName: "Customer entitlement deletion",
executeBatch: ({ db, batchSize }) =>
deleteCustomerEntitlementsBatch({
db,
customerLicenseLinkId,
operation,
batchSize,
}),
});
};
const operationFeatureId = (operation: EntitlementPriceOperation): string => {
if (operation.type === "replace") {
return operation.fromEntitlementPrice.entitlement.internal_feature_id;
}
return operation.entitlementPrice.entitlement.internal_feature_id;
};
const groupOperationsByFeature = ({
operations,
}: {
operations: EntitlementPriceOperation[];
}): EntitlementPriceOperation[][] => {
const groups = new Map<string, EntitlementPriceOperation[]>();
for (const operation of operations) {
const featureId = operationFeatureId(operation);
const group = groups.get(featureId) ?? [];
group.push(operation);
groups.set(featureId, group);
}
return [...groups.values()];
};
const executeOperation = async ({
ctx,
batchTransition,
operation,
}: {
ctx: AutumnContext;
batchTransition: CustomerEntitlementBatchTransition;
operation: EntitlementPriceOperation;
}) => {
if (operation.type === "replace") {
return {
replaced: await executeReplacement({
ctx,
customerLicenseLinkId: batchTransition.customerLicenseLinkId,
operation,
}),
added: 0,
removed: 0,
};
}
if (operation.type === "add") {
return {
replaced: 0,
added: await executeAddition({ ctx, batchTransition, operation }),
removed: 0,
};
}
return {
replaced: 0,
added: 0,
removed: await executeRemoval({
ctx,
customerLicenseLinkId: batchTransition.customerLicenseLinkId,
operation,
}),
};
};
export const executeCustomerEntitlementOperations = async ({
ctx,
batchTransition,
}: {
ctx: AutumnContext;
batchTransition: CustomerEntitlementBatchTransition;
}) => {
const limit = pLimit(BATCH_TRANSITION_OPERATION_CONCURRENCY);
const groups = groupOperationsByFeature({
operations: batchTransition.operations.entitlementPrices,
});
const results = await Promise.all(
groups.map((operations) =>
limit(async () => {
const result = { replaced: 0, added: 0, removed: 0 };
for (const operation of operations) {
const operationResult = await executeOperation({
ctx,
batchTransition,
operation,
});
result.replaced += operationResult.replaced;
result.added += operationResult.added;
result.removed += operationResult.removed;
}
return result;
}),
),
);
return results.reduce(
(total, result) => ({
replaced: total.replaced + result.replaced,
added: total.added + result.added,
removed: total.removed + result.removed,
}),
{ replaced: 0, added: 0, removed: 0 },
);
};