@mj-biz-apps/orders-core-entities-server 5.48.0 → 5.49.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/ChangeSubscriptionProductOperation.d.ts +45 -0
- package/dist/ChangeSubscriptionProductOperation.d.ts.map +1 -0
- package/dist/ChangeSubscriptionProductOperation.js +198 -0
- package/dist/ChangeSubscriptionProductOperation.js.map +1 -0
- package/dist/ContractIncrease.d.ts +60 -0
- package/dist/ContractIncrease.d.ts.map +1 -0
- package/dist/ContractIncrease.js +108 -0
- package/dist/ContractIncrease.js.map +1 -0
- package/dist/GetRenewalWorklistOperation.d.ts +52 -0
- package/dist/GetRenewalWorklistOperation.d.ts.map +1 -0
- package/dist/GetRenewalWorklistOperation.js +361 -0
- package/dist/GetRenewalWorklistOperation.js.map +1 -0
- package/dist/OrderEntityServer.d.ts +14 -0
- package/dist/OrderEntityServer.d.ts.map +1 -1
- package/dist/OrderEntityServer.js +57 -0
- package/dist/OrderEntityServer.js.map +1 -1
- package/dist/RenewalPassMode.d.ts +40 -0
- package/dist/RenewalPassMode.d.ts.map +1 -0
- package/dist/RenewalPassMode.js +63 -0
- package/dist/RenewalPassMode.js.map +1 -0
- package/dist/RenewalPricing.d.ts +209 -0
- package/dist/RenewalPricing.d.ts.map +1 -0
- package/dist/RenewalPricing.js +396 -0
- package/dist/RenewalPricing.js.map +1 -0
- package/dist/SetRenewalSettingsOperation.d.ts +34 -0
- package/dist/SetRenewalSettingsOperation.d.ts.map +1 -0
- package/dist/SetRenewalSettingsOperation.js +135 -0
- package/dist/SetRenewalSettingsOperation.js.map +1 -0
- package/dist/SpawnRenewalsOperation.d.ts +83 -105
- package/dist/SpawnRenewalsOperation.d.ts.map +1 -1
- package/dist/SpawnRenewalsOperation.js +352 -467
- package/dist/SpawnRenewalsOperation.js.map +1 -1
- package/dist/SubscriptionEntityServer.d.ts +10 -2
- package/dist/SubscriptionEntityServer.d.ts.map +1 -1
- package/dist/SubscriptionEntityServer.js +16 -1
- package/dist/SubscriptionEntityServer.js.map +1 -1
- package/dist/index.d.ts +9 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +7 -1
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
|
@@ -0,0 +1,396 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a subscription renews at, and onto what — shared by the renewal pass and the Renewals page.
|
|
3
|
+
*
|
|
4
|
+
* `Orders.SpawnRenewals` used to carry this code privately. The Renewals page needs the same answer
|
|
5
|
+
* for a subscription the pass is not yet due to place — "what will this renew at, and why?" — so a
|
|
6
|
+
* person can check the figure months ahead rather than the morning the order books. Moving the
|
|
7
|
+
* pricing here is what lets both read one rule; a second implementation in a worklist operation
|
|
8
|
+
* would be the one that drifted, and the drift would show up as a renewal priced differently from
|
|
9
|
+
* its preview.
|
|
10
|
+
*
|
|
11
|
+
* The rule itself is `PriceRenewal` in orders-entities (golive #304): the prior term's undiscounted
|
|
12
|
+
* price, or a successor's list price, plus the annual increase when the new term crosses an
|
|
13
|
+
* anniversary. This module gathers that rule's inputs: the line that bought the expiring term, the
|
|
14
|
+
* successor chain, the increase ladder (subscription → product → category → company), the list
|
|
15
|
+
* prices the rule needs, and the term the renewal would buy. The increase ladder, the successor
|
|
16
|
+
* chain and the type rules are cached per instance, because a worklist prices hundreds of rows and
|
|
17
|
+
* most of them share a product.
|
|
18
|
+
*
|
|
19
|
+
* NOTHING HERE WRITES. A pricer is read-only by construction, so a preview, a worklist and the live
|
|
20
|
+
* pass all run the same code and only the pass goes on to place an order.
|
|
21
|
+
*
|
|
22
|
+
* @module @mj-biz-apps/orders-core-entities-server
|
|
23
|
+
*/
|
|
24
|
+
import { LogError, RunView } from '@memberjunction/core';
|
|
25
|
+
import { AsDateValue, BuildLinePriceContext, LoadOrdersEngine, OrdersEngine, PriceRenewal, PriorTermShare, RenewalCrossesAnniversary, ResolvePrice, ResolveRenewalIncrease, } from '@mj-biz-apps/orders-entities';
|
|
26
|
+
import { RequireUUID } from './sql-guards.js';
|
|
27
|
+
import { MultiTermLineMonths, ResolveSubscriptionTypeID, SubscriptionBehaviorFor, SubscriptionTypeRulesFrom, } from './SubscriptionBehavior.js';
|
|
28
|
+
import { SelectRenewalPromotionCodes } from './RenewalPromotions.js';
|
|
29
|
+
const ORDER_LINE_ENTITY = 'MJ_BizApps_Orders: Order Lines';
|
|
30
|
+
const ORDER_COMPANY_POLICY_ENTITY = 'MJ_BizApps_Orders: Order Company Policies';
|
|
31
|
+
/**
|
|
32
|
+
* Why a successor chain cannot be renewed onto, or null when it can (#869).
|
|
33
|
+
*
|
|
34
|
+
* A successor that resolves no subscription type would book the renewal as a plain line with no
|
|
35
|
+
* term, and every later pass would then skip the subscription as already renewed. So the candidate
|
|
36
|
+
* is skipped with this reason instead. The subscription's own product (the first entry) is not
|
|
37
|
+
* checked: it already holds the subscription.
|
|
38
|
+
*/
|
|
39
|
+
export function NonSubscriptionSuccessorRefusal(chain) {
|
|
40
|
+
for (let i = 1; i < chain.length; i++) {
|
|
41
|
+
if (chain[i].SubscriptionTypeID)
|
|
42
|
+
continue;
|
|
43
|
+
return (`${chain[i - 1].Name} names ${chain[i].Name} as its successor, but ${chain[i].Name} is not a subscription ` +
|
|
44
|
+
`product (neither it nor its product type has a subscription type), so the renewal cannot be placed on it. ` +
|
|
45
|
+
`Give ${chain[i].Name} a subscription type or change the successor.`);
|
|
46
|
+
}
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
/** The day after a `YYYY-MM-DD` (or date-like) value, as a UTC-midnight `Date`: where the renewal term starts. */
|
|
50
|
+
export function DayAfter(date) {
|
|
51
|
+
const d = new Date(date);
|
|
52
|
+
return new Date(Date.UTC(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate() + 1));
|
|
53
|
+
}
|
|
54
|
+
/** Lead days in force for a subject: its own, else its type's, else 0. */
|
|
55
|
+
export function EffectiveLeadDays(subject) {
|
|
56
|
+
return subject.SubscriptionRenewalLeadDays ?? subject.TypeRenewalLeadDays ?? 0;
|
|
57
|
+
}
|
|
58
|
+
/** Column list the pricer needs from an order line; shared so a bulk reader returns the same shape. */
|
|
59
|
+
export const RENEWAL_SOURCE_LINE_FIELDS = [
|
|
60
|
+
'ID',
|
|
61
|
+
'OrderHeaderID',
|
|
62
|
+
'ProductID',
|
|
63
|
+
'Quantity',
|
|
64
|
+
'UnitPrice',
|
|
65
|
+
'DiscountPct',
|
|
66
|
+
'PriceOverridden',
|
|
67
|
+
'ProductPriceID',
|
|
68
|
+
'ServicePeriodStart',
|
|
69
|
+
'ServicePeriodEnd',
|
|
70
|
+
];
|
|
71
|
+
/**
|
|
72
|
+
* Prices renewals for one provider and user, caching what rows share.
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* ```typescript
|
|
76
|
+
* const pricer = new RenewalPricer(provider, user);
|
|
77
|
+
* const priced = await pricer.Price(subject); // throws with a reason a person can act on
|
|
78
|
+
* priced.Price.UnitPrice; priced.Term?.Start;
|
|
79
|
+
* ```
|
|
80
|
+
*/
|
|
81
|
+
export class RenewalPricer {
|
|
82
|
+
constructor(provider, user) {
|
|
83
|
+
this.provider = provider;
|
|
84
|
+
this.user = user;
|
|
85
|
+
this.chains = new Map();
|
|
86
|
+
this.categoryIncreases = new Map();
|
|
87
|
+
this.companyIncreases = new Map();
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* What the renewal line is written with: product, quantity and price (golive #304).
|
|
91
|
+
*
|
|
92
|
+
* Read-only, so a preview and a worklist run it too. Throws with a reason a person can act on
|
|
93
|
+
* when the renewal cannot be priced; the caller reports that reason on the row.
|
|
94
|
+
*/
|
|
95
|
+
async Price(due, options = {}) {
|
|
96
|
+
const source = options.SourceLine === undefined ? await this.LoadSourceLine(due.OrderLineID) : options.SourceLine;
|
|
97
|
+
if (!source) {
|
|
98
|
+
throw new Error(`The order line that bought term ${due.TermNumber} (${due.OrderLineID}) no longer exists, ` +
|
|
99
|
+
`so there is no price to renew at.`);
|
|
100
|
+
}
|
|
101
|
+
const chain = await this.loadSuccessorChain(due.ProductID);
|
|
102
|
+
const notSubscription = NonSubscriptionSuccessorRefusal(chain);
|
|
103
|
+
if (notSubscription)
|
|
104
|
+
throw new Error(notSubscription);
|
|
105
|
+
const product = chain[chain.length - 1];
|
|
106
|
+
const movesToSuccessor = chain.length > 1;
|
|
107
|
+
const quantity = Math.max(1, Number(due.TermQuantity));
|
|
108
|
+
const renewalDay = DayAfter(due.TermEndDate);
|
|
109
|
+
const carry = !!due.CarryDiscountOnRenewal;
|
|
110
|
+
await LoadOrdersEngine(this.provider, this.user);
|
|
111
|
+
const priorLineMonths = this.priorLineMonths(due, source);
|
|
112
|
+
const share = priorLineMonths == null ? 1 : PriorTermShare(priorLineMonths, this.termMonthsOf(due));
|
|
113
|
+
// A list price is resolved only where the rule needs one: the successor's as of the renewal
|
|
114
|
+
// day, or the prior product's as of the prior order when a price typed below it may lapse.
|
|
115
|
+
const successorList = movesToSuccessor
|
|
116
|
+
? await this.listPrice({ ...source, ProductID: product.ID, Quantity: quantity, UnitPrice: null, ProductPriceID: null }, renewalDay)
|
|
117
|
+
: undefined;
|
|
118
|
+
const priorList = !movesToSuccessor && source.PriceOverridden && !carry ? await this.listPrice({ ...source, Quantity: quantity }, null) : null;
|
|
119
|
+
const increase = ResolveRenewalIncrease({
|
|
120
|
+
Subscription: due.SubscriptionIncreasePercent,
|
|
121
|
+
Product: product.RenewalIncreasePercent,
|
|
122
|
+
Categories: await this.loadCategoryIncreases(product.ProductCategoryID),
|
|
123
|
+
Company: await this.loadCompanyIncrease(due.CompanyID),
|
|
124
|
+
});
|
|
125
|
+
let price;
|
|
126
|
+
try {
|
|
127
|
+
price = PriceRenewal({
|
|
128
|
+
PriorUnitPrice: Number(source.UnitPrice),
|
|
129
|
+
PriorTermShare: share,
|
|
130
|
+
PriorDiscountPct: source.DiscountPct,
|
|
131
|
+
PriorPriceOverridden: !!source.PriceOverridden,
|
|
132
|
+
PriorListPrice: priorList,
|
|
133
|
+
SuccessorListPrice: successorList,
|
|
134
|
+
CarryDiscount: carry,
|
|
135
|
+
Increase: increase,
|
|
136
|
+
ApplyIncrease: RenewalCrossesAnniversary(due.SubscriptionStartDate, due.TermStartDate, renewalDay),
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
catch (err) {
|
|
140
|
+
throw new Error(`${due.SubscriptionNumber} moves to ${product.Name} at renewal, but ` +
|
|
141
|
+
`${err instanceof Error ? err.message : String(err)}. Add a price for ${product.Name}.`);
|
|
142
|
+
}
|
|
143
|
+
return {
|
|
144
|
+
ProductID: product.ID,
|
|
145
|
+
ProductName: product.Name,
|
|
146
|
+
MovesToSuccessor: movesToSuccessor,
|
|
147
|
+
Quantity: quantity,
|
|
148
|
+
Price: price,
|
|
149
|
+
// Only a typed price is cut (see `PriceRenewal`), so only that one is reported as a share.
|
|
150
|
+
PriorLineMonths: share < 1 && source.PriceOverridden ? priorLineMonths : null,
|
|
151
|
+
Term: this.RenewalTerm(due, product),
|
|
152
|
+
PromotionCodes: options.IncludePromotions === false ? [] : await this.loadRenewalPromotionCodes(source),
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
/** The line that bought the expiring term, or null when it is gone. */
|
|
156
|
+
async LoadSourceLine(orderLineID) {
|
|
157
|
+
const lines = await this.LoadSourceLines([orderLineID]);
|
|
158
|
+
return lines.get(orderLineID.toLowerCase()) ?? null;
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Several source lines in one read, keyed by lower-cased ID. A worklist prices hundreds of rows;
|
|
162
|
+
* one query per row would be the N+1 the architecture doc warns about.
|
|
163
|
+
*/
|
|
164
|
+
async LoadSourceLines(orderLineIDs) {
|
|
165
|
+
const out = new Map();
|
|
166
|
+
const ids = [...new Set(orderLineIDs.map((id) => RequireUUID(String(id), 'OrderLineID').toLowerCase()))];
|
|
167
|
+
const rv = new RunView(this.provider);
|
|
168
|
+
for (let i = 0; i < ids.length; i += 500) {
|
|
169
|
+
const chunk = ids.slice(i, i + 500);
|
|
170
|
+
const result = await rv.RunView({
|
|
171
|
+
EntityName: ORDER_LINE_ENTITY,
|
|
172
|
+
ExtraFilter: `ID IN (${chunk.map((id) => `'${id}'`).join(',')})`,
|
|
173
|
+
Fields: [...RENEWAL_SOURCE_LINE_FIELDS],
|
|
174
|
+
ResultType: 'simple',
|
|
175
|
+
BypassCache: true,
|
|
176
|
+
}, this.user);
|
|
177
|
+
for (const row of result?.Results ?? [])
|
|
178
|
+
out.set(String(row.ID).toLowerCase(), row);
|
|
179
|
+
}
|
|
180
|
+
return out;
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* The term the renewal buys, decided the way the confirm decides it: the renewal product's
|
|
184
|
+
* subscription type and behaviour, continuing the subscription from the day after its latest term.
|
|
185
|
+
* Null when that cannot be decided here (no type, an unregistered driver, a rejection); the
|
|
186
|
+
* confirm then reports the reason itself.
|
|
187
|
+
*/
|
|
188
|
+
RenewalTerm(due, product) {
|
|
189
|
+
const typeID = ResolveSubscriptionTypeID(product.SubscriptionTypeID, OrdersEngine.Instance.ProductTypeByID(product.ProductTypeID)?.DefaultSubscriptionTypeID ?? null);
|
|
190
|
+
const rules = this.rulesFor(typeID);
|
|
191
|
+
if (!rules)
|
|
192
|
+
return null;
|
|
193
|
+
try {
|
|
194
|
+
const holder = { OrganizationID: due.HolderOrganizationID ?? null, PersonID: due.BeneficiaryPersonID ?? null };
|
|
195
|
+
const decision = SubscriptionBehaviorFor(rules).Decide({
|
|
196
|
+
Rules: rules,
|
|
197
|
+
PurchaseDate: DayAfter(due.TermEndDate),
|
|
198
|
+
Amount: 0,
|
|
199
|
+
Existing: {
|
|
200
|
+
ID: due.SubscriptionID,
|
|
201
|
+
Status: due.SubscriptionStatus,
|
|
202
|
+
HolderOrganizationID: holder.OrganizationID,
|
|
203
|
+
BeneficiaryPersonID: holder.PersonID,
|
|
204
|
+
LatestTermEnd: new Date(due.TermEndDate),
|
|
205
|
+
LatestTermNumber: due.TermNumber,
|
|
206
|
+
},
|
|
207
|
+
Subscriber: holder,
|
|
208
|
+
IsRenewal: true,
|
|
209
|
+
});
|
|
210
|
+
const term = decision.Term;
|
|
211
|
+
if (!term)
|
|
212
|
+
return null;
|
|
213
|
+
return { Start: term.StartDate.toISOString().slice(0, 10), End: term.EndDate.toISOString().slice(0, 10) };
|
|
214
|
+
}
|
|
215
|
+
catch (err) {
|
|
216
|
+
LogError(`Renewal pricing: ${due.SubscriptionNumber} renewal term not decided: ${err instanceof Error ? err.message : String(err)}`);
|
|
217
|
+
return null;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* The months the source line's price covers when that line bought earlier terms too, or null
|
|
222
|
+
* (golive #338). Read from the line's service period, which the confirm sets to the whole coverage.
|
|
223
|
+
*/
|
|
224
|
+
priorLineMonths(due, source) {
|
|
225
|
+
const day = (v) => (v ? new Date(`${new Date(v).toISOString().slice(0, 10)}T00:00:00Z`) : null);
|
|
226
|
+
return MultiTermLineMonths(day(source.ServicePeriodStart), day(source.ServicePeriodEnd), day(due.TermStartDate));
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* The term length of the subscription's own type: the length the confirm split the source line
|
|
230
|
+
* by. Throws when the type cannot be read, since renewing at the whole line's price would bill
|
|
231
|
+
* the customer for every term the line already paid for.
|
|
232
|
+
*/
|
|
233
|
+
termMonthsOf(due) {
|
|
234
|
+
const rules = this.rulesFor(due.SubscriptionTypeID);
|
|
235
|
+
if (!rules) {
|
|
236
|
+
throw new Error(`${due.SubscriptionNumber}'s subscription type (${due.SubscriptionTypeID}) was not found, so the share ` +
|
|
237
|
+
`of its multi-term line one renewal term is worth cannot be worked out.`);
|
|
238
|
+
}
|
|
239
|
+
return SubscriptionBehaviorFor(rules).TermMonths(rules);
|
|
240
|
+
}
|
|
241
|
+
/** A subscription type's rules from the engine cache, or null when the type is not there. */
|
|
242
|
+
rulesFor(subscriptionTypeID) {
|
|
243
|
+
const cached = OrdersEngine.Instance.SubscriptionTypeByID(subscriptionTypeID);
|
|
244
|
+
return cached ? SubscriptionTypeRulesFrom(cached) : null;
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* The codes the renewal presents: those recorded on the line that bought the expiring term whose
|
|
248
|
+
* promotion carries into renewals (#289). A promotion's adjustment rows always name a line; an
|
|
249
|
+
* order-level promotion writes one per line, so the source line's own rows are complete.
|
|
250
|
+
*/
|
|
251
|
+
async loadRenewalPromotionCodes(source) {
|
|
252
|
+
const orderID = RequireUUID(String(source.OrderHeaderID), 'OrderHeaderID');
|
|
253
|
+
const lineID = RequireUUID(String(source.ID), 'OrderLineID');
|
|
254
|
+
const rows = (await this.sql(`
|
|
255
|
+
SELECT pc.ID AS PromotionCodeID, pc.Code, p.AppliesToRenewals, MIN(oa.Sequence) AS FirstSequence
|
|
256
|
+
FROM __mj_BizAppsOrders.OrderAdjustment oa
|
|
257
|
+
JOIN __mj_BizAppsOrders.PromotionCode pc ON pc.ID = oa.PromotionCodeID
|
|
258
|
+
JOIN __mj_BizAppsOrders.Promotion p ON p.ID = pc.PromotionID
|
|
259
|
+
WHERE oa.OrderHeaderID = '${orderID}'
|
|
260
|
+
AND oa.OrderLineID = '${lineID}'
|
|
261
|
+
GROUP BY pc.ID, pc.Code, p.AppliesToRenewals
|
|
262
|
+
ORDER BY MIN(oa.Sequence), pc.Code
|
|
263
|
+
`));
|
|
264
|
+
if (!Array.isArray(rows))
|
|
265
|
+
return [];
|
|
266
|
+
const keep = new Set(SelectRenewalPromotionCodes(rows.map((r) => ({ Code: r.Code, AppliesToRenewals: !!r.AppliesToRenewals }))).map((c) => c.toLowerCase()));
|
|
267
|
+
const out = [];
|
|
268
|
+
for (const r of rows) {
|
|
269
|
+
if (!keep.delete(String(r.Code).trim().toLowerCase()))
|
|
270
|
+
continue;
|
|
271
|
+
out.push({ PromotionCodeID: String(r.PromotionCodeID), Code: String(r.Code).trim(), AppliesToRenewals: true });
|
|
272
|
+
}
|
|
273
|
+
return out;
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* The subscription's product followed along `SuccessorProductID` while the next product is
|
|
277
|
+
* Active: a successor that is still a draft, or already retired, is not one to renew onto.
|
|
278
|
+
* The subscription's own product comes first; the last entry is what the renewal is placed on.
|
|
279
|
+
* Cached per product for the life of the pricer.
|
|
280
|
+
*/
|
|
281
|
+
loadSuccessorChain(productID) {
|
|
282
|
+
const key = RequireUUID(String(productID), 'ProductID').toLowerCase();
|
|
283
|
+
let pending = this.chains.get(key);
|
|
284
|
+
if (!pending) {
|
|
285
|
+
pending = this.readSuccessorChain(key);
|
|
286
|
+
this.chains.set(key, pending);
|
|
287
|
+
// A read that failed is forgotten, so one transient error does not fail every later row on this product.
|
|
288
|
+
pending.catch(() => this.chains.delete(key));
|
|
289
|
+
}
|
|
290
|
+
return pending;
|
|
291
|
+
}
|
|
292
|
+
async readSuccessorChain(productID) {
|
|
293
|
+
const rows = (await this.sql(`
|
|
294
|
+
WITH chain AS (
|
|
295
|
+
SELECT p.ID, p.Name, p.Status, p.ProductCategoryID, p.RenewalIncreasePercent, p.SuccessorProductID,
|
|
296
|
+
p.SubscriptionTypeID, p.ProductTypeID, 0 AS Depth
|
|
297
|
+
FROM __mj_BizAppsOrders.Product p
|
|
298
|
+
WHERE p.ID = '${productID}'
|
|
299
|
+
UNION ALL
|
|
300
|
+
SELECT n.ID, n.Name, n.Status, n.ProductCategoryID, n.RenewalIncreasePercent, n.SuccessorProductID,
|
|
301
|
+
n.SubscriptionTypeID, n.ProductTypeID, c.Depth + 1
|
|
302
|
+
FROM chain c
|
|
303
|
+
JOIN __mj_BizAppsOrders.Product n ON n.ID = c.SuccessorProductID
|
|
304
|
+
WHERE c.Depth < 20 AND n.Status = 'Active'
|
|
305
|
+
)
|
|
306
|
+
SELECT c.ID, c.Name, c.Status, c.ProductCategoryID, c.RenewalIncreasePercent, c.SuccessorProductID,
|
|
307
|
+
COALESCE(p.SubscriptionTypeID, pt.DefaultSubscriptionTypeID) AS SubscriptionTypeID, c.ProductTypeID
|
|
308
|
+
FROM chain c
|
|
309
|
+
JOIN __mj_BizAppsOrders.Product p ON p.ID = c.ID
|
|
310
|
+
LEFT JOIN __mj_BizAppsOrders.ProductType pt ON pt.ID = p.ProductTypeID
|
|
311
|
+
ORDER BY c.Depth
|
|
312
|
+
`));
|
|
313
|
+
if (!Array.isArray(rows) || rows.length === 0) {
|
|
314
|
+
throw new Error(`The subscription's product (${productID}) no longer exists, so there is nothing to renew.`);
|
|
315
|
+
}
|
|
316
|
+
// Successors that loop back would otherwise walk to the depth cap; stop at the first repeat.
|
|
317
|
+
const seen = new Set();
|
|
318
|
+
const chain = [];
|
|
319
|
+
for (const row of rows) {
|
|
320
|
+
const key = String(row.ID).toLowerCase();
|
|
321
|
+
if (seen.has(key))
|
|
322
|
+
break;
|
|
323
|
+
seen.add(key);
|
|
324
|
+
chain.push(row);
|
|
325
|
+
}
|
|
326
|
+
return chain;
|
|
327
|
+
}
|
|
328
|
+
/** `RenewalIncreasePercent` of a category and each ancestor, the category itself first. Cached. */
|
|
329
|
+
loadCategoryIncreases(categoryID) {
|
|
330
|
+
if (!categoryID)
|
|
331
|
+
return Promise.resolve([]);
|
|
332
|
+
const key = RequireUUID(String(categoryID), 'ProductCategoryID').toLowerCase();
|
|
333
|
+
let pending = this.categoryIncreases.get(key);
|
|
334
|
+
if (!pending) {
|
|
335
|
+
pending = (async () => {
|
|
336
|
+
const rows = (await this.sql(`
|
|
337
|
+
WITH chain AS (
|
|
338
|
+
SELECT c.ID, c.ParentProductCategoryID, c.RenewalIncreasePercent, 0 AS Depth
|
|
339
|
+
FROM __mj_BizAppsOrders.ProductCategory c
|
|
340
|
+
WHERE c.ID = '${key}'
|
|
341
|
+
UNION ALL
|
|
342
|
+
SELECT p.ID, p.ParentProductCategoryID, p.RenewalIncreasePercent, ch.Depth + 1
|
|
343
|
+
FROM chain ch
|
|
344
|
+
JOIN __mj_BizAppsOrders.ProductCategory p ON p.ID = ch.ParentProductCategoryID
|
|
345
|
+
WHERE ch.Depth < 20
|
|
346
|
+
)
|
|
347
|
+
SELECT RenewalIncreasePercent FROM chain ORDER BY Depth
|
|
348
|
+
`));
|
|
349
|
+
return Array.isArray(rows) ? rows.map((r) => (r.RenewalIncreasePercent == null ? null : Number(r.RenewalIncreasePercent))) : [];
|
|
350
|
+
})();
|
|
351
|
+
this.categoryIncreases.set(key, pending);
|
|
352
|
+
pending.catch(() => this.categoryIncreases.delete(key));
|
|
353
|
+
}
|
|
354
|
+
return pending;
|
|
355
|
+
}
|
|
356
|
+
/** The selling company's `OrderCompanyPolicy.RenewalIncreasePercent`; null when it has no policy row. Cached. */
|
|
357
|
+
loadCompanyIncrease(companyID) {
|
|
358
|
+
const key = RequireUUID(String(companyID), 'CompanyID').toLowerCase();
|
|
359
|
+
let pending = this.companyIncreases.get(key);
|
|
360
|
+
if (!pending) {
|
|
361
|
+
pending = (async () => {
|
|
362
|
+
const rv = new RunView(this.provider);
|
|
363
|
+
const result = await rv.RunView({
|
|
364
|
+
EntityName: ORDER_COMPANY_POLICY_ENTITY,
|
|
365
|
+
ExtraFilter: `ID='${key}'`,
|
|
366
|
+
Fields: ['RenewalIncreasePercent'],
|
|
367
|
+
ResultType: 'simple',
|
|
368
|
+
BypassCache: true,
|
|
369
|
+
}, this.user);
|
|
370
|
+
const value = result?.Results?.[0]?.RenewalIncreasePercent;
|
|
371
|
+
return value == null ? null : Number(value);
|
|
372
|
+
})();
|
|
373
|
+
this.companyIncreases.set(key, pending);
|
|
374
|
+
pending.catch(() => this.companyIncreases.delete(key));
|
|
375
|
+
}
|
|
376
|
+
return pending;
|
|
377
|
+
}
|
|
378
|
+
/**
|
|
379
|
+
* What the pricing engine charges for `line`'s product, for the customer on `line`'s order. As of
|
|
380
|
+
* that order's date, or as of `asOf` when given. Null when no rule prices the product.
|
|
381
|
+
*/
|
|
382
|
+
async listPrice(line, asOf) {
|
|
383
|
+
const ctx = await BuildLinePriceContext(line, this.provider, this.user);
|
|
384
|
+
if (!ctx)
|
|
385
|
+
return null;
|
|
386
|
+
if (asOf)
|
|
387
|
+
ctx.AsOf = AsDateValue(asOf) ?? ctx.AsOf;
|
|
388
|
+
const resolved = await ResolvePrice(ctx, this.provider, this.user);
|
|
389
|
+
return resolved ? Number(resolved.UnitPrice) : null;
|
|
390
|
+
}
|
|
391
|
+
sql(statement) {
|
|
392
|
+
const db = this.provider;
|
|
393
|
+
return db.ExecuteSQL(statement);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
//# sourceMappingURL=RenewalPricing.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"RenewalPricing.js","sourceRoot":"","sources":["../src/RenewalPricing.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAgE,MAAM,sBAAsB,CAAC;AACvH,OAAO,EACH,WAAW,EACX,qBAAqB,EACrB,gBAAgB,EAChB,YAAY,EACZ,YAAY,EACZ,cAAc,EACd,yBAAyB,EACzB,YAAY,EACZ,sBAAsB,GAGzB,MAAM,8BAA8B,CAAC;AACtC,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAC9C,OAAO,EACH,mBAAmB,EACnB,yBAAyB,EACzB,uBAAuB,EACvB,yBAAyB,GAE5B,MAAM,2BAA2B,CAAC;AACnC,OAAO,EAAE,2BAA2B,EAAE,MAAM,wBAAwB,CAAC;AAErE,MAAM,iBAAiB,GAAG,gCAAgC,CAAC;AAC3D,MAAM,2BAA2B,GAAG,2CAA2C,CAAC;AA8FhF;;;;;;;GAOG;AACH,MAAM,UAAU,+BAA+B,CAAC,KAA0E;IACtH,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QACpC,IAAI,KAAK,CAAC,CAAC,CAAC,CAAC,kBAAkB;Y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AAC,CAAC;QACxE,IAAI,CAAC,GAAG;YAAE,OAAO,IAAI,CAAC;QACtB,IAAI,IAAI;YAAE,GAAG,CAAC,IAAI,GAAG,WAAW,CAAC,IAAI,CAAC,IAAI,GAAG,CAAC,IAAI,CAAC;QACnD,MAAM,QAAQ,GAAG,MAAM,YAAY,CAAC,GAAG,EAAE,IAAI,CAAC,QAAQ,EAAE,IAAI,CAAC,IAAI,CAAC,CAAC;QACnE,OAAO,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;IACxD,CAAC;IAEO,GAAG,CAAC,SAAiB;QACzB,MAAM,EAAE,GAAG,IAAI,CAAC,QAAoE,CAAC;QACrF,OAAO,EAAE,CAAC,UAAU,CAAC,SAAS,CAAC,CAAC;IACpC,CAAC;CACJ"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview `Orders.SetRenewalSettings` — set auto-renew and the annual increase on chosen subscriptions.
|
|
3
|
+
*
|
|
4
|
+
* WHY AN OPERATION, FOR A TWO-COLUMN WRITE. The Renewals page lets a person tick subscriptions and
|
|
5
|
+
* flip auto-renew or give them a rate in one go. A browser saving each entity in turn would work,
|
|
6
|
+
* but every save is a round trip, a refused one lands mid-list with no account of the rest, and an
|
|
7
|
+
* API caller fixing a book of rates would have to reinvent the loop. Here the loop runs once, on
|
|
8
|
+
* the server, and comes back with one line per subscription saying what happened.
|
|
9
|
+
*
|
|
10
|
+
* IT WRITES SUBSCRIPTIONS AND NOTHING ELSE. Through the entity, so the server subclass's rules
|
|
11
|
+
* apply: a subscription a payment gateway bills refuses an auto-renew change (the gateway owns that
|
|
12
|
+
* switch), and that refusal comes back on the row rather than failing the batch. No order is
|
|
13
|
+
* touched: a renewal order already placed keeps its price, and the next one each subscription
|
|
14
|
+
* places follows the new settings.
|
|
15
|
+
*
|
|
16
|
+
* A FIELD LEFT OUT IS LEFT ALONE. `IncreasePercent: null` clears the rate so the subscription
|
|
17
|
+
* inherits; `0` is a negotiated "no increase" that overrides product, category and company. The
|
|
18
|
+
* three are different answers and the input keeps them apart.
|
|
19
|
+
*
|
|
20
|
+
* CONNECTS TO:
|
|
21
|
+
* CALLERS: the Renewals page (`renewals.page.ts`) · API callers
|
|
22
|
+
* WRITES: MJ_BizApps_Orders: Subscriptions, through SubscriptionEntityServer
|
|
23
|
+
*/
|
|
24
|
+
import { type IMetadataProvider, type UserInfo } from '@memberjunction/core';
|
|
25
|
+
import { OrdersSetRenewalSettingsOperation as OrdersSetRenewalSettingsOperationBase, type OrdersSetRenewalSettingsInput, type OrdersSetRenewalSettingsOutput } from '@mj-biz-apps/orders-entities';
|
|
26
|
+
/** Refuses a rate the column cannot hold or a percentage that makes no sense. Returns the rate as a number, or null. */
|
|
27
|
+
export declare function RequireIncreasePercent(value: unknown): number | null;
|
|
28
|
+
export declare class SetRenewalSettingsOperation extends OrdersSetRenewalSettingsOperationBase {
|
|
29
|
+
protected InternalExecute(input: OrdersSetRenewalSettingsInput, provider: IMetadataProvider, user: UserInfo): Promise<OrdersSetRenewalSettingsOutput>;
|
|
30
|
+
private setOne;
|
|
31
|
+
}
|
|
32
|
+
/** Tree-shaking anchor — call from the server bootstrap so @RegisterClass is retained. */
|
|
33
|
+
export declare function LoadSetRenewalSettingsOperation(): void;
|
|
34
|
+
//# sourceMappingURL=SetRenewalSettingsOperation.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SetRenewalSettingsOperation.d.ts","sourceRoot":"","sources":["../src/SetRenewalSettingsOperation.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,OAAO,EAAoC,KAAK,iBAAiB,EAAE,KAAK,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAE/G,OAAO,EACH,iCAAiC,IAAI,qCAAqC,EAC1E,KAAK,6BAA6B,EAClC,KAAK,8BAA8B,EAItC,MAAM,8BAA8B,CAAC;AAOtC,wHAAwH;AACxH,wBAAgB,sBAAsB,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,GAAG,IAAI,CAQpE;AAQD,qBACa,2BAA4B,SAAQ,qCAAqC;cAClE,eAAe,CAC3B,KAAK,EAAE,6BAA6B,EACpC,QAAQ,EAAE,iBAAiB,EAC3B,IAAI,EAAE,QAAQ,GACf,OAAO,CAAC,8BAA8B,CAAC;YAoC5B,MAAM;CA+BvB;AAED,0FAA0F;AAC1F,wBAAgB,+BAA+B,IAAI,IAAI,CAEtD"}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* @fileoverview `Orders.SetRenewalSettings` — set auto-renew and the annual increase on chosen subscriptions.
|
|
9
|
+
*
|
|
10
|
+
* WHY AN OPERATION, FOR A TWO-COLUMN WRITE. The Renewals page lets a person tick subscriptions and
|
|
11
|
+
* flip auto-renew or give them a rate in one go. A browser saving each entity in turn would work,
|
|
12
|
+
* but every save is a round trip, a refused one lands mid-list with no account of the rest, and an
|
|
13
|
+
* API caller fixing a book of rates would have to reinvent the loop. Here the loop runs once, on
|
|
14
|
+
* the server, and comes back with one line per subscription saying what happened.
|
|
15
|
+
*
|
|
16
|
+
* IT WRITES SUBSCRIPTIONS AND NOTHING ELSE. Through the entity, so the server subclass's rules
|
|
17
|
+
* apply: a subscription a payment gateway bills refuses an auto-renew change (the gateway owns that
|
|
18
|
+
* switch), and that refusal comes back on the row rather than failing the batch. No order is
|
|
19
|
+
* touched: a renewal order already placed keeps its price, and the next one each subscription
|
|
20
|
+
* places follows the new settings.
|
|
21
|
+
*
|
|
22
|
+
* A FIELD LEFT OUT IS LEFT ALONE. `IncreasePercent: null` clears the rate so the subscription
|
|
23
|
+
* inherits; `0` is a negotiated "no increase" that overrides product, category and company. The
|
|
24
|
+
* three are different answers and the input keeps them apart.
|
|
25
|
+
*
|
|
26
|
+
* CONNECTS TO:
|
|
27
|
+
* CALLERS: the Renewals page (`renewals.page.ts`) · API callers
|
|
28
|
+
* WRITES: MJ_BizApps_Orders: Subscriptions, through SubscriptionEntityServer
|
|
29
|
+
*/
|
|
30
|
+
import { BaseRemotableOperation, LogError } from '@memberjunction/core';
|
|
31
|
+
import { RegisterClass } from '@memberjunction/global';
|
|
32
|
+
import { OrdersSetRenewalSettingsOperation as OrdersSetRenewalSettingsOperationBase, } from '@mj-biz-apps/orders-entities';
|
|
33
|
+
import { RequireUUID } from './sql-guards.js';
|
|
34
|
+
const SUBSCRIPTION_ENTITY = 'MJ_BizApps_Orders: Subscriptions';
|
|
35
|
+
/** The column is decimal(9,4): four decimals, and a percentage, so 0 to 100. */
|
|
36
|
+
const MAX_DECIMALS = 4;
|
|
37
|
+
/** Refuses a rate the column cannot hold or a percentage that makes no sense. Returns the rate as a number, or null. */
|
|
38
|
+
export function RequireIncreasePercent(value) {
|
|
39
|
+
if (value === null || value === undefined)
|
|
40
|
+
return null;
|
|
41
|
+
const n = typeof value === 'number' ? value : Number(String(value).trim());
|
|
42
|
+
if (!Number.isFinite(n))
|
|
43
|
+
throw new Error(`IncreasePercent must be a number of percent (5 for five percent), or null to clear; got '${String(value)}'.`);
|
|
44
|
+
if (n < 0 || n > 100)
|
|
45
|
+
throw new Error(`IncreasePercent must be between 0 and 100; got ${n}.`);
|
|
46
|
+
// Compared after rounding back, not on the scaled product: 12.3456 * 10000 is 123455.99999999999 in floating point.
|
|
47
|
+
if (Math.round(n * 10 ** MAX_DECIMALS) / 10 ** MAX_DECIMALS !== n)
|
|
48
|
+
throw new Error(`IncreasePercent takes at most ${MAX_DECIMALS} decimals; got ${n}.`);
|
|
49
|
+
return n;
|
|
50
|
+
}
|
|
51
|
+
let SetRenewalSettingsOperation = class SetRenewalSettingsOperation extends OrdersSetRenewalSettingsOperationBase {
|
|
52
|
+
async InternalExecute(input, provider, user) {
|
|
53
|
+
let ids;
|
|
54
|
+
const wanted = {};
|
|
55
|
+
try {
|
|
56
|
+
if (!Array.isArray(input?.SubscriptionIDs) || !input.SubscriptionIDs.length)
|
|
57
|
+
throw new Error('SubscriptionIDs must name at least one subscription.');
|
|
58
|
+
ids = [...new Set(input.SubscriptionIDs.map((id) => RequireUUID(id, 'SubscriptionID').toLowerCase()))];
|
|
59
|
+
if (input.AutoRenew !== undefined) {
|
|
60
|
+
if (typeof input.AutoRenew !== 'boolean')
|
|
61
|
+
throw new Error(`AutoRenew must be true or false; got '${String(input.AutoRenew)}'.`);
|
|
62
|
+
wanted.AutoRenew = input.AutoRenew;
|
|
63
|
+
}
|
|
64
|
+
if (input.IncreasePercent !== undefined)
|
|
65
|
+
wanted.IncreasePercent = RequireIncreasePercent(input.IncreasePercent);
|
|
66
|
+
if (wanted.AutoRenew === undefined && wanted.IncreasePercent === undefined)
|
|
67
|
+
throw new Error('Nothing to set: give AutoRenew, IncreasePercent, or both.');
|
|
68
|
+
}
|
|
69
|
+
catch (err) {
|
|
70
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
71
|
+
return { Success: false, Message: message, Updated: 0, Unchanged: 0, Failed: 0, Changes: [] };
|
|
72
|
+
}
|
|
73
|
+
const changes = [];
|
|
74
|
+
for (const id of ids) {
|
|
75
|
+
changes.push(await this.setOne(provider, user, id, wanted));
|
|
76
|
+
}
|
|
77
|
+
const updated = changes.filter((c) => c.Changed).length;
|
|
78
|
+
const failed = changes.filter((c) => c.Error).length;
|
|
79
|
+
const unchanged = changes.length - updated - failed;
|
|
80
|
+
const what = [];
|
|
81
|
+
if (wanted.AutoRenew !== undefined)
|
|
82
|
+
what.push(`auto-renew ${wanted.AutoRenew ? 'on' : 'off'}`);
|
|
83
|
+
if (wanted.IncreasePercent !== undefined)
|
|
84
|
+
what.push(`renewal increase ${wanted.IncreasePercent == null ? 'cleared (inherits)' : `${wanted.IncreasePercent}%`}`);
|
|
85
|
+
const head = what.join(', ');
|
|
86
|
+
const message = `${head.charAt(0).toUpperCase()}${head.slice(1)}: ${updated} subscription${updated === 1 ? '' : 's'} updated` +
|
|
87
|
+
`${unchanged ? `, ${unchanged} already there` : ''}${failed ? `, ${failed} failed` : ''}.`;
|
|
88
|
+
// Success is "the call did its job", so one refused row does not fail the batch; every row
|
|
89
|
+
// failing does, because then nothing a person asked for happened.
|
|
90
|
+
return { Success: failed < changes.length, Message: message, Updated: updated, Unchanged: unchanged, Failed: failed, Changes: changes };
|
|
91
|
+
}
|
|
92
|
+
async setOne(provider, user, id, wanted) {
|
|
93
|
+
const nothing = { AutoRenew: null, IncreasePercent: null };
|
|
94
|
+
try {
|
|
95
|
+
const sub = await provider.GetEntityObject(SUBSCRIPTION_ENTITY, user);
|
|
96
|
+
if (!(await sub.Load(id))) {
|
|
97
|
+
return { SubscriptionID: id, SubscriptionNumber: null, Previous: nothing, Current: nothing, Changed: false, Error: 'no such subscription' };
|
|
98
|
+
}
|
|
99
|
+
const previous = {
|
|
100
|
+
AutoRenew: !!sub.AutoRenew,
|
|
101
|
+
IncreasePercent: sub.RenewalIncreasePercent == null ? null : Number(sub.RenewalIncreasePercent),
|
|
102
|
+
};
|
|
103
|
+
const target = {
|
|
104
|
+
AutoRenew: wanted.AutoRenew === undefined ? previous.AutoRenew : wanted.AutoRenew,
|
|
105
|
+
IncreasePercent: wanted.IncreasePercent === undefined ? previous.IncreasePercent : wanted.IncreasePercent,
|
|
106
|
+
};
|
|
107
|
+
if (target.AutoRenew === previous.AutoRenew && target.IncreasePercent === previous.IncreasePercent) {
|
|
108
|
+
return { SubscriptionID: id, SubscriptionNumber: sub.SubscriptionNumber, Previous: previous, Current: previous, Changed: false };
|
|
109
|
+
}
|
|
110
|
+
if (wanted.AutoRenew !== undefined)
|
|
111
|
+
sub.AutoRenew = wanted.AutoRenew;
|
|
112
|
+
if (wanted.IncreasePercent !== undefined)
|
|
113
|
+
sub.RenewalIncreasePercent = wanted.IncreasePercent;
|
|
114
|
+
if (!(await sub.Save())) {
|
|
115
|
+
const reason = sub.LatestResult?.CompleteMessage ?? 'the save was refused';
|
|
116
|
+
return { SubscriptionID: id, SubscriptionNumber: sub.SubscriptionNumber, Previous: previous, Current: previous, Changed: false, Error: reason };
|
|
117
|
+
}
|
|
118
|
+
return { SubscriptionID: id, SubscriptionNumber: sub.SubscriptionNumber, Previous: previous, Current: target, Changed: true };
|
|
119
|
+
}
|
|
120
|
+
catch (err) {
|
|
121
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
122
|
+
LogError(`Orders.SetRenewalSettings: subscription ${id} could not be updated: ${message}`);
|
|
123
|
+
return { SubscriptionID: id, SubscriptionNumber: null, Previous: nothing, Current: nothing, Changed: false, Error: message };
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
SetRenewalSettingsOperation = __decorate([
|
|
128
|
+
RegisterClass(BaseRemotableOperation, 'Orders.SetRenewalSettings')
|
|
129
|
+
], SetRenewalSettingsOperation);
|
|
130
|
+
export { SetRenewalSettingsOperation };
|
|
131
|
+
/** Tree-shaking anchor — call from the server bootstrap so @RegisterClass is retained. */
|
|
132
|
+
export function LoadSetRenewalSettingsOperation() {
|
|
133
|
+
// intentionally empty
|
|
134
|
+
}
|
|
135
|
+
//# sourceMappingURL=SetRenewalSettingsOperation.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SetRenewalSettingsOperation.js","sourceRoot":"","sources":["../src/SetRenewalSettingsOperation.ts"],"names":[],"mappings":";;;;;;AAAA;;;;;;;;;;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