@unchainedshop/core-orders 4.8.29 → 5.0.0-alpha.10
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/README.md +7 -115
- package/lib/module/configureOrderDiscountsModule.js +5 -6
- package/lib/module/configureOrderPaymentsModule.js +1 -1
- package/lib/module/configureOrderPositionsModule.js +5 -9
- package/lib/module/configureOrdersModule-mutations.d.ts +1 -1
- package/lib/module/configureOrdersModule-mutations.js +2 -1
- package/lib/module/configureOrdersModule-queries.js +18 -10
- package/lib/module/configureOrdersModule.d.ts +1 -1
- package/lib/module/configureOrdersModule.js +2 -0
- package/package.json +8 -5
package/README.md
CHANGED
|
@@ -13,126 +13,18 @@ npm install @unchainedshop/core-orders
|
|
|
13
13
|
|
|
14
14
|
## Usage
|
|
15
15
|
|
|
16
|
-
|
|
17
|
-
import { configureOrdersModule, OrderStatus } from '@unchainedshop/core-orders';
|
|
18
|
-
|
|
19
|
-
const ordersModule = await configureOrdersModule({ db });
|
|
20
|
-
|
|
21
|
-
// Create an order
|
|
22
|
-
const orderId = await ordersModule.create({
|
|
23
|
-
userId: 'user-123',
|
|
24
|
-
currency: 'CHF',
|
|
25
|
-
countryCode: 'CH',
|
|
26
|
-
});
|
|
16
|
+
The platform initializes this module as `platform.unchainedAPI.modules.orders`.
|
|
27
17
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
orderId,
|
|
31
|
-
productId: 'product-456',
|
|
32
|
-
quantity: 2,
|
|
33
|
-
});
|
|
18
|
+
```typescript
|
|
19
|
+
const { orders } = platform.unchainedAPI.modules;
|
|
34
20
|
|
|
35
|
-
|
|
36
|
-
await
|
|
21
|
+
const order = await orders.findOrder({ orderId: 'order-123' });
|
|
22
|
+
const userOrders = await orders.findOrders({ userId: 'user-123', limit: 20 });
|
|
37
23
|
```
|
|
38
24
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
### Module Configuration
|
|
42
|
-
|
|
43
|
-
| Export | Description |
|
|
44
|
-
|--------|-------------|
|
|
45
|
-
| `configureOrdersModule` | Configure and return the orders module |
|
|
46
|
-
|
|
47
|
-
### Queries
|
|
48
|
-
|
|
49
|
-
| Method | Description |
|
|
50
|
-
|--------|-------------|
|
|
51
|
-
| `findOrder` | Find order by ID or number |
|
|
52
|
-
| `findOrders` | Find orders with filtering and pagination |
|
|
53
|
-
| `count` | Count orders matching query |
|
|
54
|
-
| `orderExists` | Check if order exists |
|
|
55
|
-
|
|
56
|
-
### Mutations
|
|
57
|
-
|
|
58
|
-
| Method | Description |
|
|
59
|
-
|--------|-------------|
|
|
60
|
-
| `create` | Create a new order |
|
|
61
|
-
| `update` | Update order data |
|
|
62
|
-
| `delete` | Delete an order |
|
|
63
|
-
| `checkout` | Process order checkout |
|
|
64
|
-
| `confirm` | Confirm an order |
|
|
65
|
-
| `reject` | Reject an order |
|
|
66
|
-
| `setPaymentProvider` | Set payment provider |
|
|
67
|
-
| `setDeliveryProvider` | Set delivery provider |
|
|
68
|
-
|
|
69
|
-
### Submodules
|
|
70
|
-
|
|
71
|
-
#### Positions (`orders.positions`)
|
|
72
|
-
| Method | Description |
|
|
73
|
-
|--------|-------------|
|
|
74
|
-
| `findPositions` | Find order positions |
|
|
75
|
-
| `create` | Add position to order |
|
|
76
|
-
| `update` | Update position |
|
|
77
|
-
| `delete` | Remove position |
|
|
78
|
-
|
|
79
|
-
#### Payments (`orders.payments`)
|
|
80
|
-
| Method | Description |
|
|
81
|
-
|--------|-------------|
|
|
82
|
-
| `findPayment` | Find order payment |
|
|
83
|
-
| `create` | Create payment for order |
|
|
84
|
-
| `markPaid` | Mark payment as paid |
|
|
85
|
-
| `charge` | Charge the payment |
|
|
86
|
-
|
|
87
|
-
#### Deliveries (`orders.deliveries`)
|
|
88
|
-
| Method | Description |
|
|
89
|
-
|--------|-------------|
|
|
90
|
-
| `findDelivery` | Find order delivery |
|
|
91
|
-
| `create` | Create delivery for order |
|
|
92
|
-
| `markDelivered` | Mark as delivered |
|
|
93
|
-
| `send` | Trigger delivery |
|
|
94
|
-
|
|
95
|
-
#### Discounts (`orders.discounts`)
|
|
96
|
-
| Method | Description |
|
|
97
|
-
|--------|-------------|
|
|
98
|
-
| `findDiscounts` | Find order discounts |
|
|
99
|
-
| `create` | Add discount to order |
|
|
100
|
-
| `delete` | Remove discount |
|
|
101
|
-
|
|
102
|
-
### Constants
|
|
103
|
-
|
|
104
|
-
| Export | Description |
|
|
105
|
-
|--------|-------------|
|
|
106
|
-
| `OrderStatus` | Order status values (OPEN, PENDING, CONFIRMED, FULFILLED, REJECTED) |
|
|
107
|
-
|
|
108
|
-
### Settings
|
|
109
|
-
|
|
110
|
-
| Export | Description |
|
|
111
|
-
|--------|-------------|
|
|
112
|
-
| `ordersSettings` | Access order module settings |
|
|
113
|
-
|
|
114
|
-
### Types
|
|
115
|
-
|
|
116
|
-
| Export | Description |
|
|
117
|
-
|--------|-------------|
|
|
118
|
-
| `Order` | Order document type |
|
|
119
|
-
| `OrderPosition` | Position document type |
|
|
120
|
-
| `OrderPayment` | Payment document type |
|
|
121
|
-
| `OrderDelivery` | Delivery document type |
|
|
122
|
-
| `OrderDiscount` | Discount document type |
|
|
123
|
-
| `OrdersModule` | Module interface type |
|
|
124
|
-
|
|
125
|
-
## Events
|
|
25
|
+
Order positions, payments, deliveries, discounts, and statistics are exposed through the corresponding submodules. Use `unchainedAPI.services.orders` or the GraphQL API for cart operations and checkout so pricing, validation, payment, and delivery are coordinated.
|
|
126
26
|
|
|
127
|
-
|
|
128
|
-
|-------|-------------|
|
|
129
|
-
| `ORDER_CREATE` | Order created |
|
|
130
|
-
| `ORDER_UPDATE` | Order updated |
|
|
131
|
-
| `ORDER_REMOVE` | Order deleted |
|
|
132
|
-
| `ORDER_CHECKOUT` | Order checked out |
|
|
133
|
-
| `ORDER_CONFIRMED` | Order confirmed |
|
|
134
|
-
| `ORDER_REJECTED` | Order rejected |
|
|
135
|
-
| `ORDER_FULFILLED` | Order fulfilled |
|
|
27
|
+
See the [module guide](https://docs.unchained.shop/platform-configuration/modules/orders), [checkout guide](https://docs.unchained.shop/guides/checkout-implementation), [public exports](src/orders-index.ts), and [module implementation](src/module/configureOrdersModule.ts).
|
|
136
28
|
|
|
137
29
|
## License
|
|
138
30
|
|
|
@@ -14,12 +14,11 @@ export const configureOrderDiscountsModule = ({ OrderDiscounts, }) => {
|
|
|
14
14
|
return OrderDiscounts.findOne(buildFindOrderDiscountByIdSelector(discountId), options);
|
|
15
15
|
},
|
|
16
16
|
findOrderDiscounts: async (query) => {
|
|
17
|
-
if ('orderIds' in query)
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
return OrderDiscounts.find({ orderId: query.orderId }, { sort: { created: 1 } }).toArray();
|
|
17
|
+
if ('orderIds' in query && !query.orderIds.length)
|
|
18
|
+
return [];
|
|
19
|
+
const orderId = 'orderIds' in query ? { $in: query.orderIds } : query.orderId;
|
|
20
|
+
const discounts = OrderDiscounts.find({ orderId }, { sort: { created: 1, _id: 1 } });
|
|
21
|
+
return discounts.toArray();
|
|
23
22
|
},
|
|
24
23
|
create: async (doc) => {
|
|
25
24
|
const normalizedTrigger = doc.trigger || OrderDiscountTrigger.USER;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { emit, registerEvents } from '@unchainedshop/events';
|
|
2
2
|
import { generateDbFilterById, generateDbObjectId, mongodb } from '@unchainedshop/mongodb';
|
|
3
3
|
import { OrderPaymentStatus } from "../db/OrderPaymentsCollection.js";
|
|
4
|
-
const ORDER_PAYMENT_EVENTS = ['ORDER_UPDATE_PAYMENT', '
|
|
4
|
+
const ORDER_PAYMENT_EVENTS = ['ORDER_UPDATE_PAYMENT', 'ORDER_PAY'];
|
|
5
5
|
export const buildFindOrderPaymentByIdSelector = (orderPaymentId) => generateDbFilterById(orderPaymentId);
|
|
6
6
|
export const configureOrderPaymentsModule = ({ OrderPayments, }) => {
|
|
7
7
|
registerEvents(ORDER_PAYMENT_EVENTS);
|
|
@@ -14,15 +14,11 @@ export const configureOrderPositionsModule = ({ OrderPositions, }) => {
|
|
|
14
14
|
return OrderPositions.findOne(buildFindOrderPositionByIdSelector(itemId), options);
|
|
15
15
|
},
|
|
16
16
|
findOrderPositions: async (query) => {
|
|
17
|
-
if ('orderIds' in query)
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
quantity: { $gt: 0 },
|
|
23
|
-
}, { sort: { created: 1 } }).toArray();
|
|
24
|
-
}
|
|
25
|
-
return OrderPositions.find({ orderId: query.orderId, quantity: { $gt: 0 } }, { sort: { created: 1 } }).toArray();
|
|
17
|
+
if ('orderIds' in query && !query.orderIds.length)
|
|
18
|
+
return [];
|
|
19
|
+
const orderId = 'orderIds' in query ? { $in: query.orderIds } : query.orderId;
|
|
20
|
+
const positions = OrderPositions.find({ orderId, quantity: { $gt: 0 } }, { sort: { created: 1, _id: 1 } });
|
|
21
|
+
return positions.toArray();
|
|
26
22
|
},
|
|
27
23
|
delete: async (orderPositionId) => {
|
|
28
24
|
const selector = buildFindOrderPositionByIdSelector(orderPositionId);
|
|
@@ -5,7 +5,7 @@ export declare const configureOrderModuleMutations: ({ Orders, OrderPositions, }
|
|
|
5
5
|
Orders: mongodb.Collection<Order>;
|
|
6
6
|
OrderPositions: mongodb.Collection<OrderPosition>;
|
|
7
7
|
}) => {
|
|
8
|
-
create: ({ userId, orderNumber, currencyCode, countryCode, billingAddress, contact, context, }: {
|
|
8
|
+
create: ({ userId, orderNumber, currencyCode, countryCode, billingAddress, contact, originEnrollmentId, context, }: {
|
|
9
9
|
userId: string;
|
|
10
10
|
billingAddress?: Address;
|
|
11
11
|
contact?: Contact;
|
|
@@ -20,7 +20,7 @@ export const configureOrderModuleMutations = ({ Orders, OrderPositions, }) => {
|
|
|
20
20
|
};
|
|
21
21
|
};
|
|
22
22
|
return {
|
|
23
|
-
create: async ({ userId, orderNumber, currencyCode, countryCode, billingAddress, contact, context, }) => {
|
|
23
|
+
create: async ({ userId, orderNumber, currencyCode, countryCode, billingAddress, contact, originEnrollmentId, context, }) => {
|
|
24
24
|
const { insertedId: orderId } = await Orders.insertOne({
|
|
25
25
|
_id: generateDbObjectId(),
|
|
26
26
|
created: new Date(),
|
|
@@ -33,6 +33,7 @@ export const configureOrderModuleMutations = ({ Orders, OrderPositions, }) => {
|
|
|
33
33
|
calculation: [],
|
|
34
34
|
log: [],
|
|
35
35
|
orderNumber,
|
|
36
|
+
originEnrollmentId,
|
|
36
37
|
context: context || {},
|
|
37
38
|
});
|
|
38
39
|
const order = (await Orders.findOne(generateDbFilterById(orderId), {}));
|
|
@@ -66,6 +66,7 @@ export const configureOrdersModuleQueries = ({ Orders }) => {
|
|
|
66
66
|
const selector = buildFindSelector({ queryString, ...query });
|
|
67
67
|
if (queryString) {
|
|
68
68
|
return Orders.find(selector, {
|
|
69
|
+
...findOptions,
|
|
69
70
|
...options,
|
|
70
71
|
projection: { score: { $meta: 'textScore' } },
|
|
71
72
|
sort: { score: { $meta: 'textScore' } },
|
|
@@ -160,19 +161,26 @@ export const configureOrdersModuleQueries = ({ Orders }) => {
|
|
|
160
161
|
created: 1,
|
|
161
162
|
currencyCode: 1,
|
|
162
163
|
itemAmount: {
|
|
163
|
-
$
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
$
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
164
|
+
$reduce: {
|
|
165
|
+
input: {
|
|
166
|
+
$filter: {
|
|
167
|
+
input: { $ifNull: ['$calculation', []] },
|
|
168
|
+
as: 'c',
|
|
169
|
+
cond: {
|
|
170
|
+
$or: [
|
|
171
|
+
{ $eq: ['$$c.category', 'ITEMS'] },
|
|
172
|
+
{
|
|
173
|
+
$and: [
|
|
174
|
+
{ $eq: ['$$c.category', 'TAXES'] },
|
|
175
|
+
{ $eq: ['$$c.baseCategory', 'ITEMS'] },
|
|
176
|
+
],
|
|
177
|
+
},
|
|
178
|
+
],
|
|
172
179
|
},
|
|
173
180
|
},
|
|
174
181
|
},
|
|
175
|
-
|
|
182
|
+
initialValue: 0,
|
|
183
|
+
in: { $add: ['$$value', { $ifNull: ['$$this.amount', 0] }] },
|
|
176
184
|
},
|
|
177
185
|
},
|
|
178
186
|
},
|
|
@@ -120,7 +120,7 @@ export declare const configureOrdersModule: ({ db, migrationRepository, options:
|
|
|
120
120
|
}>;
|
|
121
121
|
setDeliveryProvider: (orderId: string, deliveryProviderId: string) => Promise<mongodb.WithId<Order> | null>;
|
|
122
122
|
setPaymentProvider: (orderId: string, paymentProviderId: string) => Promise<mongodb.WithId<Order> | null>;
|
|
123
|
-
create: ({ userId, orderNumber, currencyCode, countryCode, billingAddress, contact, context, }: {
|
|
123
|
+
create: ({ userId, orderNumber, currencyCode, countryCode, billingAddress, contact, originEnrollmentId, context, }: {
|
|
124
124
|
userId: string;
|
|
125
125
|
billingAddress?: import("@unchainedshop/mongodb").Address;
|
|
126
126
|
contact?: import("@unchainedshop/mongodb").Contact;
|
|
@@ -15,6 +15,7 @@ import { configureOrdersModuleQueries } from "./configureOrdersModule-queries.js
|
|
|
15
15
|
import { emit, registerEvents } from '@unchainedshop/events';
|
|
16
16
|
import renameCurrencyCode from "../migrations/20250502111800-currency-code.js";
|
|
17
17
|
import normalizeOrderContactPhone from "../migrations/20260625120000-normalize-contact-phone.js";
|
|
18
|
+
import migrateOrderCalculationToNet from "../migrations/20260907120000-order-calculation-net.js";
|
|
18
19
|
const ORDER_EVENTS = [
|
|
19
20
|
'ORDER_CHECKOUT',
|
|
20
21
|
'ORDER_CONFIRMED',
|
|
@@ -24,6 +25,7 @@ const ORDER_EVENTS = [
|
|
|
24
25
|
export const configureOrdersModule = async ({ db, migrationRepository, options: orderOptions = {}, }) => {
|
|
25
26
|
renameCurrencyCode(migrationRepository);
|
|
26
27
|
normalizeOrderContactPhone(migrationRepository);
|
|
28
|
+
migrateOrderCalculationToNet(migrationRepository);
|
|
27
29
|
registerEvents(ORDER_EVENTS);
|
|
28
30
|
ordersSettings.configureSettings(orderOptions);
|
|
29
31
|
const Orders = await OrdersCollection(db);
|
package/package.json
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unchainedshop/core-orders",
|
|
3
3
|
"description": "Order management module for the Unchained Engine",
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "5.0.0-alpha.10",
|
|
5
|
+
"files": [
|
|
6
|
+
"lib"
|
|
7
|
+
],
|
|
5
8
|
"main": "lib/orders-index.js",
|
|
6
9
|
"types": "lib/orders-index.d.ts",
|
|
7
10
|
"type": "module",
|
|
@@ -35,12 +38,12 @@
|
|
|
35
38
|
},
|
|
36
39
|
"homepage": "https://github.com/unchainedshop/unchained#readme",
|
|
37
40
|
"dependencies": {
|
|
38
|
-
"@unchainedshop/events": "^
|
|
39
|
-
"@unchainedshop/mongodb": "^
|
|
40
|
-
"@unchainedshop/utils": "^
|
|
41
|
+
"@unchainedshop/events": "^5.0.0-alpha.10",
|
|
42
|
+
"@unchainedshop/mongodb": "^5.0.0-alpha.10",
|
|
43
|
+
"@unchainedshop/utils": "^5.0.0-alpha.10"
|
|
41
44
|
},
|
|
42
45
|
"devDependencies": {
|
|
43
46
|
"@types/node": "^26.2.0",
|
|
44
|
-
"typescript": "^
|
|
47
|
+
"typescript": "^6.0.3"
|
|
45
48
|
}
|
|
46
49
|
}
|