@unchainedshop/core-orders 3.0.0-alpha7 → 3.0.0-rc1
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/director/OrderDiscountAdapter.d.ts +2 -2
- 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/OrderDiscountDirector.js.map +1 -1
- package/lib/director/OrderPricingAdapter.d.ts +3 -3
- package/lib/director/OrderPricingAdapter.d.ts.map +1 -1
- package/lib/director/OrderPricingAdapter.js.map +1 -1
- package/lib/director/OrderPricingDirector.d.ts +1 -1
- package/lib/director/OrderPricingDirector.d.ts.map +1 -1
- package/lib/module/buildFindSelector.d.ts +5 -0
- package/lib/module/buildFindSelector.d.ts.map +1 -0
- package/lib/module/buildFindSelector.js +18 -0
- package/lib/module/buildFindSelector.js.map +1 -0
- package/lib/module/configureOrderDeliveriesModule.d.ts +14 -27
- package/lib/module/configureOrderDeliveriesModule.d.ts.map +1 -1
- package/lib/module/configureOrderDeliveriesModule.js +7 -68
- package/lib/module/configureOrderDeliveriesModule.js.map +1 -1
- package/lib/module/configureOrderDeliveriesModule_BACKUP_54554.d.ts +24 -0
- package/lib/module/configureOrderDeliveriesModule_BACKUP_54554.d.ts.map +1 -0
- package/lib/module/configureOrderDeliveriesModule_BACKUP_54554.js +101 -0
- package/lib/module/configureOrderDeliveriesModule_BACKUP_54554.js.map +1 -0
- package/lib/module/configureOrderDeliveriesModule_BASE_54554.d.ts +36 -0
- package/lib/module/configureOrderDeliveriesModule_BASE_54554.d.ts.map +1 -0
- package/lib/module/configureOrderDeliveriesModule_BASE_54554.js +162 -0
- package/lib/module/configureOrderDeliveriesModule_BASE_54554.js.map +1 -0
- package/lib/module/configureOrderDeliveriesModule_LOCAL_54554.d.ts +23 -0
- package/lib/module/configureOrderDeliveriesModule_LOCAL_54554.d.ts.map +1 -0
- package/lib/module/configureOrderDeliveriesModule_LOCAL_54554.js +97 -0
- package/lib/module/configureOrderDeliveriesModule_LOCAL_54554.js.map +1 -0
- package/lib/module/configureOrderDeliveriesModule_REMOTE_54554.d.ts +37 -0
- package/lib/module/configureOrderDeliveriesModule_REMOTE_54554.d.ts.map +1 -0
- package/lib/module/configureOrderDeliveriesModule_REMOTE_54554.js +166 -0
- package/lib/module/configureOrderDeliveriesModule_REMOTE_54554.js.map +1 -0
- package/lib/module/configureOrderDiscountsModule.d.ts +17 -18
- package/lib/module/configureOrderDiscountsModule.d.ts.map +1 -1
- package/lib/module/configureOrderDiscountsModule.js +31 -140
- package/lib/module/configureOrderDiscountsModule.js.map +1 -1
- package/lib/module/configureOrderDiscountsModule_BACKUP_55046.d.ts +26 -0
- package/lib/module/configureOrderDiscountsModule_BACKUP_55046.d.ts.map +1 -0
- package/lib/module/configureOrderDiscountsModule_BACKUP_55046.js +69 -0
- package/lib/module/configureOrderDiscountsModule_BACKUP_55046.js.map +1 -0
- package/lib/module/configureOrderDiscountsModule_BASE_55046.d.ts +26 -0
- package/lib/module/configureOrderDiscountsModule_BASE_55046.d.ts.map +1 -0
- package/lib/module/configureOrderDiscountsModule_BASE_55046.js +178 -0
- package/lib/module/configureOrderDiscountsModule_BASE_55046.js.map +1 -0
- package/lib/module/configureOrderDiscountsModule_LOCAL_55046.d.ts +25 -0
- package/lib/module/configureOrderDiscountsModule_LOCAL_55046.d.ts.map +1 -0
- package/lib/module/configureOrderDiscountsModule_LOCAL_55046.js +65 -0
- package/lib/module/configureOrderDiscountsModule_LOCAL_55046.js.map +1 -0
- package/lib/module/configureOrderDiscountsModule_REMOTE_55046.d.ts +27 -0
- package/lib/module/configureOrderDiscountsModule_REMOTE_55046.d.ts.map +1 -0
- package/lib/module/configureOrderDiscountsModule_REMOTE_55046.js +182 -0
- package/lib/module/configureOrderDiscountsModule_REMOTE_55046.js.map +1 -0
- package/lib/module/configureOrderPaymentsModule.d.ts +17 -37
- package/lib/module/configureOrderPaymentsModule.d.ts.map +1 -1
- package/lib/module/configureOrderPaymentsModule.js +10 -118
- package/lib/module/configureOrderPaymentsModule.js.map +1 -1
- package/lib/module/configureOrderPaymentsModule_BACKUP_55464.d.ts +32 -0
- package/lib/module/configureOrderPaymentsModule_BACKUP_55464.d.ts.map +1 -0
- package/lib/module/configureOrderPaymentsModule_BACKUP_55464.js +135 -0
- package/lib/module/configureOrderPaymentsModule_BACKUP_55464.js.map +1 -0
- package/lib/module/configureOrderPaymentsModule_BACKUP_55877.d.ts +32 -0
- package/lib/module/configureOrderPaymentsModule_BACKUP_55877.d.ts.map +1 -0
- package/lib/module/configureOrderPaymentsModule_BACKUP_55877.js +135 -0
- package/lib/module/configureOrderPaymentsModule_BACKUP_55877.js.map +1 -0
- package/lib/module/configureOrderPaymentsModule_BASE_55464.d.ts +51 -0
- package/lib/module/configureOrderPaymentsModule_BASE_55464.d.ts.map +1 -0
- package/lib/module/configureOrderPaymentsModule_BASE_55464.js +243 -0
- package/lib/module/configureOrderPaymentsModule_BASE_55464.js.map +1 -0
- package/lib/module/configureOrderPaymentsModule_BASE_55877.d.ts +51 -0
- package/lib/module/configureOrderPaymentsModule_BASE_55877.d.ts.map +1 -0
- package/lib/module/configureOrderPaymentsModule_BASE_55877.js +243 -0
- package/lib/module/configureOrderPaymentsModule_BASE_55877.js.map +1 -0
- package/lib/module/configureOrderPaymentsModule_LOCAL_55464.d.ts +31 -0
- package/lib/module/configureOrderPaymentsModule_LOCAL_55464.d.ts.map +1 -0
- package/lib/module/configureOrderPaymentsModule_LOCAL_55464.js +131 -0
- package/lib/module/configureOrderPaymentsModule_LOCAL_55464.js.map +1 -0
- package/lib/module/configureOrderPaymentsModule_LOCAL_55877.d.ts +31 -0
- package/lib/module/configureOrderPaymentsModule_LOCAL_55877.d.ts.map +1 -0
- package/lib/module/configureOrderPaymentsModule_LOCAL_55877.js +131 -0
- package/lib/module/configureOrderPaymentsModule_LOCAL_55877.js.map +1 -0
- package/lib/module/configureOrderPaymentsModule_REMOTE_55464.d.ts +52 -0
- package/lib/module/configureOrderPaymentsModule_REMOTE_55464.d.ts.map +1 -0
- package/lib/module/configureOrderPaymentsModule_REMOTE_55464.js +247 -0
- package/lib/module/configureOrderPaymentsModule_REMOTE_55464.js.map +1 -0
- package/lib/module/configureOrderPaymentsModule_REMOTE_55877.d.ts +52 -0
- package/lib/module/configureOrderPaymentsModule_REMOTE_55877.d.ts.map +1 -0
- package/lib/module/configureOrderPaymentsModule_REMOTE_55877.js +247 -0
- package/lib/module/configureOrderPaymentsModule_REMOTE_55877.js.map +1 -0
- package/lib/module/configureOrderPositionsModule.d.ts +22 -41
- package/lib/module/configureOrderPositionsModule.d.ts.map +1 -1
- package/lib/module/configureOrderPositionsModule.js +19 -82
- package/lib/module/configureOrderPositionsModule.js.map +1 -1
- package/lib/module/configureOrderPositionsModule_BACKUP_56197.d.ts +44 -0
- package/lib/module/configureOrderPositionsModule_BACKUP_56197.d.ts.map +1 -0
- package/lib/module/configureOrderPositionsModule_BACKUP_56197.js +138 -0
- package/lib/module/configureOrderPositionsModule_BACKUP_56197.js.map +1 -0
- package/lib/module/configureOrderPositionsModule_BASE_56197.d.ts +60 -0
- package/lib/module/configureOrderPositionsModule_BASE_56197.d.ts.map +1 -0
- package/lib/module/configureOrderPositionsModule_BASE_56197.js +201 -0
- package/lib/module/configureOrderPositionsModule_BASE_56197.js.map +1 -0
- package/lib/module/configureOrderPositionsModule_LOCAL_56197.d.ts +43 -0
- package/lib/module/configureOrderPositionsModule_LOCAL_56197.d.ts.map +1 -0
- package/lib/module/configureOrderPositionsModule_LOCAL_56197.js +134 -0
- package/lib/module/configureOrderPositionsModule_LOCAL_56197.js.map +1 -0
- package/lib/module/configureOrderPositionsModule_REMOTE_56197.d.ts +61 -0
- package/lib/module/configureOrderPositionsModule_REMOTE_56197.d.ts.map +1 -0
- package/lib/module/configureOrderPositionsModule_REMOTE_56197.js +205 -0
- package/lib/module/configureOrderPositionsModule_REMOTE_56197.js.map +1 -0
- package/lib/module/configureOrdersModule-mutations.d.ts +4 -11
- package/lib/module/configureOrdersModule-mutations.d.ts.map +1 -1
- package/lib/module/configureOrdersModule-mutations.js +10 -49
- package/lib/module/configureOrdersModule-mutations.js.map +1 -1
- package/lib/module/configureOrdersModule-processing.d.ts +4 -8
- package/lib/module/configureOrdersModule-processing.d.ts.map +1 -1
- package/lib/module/configureOrdersModule-processing.js +3 -1
- package/lib/module/configureOrdersModule-processing.js.map +1 -1
- package/lib/module/configureOrdersModule-queries.d.ts +10 -5
- package/lib/module/configureOrdersModule-queries.d.ts.map +1 -1
- package/lib/module/configureOrdersModule-queries.js +35 -22
- package/lib/module/configureOrdersModule-queries.js.map +1 -1
- package/lib/module/configureOrdersModule-transformations.d.ts +2 -3
- package/lib/module/configureOrdersModule-transformations.d.ts.map +1 -1
- package/lib/module/configureOrdersModule-transformations.js +3 -3
- package/lib/module/configureOrdersModule-transformations.js.map +1 -1
- package/lib/module/configureOrdersModule.d.ts +11 -8
- package/lib/module/configureOrdersModule.d.ts.map +1 -1
- package/lib/module/configureOrdersModule.js +133 -153
- package/lib/module/configureOrdersModule.js.map +1 -1
- package/lib/orders-index.d.ts +0 -7
- package/lib/orders-index.d.ts.map +1 -1
- package/lib/orders-index.js +0 -7
- package/lib/orders-index.js.map +1 -1
- package/lib/orders-settings.d.ts +3 -7
- package/lib/orders-settings.d.ts.map +1 -1
- package/lib/orders-settings.js.map +1 -1
- package/lib/types.d.ts +8 -9
- package/lib/types.d.ts.map +1 -1
- package/lib/types.js.map +1 -1
- package/package.json +9 -12
- 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/module/buildFindSelector.test.ts +1 -1
- package/src/module/buildFindSelector.ts +24 -0
- package/src/module/configureOrderDeliveriesModule.ts +29 -172
- package/src/module/configureOrderDiscountsModule.ts +44 -210
- package/src/module/configureOrderPaymentsModule.ts +51 -261
- package/src/module/configureOrderPositionsModule.ts +54 -205
- package/src/module/configureOrdersModule-mutations.ts +12 -85
- package/src/module/configureOrdersModule-queries.ts +53 -29
- package/src/module/configureOrdersModule.ts +180 -263
- package/src/orders-index.ts +0 -9
- package/src/orders-settings.ts +4 -6
- package/src/types.ts +8 -9
- package/tsconfig.json +5 -7
- package/lib/service/migrateOrderCartService.d.ts +0 -10
- package/lib/service/migrateOrderCartService.d.ts.map +0 -1
- package/lib/service/migrateOrderCartService.js +0 -32
- package/lib/service/migrateOrderCartService.js.map +0 -1
- package/lib/service/nextUserCartService.d.ts +0 -11
- package/lib/service/nextUserCartService.d.ts.map +0 -1
- package/lib/service/nextUserCartService.js +0 -34
- package/lib/service/nextUserCartService.js.map +0 -1
- package/lib/service/orderServices.d.ts +0 -8
- package/lib/service/orderServices.d.ts.map +0 -1
- package/lib/service/orderServices.js +0 -7
- package/lib/service/orderServices.js.map +0 -1
- package/src/director/OrderDiscountAdapter.ts +0 -8
- package/src/director/OrderDiscountConfiguration.ts +0 -5
- package/src/director/OrderDiscountDirector.ts +0 -5
- package/src/director/OrderPricingAdapter.ts +0 -61
- package/src/director/OrderPricingDirector.ts +0 -72
- package/src/director/OrderPricingSheet.ts +0 -146
- package/src/module/configureOrdersModule-processing.ts +0 -508
- package/src/module/configureOrdersModule-transformations.ts +0 -171
- package/src/service/migrateOrderCartService.ts +0 -53
- package/src/service/nextUserCartService.ts +0 -54
- package/src/service/orderServices.ts +0 -12
|
@@ -1,78 +1,7 @@
|
|
|
1
|
-
import
|
|
2
|
-
import type { UnchainedCore } from '@unchainedshop/core';
|
|
3
|
-
import { IProductPricingSheet, Product } from '@unchainedshop/core-products';
|
|
4
|
-
|
|
5
|
-
import { Order, OrderPosition } from '../types.js';
|
|
1
|
+
import { OrderPosition } from '../types.js';
|
|
6
2
|
import { emit, registerEvents } from '@unchainedshop/events';
|
|
7
3
|
import { generateDbFilterById, generateDbObjectId, mongodb } from '@unchainedshop/mongodb';
|
|
8
|
-
import {
|
|
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
|
-
};
|
|
4
|
+
import { PricingCalculation } from '@unchainedshop/utils';
|
|
76
5
|
|
|
77
6
|
const ORDER_POSITION_EVENTS: string[] = [
|
|
78
7
|
'ORDER_UPDATE_CART_ITEM',
|
|
@@ -91,43 +20,24 @@ export const configureOrderPositionsModule = ({
|
|
|
91
20
|
OrderPositions,
|
|
92
21
|
}: {
|
|
93
22
|
OrderPositions: mongodb.Collection<OrderPosition>;
|
|
94
|
-
})
|
|
23
|
+
}) => {
|
|
95
24
|
registerEvents(ORDER_POSITION_EVENTS);
|
|
96
25
|
|
|
97
26
|
return {
|
|
98
27
|
// Queries
|
|
99
|
-
findOrderPosition: async (
|
|
28
|
+
findOrderPosition: async (
|
|
29
|
+
{ itemId }: { itemId: string },
|
|
30
|
+
options?: mongodb.FindOptions,
|
|
31
|
+
): Promise<OrderPosition> => {
|
|
100
32
|
return OrderPositions.findOne(buildFindByIdSelector(itemId), options);
|
|
101
33
|
},
|
|
102
34
|
|
|
103
|
-
findOrderPositions: async ({ orderId }) => {
|
|
35
|
+
findOrderPositions: async ({ orderId }: { orderId: string }): Promise<OrderPosition[]> => {
|
|
104
36
|
const positions = OrderPositions.find({ orderId, quantity: { $gt: 0 } });
|
|
105
37
|
return positions.toArray();
|
|
106
38
|
},
|
|
107
39
|
|
|
108
|
-
|
|
109
|
-
discounts: (orderPosition, { order, orderDiscount }, unchainedAPI) => {
|
|
110
|
-
const pricingSheet = unchainedAPI.modules.orders.positions.pricingSheet(
|
|
111
|
-
orderPosition,
|
|
112
|
-
order.currency,
|
|
113
|
-
unchainedAPI,
|
|
114
|
-
);
|
|
115
|
-
|
|
116
|
-
return pricingSheet.discountPrices(orderDiscount._id).map((discount) => ({
|
|
117
|
-
item: orderPosition,
|
|
118
|
-
...discount,
|
|
119
|
-
}));
|
|
120
|
-
},
|
|
121
|
-
|
|
122
|
-
pricingSheet: (orderPosition, currency, { modules }) => {
|
|
123
|
-
return modules.products.pricingSheet({
|
|
124
|
-
calculation: orderPosition.calculation,
|
|
125
|
-
currency,
|
|
126
|
-
quantity: orderPosition.quantity,
|
|
127
|
-
});
|
|
128
|
-
},
|
|
129
|
-
|
|
130
|
-
delete: async (orderPositionId) => {
|
|
40
|
+
delete: async (orderPositionId: string): Promise<OrderPosition> => {
|
|
131
41
|
const selector = buildFindByIdSelector(orderPositionId);
|
|
132
42
|
const orderPosition = await OrderPositions.findOneAndDelete(selector, {});
|
|
133
43
|
await emit('ORDER_REMOVE_CART_ITEM', {
|
|
@@ -136,52 +46,45 @@ export const configureOrderPositionsModule = ({
|
|
|
136
46
|
return { ...orderPosition, calculation: [] };
|
|
137
47
|
},
|
|
138
48
|
|
|
139
|
-
removePositions: async ({ orderId }) => {
|
|
49
|
+
removePositions: async ({ orderId }: { orderId: string }): Promise<number> => {
|
|
140
50
|
const result = await OrderPositions.deleteMany({ orderId });
|
|
141
51
|
await emit('ORDER_EMPTY_CART', { orderId, count: result.deletedCount });
|
|
142
52
|
return result.deletedCount;
|
|
143
53
|
},
|
|
144
54
|
|
|
145
|
-
updateProductItem: async (
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
55
|
+
updateProductItem: async ({
|
|
56
|
+
orderPositionId,
|
|
57
|
+
quantity,
|
|
58
|
+
configuration,
|
|
59
|
+
}: {
|
|
60
|
+
orderPositionId: string;
|
|
61
|
+
configuration?: Array<{ key: string; value: string }>;
|
|
62
|
+
quantity?: number;
|
|
63
|
+
}): Promise<OrderPosition> => {
|
|
151
64
|
const modifier: any = {
|
|
152
65
|
$set: {
|
|
153
66
|
updated: new Date(),
|
|
154
67
|
},
|
|
155
68
|
};
|
|
156
69
|
|
|
157
|
-
if (quantity !== null
|
|
70
|
+
if (quantity !== null) {
|
|
158
71
|
modifier.$set.quantity = quantity;
|
|
159
72
|
}
|
|
160
73
|
|
|
161
74
|
if (configuration !== null) {
|
|
162
|
-
const resolvedProduct = await unchainedAPI.modules.products.resolveOrderableProduct(
|
|
163
|
-
product,
|
|
164
|
-
{ configuration },
|
|
165
|
-
unchainedAPI,
|
|
166
|
-
);
|
|
167
|
-
modifier.$set.productId = resolvedProduct._id;
|
|
168
75
|
modifier.$set.configuration = configuration;
|
|
169
76
|
}
|
|
170
77
|
|
|
171
|
-
await
|
|
78
|
+
const updatedOrderPosition = await OrderPositions.findOneAndUpdate(
|
|
172
79
|
{
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
80
|
+
_id: orderPositionId,
|
|
81
|
+
},
|
|
82
|
+
modifier,
|
|
83
|
+
{
|
|
84
|
+
returnDocument: 'after',
|
|
177
85
|
},
|
|
178
|
-
unchainedAPI,
|
|
179
86
|
);
|
|
180
87
|
|
|
181
|
-
const updatedOrderPosition = await OrderPositions.findOneAndUpdate(selector, modifier, {
|
|
182
|
-
returnDocument: 'after',
|
|
183
|
-
});
|
|
184
|
-
|
|
185
88
|
await emit('ORDER_UPDATE_CART_ITEM', {
|
|
186
89
|
orderPosition: updatedOrderPosition,
|
|
187
90
|
});
|
|
@@ -189,7 +92,7 @@ export const configureOrderPositionsModule = ({
|
|
|
189
92
|
return updatedOrderPosition;
|
|
190
93
|
},
|
|
191
94
|
|
|
192
|
-
removeProductByIdFromAllOpenPositions: async (productId) => {
|
|
95
|
+
removeProductByIdFromAllOpenPositions: async (productId: string): Promise<Array<string>> => {
|
|
193
96
|
const positions = await OrderPositions.aggregate([
|
|
194
97
|
{
|
|
195
98
|
$match: {
|
|
@@ -225,56 +128,9 @@ export const configureOrderPositionsModule = ({
|
|
|
225
128
|
return orderIdsToRecalculate;
|
|
226
129
|
},
|
|
227
130
|
|
|
228
|
-
updateScheduling: async (
|
|
229
|
-
const { modules } = unchainedAPI;
|
|
230
|
-
// scheduling (store in db for auditing)
|
|
231
|
-
const product = await modules.products.findProduct({
|
|
232
|
-
productId: orderPosition.productId,
|
|
233
|
-
});
|
|
234
|
-
const deliveryProvider =
|
|
235
|
-
orderDelivery &&
|
|
236
|
-
(await modules.delivery.findProvider({
|
|
237
|
-
deliveryProviderId: orderDelivery.deliveryProviderId,
|
|
238
|
-
}));
|
|
239
|
-
const { countryCode, userId } = order;
|
|
240
|
-
|
|
241
|
-
const scheduling = await Promise.all(
|
|
242
|
-
(
|
|
243
|
-
await modules.warehousing.findSupported(
|
|
244
|
-
{
|
|
245
|
-
product,
|
|
246
|
-
deliveryProvider,
|
|
247
|
-
},
|
|
248
|
-
unchainedAPI,
|
|
249
|
-
)
|
|
250
|
-
).map(async (warehousingProvider) => {
|
|
251
|
-
const context = {
|
|
252
|
-
warehousingProvider,
|
|
253
|
-
deliveryProvider,
|
|
254
|
-
product,
|
|
255
|
-
item: orderPosition,
|
|
256
|
-
delivery: deliveryProvider,
|
|
257
|
-
order,
|
|
258
|
-
userId,
|
|
259
|
-
country: countryCode,
|
|
260
|
-
referenceDate: order.ordered,
|
|
261
|
-
quantity: orderPosition.quantity,
|
|
262
|
-
};
|
|
263
|
-
const dispatch = await unchainedAPI.modules.warehousing.estimatedDispatch(
|
|
264
|
-
warehousingProvider,
|
|
265
|
-
context,
|
|
266
|
-
unchainedAPI,
|
|
267
|
-
);
|
|
268
|
-
|
|
269
|
-
return {
|
|
270
|
-
warehousingProviderId: warehousingProvider._id,
|
|
271
|
-
...dispatch,
|
|
272
|
-
};
|
|
273
|
-
}),
|
|
274
|
-
);
|
|
275
|
-
|
|
131
|
+
updateScheduling: async (orderPositionId, scheduling): Promise<OrderPosition> => {
|
|
276
132
|
return OrderPositions.findOneAndUpdate(
|
|
277
|
-
generateDbFilterById(
|
|
133
|
+
generateDbFilterById(orderPositionId),
|
|
278
134
|
{
|
|
279
135
|
$set: { scheduling },
|
|
280
136
|
},
|
|
@@ -284,13 +140,12 @@ export const configureOrderPositionsModule = ({
|
|
|
284
140
|
);
|
|
285
141
|
},
|
|
286
142
|
|
|
287
|
-
updateCalculation: async
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
);
|
|
143
|
+
updateCalculation: async <T extends PricingCalculation>(
|
|
144
|
+
orderPositionId: string,
|
|
145
|
+
calculation: Array<T>,
|
|
146
|
+
): Promise<OrderPosition> => {
|
|
292
147
|
return OrderPositions.findOneAndUpdate(
|
|
293
|
-
|
|
148
|
+
{ _id: orderPositionId },
|
|
294
149
|
{
|
|
295
150
|
$set: { calculation },
|
|
296
151
|
},
|
|
@@ -300,34 +155,22 @@ export const configureOrderPositionsModule = ({
|
|
|
300
155
|
);
|
|
301
156
|
},
|
|
302
157
|
|
|
303
|
-
addProductItem: async (orderPosition:
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
);
|
|
314
|
-
|
|
315
|
-
// Validate add to cart mutation
|
|
316
|
-
await ordersSettings.validateOrderPosition(
|
|
317
|
-
{
|
|
318
|
-
order,
|
|
319
|
-
product,
|
|
320
|
-
configuration,
|
|
321
|
-
quantityDiff: quantity,
|
|
322
|
-
},
|
|
323
|
-
unchainedAPI,
|
|
324
|
-
);
|
|
158
|
+
addProductItem: async (orderPosition: {
|
|
159
|
+
context?: any;
|
|
160
|
+
configuration?: Array<{ key: string; value: string }>;
|
|
161
|
+
orderId: string;
|
|
162
|
+
originalProductId: string;
|
|
163
|
+
productId: string;
|
|
164
|
+
quantity: number;
|
|
165
|
+
quotationId?: string;
|
|
166
|
+
}): Promise<OrderPosition> => {
|
|
167
|
+
const { configuration, orderId, originalProductId, productId, quantity, ...scope } = orderPosition;
|
|
325
168
|
|
|
326
169
|
// Search for existing position
|
|
327
170
|
const selector: mongodb.Filter<OrderPosition> = {
|
|
328
171
|
orderId,
|
|
329
|
-
productId
|
|
330
|
-
originalProductId
|
|
172
|
+
productId,
|
|
173
|
+
originalProductId,
|
|
331
174
|
configuration: configuration || { $in: [null, undefined] },
|
|
332
175
|
...scope,
|
|
333
176
|
};
|
|
@@ -345,8 +188,8 @@ export const configureOrderPositionsModule = ({
|
|
|
345
188
|
calculation: [],
|
|
346
189
|
scheduling: [],
|
|
347
190
|
orderId,
|
|
348
|
-
productId
|
|
349
|
-
originalProductId
|
|
191
|
+
productId,
|
|
192
|
+
originalProductId,
|
|
350
193
|
configuration,
|
|
351
194
|
...scope,
|
|
352
195
|
},
|
|
@@ -360,5 +203,11 @@ export const configureOrderPositionsModule = ({
|
|
|
360
203
|
|
|
361
204
|
return upsertedOrderPosition;
|
|
362
205
|
},
|
|
206
|
+
deleteOrderPositions: async (orderId: string) => {
|
|
207
|
+
const { deletedCount } = await OrderPositions.deleteMany({ orderId });
|
|
208
|
+
return deletedCount;
|
|
209
|
+
},
|
|
363
210
|
};
|
|
364
211
|
};
|
|
212
|
+
|
|
213
|
+
export type OrderPositionsModule = ReturnType<typeof configureOrderPositionsModule>;
|
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { Order, OrderStatus } from '../types.js';
|
|
3
|
-
import { OrderDelivery } from '@unchainedshop/core-orders';
|
|
4
|
-
import { OrderPayment } from '@unchainedshop/core-orders';
|
|
1
|
+
import { Order, OrderStatus, OrderPosition } from '../types.js';
|
|
5
2
|
import { emit, registerEvents } from '@unchainedshop/events';
|
|
6
3
|
import {
|
|
7
4
|
Address,
|
|
@@ -10,7 +7,6 @@ import {
|
|
|
10
7
|
generateDbObjectId,
|
|
11
8
|
mongodb,
|
|
12
9
|
} from '@unchainedshop/mongodb';
|
|
13
|
-
import { OrderPosition } from '@unchainedshop/core-orders';
|
|
14
10
|
|
|
15
11
|
export interface OrderMutations {
|
|
16
12
|
create: (doc: {
|
|
@@ -28,20 +24,10 @@ export interface OrderMutations {
|
|
|
28
24
|
setCartOwner: (params: { orderId: string; userId: string }) => Promise<void>;
|
|
29
25
|
moveCartPositions: (params: { fromOrderId: string; toOrderId: string }) => Promise<void>;
|
|
30
26
|
|
|
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
27
|
updateBillingAddress: (orderId: string, billingAddress: Address) => Promise<Order>;
|
|
43
28
|
updateContact: (orderId: string, contact: Contact) => Promise<Order>;
|
|
44
|
-
updateContext: (orderId: string, context: any) => Promise<Order
|
|
29
|
+
updateContext: (orderId: string, context: any) => Promise<Order>;
|
|
30
|
+
updateCalculationSheet: (orderId: string, calculation) => Promise<Order>;
|
|
45
31
|
}
|
|
46
32
|
|
|
47
33
|
const ORDER_EVENTS: string[] = [
|
|
@@ -54,13 +40,9 @@ const ORDER_EVENTS: string[] = [
|
|
|
54
40
|
|
|
55
41
|
export const configureOrderModuleMutations = ({
|
|
56
42
|
Orders,
|
|
57
|
-
OrderDeliveries,
|
|
58
|
-
OrderPayments,
|
|
59
43
|
OrderPositions,
|
|
60
44
|
}: {
|
|
61
45
|
Orders: mongodb.Collection<Order>;
|
|
62
|
-
OrderDeliveries: mongodb.Collection<OrderDelivery>;
|
|
63
|
-
OrderPayments: mongodb.Collection<OrderPayment>;
|
|
64
46
|
OrderPositions: mongodb.Collection<OrderPosition>;
|
|
65
47
|
}): OrderMutations => {
|
|
66
48
|
registerEvents(ORDER_EVENTS);
|
|
@@ -112,109 +94,54 @@ export const configureOrderModuleMutations = ({
|
|
|
112
94
|
);
|
|
113
95
|
},
|
|
114
96
|
|
|
115
|
-
|
|
116
|
-
const delivery = await OrderDeliveries.findOne({
|
|
117
|
-
orderId,
|
|
118
|
-
deliveryProviderId,
|
|
119
|
-
});
|
|
120
|
-
const deliveryId =
|
|
121
|
-
delivery?._id ||
|
|
122
|
-
(
|
|
123
|
-
await modules.orders.deliveries.create({
|
|
124
|
-
calculation: [],
|
|
125
|
-
deliveryProviderId,
|
|
126
|
-
log: [],
|
|
127
|
-
orderId,
|
|
128
|
-
status: null,
|
|
129
|
-
})
|
|
130
|
-
)._id;
|
|
131
|
-
|
|
97
|
+
updateBillingAddress: async (orderId, billingAddress) => {
|
|
132
98
|
const selector = generateDbFilterById(orderId);
|
|
133
99
|
const order = await Orders.findOneAndUpdate(
|
|
134
100
|
selector,
|
|
135
101
|
{
|
|
136
102
|
$set: {
|
|
137
|
-
|
|
103
|
+
billingAddress,
|
|
138
104
|
updated: new Date(),
|
|
139
105
|
},
|
|
140
106
|
},
|
|
141
107
|
{ returnDocument: 'after' },
|
|
142
108
|
);
|
|
143
109
|
|
|
144
|
-
await emit('
|
|
145
|
-
order,
|
|
146
|
-
deliveryProviderId,
|
|
147
|
-
});
|
|
148
|
-
|
|
149
|
-
return order;
|
|
150
|
-
},
|
|
151
|
-
|
|
152
|
-
setPaymentProvider: async (orderId, paymentProviderId, unchainedAPI) => {
|
|
153
|
-
const { modules } = unchainedAPI;
|
|
154
|
-
const payment = await OrderPayments.findOne({
|
|
155
|
-
orderId,
|
|
156
|
-
paymentProviderId,
|
|
157
|
-
});
|
|
158
|
-
|
|
159
|
-
const paymentId =
|
|
160
|
-
payment?._id ||
|
|
161
|
-
(
|
|
162
|
-
await modules.orders.payments.create({
|
|
163
|
-
calculation: [],
|
|
164
|
-
paymentProviderId,
|
|
165
|
-
log: [],
|
|
166
|
-
orderId,
|
|
167
|
-
status: null,
|
|
168
|
-
})
|
|
169
|
-
)._id;
|
|
170
|
-
const selector = generateDbFilterById(orderId);
|
|
171
|
-
const order = await Orders.findOneAndUpdate(
|
|
172
|
-
selector,
|
|
173
|
-
{
|
|
174
|
-
$set: { paymentId, updated: new Date() },
|
|
175
|
-
},
|
|
176
|
-
{ returnDocument: 'after' },
|
|
177
|
-
);
|
|
178
|
-
|
|
179
|
-
await emit('ORDER_SET_PAYMENT_PROVIDER', {
|
|
180
|
-
order,
|
|
181
|
-
paymentProviderId,
|
|
182
|
-
});
|
|
183
|
-
|
|
110
|
+
await emit('ORDER_UPDATE', { order, field: 'billing' });
|
|
184
111
|
return order;
|
|
185
112
|
},
|
|
186
113
|
|
|
187
|
-
|
|
114
|
+
updateContact: async (orderId, contact) => {
|
|
188
115
|
const selector = generateDbFilterById(orderId);
|
|
189
116
|
const order = await Orders.findOneAndUpdate(
|
|
190
117
|
selector,
|
|
191
118
|
{
|
|
192
119
|
$set: {
|
|
193
|
-
|
|
120
|
+
contact,
|
|
194
121
|
updated: new Date(),
|
|
195
122
|
},
|
|
196
123
|
},
|
|
197
124
|
{ returnDocument: 'after' },
|
|
198
125
|
);
|
|
199
126
|
|
|
200
|
-
await emit('ORDER_UPDATE', { order, field: '
|
|
127
|
+
await emit('ORDER_UPDATE', { order, field: 'contact' });
|
|
201
128
|
return order;
|
|
202
129
|
},
|
|
203
130
|
|
|
204
|
-
|
|
131
|
+
updateCalculationSheet: async (orderId, calculation) => {
|
|
205
132
|
const selector = generateDbFilterById(orderId);
|
|
206
133
|
const order = await Orders.findOneAndUpdate(
|
|
207
134
|
selector,
|
|
208
135
|
{
|
|
209
136
|
$set: {
|
|
210
|
-
|
|
137
|
+
calculation,
|
|
211
138
|
updated: new Date(),
|
|
212
139
|
},
|
|
213
140
|
},
|
|
214
141
|
{ returnDocument: 'after' },
|
|
215
142
|
);
|
|
216
143
|
|
|
217
|
-
await emit('ORDER_UPDATE', { order, field: '
|
|
144
|
+
await emit('ORDER_UPDATE', { order, field: 'calculation' });
|
|
218
145
|
return order;
|
|
219
146
|
},
|
|
220
147
|
|
|
@@ -1,26 +1,7 @@
|
|
|
1
|
-
import { SortDirection, SortOption } from '@unchainedshop/utils';
|
|
1
|
+
import { SortDirection, SortOption, DateFilterInput } from '@unchainedshop/utils';
|
|
2
2
|
import { Order, OrderQuery, OrderReport } from '../types.js';
|
|
3
3
|
import { generateDbFilterById, buildSortOptions, mongodb } from '@unchainedshop/mongodb';
|
|
4
|
-
|
|
5
|
-
export const buildFindSelector = ({ includeCarts, status, userId, queryString }: OrderQuery) => {
|
|
6
|
-
const selector: mongodb.Filter<Order> = {};
|
|
7
|
-
|
|
8
|
-
if (userId) {
|
|
9
|
-
selector.userId = userId;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
if (Array.isArray(status) && status?.length) {
|
|
13
|
-
selector.status = { $in: status };
|
|
14
|
-
} else if (!includeCarts) {
|
|
15
|
-
selector.status = { $ne: null }; // TODO: Slow performance! IDXSCAN in common query!
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
if (queryString) {
|
|
19
|
-
(selector as any).$text = { $search: queryString };
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
return selector;
|
|
23
|
-
};
|
|
4
|
+
import buildFindSelector from './buildFindSelector.js';
|
|
24
5
|
|
|
25
6
|
const normalizeOrderAggregateResult = (data = {}): OrderReport => {
|
|
26
7
|
const statusToFieldMap = {
|
|
@@ -39,11 +20,41 @@ const normalizeOrderAggregateResult = (data = {}): OrderReport => {
|
|
|
39
20
|
|
|
40
21
|
export const configureOrdersModuleQueries = ({ Orders }: { Orders: mongodb.Collection<Order> }) => {
|
|
41
22
|
return {
|
|
23
|
+
isCart: (order: Order) => {
|
|
24
|
+
return order.status === null;
|
|
25
|
+
},
|
|
26
|
+
|
|
27
|
+
cart: async ({
|
|
28
|
+
orderNumber,
|
|
29
|
+
countryContext,
|
|
30
|
+
userId,
|
|
31
|
+
}: {
|
|
32
|
+
countryContext?: string;
|
|
33
|
+
orderNumber?: string;
|
|
34
|
+
userId: string;
|
|
35
|
+
}): Promise<Order> => {
|
|
36
|
+
const selector: mongodb.Filter<Order> = {
|
|
37
|
+
countryCode: countryContext,
|
|
38
|
+
status: { $eq: null },
|
|
39
|
+
userId,
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
if (orderNumber) {
|
|
43
|
+
selector.orderNumber = orderNumber;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const options: mongodb.FindOptions = {
|
|
47
|
+
sort: {
|
|
48
|
+
updated: -1,
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
return Orders.findOne(selector, options);
|
|
52
|
+
},
|
|
53
|
+
|
|
42
54
|
count: async (query: OrderQuery): Promise<number> => {
|
|
43
55
|
const orderCount = await Orders.countDocuments(buildFindSelector(query));
|
|
44
56
|
return orderCount;
|
|
45
57
|
},
|
|
46
|
-
|
|
47
58
|
findOrder: async (
|
|
48
59
|
{
|
|
49
60
|
orderId,
|
|
@@ -58,6 +69,19 @@ export const configureOrdersModuleQueries = ({ Orders }: { Orders: mongodb.Colle
|
|
|
58
69
|
return Orders.findOne(selector, options);
|
|
59
70
|
},
|
|
60
71
|
|
|
72
|
+
findCartsToInvalidate: async (maxAgeDays = 30) => {
|
|
73
|
+
const ONE_DAY_IN_MILLISECONDS = 86400000;
|
|
74
|
+
|
|
75
|
+
const minValidDate = new Date(new Date().getTime() - maxAgeDays * ONE_DAY_IN_MILLISECONDS);
|
|
76
|
+
|
|
77
|
+
const orders = await Orders.find({
|
|
78
|
+
status: { $eq: null },
|
|
79
|
+
updated: { $gte: minValidDate },
|
|
80
|
+
}).toArray();
|
|
81
|
+
|
|
82
|
+
return orders;
|
|
83
|
+
},
|
|
84
|
+
|
|
61
85
|
findOrders: async (
|
|
62
86
|
{
|
|
63
87
|
limit,
|
|
@@ -91,16 +115,16 @@ export const configureOrdersModuleQueries = ({ Orders }: { Orders: mongodb.Colle
|
|
|
91
115
|
return Orders.find(selector, findOptions).toArray();
|
|
92
116
|
},
|
|
93
117
|
|
|
94
|
-
getReport: async (params
|
|
95
|
-
const {
|
|
118
|
+
getReport: async (params: { dateRange?: DateFilterInput }): Promise<OrderReport> => {
|
|
119
|
+
const { dateRange } = params || {};
|
|
96
120
|
const selector: any = { $exists: true };
|
|
97
|
-
if (
|
|
98
|
-
if (
|
|
99
|
-
const fromDate = new Date(
|
|
121
|
+
if (dateRange?.end || dateRange?.start) {
|
|
122
|
+
if (dateRange?.start) {
|
|
123
|
+
const fromDate = new Date(dateRange?.start);
|
|
100
124
|
selector.$gte = fromDate;
|
|
101
125
|
}
|
|
102
|
-
if (
|
|
103
|
-
const toDate = new Date(
|
|
126
|
+
if (dateRange?.end) {
|
|
127
|
+
const toDate = new Date(dateRange?.end);
|
|
104
128
|
selector.$lte = toDate;
|
|
105
129
|
}
|
|
106
130
|
}
|