@unchainedshop/core-orders 4.8.26 → 4.8.28
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.
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { MigrationRepository } from '@unchainedshop/mongodb';
|
|
2
|
+
import type { PricingCalculation } from '@unchainedshop/utils';
|
|
3
|
+
export declare const convertOrderCalculationToNet: (calculation: PricingCalculation[]) => PricingCalculation[];
|
|
4
|
+
export default function migrateOrderCalculationToNet(repository: MigrationRepository): void;
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { OrdersCollection } from "../db/OrdersCollection.js";
|
|
2
|
+
const legacyCalculationSelector = {
|
|
3
|
+
'calculation.0': { $exists: true },
|
|
4
|
+
calculation: {
|
|
5
|
+
$not: { $elemMatch: { category: { $ne: 'TAXES' }, isNetPrice: true } },
|
|
6
|
+
},
|
|
7
|
+
};
|
|
8
|
+
export const convertOrderCalculationToNet = (calculation) => {
|
|
9
|
+
let previousBaseRow;
|
|
10
|
+
const attributed = calculation.map((row) => {
|
|
11
|
+
if (!row || !Number.isFinite(row.amount)) {
|
|
12
|
+
throw new Error('Calculation contains an invalid amount');
|
|
13
|
+
}
|
|
14
|
+
if (row.category !== 'TAXES') {
|
|
15
|
+
previousBaseRow = row;
|
|
16
|
+
return row;
|
|
17
|
+
}
|
|
18
|
+
const baseCategory = row.baseCategory || previousBaseRow?.category;
|
|
19
|
+
if (!baseCategory || baseCategory === 'TAXES') {
|
|
20
|
+
throw new Error('Cannot determine the base category of a tax row');
|
|
21
|
+
}
|
|
22
|
+
const discountId = row.discountId ??
|
|
23
|
+
(previousBaseRow?.category === baseCategory ? previousBaseRow.discountId : undefined);
|
|
24
|
+
return { ...row, baseCategory, discountId, isNetPrice: false };
|
|
25
|
+
});
|
|
26
|
+
const converted = attributed.flatMap((row) => {
|
|
27
|
+
if (row.category !== 'TAXES')
|
|
28
|
+
return [row];
|
|
29
|
+
return [
|
|
30
|
+
{
|
|
31
|
+
category: row.baseCategory,
|
|
32
|
+
amount: -row.amount,
|
|
33
|
+
discountId: row.discountId,
|
|
34
|
+
isNetPrice: true,
|
|
35
|
+
meta: row.meta,
|
|
36
|
+
},
|
|
37
|
+
row,
|
|
38
|
+
];
|
|
39
|
+
});
|
|
40
|
+
const sum = (rows, category, discountId) => rows.reduce((total, row) => (category === undefined || row.category === category) &&
|
|
41
|
+
(discountId === undefined || row.discountId === discountId)
|
|
42
|
+
? total + row.amount
|
|
43
|
+
: total, 0);
|
|
44
|
+
const taxes = attributed.filter((row) => row.category === 'TAXES');
|
|
45
|
+
const taxSum = (category, discountId) => sum(category === undefined ? taxes : taxes.filter((row) => row.baseCategory === category), undefined, discountId);
|
|
46
|
+
const categories = [
|
|
47
|
+
...new Set(attributed.map((row) => (row.category === 'TAXES' ? row.baseCategory : row.category))),
|
|
48
|
+
];
|
|
49
|
+
const discountIds = [...new Set(attributed.map((row) => row.discountId))].filter((discountId) => discountId !== undefined);
|
|
50
|
+
let roundingCategory = 'ROUNDING';
|
|
51
|
+
while (categories.includes(roundingCategory))
|
|
52
|
+
roundingCategory = `_${roundingCategory}`;
|
|
53
|
+
const scopes = [
|
|
54
|
+
...categories.flatMap((category) => [
|
|
55
|
+
...discountIds.map((discountId) => ({ category, discountId })),
|
|
56
|
+
{ category, discountId: undefined },
|
|
57
|
+
]),
|
|
58
|
+
...discountIds.map((discountId) => ({ category: undefined, discountId })),
|
|
59
|
+
{ category: undefined, discountId: undefined },
|
|
60
|
+
];
|
|
61
|
+
const balances = scopes.map(({ category, discountId }) => ({
|
|
62
|
+
category,
|
|
63
|
+
discountId,
|
|
64
|
+
amount: sum(attributed, category, discountId) - taxSum(category, discountId),
|
|
65
|
+
}));
|
|
66
|
+
for (const { category, discountId, amount } of balances) {
|
|
67
|
+
const difference = amount - sum(converted, category, discountId);
|
|
68
|
+
if (!Number.isFinite(amount) || !Number.isFinite(difference)) {
|
|
69
|
+
throw new Error('Calculation contains an invalid balance');
|
|
70
|
+
}
|
|
71
|
+
if (difference) {
|
|
72
|
+
converted.push({
|
|
73
|
+
category: category ?? roundingCategory,
|
|
74
|
+
discountId,
|
|
75
|
+
amount: difference,
|
|
76
|
+
isNetPrice: true,
|
|
77
|
+
meta: { migration: 20260907120000, reason: 'Preserve historical floating-point balance' },
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
for (const { category, discountId, amount } of balances) {
|
|
82
|
+
if (sum(converted, category, discountId) !== amount) {
|
|
83
|
+
throw new Error(`Cannot preserve historical balance for ${category ?? 'order'} ${discountId ?? ''}`);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
for (const discountId of discountIds) {
|
|
87
|
+
const before = sum(attributed, 'DISCOUNTS', discountId);
|
|
88
|
+
const after = sum(converted, 'DISCOUNTS', discountId) + taxSum('DISCOUNTS', discountId);
|
|
89
|
+
if (Math.round(before) !== Math.round(after) || Boolean(before) !== Boolean(after)) {
|
|
90
|
+
throw new Error(`Cannot preserve historical discount ${discountId}`);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return converted;
|
|
94
|
+
};
|
|
95
|
+
export default function migrateOrderCalculationToNet(repository) {
|
|
96
|
+
repository?.register({
|
|
97
|
+
id: 20260907120000,
|
|
98
|
+
name: 'Convert persisted order calculations from gross to net category balances',
|
|
99
|
+
up: async ({ logger }) => {
|
|
100
|
+
const Orders = await OrdersCollection(repository.db);
|
|
101
|
+
const projection = { _id: 1, calculation: 1 };
|
|
102
|
+
let migrated = 0;
|
|
103
|
+
for await (const order of Orders.find(legacyCalculationSelector, { projection })) {
|
|
104
|
+
let calculation;
|
|
105
|
+
try {
|
|
106
|
+
calculation = convertOrderCalculationToNet(order.calculation);
|
|
107
|
+
}
|
|
108
|
+
catch (error) {
|
|
109
|
+
throw new Error(`Cannot migrate order ${order._id}: ${error.message}`, { cause: error });
|
|
110
|
+
}
|
|
111
|
+
if (!calculation.some((row) => row.isNetPrice === true)) {
|
|
112
|
+
calculation = calculation.map((row) => ({ ...row, isNetPrice: true }));
|
|
113
|
+
}
|
|
114
|
+
const result = await Orders.updateOne({ _id: order._id, calculation: order.calculation }, { $set: { calculation } });
|
|
115
|
+
if (result.matchedCount) {
|
|
116
|
+
migrated += 1;
|
|
117
|
+
}
|
|
118
|
+
else if (await Orders.findOne({ _id: order._id, ...legacyCalculationSelector }, { projection })) {
|
|
119
|
+
throw new Error(`Order ${order._id} changed during migration; retry after stopping old writers`);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
logger?.info(`Converted ${migrated} order calculation(s) to net category balances`);
|
|
123
|
+
},
|
|
124
|
+
});
|
|
125
|
+
}
|
|
@@ -29,12 +29,13 @@ export const configureOrdersModuleQueries = ({ Orders }) => {
|
|
|
29
29
|
const options = {
|
|
30
30
|
sort: {
|
|
31
31
|
updated: -1,
|
|
32
|
+
_id: -1,
|
|
32
33
|
},
|
|
33
34
|
};
|
|
34
35
|
return Orders.findOne(selector, options);
|
|
35
36
|
},
|
|
36
37
|
findCarts: async ({ userIds }, options) => {
|
|
37
|
-
return Orders.find({ status: { $eq: null }, userId: { $in: userIds } }, { sort: { updated: -1 }, ...options }).toArray();
|
|
38
|
+
return Orders.find({ status: { $eq: null }, userId: { $in: userIds } }, { sort: { updated: -1, _id: -1 }, ...options }).toArray();
|
|
38
39
|
},
|
|
39
40
|
count: async (query) => {
|
|
40
41
|
const orderCount = await Orders.countDocuments(buildFindSelector(query));
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unchainedshop/core-orders",
|
|
3
3
|
"description": "Order management module for the Unchained Engine",
|
|
4
|
-
"version": "4.8.
|
|
4
|
+
"version": "4.8.28",
|
|
5
5
|
"main": "lib/orders-index.js",
|
|
6
6
|
"types": "lib/orders-index.d.ts",
|
|
7
7
|
"type": "module",
|
|
@@ -35,9 +35,9 @@
|
|
|
35
35
|
},
|
|
36
36
|
"homepage": "https://github.com/unchainedshop/unchained#readme",
|
|
37
37
|
"dependencies": {
|
|
38
|
-
"@unchainedshop/events": "^4.8.
|
|
39
|
-
"@unchainedshop/mongodb": "^4.8.
|
|
40
|
-
"@unchainedshop/utils": "^4.8.
|
|
38
|
+
"@unchainedshop/events": "^4.8.27",
|
|
39
|
+
"@unchainedshop/mongodb": "^4.8.27",
|
|
40
|
+
"@unchainedshop/utils": "^4.8.27"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
43
|
"@types/node": "^26.2.0",
|