@yoryoboy/bi-mcp 1.5.3 → 1.7.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/.tsbuildinfo +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/mcp-use.json +2 -2
- package/dist/scripts/decrypt-secret.js +36 -0
- package/dist/scripts/decrypt-secret.js.map +7 -0
- package/dist/src/services/mercadolibre/mercadolibre-orders.js +19 -5
- package/dist/src/services/mercadolibre/mercadolibre-orders.js.map +2 -2
- package/dist/src/tools/mercadolibre/get-orders-summary.js +1011 -41
- package/dist/src/tools/mercadolibre/get-orders-summary.js.map +2 -2
- package/dist/src/tools/mercadolibre/helpers.js +13 -0
- package/dist/src/tools/mercadolibre/helpers.js.map +2 -2
- package/package.json +4 -3
|
@@ -5,7 +5,10 @@ import {
|
|
|
5
5
|
formatMercadoLibreError,
|
|
6
6
|
normalizeMercadoLibrePaging
|
|
7
7
|
} from "../../services/mercadolibre/mercadolibre-api.js";
|
|
8
|
-
import {
|
|
8
|
+
import {
|
|
9
|
+
searchMercadoLibreOrders,
|
|
10
|
+
searchMercadoLibreOrdersBatch
|
|
11
|
+
} from "../../services/mercadolibre/mercadolibre-orders.js";
|
|
9
12
|
import { stripNulls } from "../../utils/strip-payload.js";
|
|
10
13
|
import {
|
|
11
14
|
asArray,
|
|
@@ -13,13 +16,922 @@ import {
|
|
|
13
16
|
compactDateTime,
|
|
14
17
|
currencyBucket,
|
|
15
18
|
mercadoLibreDateRegex,
|
|
19
|
+
normalizeScalarString,
|
|
16
20
|
normalizeString,
|
|
17
21
|
roundMoney,
|
|
18
22
|
toNumber
|
|
19
23
|
} from "./helpers.js";
|
|
20
24
|
import { resolveMercadoLibreProfileOrSelection } from "./profile-resolution.js";
|
|
21
25
|
import { mercadolibreProfileIdSchemaField } from "./write-helpers.js";
|
|
22
|
-
const
|
|
26
|
+
const PAGE_FETCH_CONCURRENCY = 15;
|
|
27
|
+
const PAGE_FETCH_MAX_RETRIES = 2;
|
|
28
|
+
const ORDER_DETAIL_MAX_PAGES_PER_CALL = 15;
|
|
29
|
+
const DEFAULT_ORDER_DETAIL_PAGE_LIMIT = 50;
|
|
30
|
+
const TOP_LIMIT = 10;
|
|
31
|
+
const ordersSchema = [
|
|
32
|
+
"id",
|
|
33
|
+
"date_created",
|
|
34
|
+
"status",
|
|
35
|
+
"total_amount",
|
|
36
|
+
"currency",
|
|
37
|
+
"buyer",
|
|
38
|
+
"shipping_status",
|
|
39
|
+
"items",
|
|
40
|
+
"date_closed",
|
|
41
|
+
"last_updated",
|
|
42
|
+
"paid_amount",
|
|
43
|
+
"buyer_id",
|
|
44
|
+
"seller",
|
|
45
|
+
"seller_id",
|
|
46
|
+
"shipping_id",
|
|
47
|
+
"pack_id",
|
|
48
|
+
"fulfilled",
|
|
49
|
+
"tags",
|
|
50
|
+
"channel",
|
|
51
|
+
"site",
|
|
52
|
+
"payment_id",
|
|
53
|
+
"payment_status",
|
|
54
|
+
"payment_status_detail",
|
|
55
|
+
"payment_method",
|
|
56
|
+
"payment_type",
|
|
57
|
+
"payment_total_paid",
|
|
58
|
+
"payment_transaction_amount",
|
|
59
|
+
"installments",
|
|
60
|
+
"coupon_amount",
|
|
61
|
+
"available_actions",
|
|
62
|
+
"feedback_buyer",
|
|
63
|
+
"feedback_seller",
|
|
64
|
+
"order_request_change",
|
|
65
|
+
"order_request_return",
|
|
66
|
+
"first_item_id",
|
|
67
|
+
"first_item_title",
|
|
68
|
+
"first_item_sku",
|
|
69
|
+
"first_item_quantity",
|
|
70
|
+
"first_item_unit_price",
|
|
71
|
+
"first_item_sale_fee",
|
|
72
|
+
"first_item_listing_type",
|
|
73
|
+
"variation_attributes",
|
|
74
|
+
"stock_node_id"
|
|
75
|
+
];
|
|
76
|
+
function compactStringList(values, separator = " | ") {
|
|
77
|
+
return asArray(values).map((value) => normalizeScalarString(value).trim()).filter(Boolean).join(separator);
|
|
78
|
+
}
|
|
79
|
+
function compactNestedValue(value) {
|
|
80
|
+
if (value == null) {
|
|
81
|
+
return "";
|
|
82
|
+
}
|
|
83
|
+
const scalar = normalizeScalarString(value);
|
|
84
|
+
if (scalar) {
|
|
85
|
+
return scalar;
|
|
86
|
+
}
|
|
87
|
+
if (Array.isArray(value)) {
|
|
88
|
+
return value.map((entry) => compactNestedValue(entry)).filter(Boolean).join(" | ");
|
|
89
|
+
}
|
|
90
|
+
const record = asRecord(value);
|
|
91
|
+
return Object.entries(record).map(([key, entryValue]) => {
|
|
92
|
+
const normalized = compactNestedValue(entryValue);
|
|
93
|
+
return normalized ? `${key}:${normalized}` : "";
|
|
94
|
+
}).filter(Boolean).join(" | ");
|
|
95
|
+
}
|
|
96
|
+
function compactVariationAttributes(value) {
|
|
97
|
+
return asArray(value).map((attribute) => {
|
|
98
|
+
const name = normalizeString(attribute.name) || normalizeString(attribute.id);
|
|
99
|
+
const normalizedValue = normalizeString(attribute.value_name) || normalizeString(attribute.value_id) || normalizeScalarString(attribute.value);
|
|
100
|
+
if (!name || !normalizedValue) {
|
|
101
|
+
return "";
|
|
102
|
+
}
|
|
103
|
+
return `${name}: ${normalizedValue}`;
|
|
104
|
+
}).filter(Boolean).join(" | ");
|
|
105
|
+
}
|
|
106
|
+
function createMetricsBucket() {
|
|
107
|
+
return {
|
|
108
|
+
orders: 0,
|
|
109
|
+
revenue: 0,
|
|
110
|
+
avg_order_value: 0
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
function safeRate(numerator, denominator) {
|
|
114
|
+
if (!Number.isFinite(denominator) || denominator <= 0) {
|
|
115
|
+
return 0;
|
|
116
|
+
}
|
|
117
|
+
return Number((numerator / denominator).toFixed(4));
|
|
118
|
+
}
|
|
119
|
+
function topBreakdownFromMap(counts, totalOrders, limit = TOP_LIMIT) {
|
|
120
|
+
const sorted = Array.from(counts.entries()).filter(([, count]) => count > 0).sort((left, right) => right[1] - left[1] || left[0].localeCompare(right[0]));
|
|
121
|
+
const entries = sorted.slice(0, limit).map(([key, orders]) => ({
|
|
122
|
+
key,
|
|
123
|
+
orders,
|
|
124
|
+
rate: safeRate(orders, totalOrders)
|
|
125
|
+
}));
|
|
126
|
+
const otherOrders = sorted.slice(limit).reduce((sum, [, count]) => sum + count, 0);
|
|
127
|
+
return stripNulls({
|
|
128
|
+
entries,
|
|
129
|
+
other_orders: otherOrders > 0 ? otherOrders : void 0,
|
|
130
|
+
other_rate: otherOrders > 0 ? safeRate(otherOrders, totalOrders) : void 0
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
function incrementCount(map, key) {
|
|
134
|
+
if (!key) {
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
map.set(key, (map.get(key) ?? 0) + 1);
|
|
138
|
+
}
|
|
139
|
+
function incrementCurrency(map, currencyId, amount) {
|
|
140
|
+
map[currencyId] = roundMoney((map[currencyId] ?? 0) + amount);
|
|
141
|
+
}
|
|
142
|
+
function sortedCurrencyTotals(input) {
|
|
143
|
+
return Object.fromEntries(
|
|
144
|
+
Object.entries(input).filter(([, amount]) => amount !== 0).sort(([left], [right]) => left.localeCompare(right)).map(([currency, amount]) => [currency, roundMoney(amount)])
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
function createCurrencyMetricBucket() {
|
|
148
|
+
return {
|
|
149
|
+
orders: 0,
|
|
150
|
+
units: 0,
|
|
151
|
+
revenue: 0,
|
|
152
|
+
paidRevenue: 0,
|
|
153
|
+
paymentTotalPaid: 0,
|
|
154
|
+
transactionAmount: 0,
|
|
155
|
+
refundedAmount: 0,
|
|
156
|
+
cancelledGmv: 0,
|
|
157
|
+
saleFee: 0,
|
|
158
|
+
shippingCost: 0,
|
|
159
|
+
couponAmount: 0,
|
|
160
|
+
paymentCouponAmount: 0,
|
|
161
|
+
discountedGmv: 0,
|
|
162
|
+
notDeliveredGmv: 0,
|
|
163
|
+
cancelledOrders: 0,
|
|
164
|
+
refundedOrders: 0,
|
|
165
|
+
ordersWithShippingCost: 0,
|
|
166
|
+
ordersWithCoupon: 0,
|
|
167
|
+
discountedOrders: 0,
|
|
168
|
+
notDeliveredOrders: 0
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
function incrementCurrencyMetric(map, currencyId, updater) {
|
|
172
|
+
map[currencyId] = map[currencyId] ?? createCurrencyMetricBucket();
|
|
173
|
+
updater(map[currencyId]);
|
|
174
|
+
}
|
|
175
|
+
function sortedCurrencyMetrics(input, mapper) {
|
|
176
|
+
return Object.fromEntries(
|
|
177
|
+
Object.entries(input).sort(([left], [right]) => left.localeCompare(right)).map(([currencyId, bucket]) => [currencyId, stripNulls(mapper(bucket))])
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
function topAmountBreakdownFromMap(counts, amounts, totalOrders, limit = TOP_LIMIT) {
|
|
181
|
+
const sorted = Array.from(counts.entries()).filter(([, count]) => count > 0).sort((left, right) => right[1] - left[1] || left[0].localeCompare(right[0]));
|
|
182
|
+
const entries = sorted.slice(0, limit).map(([key, orders]) => ({
|
|
183
|
+
key,
|
|
184
|
+
orders,
|
|
185
|
+
rate: safeRate(orders, totalOrders),
|
|
186
|
+
amount_by_currency: sortedCurrencyTotals(amounts.get(key) ?? {})
|
|
187
|
+
}));
|
|
188
|
+
const otherOrders = sorted.slice(limit).reduce((sum, [, count]) => sum + count, 0);
|
|
189
|
+
return stripNulls({
|
|
190
|
+
entries,
|
|
191
|
+
other_orders: otherOrders > 0 ? otherOrders : void 0,
|
|
192
|
+
other_rate: otherOrders > 0 ? safeRate(otherOrders, totalOrders) : void 0
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
function revenueSum(input) {
|
|
196
|
+
return Object.values(input).reduce((sum, value) => sum + value, 0);
|
|
197
|
+
}
|
|
198
|
+
function compactFeedbackValue(value) {
|
|
199
|
+
const compact = compactNestedValue(value);
|
|
200
|
+
return compact.trim();
|
|
201
|
+
}
|
|
202
|
+
function getFirstPayment(order) {
|
|
203
|
+
return asRecord(asArray(order.payments)[0]);
|
|
204
|
+
}
|
|
205
|
+
function getOrderCurrency(order) {
|
|
206
|
+
return normalizeString(order.currency_id, "UNKNOWN");
|
|
207
|
+
}
|
|
208
|
+
function buildCalculations(orders, metadata, failedPages) {
|
|
209
|
+
const ordersTotal = orders.length;
|
|
210
|
+
const statusCounts = /* @__PURE__ */ new Map();
|
|
211
|
+
const tagCounts = /* @__PURE__ */ new Map();
|
|
212
|
+
const channelCounts = /* @__PURE__ */ new Map();
|
|
213
|
+
const siteCounts = /* @__PURE__ */ new Map();
|
|
214
|
+
const paymentStatusCounts = /* @__PURE__ */ new Map();
|
|
215
|
+
const paymentStatusDetailCounts = /* @__PURE__ */ new Map();
|
|
216
|
+
const paymentMethodCounts = /* @__PURE__ */ new Map();
|
|
217
|
+
const paymentTypeCounts = /* @__PURE__ */ new Map();
|
|
218
|
+
const installmentsCounts = /* @__PURE__ */ new Map();
|
|
219
|
+
const stockNodeCounts = /* @__PURE__ */ new Map();
|
|
220
|
+
const listingTypeCounts = /* @__PURE__ */ new Map();
|
|
221
|
+
const variationValueCounts = /* @__PURE__ */ new Map();
|
|
222
|
+
const buyers = /* @__PURE__ */ new Map();
|
|
223
|
+
const items = /* @__PURE__ */ new Map();
|
|
224
|
+
const variations = /* @__PURE__ */ new Map();
|
|
225
|
+
const skus = /* @__PURE__ */ new Map();
|
|
226
|
+
const dailyMetrics = /* @__PURE__ */ new Map();
|
|
227
|
+
const flowMetrics = /* @__PURE__ */ new Map();
|
|
228
|
+
const skuEconomics = /* @__PURE__ */ new Map();
|
|
229
|
+
const cancelGroupCounts = /* @__PURE__ */ new Map();
|
|
230
|
+
const cancelCodeCounts = /* @__PURE__ */ new Map();
|
|
231
|
+
const cancelRequestedByCounts = /* @__PURE__ */ new Map();
|
|
232
|
+
const cancelReasonCounts = /* @__PURE__ */ new Map();
|
|
233
|
+
const cancelReasonAmounts = /* @__PURE__ */ new Map();
|
|
234
|
+
let unitsTotal = 0;
|
|
235
|
+
let fulfilledOrders = 0;
|
|
236
|
+
let deliveredOrders = 0;
|
|
237
|
+
let packOrders = 0;
|
|
238
|
+
let discountedOrders = 0;
|
|
239
|
+
let catalogOrders = 0;
|
|
240
|
+
let ordersWithFeedback = 0;
|
|
241
|
+
let ordersWithChangeRequest = 0;
|
|
242
|
+
let ordersWithReturnRequest = 0;
|
|
243
|
+
let feedbackBuyerCount = 0;
|
|
244
|
+
let feedbackSellerCount = 0;
|
|
245
|
+
let ordersWithShipping = 0;
|
|
246
|
+
let singleItemOrders = 0;
|
|
247
|
+
let multiItemOrders = 0;
|
|
248
|
+
let ordersWithoutPaymentData = 0;
|
|
249
|
+
let paymentApprovedOrders = 0;
|
|
250
|
+
let itemLineCount = 0;
|
|
251
|
+
let ordersWithAnyPostSaleIssue = 0;
|
|
252
|
+
let ordersWithRefundedAmount = 0;
|
|
253
|
+
let ordersWithShippingCostAmount = 0;
|
|
254
|
+
let ordersWithCouponAmount = 0;
|
|
255
|
+
let notDeliveredOrders = 0;
|
|
256
|
+
let multiUnitItemLines = 0;
|
|
257
|
+
let multiUnitSaleFeeObservations = 0;
|
|
258
|
+
const itemsSeen = /* @__PURE__ */ new Set();
|
|
259
|
+
const buyerSeen = /* @__PURE__ */ new Set();
|
|
260
|
+
const financialRaw = {};
|
|
261
|
+
const currencyMetrics = {};
|
|
262
|
+
for (const order of orders) {
|
|
263
|
+
const currencyId = getOrderCurrency(order);
|
|
264
|
+
const totalAmount = toNumber(order.total_amount);
|
|
265
|
+
const paidAmount = toNumber(order.paid_amount);
|
|
266
|
+
const couponAmount = toNumber(asRecord(order.coupon).amount);
|
|
267
|
+
const payment = getFirstPayment(order);
|
|
268
|
+
const orderItems = asArray(order.order_items);
|
|
269
|
+
const buyer = asRecord(order.buyer);
|
|
270
|
+
const seller = asRecord(order.seller);
|
|
271
|
+
const feedback = asRecord(order.feedback);
|
|
272
|
+
const orderRequest = asRecord(order.order_request);
|
|
273
|
+
const shipping = asRecord(order.shipping);
|
|
274
|
+
const context = asRecord(order.context);
|
|
275
|
+
const paymentStatus = normalizeString(payment.status);
|
|
276
|
+
const paymentStatusDetail = normalizeString(payment.status_detail);
|
|
277
|
+
const paymentMethod = normalizeString(payment.payment_method_id);
|
|
278
|
+
const paymentType = normalizeString(payment.payment_type);
|
|
279
|
+
const installments = normalizeScalarString(payment.installments);
|
|
280
|
+
const tags = asArray(order.tags).map((tag) => normalizeScalarString(tag)).filter(Boolean);
|
|
281
|
+
const cancelDetail = asRecord(order.cancel_detail);
|
|
282
|
+
const isCancelled = normalizeString(order.status) === "cancelled";
|
|
283
|
+
const refundedAmount = toNumber(payment.transaction_amount_refunded);
|
|
284
|
+
const shippingCost = toNumber(payment.shipping_cost);
|
|
285
|
+
const paymentCouponAmount = toNumber(payment.coupon_amount);
|
|
286
|
+
const dayKey = compactDateTime(order.date_created)?.slice(0, 10) || "UNKNOWN_DATE";
|
|
287
|
+
const flows = asArray(context.flows).map((flow) => normalizeScalarString(flow)).filter(Boolean);
|
|
288
|
+
const effectiveFlows = flows.length > 0 ? flows : ["NO_FLOW"];
|
|
289
|
+
const isDiscounted = tags.includes("order_has_discount") || couponAmount > 0 || paymentCouponAmount > 0;
|
|
290
|
+
const isNotDelivered = tags.includes("not_delivered") || normalizeString(cancelDetail.group) === "shipment";
|
|
291
|
+
const orderItemDistinctIds = /* @__PURE__ */ new Set();
|
|
292
|
+
financialRaw[currencyId] = financialRaw[currencyId] ?? {
|
|
293
|
+
orders: 0,
|
|
294
|
+
units: 0,
|
|
295
|
+
distinctItems: 0,
|
|
296
|
+
revenue: 0,
|
|
297
|
+
paidAmount: 0,
|
|
298
|
+
paymentTotalPaidAmount: 0,
|
|
299
|
+
transactionAmount: 0,
|
|
300
|
+
couponAmount: 0,
|
|
301
|
+
saleFee: 0
|
|
302
|
+
};
|
|
303
|
+
financialRaw[currencyId].orders += 1;
|
|
304
|
+
financialRaw[currencyId].revenue += totalAmount;
|
|
305
|
+
financialRaw[currencyId].paidAmount += paidAmount;
|
|
306
|
+
financialRaw[currencyId].paymentTotalPaidAmount += toNumber(payment.total_paid_amount);
|
|
307
|
+
financialRaw[currencyId].transactionAmount += toNumber(payment.transaction_amount);
|
|
308
|
+
financialRaw[currencyId].couponAmount += couponAmount;
|
|
309
|
+
const dailyCurrencyMetrics = dailyMetrics.get(dayKey) ?? {};
|
|
310
|
+
dailyMetrics.set(dayKey, dailyCurrencyMetrics);
|
|
311
|
+
const updateOrderCurrencyMetrics = (bucket) => {
|
|
312
|
+
bucket.orders += 1;
|
|
313
|
+
bucket.revenue += totalAmount;
|
|
314
|
+
bucket.paidRevenue += paidAmount;
|
|
315
|
+
bucket.paymentTotalPaid += toNumber(payment.total_paid_amount);
|
|
316
|
+
bucket.transactionAmount += toNumber(payment.transaction_amount);
|
|
317
|
+
bucket.refundedAmount += refundedAmount;
|
|
318
|
+
bucket.shippingCost += shippingCost;
|
|
319
|
+
bucket.couponAmount += couponAmount;
|
|
320
|
+
bucket.paymentCouponAmount += paymentCouponAmount;
|
|
321
|
+
if (isCancelled) {
|
|
322
|
+
bucket.cancelledOrders += 1;
|
|
323
|
+
bucket.cancelledGmv += totalAmount;
|
|
324
|
+
}
|
|
325
|
+
if (refundedAmount > 0) {
|
|
326
|
+
bucket.refundedOrders += 1;
|
|
327
|
+
}
|
|
328
|
+
if (shippingCost > 0) {
|
|
329
|
+
bucket.ordersWithShippingCost += 1;
|
|
330
|
+
}
|
|
331
|
+
if (couponAmount > 0 || paymentCouponAmount > 0) {
|
|
332
|
+
bucket.ordersWithCoupon += 1;
|
|
333
|
+
}
|
|
334
|
+
if (isDiscounted) {
|
|
335
|
+
bucket.discountedOrders += 1;
|
|
336
|
+
}
|
|
337
|
+
if (isNotDelivered) {
|
|
338
|
+
bucket.notDeliveredOrders += 1;
|
|
339
|
+
}
|
|
340
|
+
};
|
|
341
|
+
incrementCurrencyMetric(currencyMetrics, currencyId, updateOrderCurrencyMetrics);
|
|
342
|
+
incrementCurrencyMetric(dailyCurrencyMetrics, currencyId, updateOrderCurrencyMetrics);
|
|
343
|
+
incrementCount(statusCounts, normalizeString(order.status));
|
|
344
|
+
incrementCount(channelCounts, normalizeString(context.channel));
|
|
345
|
+
incrementCount(siteCounts, normalizeString(context.site));
|
|
346
|
+
if (paymentStatus) {
|
|
347
|
+
incrementCount(paymentStatusCounts, paymentStatus);
|
|
348
|
+
} else {
|
|
349
|
+
ordersWithoutPaymentData += 1;
|
|
350
|
+
}
|
|
351
|
+
incrementCount(paymentStatusDetailCounts, paymentStatusDetail);
|
|
352
|
+
incrementCount(paymentMethodCounts, paymentMethod);
|
|
353
|
+
incrementCount(paymentTypeCounts, paymentType);
|
|
354
|
+
incrementCount(installmentsCounts, installments);
|
|
355
|
+
if (paymentStatus === "approved") {
|
|
356
|
+
paymentApprovedOrders += 1;
|
|
357
|
+
}
|
|
358
|
+
if (order.fulfilled === true) {
|
|
359
|
+
fulfilledOrders += 1;
|
|
360
|
+
}
|
|
361
|
+
if (normalizeScalarString(shipping.id)) {
|
|
362
|
+
ordersWithShipping += 1;
|
|
363
|
+
}
|
|
364
|
+
if (orderItems.length === 1) {
|
|
365
|
+
singleItemOrders += 1;
|
|
366
|
+
} else if (orderItems.length > 1) {
|
|
367
|
+
multiItemOrders += 1;
|
|
368
|
+
}
|
|
369
|
+
for (const tag of tags) {
|
|
370
|
+
incrementCount(tagCounts, tag);
|
|
371
|
+
}
|
|
372
|
+
if (tags.includes("delivered")) {
|
|
373
|
+
deliveredOrders += 1;
|
|
374
|
+
}
|
|
375
|
+
if (tags.includes("pack_order")) {
|
|
376
|
+
packOrders += 1;
|
|
377
|
+
}
|
|
378
|
+
if (tags.includes("order_has_discount")) {
|
|
379
|
+
discountedOrders += 1;
|
|
380
|
+
}
|
|
381
|
+
if (tags.includes("catalog")) {
|
|
382
|
+
catalogOrders += 1;
|
|
383
|
+
}
|
|
384
|
+
if (refundedAmount > 0) {
|
|
385
|
+
ordersWithRefundedAmount += 1;
|
|
386
|
+
}
|
|
387
|
+
if (shippingCost > 0) {
|
|
388
|
+
ordersWithShippingCostAmount += 1;
|
|
389
|
+
}
|
|
390
|
+
if (couponAmount > 0 || paymentCouponAmount > 0) {
|
|
391
|
+
ordersWithCouponAmount += 1;
|
|
392
|
+
}
|
|
393
|
+
if (isNotDelivered) {
|
|
394
|
+
notDeliveredOrders += 1;
|
|
395
|
+
}
|
|
396
|
+
if (isCancelled) {
|
|
397
|
+
const cancelGroup = normalizeString(cancelDetail.group, "UNKNOWN_GROUP");
|
|
398
|
+
const cancelCode = normalizeString(cancelDetail.code, "UNKNOWN_CODE");
|
|
399
|
+
const requestedBy = normalizeString(cancelDetail.requested_by, "UNKNOWN_REQUESTER");
|
|
400
|
+
const reasonKey = `${cancelGroup}:${cancelCode}:${requestedBy}`;
|
|
401
|
+
incrementCount(cancelGroupCounts, cancelGroup);
|
|
402
|
+
incrementCount(cancelCodeCounts, cancelCode);
|
|
403
|
+
incrementCount(cancelRequestedByCounts, requestedBy);
|
|
404
|
+
incrementCount(cancelReasonCounts, reasonKey);
|
|
405
|
+
const reasonAmount = cancelReasonAmounts.get(reasonKey) ?? {};
|
|
406
|
+
incrementCurrency(reasonAmount, currencyId, totalAmount);
|
|
407
|
+
cancelReasonAmounts.set(reasonKey, reasonAmount);
|
|
408
|
+
}
|
|
409
|
+
for (const flow of effectiveFlows) {
|
|
410
|
+
const flowAggregate = flowMetrics.get(flow) ?? {
|
|
411
|
+
flow,
|
|
412
|
+
orders: 0,
|
|
413
|
+
units: 0,
|
|
414
|
+
cancelled_orders: 0,
|
|
415
|
+
revenue_by_currency: {},
|
|
416
|
+
paid_revenue_by_currency: {},
|
|
417
|
+
sale_fee_by_currency: {}
|
|
418
|
+
};
|
|
419
|
+
flowAggregate.orders += 1;
|
|
420
|
+
if (isCancelled) {
|
|
421
|
+
flowAggregate.cancelled_orders += 1;
|
|
422
|
+
}
|
|
423
|
+
incrementCurrency(flowAggregate.revenue_by_currency, currencyId, totalAmount);
|
|
424
|
+
incrementCurrency(flowAggregate.paid_revenue_by_currency, currencyId, paidAmount);
|
|
425
|
+
flowMetrics.set(flow, flowAggregate);
|
|
426
|
+
}
|
|
427
|
+
const feedbackBuyer = compactFeedbackValue(feedback.buyer);
|
|
428
|
+
const feedbackSeller = compactFeedbackValue(feedback.seller);
|
|
429
|
+
const hasFeedback = Boolean(feedbackBuyer || feedbackSeller);
|
|
430
|
+
if (hasFeedback) {
|
|
431
|
+
ordersWithFeedback += 1;
|
|
432
|
+
}
|
|
433
|
+
if (feedbackBuyer) {
|
|
434
|
+
feedbackBuyerCount += 1;
|
|
435
|
+
}
|
|
436
|
+
if (feedbackSeller) {
|
|
437
|
+
feedbackSellerCount += 1;
|
|
438
|
+
}
|
|
439
|
+
const hasChangeRequest = Boolean(compactFeedbackValue(orderRequest.change));
|
|
440
|
+
const hasReturnRequest = Boolean(compactFeedbackValue(orderRequest.return));
|
|
441
|
+
if (hasChangeRequest) {
|
|
442
|
+
ordersWithChangeRequest += 1;
|
|
443
|
+
}
|
|
444
|
+
if (hasReturnRequest) {
|
|
445
|
+
ordersWithReturnRequest += 1;
|
|
446
|
+
}
|
|
447
|
+
if (hasFeedback || hasChangeRequest || hasReturnRequest) {
|
|
448
|
+
ordersWithAnyPostSaleIssue += 1;
|
|
449
|
+
}
|
|
450
|
+
const buyerId = normalizeScalarString(buyer.id);
|
|
451
|
+
const buyerNickname = normalizeString(buyer.nickname);
|
|
452
|
+
if (buyerId || buyerNickname) {
|
|
453
|
+
const buyerKey = buyerId || buyerNickname;
|
|
454
|
+
buyerSeen.add(buyerKey);
|
|
455
|
+
const currentBuyer = buyers.get(buyerKey) ?? {
|
|
456
|
+
buyer_id: buyerId,
|
|
457
|
+
nickname: buyerNickname,
|
|
458
|
+
orders: 0,
|
|
459
|
+
revenue_by_currency: {}
|
|
460
|
+
};
|
|
461
|
+
currentBuyer.orders += 1;
|
|
462
|
+
incrementCurrency(currentBuyer.revenue_by_currency, currencyId, totalAmount);
|
|
463
|
+
buyers.set(buyerKey, currentBuyer);
|
|
464
|
+
}
|
|
465
|
+
for (const orderItem of orderItems) {
|
|
466
|
+
itemLineCount += 1;
|
|
467
|
+
const item = asRecord(orderItem.item);
|
|
468
|
+
const stock = asRecord(orderItem.stock);
|
|
469
|
+
const itemId = normalizeString(item.id);
|
|
470
|
+
const title = normalizeString(item.title);
|
|
471
|
+
const sellerSku = normalizeString(item.seller_sku);
|
|
472
|
+
const quantity = toNumber(orderItem.quantity);
|
|
473
|
+
const unitPrice = toNumber(orderItem.unit_price);
|
|
474
|
+
const saleFee = toNumber(orderItem.sale_fee);
|
|
475
|
+
const listingType = normalizeString(orderItem.listing_type_id);
|
|
476
|
+
const stockNode = normalizeString(stock.node_id);
|
|
477
|
+
const variationSummary = compactVariationAttributes(item.variation_attributes);
|
|
478
|
+
const itemKey = `${itemId}::${variationSummary}::${sellerSku}`;
|
|
479
|
+
const skuKey = sellerSku || itemId || title || "UNKNOWN_SKU";
|
|
480
|
+
const variationKey = variationSummary || "NO_VARIATION";
|
|
481
|
+
const itemRevenue = unitPrice * quantity;
|
|
482
|
+
const paidRevenueApprox = totalAmount > 0 ? paidAmount * (itemRevenue / totalAmount) : itemRevenue;
|
|
483
|
+
const refundedAmountApprox = totalAmount > 0 ? refundedAmount * (itemRevenue / totalAmount) : refundedAmount;
|
|
484
|
+
if (quantity > 1) {
|
|
485
|
+
multiUnitItemLines += 1;
|
|
486
|
+
if (saleFee > 0) {
|
|
487
|
+
multiUnitSaleFeeObservations += 1;
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
unitsTotal += quantity;
|
|
491
|
+
financialRaw[currencyId].units += quantity;
|
|
492
|
+
financialRaw[currencyId].saleFee += saleFee;
|
|
493
|
+
const updateItemCurrencyMetrics = (bucket) => {
|
|
494
|
+
bucket.units += quantity;
|
|
495
|
+
bucket.saleFee += saleFee;
|
|
496
|
+
if (isDiscounted) {
|
|
497
|
+
bucket.discountedGmv += itemRevenue;
|
|
498
|
+
}
|
|
499
|
+
if (isNotDelivered) {
|
|
500
|
+
bucket.notDeliveredGmv += itemRevenue;
|
|
501
|
+
}
|
|
502
|
+
};
|
|
503
|
+
incrementCurrencyMetric(currencyMetrics, currencyId, updateItemCurrencyMetrics);
|
|
504
|
+
incrementCurrencyMetric(dailyCurrencyMetrics, currencyId, updateItemCurrencyMetrics);
|
|
505
|
+
for (const flow of effectiveFlows) {
|
|
506
|
+
const flowAggregate = flowMetrics.get(flow);
|
|
507
|
+
if (flowAggregate) {
|
|
508
|
+
flowAggregate.units += quantity;
|
|
509
|
+
incrementCurrency(flowAggregate.sale_fee_by_currency, currencyId, saleFee);
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
if (itemId) {
|
|
513
|
+
itemsSeen.add(itemId);
|
|
514
|
+
orderItemDistinctIds.add(itemId);
|
|
515
|
+
}
|
|
516
|
+
incrementCount(listingTypeCounts, listingType);
|
|
517
|
+
incrementCount(stockNodeCounts, stockNode);
|
|
518
|
+
incrementCount(variationValueCounts, variationKey);
|
|
519
|
+
const itemAggregate = items.get(itemKey) ?? {
|
|
520
|
+
item_id: itemId,
|
|
521
|
+
title,
|
|
522
|
+
seller_sku: sellerSku,
|
|
523
|
+
variation_summary: variationSummary,
|
|
524
|
+
orders: 0,
|
|
525
|
+
units: 0,
|
|
526
|
+
revenue_by_currency: {},
|
|
527
|
+
sale_fee_by_currency: {}
|
|
528
|
+
};
|
|
529
|
+
itemAggregate.orders += 1;
|
|
530
|
+
itemAggregate.units += quantity;
|
|
531
|
+
incrementCurrency(itemAggregate.revenue_by_currency, currencyId, unitPrice * quantity);
|
|
532
|
+
incrementCurrency(itemAggregate.sale_fee_by_currency, currencyId, saleFee);
|
|
533
|
+
items.set(itemKey, itemAggregate);
|
|
534
|
+
const variationAggregate = variations.get(variationKey) ?? {
|
|
535
|
+
variation_summary: variationSummary,
|
|
536
|
+
orders: 0,
|
|
537
|
+
units: 0,
|
|
538
|
+
revenue_by_currency: {}
|
|
539
|
+
};
|
|
540
|
+
variationAggregate.orders += 1;
|
|
541
|
+
variationAggregate.units += quantity;
|
|
542
|
+
incrementCurrency(variationAggregate.revenue_by_currency, currencyId, unitPrice * quantity);
|
|
543
|
+
variations.set(variationKey, variationAggregate);
|
|
544
|
+
const skuAggregate = skus.get(skuKey) ?? {
|
|
545
|
+
seller_sku: sellerSku,
|
|
546
|
+
orders: 0,
|
|
547
|
+
units: 0,
|
|
548
|
+
revenue_by_currency: {}
|
|
549
|
+
};
|
|
550
|
+
skuAggregate.orders += 1;
|
|
551
|
+
skuAggregate.units += quantity;
|
|
552
|
+
incrementCurrency(skuAggregate.revenue_by_currency, currencyId, unitPrice * quantity);
|
|
553
|
+
skus.set(skuKey, skuAggregate);
|
|
554
|
+
const skuEconomicsAggregate = skuEconomics.get(skuKey) ?? {
|
|
555
|
+
seller_sku: sellerSku,
|
|
556
|
+
item_id: itemId,
|
|
557
|
+
title,
|
|
558
|
+
orders: 0,
|
|
559
|
+
units: 0,
|
|
560
|
+
cancelled_orders: 0,
|
|
561
|
+
refunded_orders: 0,
|
|
562
|
+
revenue_by_currency: {},
|
|
563
|
+
paid_revenue_by_currency: {},
|
|
564
|
+
sale_fee_by_currency: {},
|
|
565
|
+
refunded_amount_by_currency: {}
|
|
566
|
+
};
|
|
567
|
+
skuEconomicsAggregate.orders += 1;
|
|
568
|
+
skuEconomicsAggregate.units += quantity;
|
|
569
|
+
if (isCancelled) {
|
|
570
|
+
skuEconomicsAggregate.cancelled_orders += 1;
|
|
571
|
+
}
|
|
572
|
+
if (refundedAmount > 0) {
|
|
573
|
+
skuEconomicsAggregate.refunded_orders += 1;
|
|
574
|
+
}
|
|
575
|
+
incrementCurrency(skuEconomicsAggregate.revenue_by_currency, currencyId, itemRevenue);
|
|
576
|
+
incrementCurrency(skuEconomicsAggregate.paid_revenue_by_currency, currencyId, paidRevenueApprox);
|
|
577
|
+
incrementCurrency(skuEconomicsAggregate.sale_fee_by_currency, currencyId, saleFee);
|
|
578
|
+
incrementCurrency(skuEconomicsAggregate.refunded_amount_by_currency, currencyId, refundedAmountApprox);
|
|
579
|
+
skuEconomics.set(skuKey, skuEconomicsAggregate);
|
|
580
|
+
}
|
|
581
|
+
financialRaw[currencyId].distinctItems += orderItemDistinctIds.size;
|
|
582
|
+
}
|
|
583
|
+
const financialByCurrency = {};
|
|
584
|
+
for (const [currencyId, raw] of Object.entries(financialRaw)) {
|
|
585
|
+
financialByCurrency[currencyId] = {
|
|
586
|
+
revenue_total: roundMoney(raw.revenue),
|
|
587
|
+
paid_amount_total: roundMoney(raw.paidAmount),
|
|
588
|
+
payment_total_paid_amount: roundMoney(raw.paymentTotalPaidAmount),
|
|
589
|
+
transaction_amount_total: roundMoney(raw.transactionAmount),
|
|
590
|
+
coupon_amount_total: roundMoney(raw.couponAmount),
|
|
591
|
+
sale_fee_total: roundMoney(raw.saleFee),
|
|
592
|
+
avg_order_value: roundMoney(raw.revenue / Math.max(1, raw.orders)),
|
|
593
|
+
avg_paid_amount_per_order: roundMoney(raw.paidAmount / Math.max(1, raw.orders)),
|
|
594
|
+
avg_units_per_order: roundMoney(raw.units / Math.max(1, raw.orders)),
|
|
595
|
+
avg_distinct_items_per_order: roundMoney(raw.distinctItems / Math.max(1, raw.orders)),
|
|
596
|
+
avg_sale_fee_per_order: roundMoney(raw.saleFee / Math.max(1, raw.orders)),
|
|
597
|
+
sale_fee_rate: safeRate(raw.saleFee, raw.revenue)
|
|
598
|
+
};
|
|
599
|
+
}
|
|
600
|
+
const topItemsByRevenue = Array.from(items.values()).sort((left, right) => {
|
|
601
|
+
const leftRevenue = Object.values(left.revenue_by_currency).reduce((sum, value) => sum + value, 0);
|
|
602
|
+
const rightRevenue = Object.values(right.revenue_by_currency).reduce((sum, value) => sum + value, 0);
|
|
603
|
+
return rightRevenue - leftRevenue || left.item_id.localeCompare(right.item_id);
|
|
604
|
+
}).slice(0, TOP_LIMIT).map((item) => ({
|
|
605
|
+
...item,
|
|
606
|
+
revenue_by_currency: sortedCurrencyTotals(item.revenue_by_currency),
|
|
607
|
+
sale_fee_by_currency: sortedCurrencyTotals(item.sale_fee_by_currency)
|
|
608
|
+
}));
|
|
609
|
+
const topItemsByUnits = Array.from(items.values()).sort((left, right) => right.units - left.units || left.item_id.localeCompare(right.item_id)).slice(0, TOP_LIMIT).map((item) => ({
|
|
610
|
+
...item,
|
|
611
|
+
revenue_by_currency: sortedCurrencyTotals(item.revenue_by_currency),
|
|
612
|
+
sale_fee_by_currency: sortedCurrencyTotals(item.sale_fee_by_currency)
|
|
613
|
+
}));
|
|
614
|
+
const topItemsByOrders = Array.from(items.values()).sort((left, right) => right.orders - left.orders || left.item_id.localeCompare(right.item_id)).slice(0, TOP_LIMIT).map((item) => ({
|
|
615
|
+
...item,
|
|
616
|
+
revenue_by_currency: sortedCurrencyTotals(item.revenue_by_currency),
|
|
617
|
+
sale_fee_by_currency: sortedCurrencyTotals(item.sale_fee_by_currency)
|
|
618
|
+
}));
|
|
619
|
+
const topVariations = Array.from(variations.values()).filter((variation) => variation.variation_summary).sort((left, right) => right.units - left.units || left.variation_summary.localeCompare(right.variation_summary)).slice(0, TOP_LIMIT).map((variation) => ({
|
|
620
|
+
...variation,
|
|
621
|
+
revenue_by_currency: sortedCurrencyTotals(variation.revenue_by_currency)
|
|
622
|
+
}));
|
|
623
|
+
const topSkus = Array.from(skus.values()).filter((sku) => sku.seller_sku).sort((left, right) => right.units - left.units || left.seller_sku.localeCompare(right.seller_sku)).slice(0, TOP_LIMIT).map((sku) => ({
|
|
624
|
+
...sku,
|
|
625
|
+
revenue_by_currency: sortedCurrencyTotals(sku.revenue_by_currency)
|
|
626
|
+
}));
|
|
627
|
+
const topBuyersByRevenue = Array.from(buyers.values()).sort((left, right) => {
|
|
628
|
+
const leftRevenue = Object.values(left.revenue_by_currency).reduce((sum, value) => sum + value, 0);
|
|
629
|
+
const rightRevenue = Object.values(right.revenue_by_currency).reduce((sum, value) => sum + value, 0);
|
|
630
|
+
return rightRevenue - leftRevenue || left.buyer_id.localeCompare(right.buyer_id);
|
|
631
|
+
}).slice(0, TOP_LIMIT).map((buyer) => ({
|
|
632
|
+
...buyer,
|
|
633
|
+
revenue_by_currency: sortedCurrencyTotals(buyer.revenue_by_currency)
|
|
634
|
+
}));
|
|
635
|
+
const topBuyersByOrders = Array.from(buyers.values()).sort((left, right) => right.orders - left.orders || left.buyer_id.localeCompare(right.buyer_id)).slice(0, TOP_LIMIT).map((buyer) => ({
|
|
636
|
+
...buyer,
|
|
637
|
+
revenue_by_currency: sortedCurrencyTotals(buyer.revenue_by_currency)
|
|
638
|
+
}));
|
|
639
|
+
const buyersWithMultipleOrders = Array.from(buyers.values()).filter((buyer) => buyer.orders > 1).length;
|
|
640
|
+
const topVariationValues = Array.from(variationValueCounts.entries()).filter(([key]) => key && key !== "NO_VARIATION").sort((left, right) => right[1] - left[1] || left[0].localeCompare(right[0])).slice(0, TOP_LIMIT).map(([key, occurrences]) => ({ key, occurrences }));
|
|
641
|
+
const topSingleValue = (counts) => Array.from(counts.entries()).filter(([key]) => key).sort((left, right) => right[1] - left[1] || left[0].localeCompare(right[0]))[0]?.[0] ?? "";
|
|
642
|
+
const revenueQuality = {
|
|
643
|
+
by_currency: sortedCurrencyMetrics(currencyMetrics, (bucket) => ({
|
|
644
|
+
gross_gmv: roundMoney(bucket.revenue),
|
|
645
|
+
paid_gmv: roundMoney(bucket.paidRevenue),
|
|
646
|
+
payment_total_paid_amount: roundMoney(bucket.paymentTotalPaid),
|
|
647
|
+
transaction_amount_total: roundMoney(bucket.transactionAmount),
|
|
648
|
+
refunded_amount_total: roundMoney(bucket.refundedAmount),
|
|
649
|
+
net_after_refunds: roundMoney(bucket.transactionAmount - bucket.refundedAmount),
|
|
650
|
+
cancelled_gmv: roundMoney(bucket.cancelledGmv),
|
|
651
|
+
cancelled_rate_by_orders: safeRate(bucket.cancelledOrders, bucket.orders),
|
|
652
|
+
cancelled_rate_by_gmv: safeRate(bucket.cancelledGmv, bucket.revenue),
|
|
653
|
+
paid_rate_by_gmv: safeRate(bucket.paidRevenue, bucket.revenue)
|
|
654
|
+
}))
|
|
655
|
+
};
|
|
656
|
+
const refundsCancellations = {
|
|
657
|
+
by_currency: sortedCurrencyMetrics(currencyMetrics, (bucket) => ({
|
|
658
|
+
refunded_amount_total: roundMoney(bucket.refundedAmount),
|
|
659
|
+
refunded_orders: bucket.refundedOrders,
|
|
660
|
+
refund_rate_by_orders: safeRate(bucket.refundedOrders, bucket.orders),
|
|
661
|
+
refund_rate_by_transaction_amount: safeRate(bucket.refundedAmount, bucket.transactionAmount),
|
|
662
|
+
cancelled_orders: bucket.cancelledOrders,
|
|
663
|
+
cancelled_gmv: roundMoney(bucket.cancelledGmv),
|
|
664
|
+
cancelled_rate_by_orders: safeRate(bucket.cancelledOrders, bucket.orders),
|
|
665
|
+
cancelled_rate_by_gmv: safeRate(bucket.cancelledGmv, bucket.revenue)
|
|
666
|
+
})),
|
|
667
|
+
cancel_group_breakdown: topBreakdownFromMap(cancelGroupCounts, ordersTotal),
|
|
668
|
+
cancel_code_breakdown: topBreakdownFromMap(cancelCodeCounts, ordersTotal),
|
|
669
|
+
cancel_requested_by_breakdown: topBreakdownFromMap(cancelRequestedByCounts, ordersTotal),
|
|
670
|
+
top_cancel_reasons: topAmountBreakdownFromMap(cancelReasonCounts, cancelReasonAmounts, ordersTotal)
|
|
671
|
+
};
|
|
672
|
+
const shippingAndDiscounts = {
|
|
673
|
+
by_currency: sortedCurrencyMetrics(currencyMetrics, (bucket) => ({
|
|
674
|
+
buyer_shipping_paid_total: roundMoney(bucket.shippingCost),
|
|
675
|
+
orders_with_shipping_cost: bucket.ordersWithShippingCost,
|
|
676
|
+
avg_shipping_paid_by_order_with_cost: roundMoney(bucket.shippingCost / Math.max(1, bucket.ordersWithShippingCost)),
|
|
677
|
+
shipping_cost_rate_on_paid_gmv: safeRate(bucket.shippingCost, bucket.paidRevenue),
|
|
678
|
+
coupon_amount_total: roundMoney(bucket.couponAmount),
|
|
679
|
+
payment_coupon_amount_total: roundMoney(bucket.paymentCouponAmount),
|
|
680
|
+
orders_with_coupon: bucket.ordersWithCoupon,
|
|
681
|
+
coupon_rate_by_orders: safeRate(bucket.ordersWithCoupon, bucket.orders),
|
|
682
|
+
coupon_rate_on_gmv: safeRate(bucket.couponAmount + bucket.paymentCouponAmount, bucket.revenue),
|
|
683
|
+
discounted_orders: bucket.discountedOrders,
|
|
684
|
+
discounted_order_rate: safeRate(bucket.discountedOrders, bucket.orders),
|
|
685
|
+
discounted_gmv: roundMoney(bucket.discountedGmv),
|
|
686
|
+
not_delivered_orders: bucket.notDeliveredOrders,
|
|
687
|
+
not_delivered_gmv: roundMoney(bucket.notDeliveredGmv),
|
|
688
|
+
not_delivered_rate_by_orders: safeRate(bucket.notDeliveredOrders, bucket.orders),
|
|
689
|
+
not_delivered_rate_by_gmv: safeRate(bucket.notDeliveredGmv, bucket.revenue)
|
|
690
|
+
}))
|
|
691
|
+
};
|
|
692
|
+
const dailyTrend = Array.from(dailyMetrics.entries()).sort(([left], [right]) => left.localeCompare(right)).map(([date, byCurrency]) => ({
|
|
693
|
+
date,
|
|
694
|
+
by_currency: sortedCurrencyMetrics(byCurrency, (bucket) => ({
|
|
695
|
+
orders: bucket.orders,
|
|
696
|
+
units: bucket.units,
|
|
697
|
+
revenue: roundMoney(bucket.revenue),
|
|
698
|
+
paid_revenue: roundMoney(bucket.paidRevenue),
|
|
699
|
+
cancelled_orders: bucket.cancelledOrders,
|
|
700
|
+
refunded_amount: roundMoney(bucket.refundedAmount),
|
|
701
|
+
avg_order_value: roundMoney(bucket.revenue / Math.max(1, bucket.orders))
|
|
702
|
+
}))
|
|
703
|
+
}));
|
|
704
|
+
const flowPerformance = Array.from(flowMetrics.values()).sort((left, right) => revenueSum(right.revenue_by_currency) - revenueSum(left.revenue_by_currency) || left.flow.localeCompare(right.flow)).slice(0, TOP_LIMIT).map((flow) => ({
|
|
705
|
+
flow: flow.flow,
|
|
706
|
+
orders: flow.orders,
|
|
707
|
+
units: flow.units,
|
|
708
|
+
revenue_by_currency: sortedCurrencyTotals(flow.revenue_by_currency),
|
|
709
|
+
paid_revenue_by_currency: sortedCurrencyTotals(flow.paid_revenue_by_currency),
|
|
710
|
+
sale_fee_by_currency: sortedCurrencyTotals(flow.sale_fee_by_currency),
|
|
711
|
+
cancelled_orders: flow.cancelled_orders,
|
|
712
|
+
cancelled_rate: safeRate(flow.cancelled_orders, flow.orders),
|
|
713
|
+
fee_rate_by_currency: Object.fromEntries(
|
|
714
|
+
Object.keys(flow.revenue_by_currency).sort((left, right) => left.localeCompare(right)).map((currencyId) => [currencyId, safeRate(flow.sale_fee_by_currency[currencyId] ?? 0, flow.revenue_by_currency[currencyId] ?? 0)])
|
|
715
|
+
)
|
|
716
|
+
}));
|
|
717
|
+
const skuUnitEconomics = Array.from(skuEconomics.values()).sort((left, right) => revenueSum(right.revenue_by_currency) - revenueSum(left.revenue_by_currency) || left.seller_sku.localeCompare(right.seller_sku)).slice(0, TOP_LIMIT).map((sku) => ({
|
|
718
|
+
seller_sku: sku.seller_sku,
|
|
719
|
+
item_id: sku.item_id,
|
|
720
|
+
title: sku.title,
|
|
721
|
+
orders: sku.orders,
|
|
722
|
+
units: sku.units,
|
|
723
|
+
revenue_by_currency: sortedCurrencyTotals(sku.revenue_by_currency),
|
|
724
|
+
paid_revenue_by_currency: sortedCurrencyTotals(sku.paid_revenue_by_currency),
|
|
725
|
+
sale_fee_by_currency: sortedCurrencyTotals(sku.sale_fee_by_currency),
|
|
726
|
+
refunded_amount_by_currency: sortedCurrencyTotals(sku.refunded_amount_by_currency),
|
|
727
|
+
avg_unit_price_by_currency: Object.fromEntries(
|
|
728
|
+
Object.keys(sku.revenue_by_currency).sort((left, right) => left.localeCompare(right)).map((currencyId) => [currencyId, roundMoney((sku.revenue_by_currency[currencyId] ?? 0) / Math.max(1, sku.units))])
|
|
729
|
+
),
|
|
730
|
+
fee_rate_by_currency: Object.fromEntries(
|
|
731
|
+
Object.keys(sku.revenue_by_currency).sort((left, right) => left.localeCompare(right)).map((currencyId) => [currencyId, safeRate(sku.sale_fee_by_currency[currencyId] ?? 0, sku.revenue_by_currency[currencyId] ?? 0)])
|
|
732
|
+
),
|
|
733
|
+
cancelled_orders: sku.cancelled_orders,
|
|
734
|
+
cancelled_rate: safeRate(sku.cancelled_orders, sku.orders),
|
|
735
|
+
refunded_orders: sku.refunded_orders,
|
|
736
|
+
refund_rate: safeRate(sku.refunded_orders, sku.orders)
|
|
737
|
+
}));
|
|
738
|
+
const feeDiagnostics = {
|
|
739
|
+
sale_fee_assumption: "order_items.sale_fee is used as returned by MercadoLibre and is not multiplied by quantity yet.",
|
|
740
|
+
multi_unit_item_lines: multiUnitItemLines,
|
|
741
|
+
multi_unit_sale_fee_observations: multiUnitSaleFeeObservations,
|
|
742
|
+
needs_sale_fee_granularity_validation: true
|
|
743
|
+
};
|
|
744
|
+
return {
|
|
745
|
+
overview: {
|
|
746
|
+
orders_total: ordersTotal,
|
|
747
|
+
units_total: unitsTotal,
|
|
748
|
+
distinct_items_total: itemsSeen.size,
|
|
749
|
+
distinct_buyers_total: buyerSeen.size,
|
|
750
|
+
fulfilled_orders: fulfilledOrders,
|
|
751
|
+
delivered_orders: deliveredOrders,
|
|
752
|
+
pack_orders: packOrders,
|
|
753
|
+
orders_with_discount: discountedOrders,
|
|
754
|
+
orders_with_feedback: ordersWithFeedback,
|
|
755
|
+
orders_with_change_request: ordersWithChangeRequest,
|
|
756
|
+
orders_with_return_request: ordersWithReturnRequest
|
|
757
|
+
},
|
|
758
|
+
financial_by_currency: financialByCurrency,
|
|
759
|
+
revenue_quality: revenueQuality,
|
|
760
|
+
refunds_cancellations: refundsCancellations,
|
|
761
|
+
shipping_and_discounts: shippingAndDiscounts,
|
|
762
|
+
daily_trend: dailyTrend,
|
|
763
|
+
flow_performance: flowPerformance,
|
|
764
|
+
sku_unit_economics: skuUnitEconomics,
|
|
765
|
+
fee_diagnostics: feeDiagnostics,
|
|
766
|
+
order_mix: {
|
|
767
|
+
status_breakdown: topBreakdownFromMap(statusCounts, ordersTotal),
|
|
768
|
+
tags_breakdown: topBreakdownFromMap(tagCounts, ordersTotal),
|
|
769
|
+
channel_breakdown: topBreakdownFromMap(channelCounts, ordersTotal),
|
|
770
|
+
site_breakdown: topBreakdownFromMap(siteCounts, ordersTotal),
|
|
771
|
+
single_item_order_rate: safeRate(singleItemOrders, ordersTotal),
|
|
772
|
+
multi_item_order_rate: safeRate(multiItemOrders, ordersTotal),
|
|
773
|
+
fulfilled_rate: safeRate(fulfilledOrders, ordersTotal),
|
|
774
|
+
delivered_rate: safeRate(deliveredOrders, ordersTotal),
|
|
775
|
+
pack_order_rate: safeRate(packOrders, ordersTotal),
|
|
776
|
+
discounted_order_rate: safeRate(discountedOrders, ordersTotal),
|
|
777
|
+
catalog_order_rate: safeRate(catalogOrders, ordersTotal)
|
|
778
|
+
},
|
|
779
|
+
payment_mix: {
|
|
780
|
+
payment_status_breakdown: topBreakdownFromMap(paymentStatusCounts, ordersTotal),
|
|
781
|
+
payment_status_detail_breakdown: topBreakdownFromMap(paymentStatusDetailCounts, ordersTotal),
|
|
782
|
+
payment_method_breakdown: topBreakdownFromMap(paymentMethodCounts, ordersTotal),
|
|
783
|
+
payment_type_breakdown: topBreakdownFromMap(paymentTypeCounts, ordersTotal),
|
|
784
|
+
installments_breakdown: topBreakdownFromMap(installmentsCounts, ordersTotal),
|
|
785
|
+
payment_approved_rate: safeRate(paymentApprovedOrders, ordersTotal),
|
|
786
|
+
orders_without_payment_data: ordersWithoutPaymentData
|
|
787
|
+
},
|
|
788
|
+
fulfillment: {
|
|
789
|
+
orders_with_shipping: ordersWithShipping,
|
|
790
|
+
shipping_attached_rate: safeRate(ordersWithShipping, ordersTotal),
|
|
791
|
+
shipping_id_coverage: ordersWithShipping,
|
|
792
|
+
stock_node_breakdown: topBreakdownFromMap(stockNodeCounts, Math.max(1, itemLineCount)),
|
|
793
|
+
listing_type_breakdown: topBreakdownFromMap(listingTypeCounts, Math.max(1, itemLineCount))
|
|
794
|
+
},
|
|
795
|
+
post_sale_friction: {
|
|
796
|
+
orders_with_feedback_rate: safeRate(ordersWithFeedback, ordersTotal),
|
|
797
|
+
feedback_buyer_count: feedbackBuyerCount,
|
|
798
|
+
feedback_seller_count: feedbackSellerCount,
|
|
799
|
+
change_request_rate: safeRate(ordersWithChangeRequest, ordersTotal),
|
|
800
|
+
return_request_rate: safeRate(ordersWithReturnRequest, ordersTotal),
|
|
801
|
+
orders_with_any_post_sale_issue_rate: safeRate(ordersWithAnyPostSaleIssue, ordersTotal)
|
|
802
|
+
},
|
|
803
|
+
product_insights: {
|
|
804
|
+
top_items_by_revenue: topItemsByRevenue,
|
|
805
|
+
top_items_by_units: topItemsByUnits,
|
|
806
|
+
top_items_by_orders: topItemsByOrders,
|
|
807
|
+
top_variations: topVariations,
|
|
808
|
+
top_skus: topSkus,
|
|
809
|
+
top_listing_types: topBreakdownFromMap(listingTypeCounts, Math.max(1, itemLineCount)),
|
|
810
|
+
top_stock_nodes: topBreakdownFromMap(stockNodeCounts, Math.max(1, itemLineCount))
|
|
811
|
+
},
|
|
812
|
+
customer_insights: {
|
|
813
|
+
top_buyers_by_revenue: topBuyersByRevenue,
|
|
814
|
+
top_buyers_by_orders: topBuyersByOrders,
|
|
815
|
+
repeat_buyer_rate: safeRate(buyersWithMultipleOrders, buyerSeen.size),
|
|
816
|
+
buyers_with_multiple_orders: buyersWithMultipleOrders
|
|
817
|
+
},
|
|
818
|
+
operational_context: {
|
|
819
|
+
most_used_payment_method: topSingleValue(paymentMethodCounts),
|
|
820
|
+
most_used_listing_type: topSingleValue(listingTypeCounts),
|
|
821
|
+
most_used_stock_node: topSingleValue(stockNodeCounts),
|
|
822
|
+
most_used_channel: topSingleValue(channelCounts),
|
|
823
|
+
most_common_tag: topSingleValue(tagCounts),
|
|
824
|
+
most_common_variation_values: topVariationValues
|
|
825
|
+
},
|
|
826
|
+
coverage: {
|
|
827
|
+
fetch_mode: metadata.fetch_mode,
|
|
828
|
+
universe_fully_fetched: metadata.universe_fully_fetched,
|
|
829
|
+
orders_returned: metadata.returned,
|
|
830
|
+
pages_requested: metadata.pages_requested,
|
|
831
|
+
pages_succeeded: metadata.pages_succeeded,
|
|
832
|
+
pages_failed: metadata.pages_failed,
|
|
833
|
+
failed_pages_count: failedPages.length,
|
|
834
|
+
failed_pages: failedPages
|
|
835
|
+
}
|
|
836
|
+
};
|
|
837
|
+
}
|
|
838
|
+
function buildOrdersSummaryMetadata(params) {
|
|
839
|
+
const requestedWindowEndOffset = params.effectiveOffset + params.effectiveLimit * params.pagesRequested;
|
|
840
|
+
const nextOffset = params.responseMode === "orders_chunk" ? requestedWindowEndOffset : params.effectiveOffset + params.returned;
|
|
841
|
+
const hasMoreOrders = nextOffset < params.total;
|
|
842
|
+
const universeFullyFetched = params.responseMode === "calculations" && params.pagesFailed === 0 && params.returned >= params.total;
|
|
843
|
+
let continuation = "No more pages for this query.";
|
|
844
|
+
if (params.pagesFailed > 0) {
|
|
845
|
+
const offsets = params.failedOffsets.slice(0, 5).join(", ");
|
|
846
|
+
const moreOffsets = params.failedOffsets.length > 5 ? ` y ${params.failedOffsets.length - 5} offsets adicionales` : "";
|
|
847
|
+
continuation = params.responseMode === "orders_chunk" ? `Se recuperaron paginas parciales. Reintentar primero la misma tool con responseMode="orders_chunk", offset=${params.failedOffsets[0]} y limit=${params.effectiveLimit}. Offsets fallidos: ${offsets}${moreOffsets}. Luego continuar con offset=${nextOffset} si hace falta.` : `Calculos parciales por paginas fallidas. Reintentar la misma tool con responseMode="calculations". Offsets fallidos: ${offsets}${moreOffsets}.`;
|
|
848
|
+
} else if (params.responseMode === "orders_chunk" && hasMoreOrders) {
|
|
849
|
+
continuation = `Para continuar trayendo ordenes, volver a invocar meli_get_orders_summary con los mismos filtros, responseMode="orders_chunk", offset=${nextOffset} y limit=${params.effectiveLimit}. Repetir hasta has_more=false.`;
|
|
850
|
+
} else if (params.responseMode === "calculations") {
|
|
851
|
+
continuation = 'Calculos completos. Si el usuario pide detalle de ordenes, invocar la misma tool con responseMode="orders_chunk", offset=0 y limit=50.';
|
|
852
|
+
}
|
|
853
|
+
return {
|
|
854
|
+
total: params.total,
|
|
855
|
+
limit: params.effectiveLimit,
|
|
856
|
+
offset: params.effectiveOffset,
|
|
857
|
+
returned: params.returned,
|
|
858
|
+
has_more: params.responseMode === "orders_chunk" ? hasMoreOrders || params.pagesFailed > 0 : params.pagesFailed > 0,
|
|
859
|
+
next_offset: params.responseMode === "orders_chunk" && hasMoreOrders ? nextOffset : void 0,
|
|
860
|
+
continuation,
|
|
861
|
+
fetch_mode: params.responseMode === "orders_chunk" ? "orders_chunk" : "full_calculations",
|
|
862
|
+
pages_per_call_limit: params.responseMode === "orders_chunk" ? ORDER_DETAIL_MAX_PAGES_PER_CALL : void 0,
|
|
863
|
+
pages_requested: params.pagesRequested,
|
|
864
|
+
pages_succeeded: params.pagesSucceeded,
|
|
865
|
+
pages_failed: params.pagesFailed,
|
|
866
|
+
universe_fully_fetched: universeFullyFetched
|
|
867
|
+
};
|
|
868
|
+
}
|
|
869
|
+
function formatCompactOrder(order, metricsByCurrency) {
|
|
870
|
+
const currencyId = normalizeString(order.currency_id, "UNKNOWN");
|
|
871
|
+
const totalAmount = toNumber(order.total_amount);
|
|
872
|
+
const paidAmount = toNumber(order.paid_amount);
|
|
873
|
+
const bucket = currencyBucket(metricsByCurrency, currencyId, createMetricsBucket);
|
|
874
|
+
bucket.orders += 1;
|
|
875
|
+
bucket.revenue += totalAmount;
|
|
876
|
+
bucket.avg_order_value = bucket.orders > 0 ? bucket.revenue / bucket.orders : 0;
|
|
877
|
+
const orderItems = asArray(order.order_items);
|
|
878
|
+
const firstOrderItem = asRecord(orderItems[0]);
|
|
879
|
+
const firstItem = asRecord(firstOrderItem.item);
|
|
880
|
+
const firstPayment = asRecord(asArray(order.payments)[0]);
|
|
881
|
+
const shipping = asRecord(order.shipping);
|
|
882
|
+
const buyer = asRecord(order.buyer);
|
|
883
|
+
const seller = asRecord(order.seller);
|
|
884
|
+
const context = asRecord(order.context);
|
|
885
|
+
const feedback = asRecord(order.feedback);
|
|
886
|
+
const orderRequest = asRecord(order.order_request);
|
|
887
|
+
const coupon = asRecord(order.coupon);
|
|
888
|
+
const stock = asRecord(firstOrderItem.stock);
|
|
889
|
+
return [
|
|
890
|
+
normalizeScalarString(order.id),
|
|
891
|
+
compactDateTime(order.date_created),
|
|
892
|
+
normalizeString(order.status),
|
|
893
|
+
totalAmount,
|
|
894
|
+
currencyId,
|
|
895
|
+
normalizeString(buyer.nickname) || normalizeScalarString(buyer.id),
|
|
896
|
+
normalizeString(shipping.status),
|
|
897
|
+
orderItems.length,
|
|
898
|
+
compactDateTime(order.date_closed),
|
|
899
|
+
compactDateTime(order.date_last_updated) || compactDateTime(order.last_updated),
|
|
900
|
+
paidAmount,
|
|
901
|
+
normalizeScalarString(buyer.id),
|
|
902
|
+
normalizeString(seller.nickname) || normalizeScalarString(seller.id),
|
|
903
|
+
normalizeScalarString(seller.id),
|
|
904
|
+
normalizeScalarString(shipping.id),
|
|
905
|
+
normalizeScalarString(order.pack_id),
|
|
906
|
+
Boolean(order.fulfilled),
|
|
907
|
+
compactStringList(order.tags),
|
|
908
|
+
normalizeString(context.channel),
|
|
909
|
+
normalizeString(context.site),
|
|
910
|
+
normalizeScalarString(firstPayment.id),
|
|
911
|
+
normalizeString(firstPayment.status),
|
|
912
|
+
normalizeString(firstPayment.status_detail),
|
|
913
|
+
normalizeString(firstPayment.payment_method_id),
|
|
914
|
+
normalizeString(firstPayment.payment_type),
|
|
915
|
+
toNumber(firstPayment.total_paid_amount),
|
|
916
|
+
toNumber(firstPayment.transaction_amount),
|
|
917
|
+
toNumber(firstPayment.installments),
|
|
918
|
+
toNumber(coupon.amount),
|
|
919
|
+
compactStringList(firstPayment.available_actions),
|
|
920
|
+
compactNestedValue(feedback.buyer),
|
|
921
|
+
compactNestedValue(feedback.seller),
|
|
922
|
+
compactNestedValue(orderRequest.change),
|
|
923
|
+
compactNestedValue(orderRequest.return),
|
|
924
|
+
normalizeString(firstItem.id),
|
|
925
|
+
normalizeString(firstItem.title),
|
|
926
|
+
normalizeString(firstItem.seller_sku),
|
|
927
|
+
toNumber(firstOrderItem.quantity),
|
|
928
|
+
toNumber(firstOrderItem.unit_price),
|
|
929
|
+
toNumber(firstOrderItem.sale_fee),
|
|
930
|
+
normalizeString(firstOrderItem.listing_type_id),
|
|
931
|
+
compactVariationAttributes(firstItem.variation_attributes),
|
|
932
|
+
normalizeString(stock.node_id)
|
|
933
|
+
];
|
|
934
|
+
}
|
|
23
935
|
const meliGetOrdersSummarySchema = z.object({
|
|
24
936
|
profileId: mercadolibreProfileIdSchemaField,
|
|
25
937
|
startDate: z.string().regex(mercadoLibreDateRegex).describe("Start date in YYYY-MM-DD format."),
|
|
@@ -27,8 +939,9 @@ const meliGetOrdersSummarySchema = z.object({
|
|
|
27
939
|
status: z.string().trim().min(1).optional().describe("Optional MercadoLibre order status filter."),
|
|
28
940
|
q: z.string().trim().min(1).optional().describe("Optional free-text search query supported by MercadoLibre orders search."),
|
|
29
941
|
sort: z.string().trim().min(1).optional().describe("Optional MercadoLibre sort expression."),
|
|
30
|
-
|
|
31
|
-
|
|
942
|
+
responseMode: z.enum(["calculations", "orders_chunk"]).optional().default("calculations").describe("Response mode. calculations (default) fetches all matching orders internally and returns only aggregate KPIs. orders_chunk returns compact order rows for up to 15 MercadoLibre pages and includes continuation metadata."),
|
|
943
|
+
offset: z.number().int().min(0).max(5e3).optional().describe("Starting offset for responseMode=orders_chunk. Use metadata.next_offset to continue."),
|
|
944
|
+
limit: z.number().int().min(1).max(50).optional().describe("MercadoLibre page size for responseMode=orders_chunk (1-50). Defaults to 50.")
|
|
32
945
|
});
|
|
33
946
|
async function meliGetOrdersSummaryHandler(params) {
|
|
34
947
|
try {
|
|
@@ -37,6 +950,9 @@ async function meliGetOrdersSummaryHandler(params) {
|
|
|
37
950
|
return profileResolution.response;
|
|
38
951
|
}
|
|
39
952
|
const profileId = profileResolution.value.profileId;
|
|
953
|
+
const responseMode = params.responseMode ?? "calculations";
|
|
954
|
+
const requestedLimit = responseMode === "orders_chunk" ? params.limit ?? DEFAULT_ORDER_DETAIL_PAGE_LIMIT : DEFAULT_ORDER_DETAIL_PAGE_LIMIT;
|
|
955
|
+
const requestedOffset = responseMode === "orders_chunk" ? params.offset ?? 0 : 0;
|
|
40
956
|
const response = await searchMercadoLibreOrders(profileId, {
|
|
41
957
|
seller: "",
|
|
42
958
|
from: params.startDate,
|
|
@@ -44,54 +960,108 @@ async function meliGetOrdersSummaryHandler(params) {
|
|
|
44
960
|
status: params.status,
|
|
45
961
|
q: params.q,
|
|
46
962
|
sort: params.sort,
|
|
47
|
-
offset:
|
|
48
|
-
limit:
|
|
963
|
+
offset: requestedOffset,
|
|
964
|
+
limit: requestedLimit
|
|
49
965
|
});
|
|
50
|
-
const orders = asArray(response.results);
|
|
51
966
|
const paging = normalizeMercadoLibrePaging(asRecord(response.paging));
|
|
967
|
+
const baseOrders = asArray(response.results);
|
|
52
968
|
const metricsByCurrency = {};
|
|
53
|
-
const
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
969
|
+
const effectiveLimit = paging.limit || requestedLimit || baseOrders.length;
|
|
970
|
+
const effectiveOffset = paging.offset || requestedOffset || 0;
|
|
971
|
+
const maxPagesForResponse = responseMode === "orders_chunk" ? ORDER_DETAIL_MAX_PAGES_PER_CALL : Infinity;
|
|
972
|
+
const allOrders = [...baseOrders];
|
|
973
|
+
const failedPages = [];
|
|
974
|
+
let pagesRequested = 1;
|
|
975
|
+
let pagesSucceeded = 1;
|
|
976
|
+
if (effectiveLimit > 0 && paging.total > baseOrders.length) {
|
|
977
|
+
const nextOffsets = [];
|
|
978
|
+
const maxPagesRemaining = Math.max(0, maxPagesForResponse - 1);
|
|
979
|
+
let pagesAdded = 0;
|
|
980
|
+
for (let nextOffset = effectiveOffset + effectiveLimit; nextOffset < paging.total; nextOffset += effectiveLimit) {
|
|
981
|
+
if (responseMode === "orders_chunk" && pagesAdded >= maxPagesRemaining) {
|
|
982
|
+
break;
|
|
983
|
+
}
|
|
984
|
+
nextOffsets.push(nextOffset);
|
|
985
|
+
pagesAdded += 1;
|
|
986
|
+
}
|
|
987
|
+
pagesRequested += nextOffsets.length;
|
|
988
|
+
if (nextOffsets.length > 0) {
|
|
989
|
+
const batch = await searchMercadoLibreOrdersBatch(
|
|
990
|
+
profileId,
|
|
991
|
+
{
|
|
992
|
+
seller: "",
|
|
993
|
+
from: params.startDate,
|
|
994
|
+
to: params.endDate,
|
|
995
|
+
status: params.status,
|
|
996
|
+
q: params.q,
|
|
997
|
+
sort: params.sort,
|
|
998
|
+
limit: effectiveLimit
|
|
999
|
+
},
|
|
1000
|
+
nextOffsets,
|
|
1001
|
+
{
|
|
1002
|
+
maxConcurrency: PAGE_FETCH_CONCURRENCY,
|
|
1003
|
+
maxRetries: PAGE_FETCH_MAX_RETRIES
|
|
1004
|
+
}
|
|
1005
|
+
);
|
|
1006
|
+
pagesSucceeded += batch.successful.length;
|
|
1007
|
+
batch.successful.forEach((entry) => {
|
|
1008
|
+
allOrders.push(...asArray(entry.document.results));
|
|
1009
|
+
});
|
|
1010
|
+
batch.failed.forEach((failure) => {
|
|
1011
|
+
const failedOffset = Number(failure.id);
|
|
1012
|
+
failedPages.push({
|
|
1013
|
+
offset: failedOffset,
|
|
1014
|
+
limit: effectiveLimit,
|
|
1015
|
+
page_number: Math.floor(failedOffset / effectiveLimit) + 1,
|
|
1016
|
+
message: failure.message,
|
|
1017
|
+
status_code: failure.statusCode,
|
|
1018
|
+
attempts: failure.attempts,
|
|
1019
|
+
retryable: failure.retryable
|
|
1020
|
+
});
|
|
1021
|
+
});
|
|
1022
|
+
}
|
|
1023
|
+
}
|
|
1024
|
+
const compactOrders = responseMode === "orders_chunk" ? allOrders.map((order) => formatCompactOrder(order, metricsByCurrency)) : [];
|
|
1025
|
+
if (responseMode === "calculations") {
|
|
1026
|
+
for (const order of allOrders) {
|
|
1027
|
+
formatCompactOrder(order, metricsByCurrency);
|
|
1028
|
+
}
|
|
1029
|
+
}
|
|
78
1030
|
for (const metrics of Object.values(metricsByCurrency)) {
|
|
79
1031
|
metrics.revenue = roundMoney(metrics.revenue);
|
|
80
1032
|
metrics.avg_order_value = roundMoney(metrics.avg_order_value);
|
|
81
1033
|
}
|
|
1034
|
+
const returned = responseMode === "orders_chunk" ? compactOrders.length : allOrders.length;
|
|
1035
|
+
const metadata = buildOrdersSummaryMetadata({
|
|
1036
|
+
total: paging.total,
|
|
1037
|
+
effectiveLimit,
|
|
1038
|
+
effectiveOffset,
|
|
1039
|
+
returned,
|
|
1040
|
+
responseMode,
|
|
1041
|
+
pagesRequested,
|
|
1042
|
+
pagesSucceeded,
|
|
1043
|
+
pagesFailed: failedPages.length,
|
|
1044
|
+
failedOffsets: failedPages.map((failure) => failure.offset)
|
|
1045
|
+
});
|
|
1046
|
+
const calculations = responseMode === "calculations" ? buildCalculations(allOrders, metadata, failedPages) : void 0;
|
|
82
1047
|
return object(
|
|
83
1048
|
stripNulls({
|
|
84
1049
|
profile_id: profileId,
|
|
85
|
-
metadata:
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
1050
|
+
metadata: responseMode === "orders_chunk" ? {
|
|
1051
|
+
...buildMercadoLibrePaginationMetadata({
|
|
1052
|
+
total: paging.total,
|
|
1053
|
+
limit: effectiveLimit,
|
|
1054
|
+
offset: effectiveOffset,
|
|
1055
|
+
returned: compactOrders.length,
|
|
1056
|
+
nextField: "offset"
|
|
1057
|
+
}),
|
|
1058
|
+
...metadata
|
|
1059
|
+
} : metadata,
|
|
92
1060
|
metrics_by_currency: metricsByCurrency,
|
|
93
|
-
orders_schema: ordersSchema,
|
|
94
|
-
orders: compactOrders
|
|
1061
|
+
orders_schema: responseMode === "orders_chunk" ? ordersSchema : void 0,
|
|
1062
|
+
orders: responseMode === "orders_chunk" ? compactOrders : void 0,
|
|
1063
|
+
calculations,
|
|
1064
|
+
failed_pages: failedPages.length > 0 ? failedPages : void 0
|
|
95
1065
|
})
|
|
96
1066
|
);
|
|
97
1067
|
} catch (err) {
|