@unchainedshop/core-orders 2.14.2 → 3.0.0-alpha3
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/lib/db/OrderDeliveriesCollection.d.ts +1 -1
- package/lib/db/OrderDeliveriesCollection.d.ts.map +1 -1
- package/lib/db/OrderDiscountsCollection.d.ts +1 -1
- package/lib/db/OrderDiscountsCollection.d.ts.map +1 -1
- package/lib/db/OrderPaymentsCollection.d.ts +1 -1
- package/lib/db/OrderPaymentsCollection.d.ts.map +1 -1
- package/lib/db/OrderPositionsCollection.d.ts +1 -1
- package/lib/db/OrderPositionsCollection.d.ts.map +1 -1
- package/lib/db/OrdersCollection.d.ts +1 -1
- package/lib/db/OrdersCollection.d.ts.map +1 -1
- package/lib/director/OrderDiscountAdapter.d.ts +1 -1
- package/lib/director/OrderDiscountAdapter.d.ts.map +1 -1
- package/lib/director/OrderDiscountAdapter.js.map +1 -1
- package/lib/director/OrderDiscountDirector.d.ts +1 -1
- package/lib/director/OrderDiscountDirector.d.ts.map +1 -1
- package/lib/director/OrderPricingAdapter.d.ts +20 -2
- package/lib/director/OrderPricingAdapter.d.ts.map +1 -1
- package/lib/director/OrderPricingAdapter.js.map +1 -1
- package/lib/director/OrderPricingDirector.d.ts +16 -1
- package/lib/director/OrderPricingDirector.d.ts.map +1 -1
- package/lib/director/OrderPricingDirector.js.map +1 -1
- package/lib/director/OrderPricingSheet.d.ts +34 -2
- package/lib/director/OrderPricingSheet.d.ts.map +1 -1
- package/lib/director/OrderPricingSheet.js +9 -2
- package/lib/director/OrderPricingSheet.js.map +1 -1
- package/lib/module/configureOrderDeliveriesModule.d.ts +32 -4
- package/lib/module/configureOrderDeliveriesModule.d.ts.map +1 -1
- package/lib/module/configureOrderDeliveriesModule.js +8 -18
- package/lib/module/configureOrderDeliveriesModule.js.map +1 -1
- package/lib/module/configureOrderDiscountsModule.d.ts +22 -4
- package/lib/module/configureOrderDiscountsModule.d.ts.map +1 -1
- package/lib/module/configureOrderDiscountsModule.js +20 -29
- package/lib/module/configureOrderDiscountsModule.js.map +1 -1
- package/lib/module/configureOrderPaymentsModule.d.ts +46 -4
- package/lib/module/configureOrderPaymentsModule.d.ts.map +1 -1
- package/lib/module/configureOrderPaymentsModule.js +7 -19
- package/lib/module/configureOrderPaymentsModule.js.map +1 -1
- package/lib/module/configureOrderPositionsModule.d.ts +58 -4
- package/lib/module/configureOrderPositionsModule.d.ts.map +1 -1
- package/lib/module/configureOrderPositionsModule.js +11 -51
- package/lib/module/configureOrderPositionsModule.js.map +1 -1
- package/lib/module/configureOrdersModule-mutations.d.ts +32 -8
- package/lib/module/configureOrdersModule-mutations.d.ts.map +1 -1
- package/lib/module/configureOrdersModule-mutations.js +21 -51
- package/lib/module/configureOrdersModule-mutations.js.map +1 -1
- package/lib/module/configureOrdersModule-processing.d.ts +22 -4
- package/lib/module/configureOrdersModule-processing.d.ts.map +1 -1
- package/lib/module/configureOrdersModule-processing.js +5 -14
- package/lib/module/configureOrdersModule-processing.js.map +1 -1
- package/lib/module/configureOrdersModule-queries.d.ts +23 -3
- package/lib/module/configureOrdersModule-queries.d.ts.map +1 -1
- package/lib/module/configureOrdersModule-queries.js +5 -4
- package/lib/module/configureOrdersModule-queries.js.map +1 -1
- package/lib/module/configureOrdersModule-transformations.d.ts +15 -1
- package/lib/module/configureOrdersModule-transformations.d.ts.map +1 -1
- package/lib/module/configureOrdersModule-transformations.js +4 -5
- package/lib/module/configureOrdersModule-transformations.js.map +1 -1
- package/lib/module/configureOrdersModule.d.ts +20 -2
- package/lib/module/configureOrdersModule.d.ts.map +1 -1
- package/lib/module/configureOrdersModule.js +17 -9
- package/lib/module/configureOrdersModule.js.map +1 -1
- package/lib/orders-index.d.ts +10 -12
- package/lib/orders-index.d.ts.map +1 -1
- package/lib/orders-index.js +10 -11
- package/lib/orders-index.js.map +1 -1
- package/lib/orders-settings.d.ts +18 -1
- package/lib/orders-settings.d.ts.map +1 -1
- package/lib/orders-settings.js.map +1 -1
- package/lib/service/migrateOrderCartService.d.ts +8 -1
- package/lib/service/migrateOrderCartService.d.ts.map +1 -1
- package/lib/service/migrateOrderCartService.js +2 -2
- package/lib/service/migrateOrderCartService.js.map +1 -1
- package/lib/service/nextUserCartService.d.ts +9 -1
- package/lib/service/nextUserCartService.d.ts.map +1 -1
- package/lib/service/nextUserCartService.js.map +1 -1
- package/lib/service/orderServices.d.ts +6 -1
- package/lib/service/orderServices.d.ts.map +1 -1
- package/lib/service/orderServices.js.map +1 -1
- package/lib/types.d.ts +116 -0
- package/lib/types.d.ts.map +1 -0
- package/lib/types.js +25 -0
- package/lib/types.js.map +1 -0
- package/package.json +6 -8
- package/src/db/OrderDeliveriesCollection.ts +1 -1
- package/src/db/OrderDiscountsCollection.ts +1 -1
- package/src/db/OrderPaymentsCollection.ts +1 -1
- package/src/db/OrderPositionsCollection.ts +1 -1
- package/src/db/OrdersCollection.ts +1 -1
- package/src/director/OrderDiscountAdapter.ts +1 -1
- package/src/director/OrderPricingAdapter.ts +26 -6
- package/src/director/OrderPricingDirector.ts +21 -5
- package/src/director/OrderPricingSheet.ts +25 -6
- package/src/module/buildFindSelector.test.ts +7 -2
- package/src/module/configureOrderDeliveriesModule.ts +65 -29
- package/src/module/configureOrderDiscountsModule.ts +70 -43
- package/src/module/configureOrderPaymentsModule.ts +94 -29
- package/src/module/configureOrderPositionsModule.ts +82 -94
- package/src/module/configureOrdersModule-mutations.ts +88 -82
- package/src/module/configureOrdersModule-processing.ts +38 -23
- package/src/module/configureOrdersModule-queries.ts +33 -13
- package/src/module/configureOrdersModule-transformations.ts +28 -6
- package/src/module/configureOrdersModule.ts +57 -21
- package/src/orders-index.ts +12 -16
- package/src/orders-settings.ts +20 -1
- package/src/service/migrateOrderCartService.ts +14 -7
- package/src/service/nextUserCartService.ts +13 -1
- package/src/service/orderServices.ts +7 -3
- package/src/types.ts +129 -0
- package/lib/db/OrderDeliveriesSchema.d.ts +0 -3
- package/lib/db/OrderDeliveriesSchema.d.ts.map +0 -1
- package/lib/db/OrderDeliveriesSchema.js +0 -20
- package/lib/db/OrderDeliveriesSchema.js.map +0 -1
- package/lib/db/OrderDiscountsSchema.d.ts +0 -3
- package/lib/db/OrderDiscountsSchema.d.ts.map +0 -1
- package/lib/db/OrderDiscountsSchema.js +0 -12
- package/lib/db/OrderDiscountsSchema.js.map +0 -1
- package/lib/db/OrderPaymentsSchema.d.ts +0 -3
- package/lib/db/OrderPaymentsSchema.d.ts.map +0 -1
- package/lib/db/OrderPaymentsSchema.js +0 -20
- package/lib/db/OrderPaymentsSchema.js.map +0 -1
- package/lib/db/OrderPositionsSchema.d.ts +0 -3
- package/lib/db/OrderPositionsSchema.d.ts.map +0 -1
- package/lib/db/OrderPositionsSchema.js +0 -37
- package/lib/db/OrderPositionsSchema.js.map +0 -1
- package/lib/db/OrdersSchema.d.ts +0 -3
- package/lib/db/OrdersSchema.d.ts.map +0 -1
- package/lib/db/OrdersSchema.js +0 -30
- package/lib/db/OrdersSchema.js.map +0 -1
- package/src/db/OrderDeliveriesSchema.ts +0 -24
- package/src/db/OrderDiscountsSchema.ts +0 -15
- package/src/db/OrderPaymentsSchema.ts +0 -24
- package/src/db/OrderPositionsSchema.ts +0 -41
- package/src/db/OrdersSchema.ts +0 -34
|
@@ -1,16 +1,78 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import type { OrderDiscount } from '@unchainedshop/core-orders';
|
|
2
|
+
import type { UnchainedCore } from '@unchainedshop/core';
|
|
3
|
+
import { IProductPricingSheet, Product } from '@unchainedshop/core-products';
|
|
4
|
+
|
|
5
|
+
import { Order, OrderPosition } from '../types.js';
|
|
4
6
|
import { emit, registerEvents } from '@unchainedshop/events';
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
generateDbFilterById,
|
|
8
|
-
generateDbMutations,
|
|
9
|
-
generateDbObjectId,
|
|
10
|
-
mongodb,
|
|
11
|
-
} from '@unchainedshop/mongodb';
|
|
12
|
-
import { OrderPositionsSchema } from '../db/OrderPositionsSchema.js';
|
|
7
|
+
import { generateDbFilterById, generateDbObjectId, mongodb } from '@unchainedshop/mongodb';
|
|
13
8
|
import { ordersSettings } from '../orders-settings.js';
|
|
9
|
+
import { OrderDelivery } from '@unchainedshop/core-orders';
|
|
10
|
+
import { OrderPricingDiscount } from '../director/OrderPricingDirector.js';
|
|
11
|
+
|
|
12
|
+
export type OrderPositionsModule = {
|
|
13
|
+
// Queries
|
|
14
|
+
findOrderPosition: (
|
|
15
|
+
params: { itemId: string },
|
|
16
|
+
options?: mongodb.FindOptions,
|
|
17
|
+
) => Promise<OrderPosition>;
|
|
18
|
+
findOrderPositions: (params: { orderId: string }) => Promise<Array<OrderPosition>>;
|
|
19
|
+
|
|
20
|
+
// Transformations
|
|
21
|
+
discounts: (
|
|
22
|
+
orderPosition: OrderPosition,
|
|
23
|
+
params: { order: Order; orderDiscount: OrderDiscount },
|
|
24
|
+
unchainedAPI: UnchainedCore,
|
|
25
|
+
) => Array<OrderPricingDiscount>;
|
|
26
|
+
|
|
27
|
+
pricingSheet: (
|
|
28
|
+
orderPosition: OrderPosition,
|
|
29
|
+
currency: string,
|
|
30
|
+
unchainedAPI: UnchainedCore,
|
|
31
|
+
) => IProductPricingSheet;
|
|
32
|
+
|
|
33
|
+
delete: (orderPositionId: string) => Promise<OrderPosition>;
|
|
34
|
+
|
|
35
|
+
removePositions: ({ orderId }: { orderId: string }) => Promise<number>;
|
|
36
|
+
removeProductByIdFromAllOpenPositions: (productId: string) => Promise<Array<string>>;
|
|
37
|
+
|
|
38
|
+
updateProductItem: (
|
|
39
|
+
doc: {
|
|
40
|
+
context?: any;
|
|
41
|
+
configuration?: Array<{ key: string; value: string }>;
|
|
42
|
+
quantity?: number;
|
|
43
|
+
},
|
|
44
|
+
params: { order: Order; product: Product; orderPosition: OrderPosition },
|
|
45
|
+
unchainedAPI: UnchainedCore,
|
|
46
|
+
) => Promise<OrderPosition>;
|
|
47
|
+
|
|
48
|
+
updateScheduling: (
|
|
49
|
+
params: {
|
|
50
|
+
order: Order;
|
|
51
|
+
orderDelivery: OrderDelivery;
|
|
52
|
+
orderPosition: OrderPosition;
|
|
53
|
+
},
|
|
54
|
+
unchainedAPI: UnchainedCore,
|
|
55
|
+
) => Promise<OrderPosition>;
|
|
56
|
+
|
|
57
|
+
updateCalculation: (
|
|
58
|
+
orderPosition: OrderPosition,
|
|
59
|
+
unchainedAPI: UnchainedCore,
|
|
60
|
+
) => Promise<OrderPosition>;
|
|
61
|
+
|
|
62
|
+
addProductItem: (
|
|
63
|
+
doc: {
|
|
64
|
+
context?: any;
|
|
65
|
+
configuration?: Array<{ key: string; value: string }>;
|
|
66
|
+
orderId?: string;
|
|
67
|
+
originalProductId?: string;
|
|
68
|
+
productId?: string;
|
|
69
|
+
quantity: number;
|
|
70
|
+
quotationId?: string;
|
|
71
|
+
},
|
|
72
|
+
params: { order: Order; product: Product },
|
|
73
|
+
unchainedAPI: UnchainedCore,
|
|
74
|
+
) => Promise<OrderPosition>;
|
|
75
|
+
};
|
|
14
76
|
|
|
15
77
|
const ORDER_POSITION_EVENTS: string[] = [
|
|
16
78
|
'ORDER_UPDATE_CART_ITEM',
|
|
@@ -27,18 +89,11 @@ export const buildFindByIdSelector = (orderPositionId: string, orderId?: string)
|
|
|
27
89
|
|
|
28
90
|
export const configureOrderPositionsModule = ({
|
|
29
91
|
OrderPositions,
|
|
30
|
-
updateCalculation,
|
|
31
92
|
}: {
|
|
32
93
|
OrderPositions: mongodb.Collection<OrderPosition>;
|
|
33
|
-
updateCalculation: OrdersModule['updateCalculation'];
|
|
34
94
|
}): OrderPositionsModule => {
|
|
35
95
|
registerEvents(ORDER_POSITION_EVENTS);
|
|
36
96
|
|
|
37
|
-
const mutations = generateDbMutations<OrderPosition>(OrderPositions, OrderPositionsSchema, {
|
|
38
|
-
permanentlyDeleteByDefault: true,
|
|
39
|
-
hasCreateOnly: false,
|
|
40
|
-
}) as ModuleMutations<OrderPosition>;
|
|
41
|
-
|
|
42
97
|
return {
|
|
43
98
|
// Queries
|
|
44
99
|
findOrderPosition: async ({ itemId }, options) => {
|
|
@@ -72,68 +127,18 @@ export const configureOrderPositionsModule = ({
|
|
|
72
127
|
});
|
|
73
128
|
},
|
|
74
129
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
create: async (
|
|
78
|
-
{ configuration, context, quantity, quotationId },
|
|
79
|
-
{ order, product, originalProduct },
|
|
80
|
-
unchainedAPI,
|
|
81
|
-
) => {
|
|
82
|
-
const orderId = order._id;
|
|
83
|
-
const productId = product._id;
|
|
84
|
-
const originalProductId = originalProduct ? originalProduct._id : undefined;
|
|
85
|
-
|
|
86
|
-
log(
|
|
87
|
-
`Create ${quantity}x Position with Product ${productId} ${
|
|
88
|
-
quotationId ? ` (${quotationId})` : ''
|
|
89
|
-
}`,
|
|
90
|
-
{ orderId, productId, originalProductId },
|
|
91
|
-
);
|
|
92
|
-
|
|
93
|
-
const positionId = await mutations.create({
|
|
94
|
-
orderId,
|
|
95
|
-
productId,
|
|
96
|
-
originalProductId,
|
|
97
|
-
quotationId,
|
|
98
|
-
quantity,
|
|
99
|
-
configuration,
|
|
100
|
-
context,
|
|
101
|
-
calculation: [],
|
|
102
|
-
scheduling: [],
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
await updateCalculation(orderId, unchainedAPI);
|
|
106
|
-
|
|
107
|
-
return OrderPositions.findOne(buildFindByIdSelector(positionId));
|
|
108
|
-
},
|
|
109
|
-
|
|
110
|
-
delete: async (orderPositionId, unchainedAPI) => {
|
|
130
|
+
delete: async (orderPositionId) => {
|
|
111
131
|
const selector = buildFindByIdSelector(orderPositionId);
|
|
112
|
-
|
|
113
|
-
log(`Remove Position ${orderPositionId}`, {
|
|
114
|
-
orderPositionId,
|
|
115
|
-
});
|
|
116
|
-
|
|
117
132
|
const orderPosition = await OrderPositions.findOneAndDelete(selector, {});
|
|
118
|
-
|
|
119
|
-
await updateCalculation(orderPosition.orderId, unchainedAPI);
|
|
120
|
-
|
|
121
133
|
await emit('ORDER_REMOVE_CART_ITEM', {
|
|
122
134
|
orderPosition,
|
|
123
135
|
});
|
|
124
|
-
|
|
125
136
|
return { ...orderPosition, calculation: [] };
|
|
126
137
|
},
|
|
127
138
|
|
|
128
|
-
removePositions: async ({ orderId }
|
|
129
|
-
log('Remove Positions', { orderId });
|
|
130
|
-
|
|
139
|
+
removePositions: async ({ orderId }) => {
|
|
131
140
|
const result = await OrderPositions.deleteMany({ orderId });
|
|
132
|
-
|
|
133
|
-
await updateCalculation(orderId, unchainedAPI);
|
|
134
|
-
|
|
135
141
|
await emit('ORDER_EMPTY_CART', { orderId, count: result.deletedCount });
|
|
136
|
-
|
|
137
142
|
return result.deletedCount;
|
|
138
143
|
},
|
|
139
144
|
|
|
@@ -173,11 +178,9 @@ export const configureOrderPositionsModule = ({
|
|
|
173
178
|
unchainedAPI,
|
|
174
179
|
);
|
|
175
180
|
|
|
176
|
-
await OrderPositions.
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
const updatedOrderPosition = await OrderPositions.findOne(selector, {});
|
|
181
|
+
const updatedOrderPosition = await OrderPositions.findOneAndUpdate(selector, modifier, {
|
|
182
|
+
returnDocument: 'after',
|
|
183
|
+
});
|
|
181
184
|
|
|
182
185
|
await emit('ORDER_UPDATE_CART_ITEM', {
|
|
183
186
|
orderPosition: updatedOrderPosition,
|
|
@@ -186,9 +189,7 @@ export const configureOrderPositionsModule = ({
|
|
|
186
189
|
return updatedOrderPosition;
|
|
187
190
|
},
|
|
188
191
|
|
|
189
|
-
removeProductByIdFromAllOpenPositions: async (productId
|
|
190
|
-
log('Remove Position Product', { productId });
|
|
191
|
-
|
|
192
|
+
removeProductByIdFromAllOpenPositions: async (productId) => {
|
|
192
193
|
const positions = await OrderPositions.aggregate([
|
|
193
194
|
{
|
|
194
195
|
$match: {
|
|
@@ -211,15 +212,7 @@ export const configureOrderPositionsModule = ({
|
|
|
211
212
|
]).toArray();
|
|
212
213
|
|
|
213
214
|
const positionIds = positions.map((o) => o._id);
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
const orderIdsToRecalculate = positions.map((o) => o.orderId);
|
|
217
|
-
await Promise.all(
|
|
218
|
-
[...new Set(orderIdsToRecalculate)].map(async (orderIdToRecalculate) => {
|
|
219
|
-
await updateCalculation(orderIdToRecalculate, unchainedAPI);
|
|
220
|
-
}),
|
|
221
|
-
);
|
|
222
|
-
|
|
215
|
+
await OrderPositions.deleteMany({ _id: { $in: positionIds } });
|
|
223
216
|
await Promise.all(
|
|
224
217
|
positions.map(async (orderPosition) => {
|
|
225
218
|
await emit('ORDER_REMOVE_CART_ITEM', {
|
|
@@ -228,7 +221,8 @@ export const configureOrderPositionsModule = ({
|
|
|
228
221
|
}),
|
|
229
222
|
);
|
|
230
223
|
|
|
231
|
-
|
|
224
|
+
const orderIdsToRecalculate = positions.map((o) => o.orderId);
|
|
225
|
+
return orderIdsToRecalculate;
|
|
232
226
|
},
|
|
233
227
|
|
|
234
228
|
updateScheduling: async ({ order, orderDelivery, orderPosition }, unchainedAPI) => {
|
|
@@ -291,10 +285,6 @@ export const configureOrderPositionsModule = ({
|
|
|
291
285
|
},
|
|
292
286
|
|
|
293
287
|
updateCalculation: async (orderPosition, unchainedAPI) => {
|
|
294
|
-
log(`OrderPosition ${orderPosition._id} -> Update Calculation`, {
|
|
295
|
-
orderId: orderPosition.orderId,
|
|
296
|
-
});
|
|
297
|
-
|
|
298
288
|
const calculation = await unchainedAPI.modules.products.calculate(
|
|
299
289
|
{ item: orderPosition, configuration: orderPosition.configuration },
|
|
300
290
|
unchainedAPI,
|
|
@@ -364,8 +354,6 @@ export const configureOrderPositionsModule = ({
|
|
|
364
354
|
{ upsert: true },
|
|
365
355
|
);
|
|
366
356
|
|
|
367
|
-
await updateCalculation(orderId, unchainedAPI);
|
|
368
|
-
|
|
369
357
|
const upsertedOrderPosition = await OrderPositions.findOne(selector);
|
|
370
358
|
|
|
371
359
|
await emit('ORDER_ADD_PRODUCT', { orderPosition: upsertedOrderPosition });
|
|
@@ -1,12 +1,48 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { Order, OrderStatus
|
|
3
|
-
import { OrderDelivery } from '@unchainedshop/
|
|
4
|
-
import { OrderPayment } from '@unchainedshop/
|
|
1
|
+
import { UnchainedCore } from '@unchainedshop/core';
|
|
2
|
+
import { Order, OrderStatus } from '../types.js';
|
|
3
|
+
import { OrderDelivery } from '@unchainedshop/core-orders';
|
|
4
|
+
import { OrderPayment } from '@unchainedshop/core-orders';
|
|
5
5
|
import { emit, registerEvents } from '@unchainedshop/events';
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
import {
|
|
7
|
+
Address,
|
|
8
|
+
Contact,
|
|
9
|
+
generateDbFilterById,
|
|
10
|
+
generateDbObjectId,
|
|
11
|
+
mongodb,
|
|
12
|
+
} from '@unchainedshop/mongodb';
|
|
13
|
+
import { OrderPosition } from '@unchainedshop/core-orders';
|
|
14
|
+
|
|
15
|
+
export interface OrderMutations {
|
|
16
|
+
create: (doc: {
|
|
17
|
+
userId: string;
|
|
18
|
+
billingAddress?: Address;
|
|
19
|
+
contact?: Contact;
|
|
20
|
+
countryCode: string;
|
|
21
|
+
currency: string;
|
|
22
|
+
orderNumber?: string;
|
|
23
|
+
originEnrollmentId?: string;
|
|
24
|
+
}) => Promise<Order>;
|
|
25
|
+
|
|
26
|
+
delete: (orderId: string) => Promise<number>;
|
|
27
|
+
|
|
28
|
+
setCartOwner: (params: { orderId: string; userId: string }) => Promise<void>;
|
|
29
|
+
moveCartPositions: (params: { fromOrderId: string; toOrderId: string }) => Promise<void>;
|
|
30
|
+
|
|
31
|
+
setDeliveryProvider: (
|
|
32
|
+
orderId: string,
|
|
33
|
+
deliveryProviderId: string,
|
|
34
|
+
unchainedAPI: UnchainedCore,
|
|
35
|
+
) => Promise<Order>;
|
|
36
|
+
setPaymentProvider: (
|
|
37
|
+
orderId: string,
|
|
38
|
+
paymentProviderId: string,
|
|
39
|
+
unchainedAPI: UnchainedCore,
|
|
40
|
+
) => Promise<Order>;
|
|
41
|
+
|
|
42
|
+
updateBillingAddress: (orderId: string, billingAddress: Address) => Promise<Order>;
|
|
43
|
+
updateContact: (orderId: string, contact: Contact) => Promise<Order>;
|
|
44
|
+
updateContext: (orderId: string, context: any) => Promise<Order | null>;
|
|
45
|
+
}
|
|
10
46
|
|
|
11
47
|
const ORDER_EVENTS: string[] = [
|
|
12
48
|
'ORDER_CREATE',
|
|
@@ -21,26 +57,18 @@ export const configureOrderModuleMutations = ({
|
|
|
21
57
|
OrderDeliveries,
|
|
22
58
|
OrderPayments,
|
|
23
59
|
OrderPositions,
|
|
24
|
-
initProviders,
|
|
25
|
-
updateCalculation,
|
|
26
60
|
}: {
|
|
27
61
|
Orders: mongodb.Collection<Order>;
|
|
28
62
|
OrderDeliveries: mongodb.Collection<OrderDelivery>;
|
|
29
63
|
OrderPayments: mongodb.Collection<OrderPayment>;
|
|
30
64
|
OrderPositions: mongodb.Collection<OrderPosition>;
|
|
31
|
-
initProviders: OrdersModule['initProviders'];
|
|
32
|
-
updateCalculation: OrdersModule['updateCalculation'];
|
|
33
65
|
}): OrderMutations => {
|
|
34
66
|
registerEvents(ORDER_EVENTS);
|
|
35
67
|
|
|
36
|
-
const mutations = generateDbMutations<Order>(Orders, OrdersSchema, {
|
|
37
|
-
permanentlyDeleteByDefault: true,
|
|
38
|
-
hasCreateOnly: false,
|
|
39
|
-
}) as ModuleMutations<Order>;
|
|
40
|
-
|
|
41
68
|
return {
|
|
42
69
|
create: async ({ userId, orderNumber, currency, countryCode, billingAddress, contact }) => {
|
|
43
|
-
const orderId = await
|
|
70
|
+
const { insertedId: orderId } = await Orders.insertOne({
|
|
71
|
+
_id: generateDbObjectId(),
|
|
44
72
|
created: new Date(),
|
|
45
73
|
status: null,
|
|
46
74
|
billingAddress,
|
|
@@ -60,30 +88,11 @@ export const configureOrderModuleMutations = ({
|
|
|
60
88
|
},
|
|
61
89
|
|
|
62
90
|
delete: async (orderId) => {
|
|
63
|
-
const deletedCount = await
|
|
91
|
+
const { deletedCount } = await Orders.deleteOne({ _id: orderId });
|
|
64
92
|
await emit('ORDER_REMOVE', { orderId });
|
|
65
93
|
return deletedCount;
|
|
66
94
|
},
|
|
67
95
|
|
|
68
|
-
initProviders,
|
|
69
|
-
|
|
70
|
-
invalidateProviders: async (unchainedAPI, maxAgeDays = 30) => {
|
|
71
|
-
log('Orders: Start invalidating cart providers', {
|
|
72
|
-
level: LogLevel.Verbose,
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
const ONE_DAY_IN_MILLISECONDS = 86400000;
|
|
76
|
-
|
|
77
|
-
const minValidDate = new Date(new Date().getTime() - maxAgeDays * ONE_DAY_IN_MILLISECONDS);
|
|
78
|
-
|
|
79
|
-
const orders = await Orders.find({
|
|
80
|
-
status: { $eq: null },
|
|
81
|
-
updated: { $gte: minValidDate },
|
|
82
|
-
}).toArray();
|
|
83
|
-
|
|
84
|
-
await Promise.all(orders.map((order) => initProviders(order, unchainedAPI)));
|
|
85
|
-
},
|
|
86
|
-
|
|
87
96
|
setCartOwner: async ({ orderId, userId }) => {
|
|
88
97
|
await Orders.updateOne(generateDbFilterById(orderId), {
|
|
89
98
|
$set: {
|
|
@@ -103,8 +112,7 @@ export const configureOrderModuleMutations = ({
|
|
|
103
112
|
);
|
|
104
113
|
},
|
|
105
114
|
|
|
106
|
-
setDeliveryProvider: async (orderId, deliveryProviderId,
|
|
107
|
-
const { modules } = unchainedAPI;
|
|
115
|
+
setDeliveryProvider: async (orderId, deliveryProviderId, { modules }) => {
|
|
108
116
|
const delivery = await OrderDeliveries.findOne({
|
|
109
117
|
orderId,
|
|
110
118
|
deliveryProviderId,
|
|
@@ -121,17 +129,17 @@ export const configureOrderModuleMutations = ({
|
|
|
121
129
|
})
|
|
122
130
|
)._id;
|
|
123
131
|
|
|
124
|
-
log(`Set Delivery Provider ${deliveryProviderId}`, { orderId });
|
|
125
|
-
|
|
126
132
|
const selector = generateDbFilterById(orderId);
|
|
127
|
-
await Orders.
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
133
|
+
const order = await Orders.findOneAndUpdate(
|
|
134
|
+
selector,
|
|
135
|
+
{
|
|
136
|
+
$set: {
|
|
137
|
+
deliveryId,
|
|
138
|
+
updated: new Date(),
|
|
139
|
+
},
|
|
131
140
|
},
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
const order = await updateCalculation(orderId, unchainedAPI);
|
|
141
|
+
{ returnDocument: 'after' },
|
|
142
|
+
);
|
|
135
143
|
|
|
136
144
|
await emit('ORDER_SET_DELIVERY_PROVIDER', {
|
|
137
145
|
order,
|
|
@@ -159,14 +167,14 @@ export const configureOrderModuleMutations = ({
|
|
|
159
167
|
status: null,
|
|
160
168
|
})
|
|
161
169
|
)._id;
|
|
162
|
-
log(`Set Payment Provider ${paymentProviderId}`, { orderId });
|
|
163
|
-
|
|
164
170
|
const selector = generateDbFilterById(orderId);
|
|
165
|
-
await Orders.
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
171
|
+
const order = await Orders.findOneAndUpdate(
|
|
172
|
+
selector,
|
|
173
|
+
{
|
|
174
|
+
$set: { paymentId, updated: new Date() },
|
|
175
|
+
},
|
|
176
|
+
{ returnDocument: 'after' },
|
|
177
|
+
);
|
|
170
178
|
|
|
171
179
|
await emit('ORDER_SET_PAYMENT_PROVIDER', {
|
|
172
180
|
order,
|
|
@@ -176,45 +184,46 @@ export const configureOrderModuleMutations = ({
|
|
|
176
184
|
return order;
|
|
177
185
|
},
|
|
178
186
|
|
|
179
|
-
updateBillingAddress: async (orderId, billingAddress
|
|
180
|
-
log('Update Invoicing Address', { orderId });
|
|
181
|
-
|
|
187
|
+
updateBillingAddress: async (orderId, billingAddress) => {
|
|
182
188
|
const selector = generateDbFilterById(orderId);
|
|
183
|
-
await Orders.
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
189
|
+
const order = await Orders.findOneAndUpdate(
|
|
190
|
+
selector,
|
|
191
|
+
{
|
|
192
|
+
$set: {
|
|
193
|
+
billingAddress,
|
|
194
|
+
updated: new Date(),
|
|
195
|
+
},
|
|
187
196
|
},
|
|
188
|
-
|
|
197
|
+
{ returnDocument: 'after' },
|
|
198
|
+
);
|
|
189
199
|
|
|
190
|
-
const order = await updateCalculation(orderId, unchainedAPI);
|
|
191
200
|
await emit('ORDER_UPDATE', { order, field: 'billing' });
|
|
192
201
|
return order;
|
|
193
202
|
},
|
|
194
203
|
|
|
195
|
-
updateContact: async (orderId, contact
|
|
196
|
-
log('Update Contact', { orderId });
|
|
197
|
-
|
|
204
|
+
updateContact: async (orderId, contact) => {
|
|
198
205
|
const selector = generateDbFilterById(orderId);
|
|
199
|
-
await Orders.
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
206
|
+
const order = await Orders.findOneAndUpdate(
|
|
207
|
+
selector,
|
|
208
|
+
{
|
|
209
|
+
$set: {
|
|
210
|
+
contact,
|
|
211
|
+
updated: new Date(),
|
|
212
|
+
},
|
|
203
213
|
},
|
|
204
|
-
|
|
214
|
+
{ returnDocument: 'after' },
|
|
215
|
+
);
|
|
205
216
|
|
|
206
|
-
const order = await updateCalculation(orderId, unchainedAPI);
|
|
207
217
|
await emit('ORDER_UPDATE', { order, field: 'contact' });
|
|
208
218
|
return order;
|
|
209
219
|
},
|
|
210
220
|
|
|
211
|
-
updateContext: async (orderId, context
|
|
221
|
+
updateContext: async (orderId, context) => {
|
|
212
222
|
const selector = generateDbFilterById<Order>(orderId);
|
|
213
223
|
selector.status = { $in: [null, OrderStatus.PENDING] };
|
|
214
224
|
|
|
215
225
|
if (!context || Object.keys(context).length === 0) return Orders.findOne(selector, {});
|
|
216
226
|
|
|
217
|
-
log('Update Arbitrary Context', { orderId, context });
|
|
218
227
|
const contextSetters = Object.fromEntries(
|
|
219
228
|
Object.entries(context).map(([key, value]) => [`context.${key}`, value]),
|
|
220
229
|
);
|
|
@@ -226,17 +235,14 @@ export const configureOrderModuleMutations = ({
|
|
|
226
235
|
updated: new Date(),
|
|
227
236
|
},
|
|
228
237
|
},
|
|
229
|
-
{ includeResultMetadata: true },
|
|
238
|
+
{ includeResultMetadata: true, returnDocument: 'after' },
|
|
230
239
|
);
|
|
231
240
|
|
|
232
241
|
if (result.ok) {
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
return calculatedOrder;
|
|
242
|
+
await emit('ORDER_UPDATE', { order: result.value, field: 'context' });
|
|
243
|
+
return result.value;
|
|
236
244
|
}
|
|
237
245
|
return null;
|
|
238
246
|
},
|
|
239
|
-
|
|
240
|
-
updateCalculation,
|
|
241
247
|
};
|
|
242
248
|
};
|
|
@@ -1,15 +1,41 @@
|
|
|
1
1
|
import { mongodb, generateDbFilterById } from '@unchainedshop/mongodb';
|
|
2
|
-
import {
|
|
3
|
-
import type { Locker } from '@kontsedal/locco';
|
|
4
|
-
import { OrderDelivery } from '@unchainedshop/types/orders.deliveries.js';
|
|
5
|
-
import { OrderPayment } from '@unchainedshop/types/orders.payments.js';
|
|
6
|
-
import { OrderPosition } from '@unchainedshop/types/orders.positions.js';
|
|
2
|
+
import { ProductTypes } from '@unchainedshop/core-products';
|
|
7
3
|
import { emit, registerEvents } from '@unchainedshop/events';
|
|
8
|
-
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
4
|
+
|
|
5
|
+
import type { Locker } from '@kontsedal/locco';
|
|
6
|
+
import { Order, OrderStatus } from '../types.js';
|
|
7
|
+
import { OrderDelivery } from '@unchainedshop/core-orders';
|
|
8
|
+
import { OrderPayment } from '@unchainedshop/core-orders';
|
|
9
|
+
import { OrderPosition } from '@unchainedshop/core-orders';
|
|
10
|
+
import { UnchainedCore } from '@unchainedshop/core';
|
|
11
|
+
|
|
11
12
|
import { ordersSettings } from '../orders-settings.js';
|
|
12
13
|
|
|
14
|
+
export type OrderContextParams<P> = (
|
|
15
|
+
order: Order,
|
|
16
|
+
params: P,
|
|
17
|
+
unchainedAPI: UnchainedCore,
|
|
18
|
+
) => Promise<Order>;
|
|
19
|
+
|
|
20
|
+
export type OrderTransactionContext = {
|
|
21
|
+
paymentContext?: any;
|
|
22
|
+
deliveryContext?: any;
|
|
23
|
+
comment?: string;
|
|
24
|
+
nextStatus?: OrderStatus;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export interface OrderProcessing {
|
|
28
|
+
checkout: (
|
|
29
|
+
orderId: string,
|
|
30
|
+
params: OrderTransactionContext,
|
|
31
|
+
unchainedAPI: UnchainedCore,
|
|
32
|
+
) => Promise<Order>;
|
|
33
|
+
confirm: OrderContextParams<OrderTransactionContext>;
|
|
34
|
+
reject: OrderContextParams<OrderTransactionContext>;
|
|
35
|
+
processOrder: OrderContextParams<OrderTransactionContext>;
|
|
36
|
+
updateStatus: (orderId: string, params: { status: OrderStatus; info?: string }) => Promise<Order>;
|
|
37
|
+
}
|
|
38
|
+
|
|
13
39
|
const ORDER_PROCESSING_EVENTS: string[] = [
|
|
14
40
|
'ORDER_CHECKOUT',
|
|
15
41
|
'ORDER_CONFIRMED',
|
|
@@ -40,7 +66,7 @@ export const configureOrderModuleProcessing = ({
|
|
|
40
66
|
return findNewOrderNumber(order, index + 1);
|
|
41
67
|
};
|
|
42
68
|
|
|
43
|
-
const updateStatus:
|
|
69
|
+
const updateStatus: OrderProcessing['updateStatus'] = async (orderId, { status, info }) => {
|
|
44
70
|
const selector = generateDbFilterById(orderId);
|
|
45
71
|
const order = await Orders.findOne(selector, {});
|
|
46
72
|
|
|
@@ -94,7 +120,6 @@ export const configureOrderModuleProcessing = ({
|
|
|
94
120
|
);
|
|
95
121
|
|
|
96
122
|
if (modificationResult.ok) {
|
|
97
|
-
log(`New Status: ${status}`, { orderId });
|
|
98
123
|
if (order.status === null) {
|
|
99
124
|
// The first time that an order transitions away from cart is a checkout event
|
|
100
125
|
await emit('ORDER_CHECKOUT', { order: modificationResult.value, oldStatus: order.status });
|
|
@@ -149,11 +174,6 @@ export const configureOrderModuleProcessing = ({
|
|
|
149
174
|
const validationErrors = await Promise.all(
|
|
150
175
|
orderPositions.map(async (orderPosition) => {
|
|
151
176
|
const errors = [];
|
|
152
|
-
|
|
153
|
-
log(`OrderPosition ${orderPosition._id} -> Validate ${orderPosition.quantity}`, {
|
|
154
|
-
orderId: orderPosition.orderId,
|
|
155
|
-
});
|
|
156
|
-
|
|
157
177
|
const product = await unchainedAPI.modules.products.findProduct({
|
|
158
178
|
productId: orderPosition.productId,
|
|
159
179
|
});
|
|
@@ -292,7 +312,7 @@ export const configureOrderModuleProcessing = ({
|
|
|
292
312
|
await services.orders.nextUserCart(
|
|
293
313
|
{
|
|
294
314
|
user,
|
|
295
|
-
countryCode: locale.
|
|
315
|
+
countryCode: locale.region,
|
|
296
316
|
},
|
|
297
317
|
unchainedAPI,
|
|
298
318
|
);
|
|
@@ -428,7 +448,7 @@ export const configureOrderModuleProcessing = ({
|
|
|
428
448
|
}),
|
|
429
449
|
);
|
|
430
450
|
const tokenizedItems = mappedProductOrderPositions.filter(
|
|
431
|
-
(item) => item.product?.type ===
|
|
451
|
+
(item) => item.product?.type === ProductTypes.TokenizedProduct,
|
|
432
452
|
);
|
|
433
453
|
if (tokenizedItems.length > 0) {
|
|
434
454
|
// Give virtual warehouse a chance to instantiate new virtual objects
|
|
@@ -443,7 +463,7 @@ export const configureOrderModuleProcessing = ({
|
|
|
443
463
|
|
|
444
464
|
// Enrollments: Generate enrollments for plan products
|
|
445
465
|
const planItems = mappedProductOrderPositions.filter(
|
|
446
|
-
(item) => item.product?.type ===
|
|
466
|
+
(item) => item.product?.type === ProductTypes.PlanProduct && !order.originEnrollmentId,
|
|
447
467
|
);
|
|
448
468
|
if (planItems.length > 0) {
|
|
449
469
|
await modules.enrollments.createFromCheckout(
|
|
@@ -475,11 +495,6 @@ export const configureOrderModuleProcessing = ({
|
|
|
475
495
|
);
|
|
476
496
|
}),
|
|
477
497
|
);
|
|
478
|
-
|
|
479
|
-
// TODO: we will use this function to keep a "Ordered in Flight" amount, allowing us to
|
|
480
|
-
// do live stock stuff
|
|
481
|
-
// 2. Reserve quantity at Warehousing Provider until order is CANCELLED/FULLFILLED
|
|
482
|
-
// ???
|
|
483
498
|
}
|
|
484
499
|
|
|
485
500
|
nextStatus = await findNextStatus(nextStatus, order, unchainedAPI);
|