@unchainedshop/core-orders 2.12.2 → 3.0.0-alpha2
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/module/configureOrderDeliveriesModule.d.ts +1 -3
- package/lib/module/configureOrderDeliveriesModule.d.ts.map +1 -1
- package/lib/module/configureOrderDeliveriesModule.js +2 -9
- package/lib/module/configureOrderDeliveriesModule.js.map +1 -1
- package/lib/module/configureOrderDiscountsModule.d.ts +1 -3
- package/lib/module/configureOrderDiscountsModule.d.ts.map +1 -1
- package/lib/module/configureOrderDiscountsModule.js +2 -15
- package/lib/module/configureOrderDiscountsModule.js.map +1 -1
- package/lib/module/configureOrderPaymentsModule.d.ts +1 -3
- package/lib/module/configureOrderPaymentsModule.d.ts.map +1 -1
- package/lib/module/configureOrderPaymentsModule.js +2 -11
- package/lib/module/configureOrderPaymentsModule.js.map +1 -1
- package/lib/module/configureOrderPositionsModule.d.ts +1 -3
- 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 +2 -4
- package/lib/module/configureOrdersModule-mutations.d.ts.map +1 -1
- package/lib/module/configureOrdersModule-mutations.js +16 -42
- package/lib/module/configureOrdersModule-mutations.js.map +1 -1
- package/lib/module/configureOrdersModule-processing.d.ts.map +1 -1
- package/lib/module/configureOrdersModule-processing.js +0 -9
- package/lib/module/configureOrdersModule-processing.js.map +1 -1
- package/lib/module/configureOrdersModule.d.ts.map +1 -1
- package/lib/module/configureOrdersModule.js +15 -7
- package/lib/module/configureOrdersModule.js.map +1 -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.map +1 -1
- package/lib/service/nextUserCartService.js +5 -4
- package/lib/service/nextUserCartService.js.map +1 -1
- package/package.json +8 -8
- package/src/module/configureOrderDeliveriesModule.ts +1 -15
- package/src/module/configureOrderDiscountsModule.ts +1 -25
- package/src/module/configureOrderPaymentsModule.ts +1 -15
- package/src/module/configureOrderPositionsModule.ts +10 -93
- package/src/module/configureOrdersModule-mutations.ts +42 -69
- package/src/module/configureOrdersModule-processing.ts +0 -12
- package/src/module/configureOrdersModule.ts +22 -7
- package/src/service/migrateOrderCartService.ts +2 -6
- package/src/service/nextUserCartService.ts +5 -7
|
@@ -1,12 +1,10 @@
|
|
|
1
1
|
import { ModuleMutations } from '@unchainedshop/types/core.js';
|
|
2
|
-
import { OrdersModule } from '@unchainedshop/types/orders.js';
|
|
3
2
|
import {
|
|
4
3
|
OrderPayment,
|
|
5
4
|
OrderPaymentsModule,
|
|
6
5
|
OrderPaymentStatus,
|
|
7
6
|
} from '@unchainedshop/types/orders.payments.js';
|
|
8
7
|
import { emit, registerEvents } from '@unchainedshop/events';
|
|
9
|
-
import { log } from '@unchainedshop/logger';
|
|
10
8
|
import { generateDbFilterById, generateDbMutations, mongodb } from '@unchainedshop/mongodb';
|
|
11
9
|
import { OrderPaymentsSchema } from '../db/OrderPaymentsSchema.js';
|
|
12
10
|
|
|
@@ -35,10 +33,8 @@ export const buildFindByContextDataSelector = (context: any): mongodb.Filter<Ord
|
|
|
35
33
|
|
|
36
34
|
export const configureOrderPaymentsModule = ({
|
|
37
35
|
OrderPayments,
|
|
38
|
-
updateCalculation,
|
|
39
36
|
}: {
|
|
40
37
|
OrderPayments: mongodb.Collection<OrderPayment>;
|
|
41
|
-
updateCalculation: OrdersModule['updateCalculation'];
|
|
42
38
|
}): OrderPaymentsModule => {
|
|
43
39
|
registerEvents(ORDER_PAYMENT_EVENTS);
|
|
44
40
|
|
|
@@ -70,8 +66,6 @@ export const configureOrderPaymentsModule = ({
|
|
|
70
66
|
orderPaymentId,
|
|
71
67
|
{ status, transactionId, info },
|
|
72
68
|
) => {
|
|
73
|
-
log(`OrderPayment ${orderPaymentId} -> New Status: ${status}`);
|
|
74
|
-
|
|
75
69
|
const date = new Date();
|
|
76
70
|
const modifier: mongodb.UpdateFilter<OrderPayment> = {
|
|
77
71
|
$set: { status, updated: new Date() },
|
|
@@ -293,13 +287,10 @@ export const configureOrderPaymentsModule = ({
|
|
|
293
287
|
await emit('ORDER_PAY', { orderPayment });
|
|
294
288
|
},
|
|
295
289
|
|
|
296
|
-
updateContext: async (orderPaymentId, context
|
|
290
|
+
updateContext: async (orderPaymentId, context) => {
|
|
297
291
|
const selector = buildFindByIdSelector(orderPaymentId);
|
|
298
292
|
if (!context || Object.keys(context).length === 0) return OrderPayments.findOne(selector, {});
|
|
299
293
|
|
|
300
|
-
log(`OrderPayment ${orderPaymentId} -> Update Context`, {
|
|
301
|
-
context,
|
|
302
|
-
});
|
|
303
294
|
const contextSetters = Object.fromEntries(
|
|
304
295
|
Object.entries(context).map(([key, value]) => [`context.${key}`, value]),
|
|
305
296
|
);
|
|
@@ -315,7 +306,6 @@ export const configureOrderPaymentsModule = ({
|
|
|
315
306
|
);
|
|
316
307
|
|
|
317
308
|
if (result.ok) {
|
|
318
|
-
await updateCalculation(result.value.orderId, unchainedAPI);
|
|
319
309
|
await emit('ORDER_UPDATE_PAYMENT', {
|
|
320
310
|
orderPayment: result.value,
|
|
321
311
|
});
|
|
@@ -328,10 +318,6 @@ export const configureOrderPaymentsModule = ({
|
|
|
328
318
|
updateStatus,
|
|
329
319
|
|
|
330
320
|
updateCalculation: async (orderPayment, unchainedAPI) => {
|
|
331
|
-
log(`OrderPayment ${orderPayment._id} -> Update Calculation`, {
|
|
332
|
-
orderId: orderPayment.orderId,
|
|
333
|
-
});
|
|
334
|
-
|
|
335
321
|
const calculation = await unchainedAPI.modules.payment.paymentProviders.calculate(
|
|
336
322
|
{
|
|
337
323
|
item: orderPayment,
|
|
@@ -1,15 +1,6 @@
|
|
|
1
|
-
import { ModuleMutations } from '@unchainedshop/types/core.js';
|
|
2
|
-
import { OrdersModule } from '@unchainedshop/types/orders.js';
|
|
3
1
|
import { OrderPosition, OrderPositionsModule } from '@unchainedshop/types/orders.positions.js';
|
|
4
2
|
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';
|
|
3
|
+
import { generateDbFilterById, generateDbObjectId, mongodb } from '@unchainedshop/mongodb';
|
|
13
4
|
import { ordersSettings } from '../orders-settings.js';
|
|
14
5
|
|
|
15
6
|
const ORDER_POSITION_EVENTS: string[] = [
|
|
@@ -27,18 +18,11 @@ export const buildFindByIdSelector = (orderPositionId: string, orderId?: string)
|
|
|
27
18
|
|
|
28
19
|
export const configureOrderPositionsModule = ({
|
|
29
20
|
OrderPositions,
|
|
30
|
-
updateCalculation,
|
|
31
21
|
}: {
|
|
32
22
|
OrderPositions: mongodb.Collection<OrderPosition>;
|
|
33
|
-
updateCalculation: OrdersModule['updateCalculation'];
|
|
34
23
|
}): OrderPositionsModule => {
|
|
35
24
|
registerEvents(ORDER_POSITION_EVENTS);
|
|
36
25
|
|
|
37
|
-
const mutations = generateDbMutations<OrderPosition>(OrderPositions, OrderPositionsSchema, {
|
|
38
|
-
permanentlyDeleteByDefault: true,
|
|
39
|
-
hasCreateOnly: false,
|
|
40
|
-
}) as ModuleMutations<OrderPosition>;
|
|
41
|
-
|
|
42
26
|
return {
|
|
43
27
|
// Queries
|
|
44
28
|
findOrderPosition: async ({ itemId }, options) => {
|
|
@@ -72,68 +56,18 @@ export const configureOrderPositionsModule = ({
|
|
|
72
56
|
});
|
|
73
57
|
},
|
|
74
58
|
|
|
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) => {
|
|
59
|
+
delete: async (orderPositionId) => {
|
|
111
60
|
const selector = buildFindByIdSelector(orderPositionId);
|
|
112
|
-
|
|
113
|
-
log(`Remove Position ${orderPositionId}`, {
|
|
114
|
-
orderPositionId,
|
|
115
|
-
});
|
|
116
|
-
|
|
117
61
|
const orderPosition = await OrderPositions.findOneAndDelete(selector, {});
|
|
118
|
-
|
|
119
|
-
await updateCalculation(orderPosition.orderId, unchainedAPI);
|
|
120
|
-
|
|
121
62
|
await emit('ORDER_REMOVE_CART_ITEM', {
|
|
122
63
|
orderPosition,
|
|
123
64
|
});
|
|
124
|
-
|
|
125
65
|
return { ...orderPosition, calculation: [] };
|
|
126
66
|
},
|
|
127
67
|
|
|
128
|
-
removePositions: async ({ orderId }
|
|
129
|
-
log('Remove Positions', { orderId });
|
|
130
|
-
|
|
68
|
+
removePositions: async ({ orderId }) => {
|
|
131
69
|
const result = await OrderPositions.deleteMany({ orderId });
|
|
132
|
-
|
|
133
|
-
await updateCalculation(orderId, unchainedAPI);
|
|
134
|
-
|
|
135
70
|
await emit('ORDER_EMPTY_CART', { orderId, count: result.deletedCount });
|
|
136
|
-
|
|
137
71
|
return result.deletedCount;
|
|
138
72
|
},
|
|
139
73
|
|
|
@@ -173,11 +107,9 @@ export const configureOrderPositionsModule = ({
|
|
|
173
107
|
unchainedAPI,
|
|
174
108
|
);
|
|
175
109
|
|
|
176
|
-
await OrderPositions.
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
const updatedOrderPosition = await OrderPositions.findOne(selector, {});
|
|
110
|
+
const updatedOrderPosition = await OrderPositions.findOneAndUpdate(selector, modifier, {
|
|
111
|
+
returnDocument: 'after',
|
|
112
|
+
});
|
|
181
113
|
|
|
182
114
|
await emit('ORDER_UPDATE_CART_ITEM', {
|
|
183
115
|
orderPosition: updatedOrderPosition,
|
|
@@ -186,9 +118,7 @@ export const configureOrderPositionsModule = ({
|
|
|
186
118
|
return updatedOrderPosition;
|
|
187
119
|
},
|
|
188
120
|
|
|
189
|
-
removeProductByIdFromAllOpenPositions: async (productId
|
|
190
|
-
log('Remove Position Product', { productId });
|
|
191
|
-
|
|
121
|
+
removeProductByIdFromAllOpenPositions: async (productId) => {
|
|
192
122
|
const positions = await OrderPositions.aggregate([
|
|
193
123
|
{
|
|
194
124
|
$match: {
|
|
@@ -211,15 +141,7 @@ export const configureOrderPositionsModule = ({
|
|
|
211
141
|
]).toArray();
|
|
212
142
|
|
|
213
143
|
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
|
-
|
|
144
|
+
await OrderPositions.deleteMany({ _id: { $in: positionIds } });
|
|
223
145
|
await Promise.all(
|
|
224
146
|
positions.map(async (orderPosition) => {
|
|
225
147
|
await emit('ORDER_REMOVE_CART_ITEM', {
|
|
@@ -228,7 +150,8 @@ export const configureOrderPositionsModule = ({
|
|
|
228
150
|
}),
|
|
229
151
|
);
|
|
230
152
|
|
|
231
|
-
|
|
153
|
+
const orderIdsToRecalculate = positions.map((o) => o.orderId);
|
|
154
|
+
return orderIdsToRecalculate;
|
|
232
155
|
},
|
|
233
156
|
|
|
234
157
|
updateScheduling: async ({ order, orderDelivery, orderPosition }, unchainedAPI) => {
|
|
@@ -291,10 +214,6 @@ export const configureOrderPositionsModule = ({
|
|
|
291
214
|
},
|
|
292
215
|
|
|
293
216
|
updateCalculation: async (orderPosition, unchainedAPI) => {
|
|
294
|
-
log(`OrderPosition ${orderPosition._id} -> Update Calculation`, {
|
|
295
|
-
orderId: orderPosition.orderId,
|
|
296
|
-
});
|
|
297
|
-
|
|
298
217
|
const calculation = await unchainedAPI.modules.products.calculate(
|
|
299
218
|
{ item: orderPosition, configuration: orderPosition.configuration },
|
|
300
219
|
unchainedAPI,
|
|
@@ -364,8 +283,6 @@ export const configureOrderPositionsModule = ({
|
|
|
364
283
|
{ upsert: true },
|
|
365
284
|
);
|
|
366
285
|
|
|
367
|
-
await updateCalculation(orderId, unchainedAPI);
|
|
368
|
-
|
|
369
286
|
const upsertedOrderPosition = await OrderPositions.findOne(selector);
|
|
370
287
|
|
|
371
288
|
await emit('ORDER_ADD_PRODUCT', { orderPosition: upsertedOrderPosition });
|
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import { ModuleMutations } from '@unchainedshop/types/core.js';
|
|
2
|
-
import { Order, OrderStatus, OrderMutations
|
|
2
|
+
import { Order, OrderStatus, OrderMutations } from '@unchainedshop/types/orders.js';
|
|
3
3
|
import { OrderDelivery } from '@unchainedshop/types/orders.deliveries.js';
|
|
4
4
|
import { OrderPayment } from '@unchainedshop/types/orders.payments.js';
|
|
5
5
|
import { emit, registerEvents } from '@unchainedshop/events';
|
|
6
|
-
import { log, LogLevel } from '@unchainedshop/logger';
|
|
7
6
|
import { generateDbFilterById, generateDbMutations, mongodb } from '@unchainedshop/mongodb';
|
|
8
7
|
import { OrderPosition } from '@unchainedshop/types/orders.positions.js';
|
|
9
8
|
import { OrdersSchema } from '../db/OrdersSchema.js';
|
|
@@ -21,15 +20,11 @@ export const configureOrderModuleMutations = ({
|
|
|
21
20
|
OrderDeliveries,
|
|
22
21
|
OrderPayments,
|
|
23
22
|
OrderPositions,
|
|
24
|
-
initProviders,
|
|
25
|
-
updateCalculation,
|
|
26
23
|
}: {
|
|
27
24
|
Orders: mongodb.Collection<Order>;
|
|
28
25
|
OrderDeliveries: mongodb.Collection<OrderDelivery>;
|
|
29
26
|
OrderPayments: mongodb.Collection<OrderPayment>;
|
|
30
27
|
OrderPositions: mongodb.Collection<OrderPosition>;
|
|
31
|
-
initProviders: OrdersModule['initProviders'];
|
|
32
|
-
updateCalculation: OrdersModule['updateCalculation'];
|
|
33
28
|
}): OrderMutations => {
|
|
34
29
|
registerEvents(ORDER_EVENTS);
|
|
35
30
|
|
|
@@ -65,25 +60,6 @@ export const configureOrderModuleMutations = ({
|
|
|
65
60
|
return deletedCount;
|
|
66
61
|
},
|
|
67
62
|
|
|
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
63
|
setCartOwner: async ({ orderId, userId }) => {
|
|
88
64
|
await Orders.updateOne(generateDbFilterById(orderId), {
|
|
89
65
|
$set: {
|
|
@@ -103,8 +79,7 @@ export const configureOrderModuleMutations = ({
|
|
|
103
79
|
);
|
|
104
80
|
},
|
|
105
81
|
|
|
106
|
-
setDeliveryProvider: async (orderId, deliveryProviderId,
|
|
107
|
-
const { modules } = unchainedAPI;
|
|
82
|
+
setDeliveryProvider: async (orderId, deliveryProviderId, { modules }) => {
|
|
108
83
|
const delivery = await OrderDeliveries.findOne({
|
|
109
84
|
orderId,
|
|
110
85
|
deliveryProviderId,
|
|
@@ -121,17 +96,17 @@ export const configureOrderModuleMutations = ({
|
|
|
121
96
|
})
|
|
122
97
|
)._id;
|
|
123
98
|
|
|
124
|
-
log(`Set Delivery Provider ${deliveryProviderId}`, { orderId });
|
|
125
|
-
|
|
126
99
|
const selector = generateDbFilterById(orderId);
|
|
127
|
-
await Orders.
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
100
|
+
const order = await Orders.findOneAndUpdate(
|
|
101
|
+
selector,
|
|
102
|
+
{
|
|
103
|
+
$set: {
|
|
104
|
+
deliveryId,
|
|
105
|
+
updated: new Date(),
|
|
106
|
+
},
|
|
131
107
|
},
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
const order = await updateCalculation(orderId, unchainedAPI);
|
|
108
|
+
{ returnDocument: 'after' },
|
|
109
|
+
);
|
|
135
110
|
|
|
136
111
|
await emit('ORDER_SET_DELIVERY_PROVIDER', {
|
|
137
112
|
order,
|
|
@@ -159,14 +134,14 @@ export const configureOrderModuleMutations = ({
|
|
|
159
134
|
status: null,
|
|
160
135
|
})
|
|
161
136
|
)._id;
|
|
162
|
-
log(`Set Payment Provider ${paymentProviderId}`, { orderId });
|
|
163
|
-
|
|
164
137
|
const selector = generateDbFilterById(orderId);
|
|
165
|
-
await Orders.
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
138
|
+
const order = await Orders.findOneAndUpdate(
|
|
139
|
+
selector,
|
|
140
|
+
{
|
|
141
|
+
$set: { paymentId, updated: new Date() },
|
|
142
|
+
},
|
|
143
|
+
{ returnDocument: 'after' },
|
|
144
|
+
);
|
|
170
145
|
|
|
171
146
|
await emit('ORDER_SET_PAYMENT_PROVIDER', {
|
|
172
147
|
order,
|
|
@@ -176,45 +151,46 @@ export const configureOrderModuleMutations = ({
|
|
|
176
151
|
return order;
|
|
177
152
|
},
|
|
178
153
|
|
|
179
|
-
updateBillingAddress: async (orderId, billingAddress
|
|
180
|
-
log('Update Invoicing Address', { orderId });
|
|
181
|
-
|
|
154
|
+
updateBillingAddress: async (orderId, billingAddress) => {
|
|
182
155
|
const selector = generateDbFilterById(orderId);
|
|
183
|
-
await Orders.
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
156
|
+
const order = await Orders.findOneAndUpdate(
|
|
157
|
+
selector,
|
|
158
|
+
{
|
|
159
|
+
$set: {
|
|
160
|
+
billingAddress,
|
|
161
|
+
updated: new Date(),
|
|
162
|
+
},
|
|
187
163
|
},
|
|
188
|
-
|
|
164
|
+
{ returnDocument: 'after' },
|
|
165
|
+
);
|
|
189
166
|
|
|
190
|
-
const order = await updateCalculation(orderId, unchainedAPI);
|
|
191
167
|
await emit('ORDER_UPDATE', { order, field: 'billing' });
|
|
192
168
|
return order;
|
|
193
169
|
},
|
|
194
170
|
|
|
195
|
-
updateContact: async (orderId, contact
|
|
196
|
-
log('Update Contact', { orderId });
|
|
197
|
-
|
|
171
|
+
updateContact: async (orderId, contact) => {
|
|
198
172
|
const selector = generateDbFilterById(orderId);
|
|
199
|
-
await Orders.
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
173
|
+
const order = await Orders.findOneAndUpdate(
|
|
174
|
+
selector,
|
|
175
|
+
{
|
|
176
|
+
$set: {
|
|
177
|
+
contact,
|
|
178
|
+
updated: new Date(),
|
|
179
|
+
},
|
|
203
180
|
},
|
|
204
|
-
|
|
181
|
+
{ returnDocument: 'after' },
|
|
182
|
+
);
|
|
205
183
|
|
|
206
|
-
const order = await updateCalculation(orderId, unchainedAPI);
|
|
207
184
|
await emit('ORDER_UPDATE', { order, field: 'contact' });
|
|
208
185
|
return order;
|
|
209
186
|
},
|
|
210
187
|
|
|
211
|
-
updateContext: async (orderId, context
|
|
188
|
+
updateContext: async (orderId, context) => {
|
|
212
189
|
const selector = generateDbFilterById<Order>(orderId);
|
|
213
190
|
selector.status = { $in: [null, OrderStatus.PENDING] };
|
|
214
191
|
|
|
215
192
|
if (!context || Object.keys(context).length === 0) return Orders.findOne(selector, {});
|
|
216
193
|
|
|
217
|
-
log('Update Arbitrary Context', { orderId, context });
|
|
218
194
|
const contextSetters = Object.fromEntries(
|
|
219
195
|
Object.entries(context).map(([key, value]) => [`context.${key}`, value]),
|
|
220
196
|
);
|
|
@@ -226,17 +202,14 @@ export const configureOrderModuleMutations = ({
|
|
|
226
202
|
updated: new Date(),
|
|
227
203
|
},
|
|
228
204
|
},
|
|
229
|
-
{ includeResultMetadata: true },
|
|
205
|
+
{ includeResultMetadata: true, returnDocument: 'after' },
|
|
230
206
|
);
|
|
231
207
|
|
|
232
208
|
if (result.ok) {
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
return calculatedOrder;
|
|
209
|
+
await emit('ORDER_UPDATE', { order: result.value, field: 'context' });
|
|
210
|
+
return result.value;
|
|
236
211
|
}
|
|
237
212
|
return null;
|
|
238
213
|
},
|
|
239
|
-
|
|
240
|
-
updateCalculation,
|
|
241
214
|
};
|
|
242
215
|
};
|
|
@@ -5,7 +5,6 @@ import { OrderDelivery } from '@unchainedshop/types/orders.deliveries.js';
|
|
|
5
5
|
import { OrderPayment } from '@unchainedshop/types/orders.payments.js';
|
|
6
6
|
import { OrderPosition } from '@unchainedshop/types/orders.positions.js';
|
|
7
7
|
import { emit, registerEvents } from '@unchainedshop/events';
|
|
8
|
-
import { log } from '@unchainedshop/logger';
|
|
9
8
|
import { ProductType } from '@unchainedshop/types/products.js';
|
|
10
9
|
import { UnchainedCore } from '@unchainedshop/types/core.js';
|
|
11
10
|
import { ordersSettings } from '../orders-settings.js';
|
|
@@ -94,7 +93,6 @@ export const configureOrderModuleProcessing = ({
|
|
|
94
93
|
);
|
|
95
94
|
|
|
96
95
|
if (modificationResult.ok) {
|
|
97
|
-
log(`New Status: ${status}`, { orderId });
|
|
98
96
|
if (order.status === null) {
|
|
99
97
|
// The first time that an order transitions away from cart is a checkout event
|
|
100
98
|
await emit('ORDER_CHECKOUT', { order: modificationResult.value, oldStatus: order.status });
|
|
@@ -149,11 +147,6 @@ export const configureOrderModuleProcessing = ({
|
|
|
149
147
|
const validationErrors = await Promise.all(
|
|
150
148
|
orderPositions.map(async (orderPosition) => {
|
|
151
149
|
const errors = [];
|
|
152
|
-
|
|
153
|
-
log(`OrderPosition ${orderPosition._id} -> Validate ${orderPosition.quantity}`, {
|
|
154
|
-
orderId: orderPosition.orderId,
|
|
155
|
-
});
|
|
156
|
-
|
|
157
150
|
const product = await unchainedAPI.modules.products.findProduct({
|
|
158
151
|
productId: orderPosition.productId,
|
|
159
152
|
});
|
|
@@ -475,11 +468,6 @@ export const configureOrderModuleProcessing = ({
|
|
|
475
468
|
);
|
|
476
469
|
}),
|
|
477
470
|
);
|
|
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
471
|
}
|
|
484
472
|
|
|
485
473
|
nextStatus = await findNextStatus(nextStatus, order, unchainedAPI);
|
|
@@ -95,7 +95,7 @@ export const configureOrdersModule = async ({
|
|
|
95
95
|
await Promise.all(
|
|
96
96
|
systemDiscounts
|
|
97
97
|
.filter((key) => currentDiscountKeys.indexOf(key) === -1)
|
|
98
|
-
.map((discountKey) =>
|
|
98
|
+
.map(async (discountKey) =>
|
|
99
99
|
modules.orders.discounts.create({
|
|
100
100
|
orderId: order._id,
|
|
101
101
|
discountKey,
|
|
@@ -256,6 +256,23 @@ export const configureOrdersModule = async ({
|
|
|
256
256
|
);
|
|
257
257
|
};
|
|
258
258
|
|
|
259
|
+
const invalidateProviders = async (unchainedAPI, maxAgeDays = 30) => {
|
|
260
|
+
const ONE_DAY_IN_MILLISECONDS = 86400000;
|
|
261
|
+
|
|
262
|
+
const minValidDate = new Date(new Date().getTime() - maxAgeDays * ONE_DAY_IN_MILLISECONDS);
|
|
263
|
+
|
|
264
|
+
const orders = await Orders.find({
|
|
265
|
+
status: { $eq: null },
|
|
266
|
+
updated: { $gte: minValidDate },
|
|
267
|
+
}).toArray();
|
|
268
|
+
|
|
269
|
+
await Promise.allSettled(
|
|
270
|
+
orders.map(async (order) => {
|
|
271
|
+
await updateCalculation(order._id, unchainedAPI);
|
|
272
|
+
}),
|
|
273
|
+
);
|
|
274
|
+
};
|
|
275
|
+
|
|
259
276
|
const orderQueries = configureOrdersModuleQueries({ Orders });
|
|
260
277
|
const orderTransformations = configureOrderModuleTransformations({
|
|
261
278
|
Orders,
|
|
@@ -272,28 +289,22 @@ export const configureOrdersModule = async ({
|
|
|
272
289
|
OrderDeliveries,
|
|
273
290
|
OrderPayments,
|
|
274
291
|
OrderPositions,
|
|
275
|
-
initProviders,
|
|
276
|
-
updateCalculation,
|
|
277
292
|
});
|
|
278
293
|
|
|
279
294
|
const orderDiscountsModule = configureOrderDiscountsModule({
|
|
280
295
|
OrderDiscounts,
|
|
281
|
-
updateCalculation,
|
|
282
296
|
});
|
|
283
297
|
|
|
284
298
|
const orderPositionsModule = configureOrderPositionsModule({
|
|
285
299
|
OrderPositions,
|
|
286
|
-
updateCalculation,
|
|
287
300
|
});
|
|
288
301
|
|
|
289
302
|
const orderPaymentsModule = configureOrderPaymentsModule({
|
|
290
303
|
OrderPayments,
|
|
291
|
-
updateCalculation,
|
|
292
304
|
});
|
|
293
305
|
|
|
294
306
|
const orderDeliveriesModule = configureOrderDeliveriesModule({
|
|
295
307
|
OrderDeliveries,
|
|
296
|
-
updateCalculation,
|
|
297
308
|
});
|
|
298
309
|
|
|
299
310
|
return {
|
|
@@ -302,6 +313,10 @@ export const configureOrdersModule = async ({
|
|
|
302
313
|
...orderProcessing,
|
|
303
314
|
...orderMutations,
|
|
304
315
|
|
|
316
|
+
initProviders,
|
|
317
|
+
updateCalculation,
|
|
318
|
+
invalidateProviders,
|
|
319
|
+
|
|
305
320
|
// Subentities
|
|
306
321
|
deliveries: orderDeliveriesModule,
|
|
307
322
|
discounts: orderDiscountsModule,
|
|
@@ -29,16 +29,12 @@ export const migrateOrderCartsService: MigrateOrderCartsService = async (
|
|
|
29
29
|
|
|
30
30
|
// Move billing address if target order has none
|
|
31
31
|
if (fromCart.billingAddress && !toCart.billingAddress) {
|
|
32
|
-
await unchainedAPI.modules.orders.updateBillingAddress(
|
|
33
|
-
toCart._id,
|
|
34
|
-
fromCart.billingAddress,
|
|
35
|
-
unchainedAPI,
|
|
36
|
-
);
|
|
32
|
+
await unchainedAPI.modules.orders.updateBillingAddress(toCart._id, fromCart.billingAddress);
|
|
37
33
|
}
|
|
38
34
|
|
|
39
35
|
// Move contact data if target order has none
|
|
40
36
|
if (fromCart.contact && !toCart.contact) {
|
|
41
|
-
await unchainedAPI.modules.orders.updateContact(toCart._id, fromCart.contact
|
|
37
|
+
await unchainedAPI.modules.orders.updateContact(toCart._id, fromCart.contact);
|
|
42
38
|
}
|
|
43
39
|
|
|
44
40
|
await unchainedAPI.modules.orders.updateCalculation(fromCart._id, unchainedAPI);
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { NextUserCartService } from '@unchainedshop/types/orders.js';
|
|
2
2
|
import { ordersSettings } from '../orders-settings.js';
|
|
3
|
+
import { resolveBestCurrency } from '@unchainedshop/utils';
|
|
3
4
|
|
|
4
5
|
export const nextUserCartService: NextUserCartService = async (
|
|
5
6
|
{ user, orderNumber, countryCode, forceCartCreation },
|
|
6
7
|
unchainedAPI,
|
|
7
8
|
) => {
|
|
8
|
-
const { modules
|
|
9
|
+
const { modules } = unchainedAPI;
|
|
9
10
|
|
|
10
11
|
const cart = await modules.orders.cart({
|
|
11
12
|
countryContext: countryCode || user.lastLogin?.countryCode,
|
|
@@ -17,12 +18,9 @@ export const nextUserCartService: NextUserCartService = async (
|
|
|
17
18
|
const shouldCreateNewCart = forceCartCreation || ordersSettings.ensureUserHasCart;
|
|
18
19
|
if (!shouldCreateNewCart) return null;
|
|
19
20
|
|
|
20
|
-
const
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
},
|
|
24
|
-
unchainedAPI,
|
|
25
|
-
);
|
|
21
|
+
const countryObject = await modules.countries.findCountry({ isoCode: countryCode });
|
|
22
|
+
const currencies = await modules.currencies.findCurrencies({ includeInactive: false });
|
|
23
|
+
const currency = resolveBestCurrency(countryObject.defaultCurrencyCode, currencies);
|
|
26
24
|
|
|
27
25
|
const order = await modules.orders.create({
|
|
28
26
|
userId: user._id,
|