@unchainedshop/core-orders 3.0.0-alpha7 → 3.0.0-rc2
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 +21 -2
- package/lib/db/OrderDeliveriesCollection.d.ts.map +1 -1
- package/lib/db/OrderDeliveriesCollection.js +6 -0
- package/lib/db/OrderDeliveriesCollection.js.map +1 -1
- package/lib/db/OrderDiscountsCollection.d.ts +16 -2
- package/lib/db/OrderDiscountsCollection.d.ts.map +1 -1
- package/lib/db/OrderDiscountsCollection.js +5 -0
- package/lib/db/OrderDiscountsCollection.js.map +1 -1
- package/lib/db/OrderPaymentsCollection.d.ts +22 -2
- package/lib/db/OrderPaymentsCollection.d.ts.map +1 -1
- package/lib/db/OrderPaymentsCollection.js +6 -0
- package/lib/db/OrderPaymentsCollection.js.map +1 -1
- package/lib/db/OrderPositionsCollection.d.ts +22 -2
- package/lib/db/OrderPositionsCollection.d.ts.map +1 -1
- package/lib/db/OrderPositionsCollection.js.map +1 -1
- package/lib/db/OrdersCollection.d.ts +33 -2
- package/lib/db/OrdersCollection.d.ts.map +1 -1
- package/lib/db/OrdersCollection.js +8 -1
- package/lib/db/OrdersCollection.js.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 +8 -69
- 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 +34 -143
- 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 +16 -124
- 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 +22 -85
- 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 +5 -11
- package/lib/module/configureOrdersModule-mutations.d.ts.map +1 -1
- package/lib/module/configureOrdersModule-mutations.js +11 -50
- 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 +18 -6
- 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 -154
- package/lib/module/configureOrdersModule.js.map +1 -1
- package/lib/orders-index.d.ts +11 -8
- package/lib/orders-index.d.ts.map +1 -1
- package/lib/orders-index.js +11 -8
- package/lib/orders-index.js.map +1 -1
- package/lib/orders-settings.d.ts +4 -8
- package/lib/orders-settings.d.ts.map +1 -1
- package/lib/orders-settings.js.map +1 -1
- package/lib/types.d.ts +7 -22
- package/lib/types.d.ts.map +1 -1
- package/lib/types.js +0 -5
- package/lib/types.js.map +1 -1
- package/package.json +9 -12
- package/src/db/OrderDeliveriesCollection.ts +25 -2
- package/src/db/OrderDiscountsCollection.ts +18 -2
- package/src/db/OrderPaymentsCollection.ts +26 -2
- package/src/db/OrderPositionsCollection.ts +21 -2
- package/src/db/OrdersCollection.ts +44 -2
- package/src/module/buildFindSelector.test.ts +8 -8
- package/src/module/buildFindSelector.ts +24 -0
- package/src/module/configureOrderDeliveriesModule.ts +29 -172
- package/src/module/configureOrderDiscountsModule.ts +46 -213
- package/src/module/configureOrderPaymentsModule.ts +58 -266
- package/src/module/configureOrderPositionsModule.ts +57 -208
- package/src/module/configureOrdersModule-mutations.ts +13 -85
- package/src/module/configureOrdersModule-queries.ts +61 -29
- package/src/module/configureOrdersModule.ts +180 -264
- package/src/orders-index.ts +11 -10
- package/src/orders-settings.ts +5 -7
- 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/db/OrderDiscountTrigger.ts +0 -4
- 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
- package/src/types.ts +0 -129
|
@@ -1,78 +1,7 @@
|
|
|
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';
|
|
6
1
|
import { emit, registerEvents } from '@unchainedshop/events';
|
|
7
2
|
import { generateDbFilterById, generateDbObjectId, mongodb } from '@unchainedshop/mongodb';
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
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
|
-
};
|
|
3
|
+
import { PricingCalculation } from '@unchainedshop/utils';
|
|
4
|
+
import { OrderPosition } from '../db/OrderPositionsCollection.js';
|
|
76
5
|
|
|
77
6
|
const ORDER_POSITION_EVENTS: string[] = [
|
|
78
7
|
'ORDER_UPDATE_CART_ITEM',
|
|
@@ -81,7 +10,7 @@ const ORDER_POSITION_EVENTS: string[] = [
|
|
|
81
10
|
'ORDER_ADD_PRODUCT',
|
|
82
11
|
];
|
|
83
12
|
|
|
84
|
-
export const
|
|
13
|
+
export const buildFindOrderPositionByIdSelector = (orderPositionId: string, orderId?: string) =>
|
|
85
14
|
generateDbFilterById(
|
|
86
15
|
orderPositionId,
|
|
87
16
|
orderId ? { orderId } : undefined,
|
|
@@ -91,44 +20,25 @@ 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 (
|
|
100
|
-
|
|
28
|
+
findOrderPosition: async (
|
|
29
|
+
{ itemId }: { itemId: string },
|
|
30
|
+
options?: mongodb.FindOptions,
|
|
31
|
+
): Promise<OrderPosition> => {
|
|
32
|
+
return OrderPositions.findOne(buildFindOrderPositionByIdSelector(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
|
-
|
|
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) => {
|
|
131
|
-
const selector = buildFindByIdSelector(orderPositionId);
|
|
40
|
+
delete: async (orderPositionId: string): Promise<OrderPosition> => {
|
|
41
|
+
const selector = buildFindOrderPositionByIdSelector(orderPositionId);
|
|
132
42
|
const orderPosition = await OrderPositions.findOneAndDelete(selector, {});
|
|
133
43
|
await emit('ORDER_REMOVE_CART_ITEM', {
|
|
134
44
|
orderPosition,
|
|
@@ -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,3 @@
|
|
|
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
1
|
import { emit, registerEvents } from '@unchainedshop/events';
|
|
6
2
|
import {
|
|
7
3
|
Address,
|
|
@@ -10,7 +6,8 @@ import {
|
|
|
10
6
|
generateDbObjectId,
|
|
11
7
|
mongodb,
|
|
12
8
|
} from '@unchainedshop/mongodb';
|
|
13
|
-
import {
|
|
9
|
+
import { Order, OrderStatus } from '../db/OrdersCollection.js';
|
|
10
|
+
import { OrderPosition } from '../db/OrderPositionsCollection.js';
|
|
14
11
|
|
|
15
12
|
export interface OrderMutations {
|
|
16
13
|
create: (doc: {
|
|
@@ -28,20 +25,10 @@ export interface OrderMutations {
|
|
|
28
25
|
setCartOwner: (params: { orderId: string; userId: string }) => Promise<void>;
|
|
29
26
|
moveCartPositions: (params: { fromOrderId: string; toOrderId: string }) => Promise<void>;
|
|
30
27
|
|
|
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
28
|
updateBillingAddress: (orderId: string, billingAddress: Address) => Promise<Order>;
|
|
43
29
|
updateContact: (orderId: string, contact: Contact) => Promise<Order>;
|
|
44
|
-
updateContext: (orderId: string, context: any) => Promise<Order
|
|
30
|
+
updateContext: (orderId: string, context: any) => Promise<Order>;
|
|
31
|
+
updateCalculationSheet: (orderId: string, calculation) => Promise<Order>;
|
|
45
32
|
}
|
|
46
33
|
|
|
47
34
|
const ORDER_EVENTS: string[] = [
|
|
@@ -54,13 +41,9 @@ const ORDER_EVENTS: string[] = [
|
|
|
54
41
|
|
|
55
42
|
export const configureOrderModuleMutations = ({
|
|
56
43
|
Orders,
|
|
57
|
-
OrderDeliveries,
|
|
58
|
-
OrderPayments,
|
|
59
44
|
OrderPositions,
|
|
60
45
|
}: {
|
|
61
46
|
Orders: mongodb.Collection<Order>;
|
|
62
|
-
OrderDeliveries: mongodb.Collection<OrderDelivery>;
|
|
63
|
-
OrderPayments: mongodb.Collection<OrderPayment>;
|
|
64
47
|
OrderPositions: mongodb.Collection<OrderPosition>;
|
|
65
48
|
}): OrderMutations => {
|
|
66
49
|
registerEvents(ORDER_EVENTS);
|
|
@@ -112,109 +95,54 @@ export const configureOrderModuleMutations = ({
|
|
|
112
95
|
);
|
|
113
96
|
},
|
|
114
97
|
|
|
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
|
-
|
|
98
|
+
updateBillingAddress: async (orderId, billingAddress) => {
|
|
132
99
|
const selector = generateDbFilterById(orderId);
|
|
133
100
|
const order = await Orders.findOneAndUpdate(
|
|
134
101
|
selector,
|
|
135
102
|
{
|
|
136
103
|
$set: {
|
|
137
|
-
|
|
104
|
+
billingAddress,
|
|
138
105
|
updated: new Date(),
|
|
139
106
|
},
|
|
140
107
|
},
|
|
141
108
|
{ returnDocument: 'after' },
|
|
142
109
|
);
|
|
143
110
|
|
|
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
|
-
|
|
111
|
+
await emit('ORDER_UPDATE', { order, field: 'billing' });
|
|
184
112
|
return order;
|
|
185
113
|
},
|
|
186
114
|
|
|
187
|
-
|
|
115
|
+
updateContact: async (orderId, contact) => {
|
|
188
116
|
const selector = generateDbFilterById(orderId);
|
|
189
117
|
const order = await Orders.findOneAndUpdate(
|
|
190
118
|
selector,
|
|
191
119
|
{
|
|
192
120
|
$set: {
|
|
193
|
-
|
|
121
|
+
contact,
|
|
194
122
|
updated: new Date(),
|
|
195
123
|
},
|
|
196
124
|
},
|
|
197
125
|
{ returnDocument: 'after' },
|
|
198
126
|
);
|
|
199
127
|
|
|
200
|
-
await emit('ORDER_UPDATE', { order, field: '
|
|
128
|
+
await emit('ORDER_UPDATE', { order, field: 'contact' });
|
|
201
129
|
return order;
|
|
202
130
|
},
|
|
203
131
|
|
|
204
|
-
|
|
132
|
+
updateCalculationSheet: async (orderId, calculation) => {
|
|
205
133
|
const selector = generateDbFilterById(orderId);
|
|
206
134
|
const order = await Orders.findOneAndUpdate(
|
|
207
135
|
selector,
|
|
208
136
|
{
|
|
209
137
|
$set: {
|
|
210
|
-
|
|
138
|
+
calculation,
|
|
211
139
|
updated: new Date(),
|
|
212
140
|
},
|
|
213
141
|
},
|
|
214
142
|
{ returnDocument: 'after' },
|
|
215
143
|
);
|
|
216
144
|
|
|
217
|
-
await emit('ORDER_UPDATE', { order, field: '
|
|
145
|
+
await emit('ORDER_UPDATE', { order, field: 'calculation' });
|
|
218
146
|
return order;
|
|
219
147
|
},
|
|
220
148
|
|
|
@@ -1,25 +1,14 @@
|
|
|
1
|
-
import { SortDirection, SortOption } from '@unchainedshop/utils';
|
|
2
|
-
import { Order, OrderQuery, OrderReport } from '../types.js';
|
|
1
|
+
import { SortDirection, SortOption, DateFilterInput } from '@unchainedshop/utils';
|
|
3
2
|
import { generateDbFilterById, buildSortOptions, mongodb } from '@unchainedshop/mongodb';
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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;
|
|
3
|
+
import buildFindSelector from './buildFindSelector.js';
|
|
4
|
+
import { Order, OrderQuery } from '../db/OrdersCollection.js';
|
|
5
|
+
|
|
6
|
+
export type OrderReport = {
|
|
7
|
+
newCount: number;
|
|
8
|
+
checkoutCount: number;
|
|
9
|
+
rejectCount: number;
|
|
10
|
+
confirmCount: number;
|
|
11
|
+
fulfillCount: number;
|
|
23
12
|
};
|
|
24
13
|
|
|
25
14
|
const normalizeOrderAggregateResult = (data = {}): OrderReport => {
|
|
@@ -39,11 +28,41 @@ const normalizeOrderAggregateResult = (data = {}): OrderReport => {
|
|
|
39
28
|
|
|
40
29
|
export const configureOrdersModuleQueries = ({ Orders }: { Orders: mongodb.Collection<Order> }) => {
|
|
41
30
|
return {
|
|
31
|
+
isCart: (order: Order) => {
|
|
32
|
+
return order.status === null;
|
|
33
|
+
},
|
|
34
|
+
|
|
35
|
+
cart: async ({
|
|
36
|
+
orderNumber,
|
|
37
|
+
countryContext,
|
|
38
|
+
userId,
|
|
39
|
+
}: {
|
|
40
|
+
countryContext?: string;
|
|
41
|
+
orderNumber?: string;
|
|
42
|
+
userId: string;
|
|
43
|
+
}): Promise<Order> => {
|
|
44
|
+
const selector: mongodb.Filter<Order> = {
|
|
45
|
+
countryCode: countryContext,
|
|
46
|
+
status: { $eq: null },
|
|
47
|
+
userId,
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
if (orderNumber) {
|
|
51
|
+
selector.orderNumber = orderNumber;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const options: mongodb.FindOptions = {
|
|
55
|
+
sort: {
|
|
56
|
+
updated: -1,
|
|
57
|
+
},
|
|
58
|
+
};
|
|
59
|
+
return Orders.findOne(selector, options);
|
|
60
|
+
},
|
|
61
|
+
|
|
42
62
|
count: async (query: OrderQuery): Promise<number> => {
|
|
43
63
|
const orderCount = await Orders.countDocuments(buildFindSelector(query));
|
|
44
64
|
return orderCount;
|
|
45
65
|
},
|
|
46
|
-
|
|
47
66
|
findOrder: async (
|
|
48
67
|
{
|
|
49
68
|
orderId,
|
|
@@ -58,6 +77,19 @@ export const configureOrdersModuleQueries = ({ Orders }: { Orders: mongodb.Colle
|
|
|
58
77
|
return Orders.findOne(selector, options);
|
|
59
78
|
},
|
|
60
79
|
|
|
80
|
+
findCartsToInvalidate: async (maxAgeDays = 30) => {
|
|
81
|
+
const ONE_DAY_IN_MILLISECONDS = 86400000;
|
|
82
|
+
|
|
83
|
+
const minValidDate = new Date(new Date().getTime() - maxAgeDays * ONE_DAY_IN_MILLISECONDS);
|
|
84
|
+
|
|
85
|
+
const orders = await Orders.find({
|
|
86
|
+
status: { $eq: null },
|
|
87
|
+
updated: { $gte: minValidDate },
|
|
88
|
+
}).toArray();
|
|
89
|
+
|
|
90
|
+
return orders;
|
|
91
|
+
},
|
|
92
|
+
|
|
61
93
|
findOrders: async (
|
|
62
94
|
{
|
|
63
95
|
limit,
|
|
@@ -91,16 +123,16 @@ export const configureOrdersModuleQueries = ({ Orders }: { Orders: mongodb.Colle
|
|
|
91
123
|
return Orders.find(selector, findOptions).toArray();
|
|
92
124
|
},
|
|
93
125
|
|
|
94
|
-
getReport: async (params
|
|
95
|
-
const {
|
|
126
|
+
getReport: async (params: { dateRange?: DateFilterInput }): Promise<OrderReport> => {
|
|
127
|
+
const { dateRange } = params || {};
|
|
96
128
|
const selector: any = { $exists: true };
|
|
97
|
-
if (
|
|
98
|
-
if (
|
|
99
|
-
const fromDate = new Date(
|
|
129
|
+
if (dateRange?.end || dateRange?.start) {
|
|
130
|
+
if (dateRange?.start) {
|
|
131
|
+
const fromDate = new Date(dateRange?.start);
|
|
100
132
|
selector.$gte = fromDate;
|
|
101
133
|
}
|
|
102
|
-
if (
|
|
103
|
-
const toDate = new Date(
|
|
134
|
+
if (dateRange?.end) {
|
|
135
|
+
const toDate = new Date(dateRange?.end);
|
|
104
136
|
selector.$lte = toDate;
|
|
105
137
|
}
|
|
106
138
|
}
|