@tallyui/connector-medusa 5.1.0 → 5.2.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/index.js +37 -4
- package/package.json +3 -3
- package/src/index.ts +2 -0
- package/src/orders.ts +42 -9
package/dist/index.js
CHANGED
|
@@ -1115,6 +1115,7 @@ var MEDUSA_DISPLAY_NUMBER_MAX_DIGITS = 6;
|
|
|
1115
1115
|
var MEDUSA_ORDERS_PLUGIN_CONTRACT = 3;
|
|
1116
1116
|
var MEDUSA_ALL_CHANNELS_PLUGIN_CONTRACT = 4;
|
|
1117
1117
|
var REFUND_LINES_PLUGIN_CONTRACT = 6;
|
|
1118
|
+
var SOLD_FIGURES_PLUGIN_CONTRACT = 7;
|
|
1118
1119
|
var REFUND_LINES_IDS_MAX = 100;
|
|
1119
1120
|
var hasOrdersRoute = (capabilities) => (capabilities?.pluginContract ?? 0) >= MEDUSA_ORDERS_PLUGIN_CONTRACT;
|
|
1120
1121
|
function medusaOrderFilters(capabilities) {
|
|
@@ -1180,9 +1181,35 @@ function withRefundLines(orders, byOrder) {
|
|
|
1180
1181
|
}) } : order;
|
|
1181
1182
|
});
|
|
1182
1183
|
}
|
|
1184
|
+
function soldFiguresByOrder(raws, orders) {
|
|
1185
|
+
const quantities = new Map(orders.map((order) => [order.id, new Map(order.lines.map((line) => [line.id, line.quantity]))]));
|
|
1186
|
+
const byOrder = /* @__PURE__ */ new Map();
|
|
1187
|
+
for (const raw of rows(raws)) {
|
|
1188
|
+
if (typeof raw.id !== "string") continue;
|
|
1189
|
+
const known = quantities.get(raw.id);
|
|
1190
|
+
if (!known) continue;
|
|
1191
|
+
for (const { id: id2, soldTotalMinor, soldTaxMinor, returnedQuantity } of rows(raw.lines)) {
|
|
1192
|
+
if (typeof id2 !== "string") continue;
|
|
1193
|
+
const quantity = known.get(id2);
|
|
1194
|
+
if (quantity === void 0 || !isCount(soldTotalMinor, 0) || !isCount(soldTaxMinor, 0) || !isCount(returnedQuantity, 0) || returnedQuantity > quantity) continue;
|
|
1195
|
+
const figures = byOrder.get(raw.id) ?? /* @__PURE__ */ new Map();
|
|
1196
|
+
figures.set(id2, { soldTotalMinor, soldTaxMinor, returnedQuantity });
|
|
1197
|
+
byOrder.set(raw.id, figures);
|
|
1198
|
+
}
|
|
1199
|
+
}
|
|
1200
|
+
return byOrder;
|
|
1201
|
+
}
|
|
1202
|
+
function withSoldFigures(orders, byOrder) {
|
|
1203
|
+
return orders.map((order) => {
|
|
1204
|
+
const figures = byOrder.get(order.id);
|
|
1205
|
+
if (!figures) return order;
|
|
1206
|
+
return { ...order, lines: order.lines.map((line) => figures.has(line.id) ? { ...line, ...figures.get(line.id) } : line) };
|
|
1207
|
+
});
|
|
1208
|
+
}
|
|
1183
1209
|
async function readRefundLines(context, orders) {
|
|
1184
|
-
|
|
1185
|
-
|
|
1210
|
+
const contract = context.capabilities?.pluginContract ?? 0;
|
|
1211
|
+
if (contract < REFUND_LINES_PLUGIN_CONTRACT) return orders;
|
|
1212
|
+
const ids = orders.filter((order) => contract >= SOLD_FIGURES_PLUGIN_CONTRACT || order.refunds.length > 0).map((order) => order.id);
|
|
1186
1213
|
const raws = [];
|
|
1187
1214
|
for (let start = 0; start < ids.length; start += REFUND_LINES_IDS_MAX) {
|
|
1188
1215
|
const chunk = ids.slice(start, start + REFUND_LINES_IDS_MAX);
|
|
@@ -1194,7 +1221,9 @@ async function readRefundLines(context, orders) {
|
|
|
1194
1221
|
if (context.signal?.aborted) throw error;
|
|
1195
1222
|
}
|
|
1196
1223
|
}
|
|
1197
|
-
|
|
1224
|
+
if (!raws.length) return orders;
|
|
1225
|
+
const refunded = withRefundLines(orders, refundLinesByOrder(raws, orders));
|
|
1226
|
+
return contract >= SOLD_FIGURES_PLUGIN_CONTRACT ? withSoldFigures(refunded, soldFiguresByOrder(raws, orders)) : refunded;
|
|
1198
1227
|
}
|
|
1199
1228
|
var DISPLAY_NUMBER = new RegExp(`^\\d{1,${MEDUSA_DISPLAY_NUMBER_MAX_DIGITS}}$`);
|
|
1200
1229
|
async function listThroughPlugin(context, params) {
|
|
@@ -1211,7 +1240,9 @@ async function listThroughPlugin(context, params) {
|
|
|
1211
1240
|
const byId = new Map(rows(body.orders).map((row) => [row.id, row]));
|
|
1212
1241
|
const picked = ids.flatMap((id2) => byId.get(id2) ?? []);
|
|
1213
1242
|
const { orders: read2, unreadable } = readOrders(picked);
|
|
1214
|
-
const
|
|
1243
|
+
const contract = context.capabilities?.pluginContract ?? 0;
|
|
1244
|
+
const refunded = contract >= REFUND_LINES_PLUGIN_CONTRACT ? withRefundLines(read2, refundLinesByOrder(hits, read2)) : read2;
|
|
1245
|
+
const orders = contract >= SOLD_FIGURES_PLUGIN_CONTRACT ? withSoldFigures(refunded, soldFiguresByOrder(hits, read2)) : refunded;
|
|
1215
1246
|
const left = unreadable + ids.length - picked.length;
|
|
1216
1247
|
return { orders, hasMore, ...left ? { unreadable: left } : {}, ...counted };
|
|
1217
1248
|
}
|
|
@@ -1522,6 +1553,8 @@ function createMedusaConnector() {
|
|
|
1522
1553
|
};
|
|
1523
1554
|
}
|
|
1524
1555
|
var medusaConnectorParts = {
|
|
1556
|
+
// Medusa's live item totals shrink once a return is received; refunds price from the plugin's sold figures (contract 7).
|
|
1557
|
+
lineTotalsShrinkOnReturn: true,
|
|
1525
1558
|
id: "medusa",
|
|
1526
1559
|
name: "MedusaJS",
|
|
1527
1560
|
description: "Connect to MedusaJS v2 backends via the Admin API",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tallyui/connector-medusa",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.2.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "MedusaJS v2 connector for Tally UI",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -29,10 +29,10 @@
|
|
|
29
29
|
"directory": "connectors/medusa"
|
|
30
30
|
},
|
|
31
31
|
"peerDependencies": {
|
|
32
|
-
"@tallyui/core": "^5.
|
|
32
|
+
"@tallyui/core": "^5.2.0"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
|
-
"@tallyui/database": "5.
|
|
35
|
+
"@tallyui/database": "5.2.0",
|
|
36
36
|
"rxdb": "17.5.0"
|
|
37
37
|
},
|
|
38
38
|
"scripts": {
|
package/src/index.ts
CHANGED
|
@@ -141,6 +141,8 @@ export function createMedusaConnector(): TallyConnector {
|
|
|
141
141
|
|
|
142
142
|
// Everything but the reconcile feed and what is wired to it: stateless, so every instance shares it.
|
|
143
143
|
const medusaConnectorParts = {
|
|
144
|
+
// Medusa's live item totals shrink once a return is received; refunds price from the plugin's sold figures (contract 7).
|
|
145
|
+
lineTotalsShrinkOnReturn: true,
|
|
144
146
|
id: 'medusa',
|
|
145
147
|
name: 'MedusaJS',
|
|
146
148
|
description: 'Connect to MedusaJS v2 backends via the Admin API',
|
package/src/orders.ts
CHANGED
|
@@ -131,6 +131,8 @@ export const MEDUSA_ORDERS_PLUGIN_CONTRACT = 3;
|
|
|
131
131
|
export const MEDUSA_ALL_CHANNELS_PLUGIN_CONTRACT = 4;
|
|
132
132
|
/** The MedusaPOS plugin contract whose GET /tally/v1/orders takes `ids=` and returns `refunds[].lines` (#1324). */
|
|
133
133
|
const REFUND_LINES_PLUGIN_CONTRACT = 6;
|
|
134
|
+
/** The MedusaPOS plugin contract whose GET /tally/v1/orders lines carry soldTotalMinor, soldTaxMinor and returnedQuantity (medusapos/app#636). */
|
|
135
|
+
const SOLD_FIGURES_PLUGIN_CONTRACT = 7;
|
|
134
136
|
/** The plugin's cap on `ids=` per request, its ORDERS_MAX_PER_PAGE (#1324). */
|
|
135
137
|
const REFUND_LINES_IDS_MAX = 100;
|
|
136
138
|
|
|
@@ -216,13 +218,40 @@ function withRefundLines(orders: StoreOrder[], byOrder: Map<string, Map<string,
|
|
|
216
218
|
});
|
|
217
219
|
}
|
|
218
220
|
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
221
|
+
function soldFiguresByOrder(raws: readonly unknown[], orders: readonly StoreOrder[]) {
|
|
222
|
+
const quantities = new Map(orders.map((order) => [order.id, new Map(order.lines.map((line) => [line.id, line.quantity]))]));
|
|
223
|
+
type SoldFigures = Required<Pick<StoreOrderLine, 'soldTotalMinor' | 'soldTaxMinor' | 'returnedQuantity'>>;
|
|
224
|
+
const byOrder = new Map<string, Map<string, SoldFigures>>();
|
|
225
|
+
for (const raw of rows(raws)) {
|
|
226
|
+
if (typeof raw.id !== 'string') continue;
|
|
227
|
+
const known = quantities.get(raw.id);
|
|
228
|
+
if (!known) continue;
|
|
229
|
+
for (const { id, soldTotalMinor, soldTaxMinor, returnedQuantity } of rows(raw.lines)) {
|
|
230
|
+
if (typeof id !== 'string') continue;
|
|
231
|
+
const quantity = known.get(id);
|
|
232
|
+
if (quantity === undefined || !isCount(soldTotalMinor, 0) || !isCount(soldTaxMinor, 0) || !isCount(returnedQuantity, 0) || returnedQuantity > quantity) continue;
|
|
233
|
+
const figures = byOrder.get(raw.id) ?? new Map<string, SoldFigures>();
|
|
234
|
+
figures.set(id, { soldTotalMinor, soldTaxMinor, returnedQuantity });
|
|
235
|
+
byOrder.set(raw.id, figures);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
return byOrder;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
function withSoldFigures(orders: StoreOrder[], byOrder: ReturnType<typeof soldFiguresByOrder>): StoreOrder[] {
|
|
242
|
+
return orders.map((order) => {
|
|
243
|
+
const figures = byOrder.get(order.id);
|
|
244
|
+
if (!figures) return order;
|
|
245
|
+
return { ...order, lines: order.lines.map((line) => figures.has(line.id) ? { ...line, ...figures.get(line.id) } : line) };
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/** Reads refund lines for orders with refunds at contract 6, and also sold figures for every order from contract 7.
|
|
250
|
+
* Reads one chunk of REFUND_LINES_IDS_MAX at a time; a failed chunk adds neither, unless the read was cancelled. */
|
|
223
251
|
async function readRefundLines(context: SyncContext, orders: StoreOrder[]): Promise<StoreOrder[]> {
|
|
224
|
-
|
|
225
|
-
|
|
252
|
+
const contract = context.capabilities?.pluginContract ?? 0;
|
|
253
|
+
if (contract < REFUND_LINES_PLUGIN_CONTRACT) return orders;
|
|
254
|
+
const ids = orders.filter((order) => contract >= SOLD_FIGURES_PLUGIN_CONTRACT || order.refunds.length > 0).map((order) => order.id);
|
|
226
255
|
const raws: unknown[] = [];
|
|
227
256
|
for (let start = 0; start < ids.length; start += REFUND_LINES_IDS_MAX) {
|
|
228
257
|
const chunk = ids.slice(start, start + REFUND_LINES_IDS_MAX);
|
|
@@ -235,7 +264,9 @@ async function readRefundLines(context: SyncContext, orders: StoreOrder[]): Prom
|
|
|
235
264
|
if (context.signal?.aborted) throw error;
|
|
236
265
|
}
|
|
237
266
|
}
|
|
238
|
-
|
|
267
|
+
if (!raws.length) return orders;
|
|
268
|
+
const refunded = withRefundLines(orders, refundLinesByOrder(raws, orders));
|
|
269
|
+
return contract >= SOLD_FIGURES_PLUGIN_CONTRACT ? withSoldFigures(refunded, soldFiguresByOrder(raws, orders)) : refunded;
|
|
239
270
|
}
|
|
240
271
|
|
|
241
272
|
// An all-digits search the plugin's route matches against display_id (medusapos orders/list.ts:78-84); the admin API has no number search.
|
|
@@ -243,6 +274,7 @@ const DISPLAY_NUMBER = new RegExp(`^\\d{1,${MEDUSA_DISPLAY_NUMBER_MAX_DIGITS}}$`
|
|
|
243
274
|
|
|
244
275
|
/**
|
|
245
276
|
* The plugin's `GET /tally/v1/orders` picks the page's ids; the admin API reads those rows, so every order maps the same way.
|
|
277
|
+
* The hits also supply refund lines from contract 6 and sold figures from contract 7 without another request.
|
|
246
278
|
* The route takes only page, perPage, search, status, customerId, registerId, after and before (plus allChannels from contract 4); any other key is a 400 (list.ts:18, 30-34).
|
|
247
279
|
*/
|
|
248
280
|
async function listThroughPlugin(context: SyncContext, params: URLSearchParams): Promise<StoreOrderPage> {
|
|
@@ -261,8 +293,9 @@ async function listThroughPlugin(context: SyncContext, params: URLSearchParams):
|
|
|
261
293
|
const byId = new Map(rows(body.orders).map((row) => [row.id, row]));
|
|
262
294
|
const picked = ids.flatMap((id) => byId.get(id) ?? []);
|
|
263
295
|
const { orders: read, unreadable } = readOrders(picked);
|
|
264
|
-
|
|
265
|
-
const
|
|
296
|
+
const contract = context.capabilities?.pluginContract ?? 0;
|
|
297
|
+
const refunded = contract >= REFUND_LINES_PLUGIN_CONTRACT ? withRefundLines(read, refundLinesByOrder(hits, read)) : read;
|
|
298
|
+
const orders = contract >= SOLD_FIGURES_PLUGIN_CONTRACT ? withSoldFigures(refunded, soldFiguresByOrder(hits, read)) : refunded;
|
|
266
299
|
// An id the admin answer lacks is left out and counted like a row the mapper cannot read.
|
|
267
300
|
const left = unreadable + ids.length - picked.length;
|
|
268
301
|
return { orders, hasMore, ...(left ? { unreadable: left } : {}), ...counted };
|