@unchainedshop/core-orders 2.13.0 → 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 +15 -19
- 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/package.json +5 -5
- 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 +15 -27
- package/src/module/configureOrdersModule.ts +22 -7
- package/src/service/migrateOrderCartService.ts +2 -6
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import { ModuleMutations, UnchainedCore } from '@unchainedshop/types/core.js';
|
|
2
|
-
import { OrdersModule } from '@unchainedshop/types/orders.js';
|
|
3
2
|
import { OrderDiscount, OrderDiscountsModule } from '@unchainedshop/types/orders.discounts.js';
|
|
4
3
|
import { emit, registerEvents } from '@unchainedshop/events';
|
|
5
|
-
import { log } from '@unchainedshop/logger';
|
|
6
4
|
import { generateDbFilterById, generateDbMutations, mongodb } from '@unchainedshop/mongodb';
|
|
7
5
|
import { OrderDiscountsSchema } from '../db/OrderDiscountsSchema.js';
|
|
8
6
|
import { OrderDiscountTrigger } from '../db/OrderDiscountTrigger.js';
|
|
@@ -25,10 +23,8 @@ export const buildFindByIdSelector = (orderDiscountId: string) =>
|
|
|
25
23
|
|
|
26
24
|
export const configureOrderDiscountsModule = ({
|
|
27
25
|
OrderDiscounts,
|
|
28
|
-
updateCalculation,
|
|
29
26
|
}: {
|
|
30
27
|
OrderDiscounts: mongodb.Collection<OrderDiscount>;
|
|
31
|
-
updateCalculation: OrdersModule['updateCalculation'];
|
|
32
28
|
}): OrderDiscountsModule => {
|
|
33
29
|
registerEvents(ORDER_DISCOUNT_EVENTS);
|
|
34
30
|
|
|
@@ -51,11 +47,6 @@ export const configureOrderDiscountsModule = ({
|
|
|
51
47
|
|
|
52
48
|
const createDiscount: OrderDiscountsModule['create'] = async (doc) => {
|
|
53
49
|
const normalizedTrigger = doc.trigger || OrderDiscountTrigger.USER;
|
|
54
|
-
|
|
55
|
-
log(`Create Order Discount: ${doc.discountKey} with trigger ${normalizedTrigger}`, {
|
|
56
|
-
orderId: doc.orderId,
|
|
57
|
-
});
|
|
58
|
-
|
|
59
50
|
const discountId = await mutations.create({ ...doc, trigger: normalizedTrigger });
|
|
60
51
|
const discount = await OrderDiscounts.findOne(buildFindByIdSelector(discountId));
|
|
61
52
|
return discount;
|
|
@@ -64,22 +55,13 @@ export const configureOrderDiscountsModule = ({
|
|
|
64
55
|
const deleteDiscount: OrderDiscountsModule['delete'] = async (orderDiscountId, unchainedAPI) => {
|
|
65
56
|
const selector = buildFindByIdSelector(orderDiscountId);
|
|
66
57
|
const discount = await OrderDiscounts.findOne(selector, {});
|
|
67
|
-
|
|
68
|
-
log(`OrderDiscounts -> Remove Discount ${orderDiscountId}`, {
|
|
69
|
-
orderId: discount.orderId,
|
|
70
|
-
});
|
|
71
|
-
|
|
72
58
|
if (discount.trigger === OrderDiscountTrigger.USER) {
|
|
73
59
|
// Release
|
|
74
60
|
const adapter = await getAdapter(discount, unchainedAPI);
|
|
75
61
|
if (!adapter) return null;
|
|
76
62
|
await adapter.release();
|
|
77
|
-
|
|
78
|
-
await OrderDiscounts.deleteOne(selector);
|
|
79
|
-
await updateCalculation(discount.orderId, unchainedAPI);
|
|
80
|
-
} else {
|
|
81
|
-
await OrderDiscounts.deleteOne(selector);
|
|
82
63
|
}
|
|
64
|
+
await OrderDiscounts.deleteOne(selector);
|
|
83
65
|
await emit('ORDER_REMOVE_DISCOUNT', { discount });
|
|
84
66
|
return discount;
|
|
85
67
|
};
|
|
@@ -110,15 +92,10 @@ export const configureOrderDiscountsModule = ({
|
|
|
110
92
|
{ code, orderId }: { code: string; orderId: string },
|
|
111
93
|
unchainedAPI: UnchainedCore,
|
|
112
94
|
) => {
|
|
113
|
-
log(`OrderDiscounts -> Try to grab ${code}`, { orderId });
|
|
114
|
-
|
|
115
95
|
const existingDiscount = await OrderDiscounts.findOne({ code, orderId });
|
|
116
|
-
|
|
117
96
|
if (existingDiscount) throw new Error(OrderDiscountErrorCode.CODE_ALREADY_PRESENT);
|
|
118
|
-
|
|
119
97
|
const discount = await OrderDiscounts.findOne({ code, orderId: null });
|
|
120
98
|
if (!discount) return null;
|
|
121
|
-
|
|
122
99
|
const discountId = discount._id;
|
|
123
100
|
try {
|
|
124
101
|
const updatedDiscount = await updateDiscount(discountId, { orderId });
|
|
@@ -199,7 +176,6 @@ export const configureOrderDiscountsModule = ({
|
|
|
199
176
|
|
|
200
177
|
try {
|
|
201
178
|
const reservedDiscount = await reserveDiscount(newDiscount, unchainedAPI);
|
|
202
|
-
await updateCalculation(order._id, unchainedAPI);
|
|
203
179
|
await emit('ORDER_ADD_DISCOUNT', { discount: reserveDiscount });
|
|
204
180
|
return reservedDiscount;
|
|
205
181
|
} catch (error) {
|
|
@@ -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
|
});
|
|
@@ -256,7 +249,7 @@ export const configureOrderModuleProcessing = ({
|
|
|
256
249
|
};
|
|
257
250
|
|
|
258
251
|
return {
|
|
259
|
-
checkout: async (orderId,
|
|
252
|
+
checkout: async (orderId, { paymentContext, deliveryContext }, unchainedAPI) => {
|
|
260
253
|
const { modules, services } = unchainedAPI;
|
|
261
254
|
|
|
262
255
|
const order = await Orders.findOne(generateDbFilterById(orderId), {});
|
|
@@ -275,7 +268,10 @@ export const configureOrderModuleProcessing = ({
|
|
|
275
268
|
try {
|
|
276
269
|
const processedOrder = await modules.orders.processOrder(
|
|
277
270
|
order,
|
|
278
|
-
|
|
271
|
+
{
|
|
272
|
+
paymentContext,
|
|
273
|
+
deliveryContext,
|
|
274
|
+
},
|
|
279
275
|
unchainedAPI,
|
|
280
276
|
);
|
|
281
277
|
|
|
@@ -303,7 +299,7 @@ export const configureOrderModuleProcessing = ({
|
|
|
303
299
|
}
|
|
304
300
|
},
|
|
305
301
|
|
|
306
|
-
confirm: async (order,
|
|
302
|
+
confirm: async (order, { paymentContext, deliveryContext }, unchainedAPI) => {
|
|
307
303
|
const { modules } = unchainedAPI;
|
|
308
304
|
|
|
309
305
|
if (order.status !== OrderStatus.PENDING) return order;
|
|
@@ -313,7 +309,8 @@ export const configureOrderModuleProcessing = ({
|
|
|
313
309
|
return await modules.orders.processOrder(
|
|
314
310
|
order,
|
|
315
311
|
{
|
|
316
|
-
|
|
312
|
+
paymentContext,
|
|
313
|
+
deliveryContext,
|
|
317
314
|
nextStatus: OrderStatus.CONFIRMED,
|
|
318
315
|
},
|
|
319
316
|
unchainedAPI,
|
|
@@ -323,7 +320,7 @@ export const configureOrderModuleProcessing = ({
|
|
|
323
320
|
}
|
|
324
321
|
},
|
|
325
322
|
|
|
326
|
-
reject: async (order,
|
|
323
|
+
reject: async (order, { paymentContext, deliveryContext }, unchainedAPI) => {
|
|
327
324
|
const { modules } = unchainedAPI;
|
|
328
325
|
|
|
329
326
|
if (order.status !== OrderStatus.PENDING) return order;
|
|
@@ -333,7 +330,8 @@ export const configureOrderModuleProcessing = ({
|
|
|
333
330
|
return await modules.orders.processOrder(
|
|
334
331
|
order,
|
|
335
332
|
{
|
|
336
|
-
|
|
333
|
+
paymentContext,
|
|
334
|
+
deliveryContext,
|
|
337
335
|
nextStatus: OrderStatus.REJECTED,
|
|
338
336
|
},
|
|
339
337
|
unchainedAPI,
|
|
@@ -343,14 +341,9 @@ export const configureOrderModuleProcessing = ({
|
|
|
343
341
|
}
|
|
344
342
|
},
|
|
345
343
|
|
|
346
|
-
processOrder: async (initialOrder,
|
|
344
|
+
processOrder: async (initialOrder, params, unchainedAPI) => {
|
|
347
345
|
const { modules } = unchainedAPI;
|
|
348
|
-
const {
|
|
349
|
-
paymentContext,
|
|
350
|
-
deliveryContext,
|
|
351
|
-
nextStatus: forceNextStatus,
|
|
352
|
-
comment,
|
|
353
|
-
} = orderTransactionContext;
|
|
346
|
+
const { paymentContext, deliveryContext, nextStatus: forceNextStatus } = params;
|
|
354
347
|
|
|
355
348
|
const orderId = initialOrder._id;
|
|
356
349
|
let order = initialOrder;
|
|
@@ -400,7 +393,7 @@ export const configureOrderModuleProcessing = ({
|
|
|
400
393
|
// numbers that are needed for delivery
|
|
401
394
|
order = await updateStatus(orderId, {
|
|
402
395
|
status: OrderStatus.CONFIRMED,
|
|
403
|
-
info:
|
|
396
|
+
info: 'before delivery',
|
|
404
397
|
});
|
|
405
398
|
|
|
406
399
|
const orderDelivery = await modules.orders.deliveries.findDelivery({
|
|
@@ -475,17 +468,12 @@ 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);
|
|
486
474
|
}
|
|
487
475
|
|
|
488
|
-
return updateStatus(order._id, { status: nextStatus, info:
|
|
476
|
+
return updateStatus(order._id, { status: nextStatus, info: 'order processed' });
|
|
489
477
|
},
|
|
490
478
|
|
|
491
479
|
updateStatus,
|