@unchainedshop/core-orders 4.0.0-rc.9 → 4.0.1
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 +2 -2
- package/lib/db/OrderDeliveriesCollection.d.ts.map +1 -1
- package/lib/db/OrderDeliveriesSchema.d.ts +3 -0
- package/lib/db/OrderDeliveriesSchema.d.ts.map +1 -0
- package/lib/db/OrderDeliveriesSchema.js +20 -0
- package/lib/db/OrderDeliveriesSchema.js.map +1 -0
- package/lib/db/OrderDiscountTrigger.d.ts +5 -0
- package/lib/db/OrderDiscountTrigger.d.ts.map +1 -0
- package/lib/db/OrderDiscountTrigger.js +6 -0
- package/lib/db/OrderDiscountTrigger.js.map +1 -0
- package/lib/db/OrderDiscountsCollection.d.ts +2 -2
- package/lib/db/OrderDiscountsCollection.d.ts.map +1 -1
- package/lib/db/OrderDiscountsSchema.d.ts +3 -0
- package/lib/db/OrderDiscountsSchema.d.ts.map +1 -0
- package/lib/db/OrderDiscountsSchema.js +12 -0
- package/lib/db/OrderDiscountsSchema.js.map +1 -0
- package/lib/db/OrderPaymentsCollection.d.ts +3 -3
- package/lib/db/OrderPaymentsCollection.d.ts.map +1 -1
- package/lib/db/OrderPaymentsSchema.d.ts +3 -0
- package/lib/db/OrderPaymentsSchema.d.ts.map +1 -0
- package/lib/db/OrderPaymentsSchema.js +20 -0
- package/lib/db/OrderPaymentsSchema.js.map +1 -0
- package/lib/db/OrderPositionsCollection.d.ts +2 -3
- package/lib/db/OrderPositionsCollection.d.ts.map +1 -1
- package/lib/db/OrderPositionsCollection.js.map +1 -1
- package/lib/db/OrderPositionsSchema.d.ts +3 -0
- package/lib/db/OrderPositionsSchema.d.ts.map +1 -0
- package/lib/db/OrderPositionsSchema.js +37 -0
- package/lib/db/OrderPositionsSchema.js.map +1 -0
- package/lib/db/OrdersCollection.d.ts +5 -1
- package/lib/db/OrdersCollection.d.ts.map +1 -1
- package/lib/db/OrdersCollection.js +25 -21
- package/lib/db/OrdersCollection.js.map +1 -1
- package/lib/db/OrdersSchema.d.ts +3 -0
- package/lib/db/OrdersSchema.d.ts.map +1 -0
- package/lib/db/OrdersSchema.js +30 -0
- package/lib/db/OrdersSchema.js.map +1 -0
- package/lib/director/OrderDiscountAdapter.d.ts +4 -0
- package/lib/director/OrderDiscountAdapter.d.ts.map +1 -0
- package/lib/director/OrderDiscountAdapter.js +3 -0
- package/lib/director/OrderDiscountAdapter.js.map +1 -0
- package/lib/director/OrderDiscountConfiguration.d.ts +6 -0
- package/lib/director/OrderDiscountConfiguration.d.ts.map +1 -0
- package/lib/director/OrderDiscountConfiguration.js +2 -0
- package/lib/director/OrderDiscountConfiguration.js.map +1 -0
- package/lib/director/OrderDiscountDirector.d.ts +3 -0
- package/lib/director/OrderDiscountDirector.d.ts.map +1 -0
- package/lib/director/OrderDiscountDirector.js +3 -0
- package/lib/director/OrderDiscountDirector.js.map +1 -0
- package/lib/director/OrderPricingAdapter.d.ts +4 -0
- package/lib/director/OrderPricingAdapter.d.ts.map +1 -0
- package/lib/director/OrderPricingAdapter.js +25 -0
- package/lib/director/OrderPricingAdapter.js.map +1 -0
- package/lib/director/OrderPricingDirector.d.ts +3 -0
- package/lib/director/OrderPricingDirector.d.ts.map +1 -0
- package/lib/director/OrderPricingDirector.js +39 -0
- package/lib/director/OrderPricingDirector.js.map +1 -0
- package/lib/director/OrderPricingSheet.d.ts +4 -0
- package/lib/director/OrderPricingSheet.d.ts.map +1 -0
- package/lib/director/OrderPricingSheet.js +101 -0
- package/lib/director/OrderPricingSheet.js.map +1 -0
- package/lib/migrations/20250502111800-currency-code copy.d.ts +3 -0
- package/lib/migrations/20250502111800-currency-code copy.d.ts.map +1 -0
- package/lib/migrations/20250502111800-currency-code copy.js +14 -0
- package/lib/migrations/20250502111800-currency-code copy.js.map +1 -0
- package/lib/migrations/20251020080000-position-config.d.ts +3 -0
- package/lib/migrations/20251020080000-position-config.d.ts.map +1 -0
- package/lib/migrations/20251020080000-position-config.js +14 -0
- package/lib/migrations/20251020080000-position-config.js.map +1 -0
- package/lib/module/buildFindSelector.d.ts +1 -1
- package/lib/module/buildFindSelector.d.ts.map +1 -1
- package/lib/module/buildFindSelector.js +17 -1
- package/lib/module/buildFindSelector.js.map +1 -1
- package/lib/module/configureOrderDeliveriesModule.d.ts +9 -6
- package/lib/module/configureOrderDeliveriesModule.d.ts.map +1 -1
- package/lib/module/configureOrderDeliveriesModule.js +14 -10
- package/lib/module/configureOrderDeliveriesModule.js.map +1 -1
- package/lib/module/configureOrderDiscountsModule.d.ts +5 -5
- package/lib/module/configureOrderDiscountsModule.d.ts.map +1 -1
- package/lib/module/configureOrderDiscountsModule.js +3 -3
- package/lib/module/configureOrderDiscountsModule.js.map +1 -1
- package/lib/module/configureOrderPaymentsModule.d.ts +10 -7
- package/lib/module/configureOrderPaymentsModule.d.ts.map +1 -1
- package/lib/module/configureOrderPaymentsModule.js +18 -10
- package/lib/module/configureOrderPaymentsModule.js.map +1 -1
- package/lib/module/configureOrderPositionsModule.d.ts +17 -6
- package/lib/module/configureOrderPositionsModule.d.ts.map +1 -1
- package/lib/module/configureOrderPositionsModule.js +25 -5
- package/lib/module/configureOrderPositionsModule.js.map +1 -1
- package/lib/module/configureOrdersModule-mutations.d.ts +14 -13
- package/lib/module/configureOrdersModule-mutations.d.ts.map +1 -1
- package/lib/module/configureOrdersModule-mutations.js +19 -11
- package/lib/module/configureOrdersModule-mutations.js.map +1 -1
- package/lib/module/configureOrdersModule-processing.d.ts +14 -0
- package/lib/module/configureOrdersModule-processing.d.ts.map +1 -0
- package/lib/module/configureOrdersModule-processing.js +348 -0
- package/lib/module/configureOrdersModule-processing.js.map +1 -0
- package/lib/module/configureOrdersModule-queries.d.ts +21 -6
- package/lib/module/configureOrdersModule-queries.d.ts.map +1 -1
- package/lib/module/configureOrdersModule-queries.js +19 -84
- package/lib/module/configureOrdersModule-queries.js.map +1 -1
- package/lib/module/configureOrdersModule-transformations.d.ts +6 -0
- package/lib/module/configureOrdersModule-transformations.d.ts.map +1 -0
- package/lib/module/configureOrdersModule-transformations.js +112 -0
- package/lib/module/configureOrdersModule-transformations.js.map +1 -0
- package/lib/module/configureOrdersModule.d.ts +154 -21
- package/lib/module/configureOrdersModule.d.ts.map +1 -1
- package/lib/module/configureOrdersModule.js +9 -3
- package/lib/module/configureOrdersModule.js.map +1 -1
- package/lib/orders-settings.d.ts +9 -13
- package/lib/orders-settings.d.ts.map +1 -1
- package/lib/orders-settings.js +8 -8
- package/lib/orders-settings.js.map +1 -1
- package/lib/service/migrateOrderCartService.d.ts +3 -0
- package/lib/service/migrateOrderCartService.d.ts.map +1 -0
- package/lib/service/migrateOrderCartService.js +32 -0
- package/lib/service/migrateOrderCartService.js.map +1 -0
- package/lib/service/nextUserCartService.d.ts +3 -0
- package/lib/service/nextUserCartService.d.ts.map +1 -0
- package/lib/service/nextUserCartService.js +34 -0
- package/lib/service/nextUserCartService.js.map +1 -0
- package/lib/service/orderServices.d.ts +3 -0
- package/lib/service/orderServices.d.ts.map +1 -0
- package/lib/service/orderServices.js +7 -0
- package/lib/service/orderServices.js.map +1 -0
- package/package.json +4 -4
- package/src/db/OrderDeliveriesCollection.ts +2 -2
- package/src/db/OrderDiscountsCollection.ts +2 -2
- package/src/db/OrderPaymentsCollection.ts +3 -3
- package/src/db/OrderPositionsCollection.ts +2 -3
- package/src/db/OrdersCollection.ts +31 -22
- package/src/module/buildFindSelector.ts +19 -1
- package/src/module/configureOrderDeliveriesModule.ts +28 -16
- package/src/module/configureOrderDiscountsModule.ts +11 -11
- package/src/module/configureOrderPaymentsModule.ts +31 -20
- package/src/module/configureOrderPositionsModule.ts +48 -17
- package/src/module/configureOrdersModule-mutations.ts +46 -44
- package/src/module/configureOrdersModule-queries.ts +42 -91
- package/src/module/configureOrdersModule.ts +21 -33
- package/src/orders-settings.ts +21 -20
|
@@ -24,7 +24,7 @@ export const configureOrderPositionsModule = ({ OrderPositions, }) => {
|
|
|
24
24
|
await emit('ORDER_REMOVE_CART_ITEM', {
|
|
25
25
|
orderPosition,
|
|
26
26
|
});
|
|
27
|
-
return
|
|
27
|
+
return orderPosition;
|
|
28
28
|
},
|
|
29
29
|
removePositions: async ({ orderId }) => {
|
|
30
30
|
const result = await OrderPositions.deleteMany({ orderId });
|
|
@@ -48,6 +48,8 @@ export const configureOrderPositionsModule = ({ OrderPositions, }) => {
|
|
|
48
48
|
}, modifier, {
|
|
49
49
|
returnDocument: 'after',
|
|
50
50
|
});
|
|
51
|
+
if (!updatedOrderPosition)
|
|
52
|
+
return null;
|
|
51
53
|
await emit('ORDER_UPDATE_CART_ITEM', {
|
|
52
54
|
orderPosition: updatedOrderPosition,
|
|
53
55
|
});
|
|
@@ -105,10 +107,10 @@ export const configureOrderPositionsModule = ({ OrderPositions, }) => {
|
|
|
105
107
|
orderId,
|
|
106
108
|
productId,
|
|
107
109
|
originalProductId,
|
|
108
|
-
configuration: configuration
|
|
110
|
+
configuration: configuration ?? null,
|
|
109
111
|
...scope,
|
|
110
112
|
};
|
|
111
|
-
await OrderPositions.
|
|
113
|
+
const upsertedOrderPosition = (await OrderPositions.findOneAndUpdate(selector, {
|
|
112
114
|
$set: {
|
|
113
115
|
updated: new Date(),
|
|
114
116
|
},
|
|
@@ -124,8 +126,7 @@ export const configureOrderPositionsModule = ({ OrderPositions, }) => {
|
|
|
124
126
|
configuration,
|
|
125
127
|
...scope,
|
|
126
128
|
},
|
|
127
|
-
}, { upsert: true });
|
|
128
|
-
const upsertedOrderPosition = await OrderPositions.findOne(selector);
|
|
129
|
+
}, { upsert: true, returnDocument: 'after' }));
|
|
129
130
|
await emit('ORDER_ADD_PRODUCT', { orderPosition: upsertedOrderPosition });
|
|
130
131
|
return upsertedOrderPosition;
|
|
131
132
|
},
|
|
@@ -133,6 +134,25 @@ export const configureOrderPositionsModule = ({ OrderPositions, }) => {
|
|
|
133
134
|
const { deletedCount } = await OrderPositions.deleteMany({ orderId });
|
|
134
135
|
return deletedCount;
|
|
135
136
|
},
|
|
137
|
+
aggregatePositions: async ({ match, project, group, addFields, sort, limit, pipeline, }) => {
|
|
138
|
+
const stages = [];
|
|
139
|
+
if (pipeline?.length) {
|
|
140
|
+
return await OrderPositions.aggregate(pipeline).toArray();
|
|
141
|
+
}
|
|
142
|
+
if (match)
|
|
143
|
+
stages.push({ $match: match });
|
|
144
|
+
if (project)
|
|
145
|
+
stages.push({ $project: project });
|
|
146
|
+
if (group)
|
|
147
|
+
stages.push({ $group: group });
|
|
148
|
+
if (addFields)
|
|
149
|
+
stages.push({ $addFields: addFields });
|
|
150
|
+
if (sort)
|
|
151
|
+
stages.push({ $sort: sort });
|
|
152
|
+
if (typeof limit === 'number')
|
|
153
|
+
stages.push({ $limit: limit });
|
|
154
|
+
return OrderPositions.aggregate(stages).toArray();
|
|
155
|
+
},
|
|
136
156
|
};
|
|
137
157
|
};
|
|
138
158
|
//# sourceMappingURL=configureOrderPositionsModule.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"configureOrderPositionsModule.js","sourceRoot":"","sources":["../../src/module/configureOrderPositionsModule.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAC7D,OAAO,EAAE,oBAAoB,EAAE,kBAAkB,EAAW,MAAM,wBAAwB,CAAC;AAI3F,MAAM,qBAAqB,GAAa;IACtC,wBAAwB;IACxB,wBAAwB;IACxB,kBAAkB;IAClB,mBAAmB;CACpB,CAAC;
|
|
1
|
+
{"version":3,"file":"configureOrderPositionsModule.js","sourceRoot":"","sources":["../../src/module/configureOrderPositionsModule.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAC7D,OAAO,EAAE,oBAAoB,EAAE,kBAAkB,EAAW,MAAM,wBAAwB,CAAC;AAI3F,MAAM,qBAAqB,GAAa;IACtC,wBAAwB;IACxB,wBAAwB;IAC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D,CAAC;YAED,IAAI,KAAK;gBAAE,MAAM,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,CAAC;YAC1C,IAAI,OAAO;gBAAE,MAAM,CAAC,IAAI,CAAC,EAAE,QAAQ,EAAE,OAAO,EAAE,CAAC,CAAC;YAChD,IAAI,KAAK;gBAAE,MAAM,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,CAAC;YAC1C,IAAI,SAAS;gBAAE,MAAM,CAAC,IAAI,CAAC,EAAE,UAAU,EAAE,SAAS,EAAE,CAAC,CAAC;YACtD,IAAI,IAAI;gBAAE,MAAM,CAAC,IAAI,CAAC,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;YACvC,IAAI,OAAO,KAAK,KAAK,QAAQ;gBAAE,MAAM,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,CAAC;YAE9D,OAAO,cAAc,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,OAAO,EAAE,CAAC;QACpD,CAAC;KACF,CAAC;AACJ,CAAC,CAAC"}
|
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import { Address, Contact, mongodb } from '@unchainedshop/mongodb';
|
|
2
2
|
import { Order } from '../db/OrdersCollection.js';
|
|
3
3
|
import { OrderPosition } from '../db/OrderPositionsCollection.js';
|
|
4
|
-
export
|
|
5
|
-
|
|
4
|
+
export declare const configureOrderModuleMutations: ({ Orders, OrderPositions, }: {
|
|
5
|
+
Orders: mongodb.Collection<Order>;
|
|
6
|
+
OrderPositions: mongodb.Collection<OrderPosition>;
|
|
7
|
+
}) => {
|
|
8
|
+
create: ({ userId, orderNumber, currencyCode, countryCode, billingAddress, contact, context, }: {
|
|
6
9
|
userId: string;
|
|
7
10
|
billingAddress?: Address;
|
|
8
11
|
contact?: Contact;
|
|
@@ -10,23 +13,21 @@ export interface OrderMutations {
|
|
|
10
13
|
currencyCode: string;
|
|
11
14
|
orderNumber?: string;
|
|
12
15
|
originEnrollmentId?: string;
|
|
16
|
+
context?: Record<string, any>;
|
|
13
17
|
}) => Promise<Order>;
|
|
14
18
|
delete: (orderId: string) => Promise<number>;
|
|
15
|
-
setCartOwner: (
|
|
19
|
+
setCartOwner: ({ orderId, userId }: {
|
|
16
20
|
orderId: string;
|
|
17
21
|
userId: string;
|
|
18
22
|
}) => Promise<void>;
|
|
19
|
-
moveCartPositions: (
|
|
23
|
+
moveCartPositions: ({ fromOrderId, toOrderId, }: {
|
|
20
24
|
fromOrderId: string;
|
|
21
25
|
toOrderId: string;
|
|
22
26
|
}) => Promise<void>;
|
|
23
|
-
updateBillingAddress: (orderId: string, billingAddress: Address) => Promise<Order>;
|
|
24
|
-
updateContact: (orderId: string, contact: Contact) => Promise<Order>;
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
}
|
|
28
|
-
export
|
|
29
|
-
Orders: mongodb.Collection<Order>;
|
|
30
|
-
OrderPositions: mongodb.Collection<OrderPosition>;
|
|
31
|
-
}) => OrderMutations;
|
|
27
|
+
updateBillingAddress: (orderId: string, billingAddress: Address) => Promise<mongodb.WithId<Order> | null>;
|
|
28
|
+
updateContact: (orderId: string, contact: Contact) => Promise<mongodb.WithId<Order> | null>;
|
|
29
|
+
updateCalculationSheet: (orderId: string, calculation: any) => Promise<mongodb.WithId<Order> | null>;
|
|
30
|
+
updateContext: (orderId: string, context: any) => Promise<mongodb.WithId<Order> | null>;
|
|
31
|
+
};
|
|
32
|
+
export type OrderMutations = ReturnType<typeof configureOrderModuleMutations>;
|
|
32
33
|
//# sourceMappingURL=configureOrdersModule-mutations.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"configureOrdersModule-mutations.d.ts","sourceRoot":"","sources":["../../src/module/configureOrdersModule-mutations.ts"],"names":[],"mappings":"AACA,OAAO,EACL,OAAO,EACP,OAAO,EAGP,OAAO,EACR,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAAE,KAAK,EAAe,MAAM,2BAA2B,CAAC;AAC/D,OAAO,EAAE,aAAa,EAAE,MAAM,mCAAmC,CAAC;
|
|
1
|
+
{"version":3,"file":"configureOrdersModule-mutations.d.ts","sourceRoot":"","sources":["../../src/module/configureOrdersModule-mutations.ts"],"names":[],"mappings":"AACA,OAAO,EACL,OAAO,EACP,OAAO,EAGP,OAAO,EACR,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAAE,KAAK,EAAe,MAAM,2BAA2B,CAAC;AAC/D,OAAO,EAAE,aAAa,EAAE,MAAM,mCAAmC,CAAC;AAUlE,eAAO,MAAM,6BAA6B,GAAI,6BAG3C;IACD,MAAM,EAAE,OAAO,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC;IAClC,cAAc,EAAE,OAAO,CAAC,UAAU,CAAC,aAAa,CAAC,CAAC;CACnD;oGAYM;QACD,MAAM,EAAE,MAAM,CAAC;QACf,cAAc,CAAC,EAAE,OAAO,CAAC;QACzB,OAAO,CAAC,EAAE,OAAO,CAAC;QAClB,WAAW,EAAE,MAAM,CAAC;QACpB,YAAY,EAAE,MAAM,CAAC;QACrB,WAAW,CAAC,EAAE,MAAM,CAAC;QACrB,kBAAkB,CAAC,EAAE,MAAM,CAAC;QAC5B,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;KAC/B;sBAsBuB,MAAM;wCAOY;QAAE,OAAO,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,MAAM,CAAA;KAAE;qDAW1E;QACD,WAAW,EAAE,MAAM,CAAC;QACpB,SAAS,EAAE,MAAM,CAAC;KACnB;oCAWqC,MAAM,kBAAkB,OAAO;6BAiBtC,MAAM,WAAW,OAAO;sCAiBf,MAAM,eAAe,GAAG;6BAiBjC,MAAM,WAAW,GAAG;CAwBtD,CAAC;AAEF,MAAM,MAAM,cAAc,GAAG,UAAU,CAAC,OAAO,6BAA6B,CAAC,CAAC"}
|
|
@@ -11,7 +11,7 @@ const ORDER_EVENTS = [
|
|
|
11
11
|
export const configureOrderModuleMutations = ({ Orders, OrderPositions, }) => {
|
|
12
12
|
registerEvents(ORDER_EVENTS);
|
|
13
13
|
return {
|
|
14
|
-
create: async ({ userId, orderNumber, currencyCode, countryCode, billingAddress, contact }) => {
|
|
14
|
+
create: async ({ userId, orderNumber, currencyCode, countryCode, billingAddress, contact, context, }) => {
|
|
15
15
|
const { insertedId: orderId } = await Orders.insertOne({
|
|
16
16
|
_id: generateDbObjectId(),
|
|
17
17
|
created: new Date(),
|
|
@@ -24,13 +24,16 @@ export const configureOrderModuleMutations = ({ Orders, OrderPositions, }) => {
|
|
|
24
24
|
calculation: [],
|
|
25
25
|
log: [],
|
|
26
26
|
orderNumber,
|
|
27
|
+
context: context || {},
|
|
27
28
|
});
|
|
28
|
-
const order = await Orders.findOne(generateDbFilterById(orderId), {});
|
|
29
|
+
const order = (await Orders.findOne(generateDbFilterById(orderId), {}));
|
|
29
30
|
await emit('ORDER_CREATE', { order });
|
|
30
31
|
return order;
|
|
31
32
|
},
|
|
32
33
|
delete: async (orderId) => {
|
|
33
34
|
const { deletedCount } = await Orders.deleteOne({ _id: orderId });
|
|
35
|
+
if (!deletedCount)
|
|
36
|
+
return 0;
|
|
34
37
|
await emit('ORDER_REMOVE', { orderId });
|
|
35
38
|
return deletedCount;
|
|
36
39
|
},
|
|
@@ -41,7 +44,7 @@ export const configureOrderModuleMutations = ({ Orders, OrderPositions, }) => {
|
|
|
41
44
|
},
|
|
42
45
|
});
|
|
43
46
|
},
|
|
44
|
-
moveCartPositions: async ({ fromOrderId, toOrderId }) => {
|
|
47
|
+
moveCartPositions: async ({ fromOrderId, toOrderId, }) => {
|
|
45
48
|
await OrderPositions.updateMany({ orderId: fromOrderId }, {
|
|
46
49
|
$set: {
|
|
47
50
|
orderId: toOrderId,
|
|
@@ -56,7 +59,9 @@ export const configureOrderModuleMutations = ({ Orders, OrderPositions, }) => {
|
|
|
56
59
|
updated: new Date(),
|
|
57
60
|
},
|
|
58
61
|
}, { returnDocument: 'after' });
|
|
59
|
-
|
|
62
|
+
if (!order)
|
|
63
|
+
return null;
|
|
64
|
+
await emit('ORDER_UPDATE', { order, field: 'billingAddress' });
|
|
60
65
|
return order;
|
|
61
66
|
},
|
|
62
67
|
updateContact: async (orderId, contact) => {
|
|
@@ -67,6 +72,8 @@ export const configureOrderModuleMutations = ({ Orders, OrderPositions, }) => {
|
|
|
67
72
|
updated: new Date(),
|
|
68
73
|
},
|
|
69
74
|
}, { returnDocument: 'after' });
|
|
75
|
+
if (!order)
|
|
76
|
+
return null;
|
|
70
77
|
await emit('ORDER_UPDATE', { order, field: 'contact' });
|
|
71
78
|
return order;
|
|
72
79
|
},
|
|
@@ -78,6 +85,8 @@ export const configureOrderModuleMutations = ({ Orders, OrderPositions, }) => {
|
|
|
78
85
|
updated: new Date(),
|
|
79
86
|
},
|
|
80
87
|
}, { returnDocument: 'after' });
|
|
88
|
+
if (!order)
|
|
89
|
+
return null;
|
|
81
90
|
await emit('ORDER_UPDATE', { order, field: 'calculation' });
|
|
82
91
|
return order;
|
|
83
92
|
},
|
|
@@ -87,17 +96,16 @@ export const configureOrderModuleMutations = ({ Orders, OrderPositions, }) => {
|
|
|
87
96
|
if (!context || Object.keys(context).length === 0)
|
|
88
97
|
return Orders.findOne(selector, {});
|
|
89
98
|
const contextSetters = Object.fromEntries(Object.entries(context).map(([key, value]) => [`context.${key}`, value]));
|
|
90
|
-
const
|
|
99
|
+
const order = await Orders.findOneAndUpdate(selector, {
|
|
91
100
|
$set: {
|
|
92
101
|
...contextSetters,
|
|
93
102
|
updated: new Date(),
|
|
94
103
|
},
|
|
95
|
-
}, {
|
|
96
|
-
if (
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
return null;
|
|
104
|
+
}, { returnDocument: 'after' });
|
|
105
|
+
if (!order)
|
|
106
|
+
return null;
|
|
107
|
+
await emit('ORDER_UPDATE', { order, field: 'context' });
|
|
108
|
+
return order;
|
|
101
109
|
},
|
|
102
110
|
};
|
|
103
111
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"configureOrdersModule-mutations.js","sourceRoot":"","sources":["../../src/module/configureOrdersModule-mutations.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAC7D,OAAO,EAGL,oBAAoB,EACpB,kBAAkB,GAEnB,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAAS,WAAW,EAAE,MAAM,2BAA2B,CAAC;
|
|
1
|
+
{"version":3,"file":"configureOrdersModule-mutations.js","sourceRoot":"","sources":["../../src/module/configureOrdersModule-mutations.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAC7D,OAAO,EAGL,oBAAoB,EACpB,kBAAkB,GAEnB,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAAS,WAAW,EAAE,MAAM,2BAA2B,CAAC;AAG/D,MAAM,YAAY,GAAa;IAC7B,cAAc;IACd,cAAc;IACd,6BAA6B;IAC7B,4BAA4B;IAC5B,cAAc;CACf,CAAC;AAEF,MAAM,CAAC,MAAM,6BAA6B,GAAG,CAAC,EAC5C,MAAM,EACN,cAAc,GAIf,EAAE,EAAE;IACH,cAAc,CAAC,YAAY,CAAC,CAAC;IAE7B,OAAO;QACL,MAAM,EAAE,KAAK,EAAE,EACb,MAAM,EACN,WAAW,EACX,YAAY,EACZ,WAAW,EACX,cAAc,EACd,OAAO,EACP,OAAO,GAUR,EAAE,EAAE;YACH,MAAM,EAAE,UAAU,EAAE,OAAO,EAAE,GAAG,MAAM,MAAM,CAAC,SAAS,CAAC;gBACrD,GAAG,EAAE,kBAAkB,EAAE;gBACzB,OAAO,EAAE,IAAI,IAAI,EAAE;gBACnB,MAAM,EAAE,IAAI;gBACZ,cAAc;gBACd,OAAO;gBACP,MAAM;gBACN,YAAY;gBACZ,WAAW;gBACX,WAAW,EAAE,EAAE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aAAa,EAAE,CAAC,CAAC;YAC5D,OAAO,KAAK,CAAC;QACf,CAAC;QAED,aAAa,EAAE,KAAK,EAAE,OAAe,EAAE,OAAY,EAAE,EAAE;YACrD,MAAM,QAAQ,GAAG,oBAAoB,CAAQ,OAAO,CAAC,CAAC;YACtD,QAAQ,CAAC,MAAM,GAAG,EAAE,GAAG,EAAE,CAAC,IAAI,EAAE,WAAW,CAAC,OAAO,CAAC,EAAE,CAAC;YAEvD,IAAI,CAAC,OAAO,IAAI,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,MAAM,KAAK,CAAC;gBAAE,OAAO,MAAM,CAAC,OAAO,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC;YAEvF,MAAM,cAAc,GAAG,MAAM,CAAC,WAAW,CACvC,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,EAAE,KAAK,CAAC,EAAE,EAAE,CAAC,CAAC,WAAW,GAAG,EAAE,EAAE,KAAK,CAAC,CAAC,CACzE,CAAC;YACF,MAAM,KAAK,GAAG,MAAM,MAAM,CAAC,gBAAgB,CACzC,QAAQ,EACR;gBACE,IAAI,EAAE;oBACJ,GAAG,cAAc;oBACjB,OAAO,EAAE,IAAI,IAAI,EAAE;iBACpB;aACF,EACD,EAAE,cAAc,EAAE,OAAO,EAAE,CAC5B,CAAC;YACF,IAAI,CAAC,KAAK;gBAAE,OAAO,IAAI,CAAC;YACxB,MAAM,IAAI,CAAC,cAAc,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,SAAS,EAAE,CAAC,CAAC;YACxD,OAAO,KAAK,CAAC;QACf,CAAC;KACF,CAAC;AACJ,CAAC,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { mongodb } from '@unchainedshop/mongodb';
|
|
2
|
+
import { Order, OrderProcessing } from '@unchainedshop/types/orders.js';
|
|
3
|
+
import type { Locker } from '@kontsedal/locco';
|
|
4
|
+
import { OrderDelivery } from '@unchainedshop/types/orders.deliveries.js';
|
|
5
|
+
import { OrderPayment } from '@unchainedshop/types/orders.payments.js';
|
|
6
|
+
import { OrderPosition } from '@unchainedshop/types/orders.positions.js';
|
|
7
|
+
export declare const configureOrderModuleProcessing: ({ Orders, OrderPositions, OrderDeliveries, OrderPayments, locker, }: {
|
|
8
|
+
Orders: mongodb.Collection<Order>;
|
|
9
|
+
OrderPositions: mongodb.Collection<OrderPosition>;
|
|
10
|
+
OrderDeliveries: mongodb.Collection<OrderDelivery>;
|
|
11
|
+
OrderPayments: mongodb.Collection<OrderPayment>;
|
|
12
|
+
locker: Locker;
|
|
13
|
+
}) => OrderProcessing;
|
|
14
|
+
//# sourceMappingURL=configureOrdersModule-processing.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"configureOrdersModule-processing.d.ts","sourceRoot":"","sources":["../../src/module/configureOrdersModule-processing.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAwB,MAAM,wBAAwB,CAAC;AACvE,OAAO,EAAE,KAAK,EAAe,eAAe,EAAgB,MAAM,gCAAgC,CAAC;AACnG,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAC/C,OAAO,EAAE,aAAa,EAAE,MAAM,2CAA2C,CAAC;AAC1E,OAAO,EAAE,YAAY,EAAE,MAAM,yCAAyC,CAAC;AACvE,OAAO,EAAE,aAAa,EAAE,MAAM,0CAA0C,CAAC;AAczE,eAAO,MAAM,8BAA8B,wEAMxC;IACD,MAAM,EAAE,OAAO,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC;IAClC,cAAc,EAAE,OAAO,CAAC,UAAU,CAAC,aAAa,CAAC,CAAC;IAClD,eAAe,EAAE,OAAO,CAAC,UAAU,CAAC,aAAa,CAAC,CAAC;IACnD,aAAa,EAAE,OAAO,CAAC,UAAU,CAAC,YAAY,CAAC,CAAC;IAChD,MAAM,EAAE,MAAM,CAAC;CAChB,KAAG,eA6cH,CAAC"}
|
|
@@ -0,0 +1,348 @@
|
|
|
1
|
+
import { generateDbFilterById } from '@unchainedshop/mongodb';
|
|
2
|
+
import { OrderStatus } from '@unchainedshop/types/orders.js';
|
|
3
|
+
import { emit, registerEvents } from '@unchainedshop/events';
|
|
4
|
+
import { log } from '@unchainedshop/logger';
|
|
5
|
+
import { ProductType } from '@unchainedshop/types/products.js';
|
|
6
|
+
import { ordersSettings } from '../orders-settings.js';
|
|
7
|
+
const ORDER_PROCESSING_EVENTS = [
|
|
8
|
+
'ORDER_CHECKOUT',
|
|
9
|
+
'ORDER_CONFIRMED',
|
|
10
|
+
'ORDER_REJECTED',
|
|
11
|
+
'ORDER_FULLFILLED',
|
|
12
|
+
];
|
|
13
|
+
export const configureOrderModuleProcessing = ({ Orders, OrderPositions, OrderDeliveries, OrderPayments, locker, }) => {
|
|
14
|
+
registerEvents(ORDER_PROCESSING_EVENTS);
|
|
15
|
+
const findNewOrderNumber = async (order, index = 0) => {
|
|
16
|
+
const newHashID = ordersSettings.orderNumberHashFn(order, index);
|
|
17
|
+
if ((await Orders.countDocuments({ orderNumber: newHashID }, { limit: 1 })) === 0) {
|
|
18
|
+
return newHashID;
|
|
19
|
+
}
|
|
20
|
+
return findNewOrderNumber(order, index + 1);
|
|
21
|
+
};
|
|
22
|
+
const updateStatus = async (orderId, { status, info }) => {
|
|
23
|
+
const selector = generateDbFilterById(orderId);
|
|
24
|
+
const order = await Orders.findOne(selector, {});
|
|
25
|
+
if (order.status === status)
|
|
26
|
+
return order;
|
|
27
|
+
const date = new Date();
|
|
28
|
+
const $set = {
|
|
29
|
+
status,
|
|
30
|
+
updated: new Date(),
|
|
31
|
+
};
|
|
32
|
+
switch (status) {
|
|
33
|
+
// explicitly use fallthrough here!
|
|
34
|
+
case OrderStatus.FULLFILLED:
|
|
35
|
+
$set.fullfilled = order.fullfilled || date;
|
|
36
|
+
case OrderStatus.REJECTED: // eslint-disable-line no-fallthrough
|
|
37
|
+
case OrderStatus.CONFIRMED: // eslint-disable-line no-fallthrough
|
|
38
|
+
if (status === OrderStatus.REJECTED) {
|
|
39
|
+
$set.rejected = order.rejected || date;
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
$set.confirmed = order.confirmed || date;
|
|
43
|
+
}
|
|
44
|
+
case OrderStatus.PENDING: // eslint-disable-line no-fallthrough
|
|
45
|
+
$set.ordered = order.ordered || date;
|
|
46
|
+
$set.orderNumber = order.orderNumber || (await findNewOrderNumber(order));
|
|
47
|
+
break;
|
|
48
|
+
default:
|
|
49
|
+
break;
|
|
50
|
+
}
|
|
51
|
+
const modifier = {
|
|
52
|
+
$set,
|
|
53
|
+
$push: {
|
|
54
|
+
log: {
|
|
55
|
+
date,
|
|
56
|
+
status,
|
|
57
|
+
info,
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
const modificationResult = await Orders.findOneAndUpdate({
|
|
62
|
+
...selector,
|
|
63
|
+
status: { $ne: status }, // Only update if status is different
|
|
64
|
+
}, modifier, {
|
|
65
|
+
returnDocument: 'after',
|
|
66
|
+
includeResultMetadata: true,
|
|
67
|
+
});
|
|
68
|
+
if (modificationResult.ok) {
|
|
69
|
+
log(`New Status: ${status}`, { orderId });
|
|
70
|
+
if (order.status === null) {
|
|
71
|
+
// The first time that an order transitions away from cart is a checkout event
|
|
72
|
+
await emit('ORDER_CHECKOUT', { order: modificationResult.value, oldStatus: order.status });
|
|
73
|
+
}
|
|
74
|
+
switch (status) {
|
|
75
|
+
case OrderStatus.FULLFILLED:
|
|
76
|
+
await emit('ORDER_FULLFILLED', { order: modificationResult.value, oldStatus: order.status });
|
|
77
|
+
break;
|
|
78
|
+
case OrderStatus.REJECTED:
|
|
79
|
+
await emit('ORDER_REJECTED', { order: modificationResult.value, oldStatus: order.status });
|
|
80
|
+
break;
|
|
81
|
+
case OrderStatus.CONFIRMED:
|
|
82
|
+
await emit('ORDER_CONFIRMED', { order: modificationResult.value, oldStatus: order.status });
|
|
83
|
+
break;
|
|
84
|
+
default:
|
|
85
|
+
break;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return modificationResult.value || Orders.findOne(selector, {});
|
|
89
|
+
};
|
|
90
|
+
const findOrderPositions = async (order) => OrderPositions.find({
|
|
91
|
+
orderId: order._id,
|
|
92
|
+
quantity: { $gt: 0 },
|
|
93
|
+
}).toArray();
|
|
94
|
+
const findOrderDelivery = async (order) => OrderDeliveries.findOne(generateDbFilterById(order.deliveryId), {});
|
|
95
|
+
const findOrderPayment = async (order) => OrderPayments.findOne(generateDbFilterById(order.paymentId), {});
|
|
96
|
+
const missingInputDataForCheckout = async (order) => {
|
|
97
|
+
const errors = [];
|
|
98
|
+
if (!order.contact)
|
|
99
|
+
errors.push(new Error('Contact data not provided'));
|
|
100
|
+
if (!order.billingAddress)
|
|
101
|
+
errors.push(new Error('Billing address not provided'));
|
|
102
|
+
if (!(await findOrderDelivery(order)))
|
|
103
|
+
errors.push('No delivery provider selected');
|
|
104
|
+
if (!(await findOrderPayment(order)))
|
|
105
|
+
errors.push('No payment provider selected');
|
|
106
|
+
return errors;
|
|
107
|
+
};
|
|
108
|
+
const itemValidationErrors = async (order, unchainedAPI) => {
|
|
109
|
+
// Check if items are valid
|
|
110
|
+
const orderPositions = await findOrderPositions(order);
|
|
111
|
+
if (orderPositions.length === 0) {
|
|
112
|
+
const NoItemsError = new Error('No items to checkout');
|
|
113
|
+
NoItemsError.name = 'NoItemsError';
|
|
114
|
+
return [NoItemsError];
|
|
115
|
+
}
|
|
116
|
+
const validationErrors = await Promise.all(orderPositions.map(async (orderPosition) => {
|
|
117
|
+
const errors = [];
|
|
118
|
+
log(`OrderPosition ${orderPosition._id} -> Validate ${orderPosition.quantity}`, {
|
|
119
|
+
orderId: orderPosition.orderId,
|
|
120
|
+
});
|
|
121
|
+
const product = await unchainedAPI.modules.products.findProduct({
|
|
122
|
+
productId: orderPosition.productId,
|
|
123
|
+
});
|
|
124
|
+
try {
|
|
125
|
+
await ordersSettings.validateOrderPosition({
|
|
126
|
+
order,
|
|
127
|
+
product,
|
|
128
|
+
configuration: orderPosition.configuration,
|
|
129
|
+
quantityDiff: 0,
|
|
130
|
+
}, unchainedAPI);
|
|
131
|
+
}
|
|
132
|
+
catch (e) {
|
|
133
|
+
errors.push(e);
|
|
134
|
+
}
|
|
135
|
+
const quotation = orderPosition.quotationId &&
|
|
136
|
+
(await unchainedAPI.modules.quotations.findQuotation({
|
|
137
|
+
quotationId: orderPosition.quotationId,
|
|
138
|
+
}));
|
|
139
|
+
if (quotation && !unchainedAPI.modules.quotations.isProposalValid(quotation)) {
|
|
140
|
+
errors.push(new Error('Quotation expired or fullfiled, please request a new offer'));
|
|
141
|
+
}
|
|
142
|
+
return errors;
|
|
143
|
+
}));
|
|
144
|
+
return validationErrors.flatMap((f) => f);
|
|
145
|
+
};
|
|
146
|
+
const isAutoConfirmationEnabled = async (order, unchainedAPI) => {
|
|
147
|
+
const { modules } = unchainedAPI;
|
|
148
|
+
if (order.status === OrderStatus.FULLFILLED || order.status === OrderStatus.CONFIRMED) {
|
|
149
|
+
return false;
|
|
150
|
+
}
|
|
151
|
+
const orderPayment = await findOrderPayment(order);
|
|
152
|
+
let isBlockingOrderConfirmation = orderPayment &&
|
|
153
|
+
(await modules.orders.payments.isBlockingOrderConfirmation(orderPayment, unchainedAPI));
|
|
154
|
+
if (isBlockingOrderConfirmation)
|
|
155
|
+
return false;
|
|
156
|
+
const orderDelivery = await findOrderDelivery(order);
|
|
157
|
+
isBlockingOrderConfirmation =
|
|
158
|
+
orderDelivery &&
|
|
159
|
+
(await modules.orders.deliveries.isBlockingOrderConfirmation(orderDelivery, unchainedAPI));
|
|
160
|
+
if (isBlockingOrderConfirmation)
|
|
161
|
+
return false;
|
|
162
|
+
return true;
|
|
163
|
+
};
|
|
164
|
+
const isAutoFullfillmentEnabled = async (order, unchainedAPI) => {
|
|
165
|
+
const { modules } = unchainedAPI;
|
|
166
|
+
const orderPayment = await findOrderPayment(order);
|
|
167
|
+
let isBlockingOrderFullfillment = orderPayment && modules.orders.payments.isBlockingOrderFullfillment(orderPayment);
|
|
168
|
+
if (isBlockingOrderFullfillment)
|
|
169
|
+
return false;
|
|
170
|
+
const orderDelivery = await findOrderDelivery(order);
|
|
171
|
+
isBlockingOrderFullfillment =
|
|
172
|
+
orderDelivery && modules.orders.deliveries.isBlockingOrderFullfillment(orderDelivery);
|
|
173
|
+
if (isBlockingOrderFullfillment)
|
|
174
|
+
return false;
|
|
175
|
+
if (order.status === OrderStatus.FULLFILLED) {
|
|
176
|
+
return false;
|
|
177
|
+
}
|
|
178
|
+
return true;
|
|
179
|
+
};
|
|
180
|
+
const findNextStatus = async (status, order, unchainedAPI) => {
|
|
181
|
+
if (status === null) {
|
|
182
|
+
return OrderStatus.PENDING;
|
|
183
|
+
}
|
|
184
|
+
if (status === OrderStatus.PENDING) {
|
|
185
|
+
if (await isAutoConfirmationEnabled(order, unchainedAPI)) {
|
|
186
|
+
return OrderStatus.CONFIRMED;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
if (status === OrderStatus.CONFIRMED) {
|
|
190
|
+
if (await isAutoFullfillmentEnabled(order, unchainedAPI)) {
|
|
191
|
+
return OrderStatus.FULLFILLED;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
return status;
|
|
195
|
+
};
|
|
196
|
+
return {
|
|
197
|
+
checkout: async (orderId, transactionContext, unchainedAPI) => {
|
|
198
|
+
const { modules, services } = unchainedAPI;
|
|
199
|
+
const order = await Orders.findOne(generateDbFilterById(orderId), {});
|
|
200
|
+
if (order.status !== null)
|
|
201
|
+
return order;
|
|
202
|
+
const errors = [
|
|
203
|
+
...(await missingInputDataForCheckout(order)),
|
|
204
|
+
...(await itemValidationErrors(order, unchainedAPI)),
|
|
205
|
+
].filter(Boolean);
|
|
206
|
+
if (errors.length > 0) {
|
|
207
|
+
throw new Error(errors[0]);
|
|
208
|
+
}
|
|
209
|
+
const lock = await locker.lock(`order:checkout:${order._id}`, 5000).acquire();
|
|
210
|
+
try {
|
|
211
|
+
const processedOrder = await modules.orders.processOrder(order, transactionContext, unchainedAPI);
|
|
212
|
+
// After checkout, store last checkout information on user
|
|
213
|
+
await modules.users.updateLastBillingAddress(processedOrder.userId, processedOrder.billingAddress);
|
|
214
|
+
await modules.users.updateLastContact(processedOrder.userId, processedOrder.contact);
|
|
215
|
+
// Then eventually build next cart
|
|
216
|
+
const user = await modules.users.findUserById(processedOrder.userId);
|
|
217
|
+
const locale = modules.users.userLocale(user);
|
|
218
|
+
await services.orders.nextUserCart({
|
|
219
|
+
user,
|
|
220
|
+
countryCode: locale.country,
|
|
221
|
+
}, unchainedAPI);
|
|
222
|
+
return processedOrder;
|
|
223
|
+
}
|
|
224
|
+
finally {
|
|
225
|
+
await lock.release();
|
|
226
|
+
}
|
|
227
|
+
},
|
|
228
|
+
confirm: async (order, transactionContext, unchainedAPI) => {
|
|
229
|
+
const { modules } = unchainedAPI;
|
|
230
|
+
if (order.status !== OrderStatus.PENDING)
|
|
231
|
+
return order;
|
|
232
|
+
const lock = await locker.lock(`order:confirm-reject:${order._id}`, 1500).acquire();
|
|
233
|
+
try {
|
|
234
|
+
return await modules.orders.processOrder(order, {
|
|
235
|
+
...transactionContext,
|
|
236
|
+
nextStatus: OrderStatus.CONFIRMED,
|
|
237
|
+
}, unchainedAPI);
|
|
238
|
+
}
|
|
239
|
+
finally {
|
|
240
|
+
await lock.release();
|
|
241
|
+
}
|
|
242
|
+
},
|
|
243
|
+
reject: async (order, transactionContext, unchainedAPI) => {
|
|
244
|
+
const { modules } = unchainedAPI;
|
|
245
|
+
if (order.status !== OrderStatus.PENDING)
|
|
246
|
+
return order;
|
|
247
|
+
const lock = await locker.lock(`order:confirm-reject:${order._id}`, 1500).acquire();
|
|
248
|
+
try {
|
|
249
|
+
return await modules.orders.processOrder(order, {
|
|
250
|
+
...transactionContext,
|
|
251
|
+
nextStatus: OrderStatus.REJECTED,
|
|
252
|
+
}, unchainedAPI);
|
|
253
|
+
}
|
|
254
|
+
finally {
|
|
255
|
+
await lock.release();
|
|
256
|
+
}
|
|
257
|
+
},
|
|
258
|
+
processOrder: async (initialOrder, orderTransactionContext, unchainedAPI) => {
|
|
259
|
+
const { modules } = unchainedAPI;
|
|
260
|
+
const { paymentContext, deliveryContext, nextStatus: forceNextStatus, comment, } = orderTransactionContext;
|
|
261
|
+
const orderId = initialOrder._id;
|
|
262
|
+
let order = initialOrder;
|
|
263
|
+
let nextStatus = forceNextStatus || (await findNextStatus(initialOrder.status, order, unchainedAPI));
|
|
264
|
+
if (nextStatus === OrderStatus.PENDING) {
|
|
265
|
+
// auto charge during transition to pending
|
|
266
|
+
const orderPayment = await modules.orders.payments.findOrderPayment({
|
|
267
|
+
orderPaymentId: order.paymentId,
|
|
268
|
+
});
|
|
269
|
+
await modules.orders.payments.charge(orderPayment, { userId: order.userId, transactionContext: paymentContext }, unchainedAPI);
|
|
270
|
+
nextStatus = await findNextStatus(nextStatus, order, unchainedAPI);
|
|
271
|
+
}
|
|
272
|
+
if (nextStatus === OrderStatus.REJECTED) {
|
|
273
|
+
// auto cancel during transition to rejected
|
|
274
|
+
const orderPayment = await modules.orders.payments.findOrderPayment({
|
|
275
|
+
orderPaymentId: order.paymentId,
|
|
276
|
+
});
|
|
277
|
+
await modules.orders.payments.cancel(orderPayment, { userId: order.userId, transactionContext: paymentContext }, unchainedAPI);
|
|
278
|
+
}
|
|
279
|
+
if (nextStatus === OrderStatus.CONFIRMED) {
|
|
280
|
+
// confirm pre-authorized payments
|
|
281
|
+
const orderPayment = await modules.orders.payments.findOrderPayment({
|
|
282
|
+
orderPaymentId: order.paymentId,
|
|
283
|
+
});
|
|
284
|
+
await modules.orders.payments.confirm(orderPayment, { userId: order.userId, transactionContext: paymentContext }, unchainedAPI);
|
|
285
|
+
if (order.status !== OrderStatus.CONFIRMED) {
|
|
286
|
+
// we have to stop here shortly to complete the confirmation
|
|
287
|
+
// before auto delivery is started, else we have no chance to create
|
|
288
|
+
// numbers that are needed for delivery
|
|
289
|
+
order = await updateStatus(orderId, {
|
|
290
|
+
status: OrderStatus.CONFIRMED,
|
|
291
|
+
info: comment,
|
|
292
|
+
});
|
|
293
|
+
const orderDelivery = await modules.orders.deliveries.findDelivery({
|
|
294
|
+
orderDeliveryId: order.deliveryId,
|
|
295
|
+
});
|
|
296
|
+
await modules.orders.deliveries.send(orderDelivery, {
|
|
297
|
+
order,
|
|
298
|
+
deliveryContext,
|
|
299
|
+
}, unchainedAPI);
|
|
300
|
+
const orderPositions = await findOrderPositions(order);
|
|
301
|
+
const mappedProductOrderPositions = await Promise.all(orderPositions.map(async (orderPosition) => {
|
|
302
|
+
const product = await modules.products.findProduct({
|
|
303
|
+
productId: orderPosition.productId,
|
|
304
|
+
});
|
|
305
|
+
return {
|
|
306
|
+
orderPosition,
|
|
307
|
+
product,
|
|
308
|
+
};
|
|
309
|
+
}));
|
|
310
|
+
const tokenizedItems = mappedProductOrderPositions.filter((item) => item.product?.type === ProductType.TokenizedProduct);
|
|
311
|
+
if (tokenizedItems.length > 0) {
|
|
312
|
+
// Give virtual warehouse a chance to instantiate new virtual objects
|
|
313
|
+
await modules.warehousing.tokenizeItems(order, {
|
|
314
|
+
items: tokenizedItems,
|
|
315
|
+
}, unchainedAPI);
|
|
316
|
+
}
|
|
317
|
+
// Enrollments: Generate enrollments for plan products
|
|
318
|
+
const planItems = mappedProductOrderPositions.filter((item) => item.product?.type === ProductType.PlanProduct && !order.originEnrollmentId);
|
|
319
|
+
if (planItems.length > 0) {
|
|
320
|
+
await modules.enrollments.createFromCheckout(order, {
|
|
321
|
+
items: planItems,
|
|
322
|
+
context: {
|
|
323
|
+
paymentContext,
|
|
324
|
+
deliveryContext,
|
|
325
|
+
},
|
|
326
|
+
}, unchainedAPI);
|
|
327
|
+
}
|
|
328
|
+
// Quotations: If we came here, the checkout succeeded, so we can fullfill underlying quotations
|
|
329
|
+
const quotationItems = mappedProductOrderPositions.filter((item) => item.orderPosition.quotationId);
|
|
330
|
+
await Promise.all(quotationItems.map(async ({ orderPosition }) => {
|
|
331
|
+
await modules.quotations.fullfillQuotation(orderPosition.quotationId, {
|
|
332
|
+
orderId,
|
|
333
|
+
orderPositionId: orderPosition._id,
|
|
334
|
+
}, unchainedAPI);
|
|
335
|
+
}));
|
|
336
|
+
// TODO: we will use this function to keep a "Ordered in Flight" amount, allowing us to
|
|
337
|
+
// do live stock stuff
|
|
338
|
+
// 2. Reserve quantity at Warehousing Provider until order is CANCELLED/FULLFILLED
|
|
339
|
+
// ???
|
|
340
|
+
}
|
|
341
|
+
nextStatus = await findNextStatus(nextStatus, order, unchainedAPI);
|
|
342
|
+
}
|
|
343
|
+
return updateStatus(order._id, { status: nextStatus, info: comment });
|
|
344
|
+
},
|
|
345
|
+
updateStatus,
|
|
346
|
+
};
|
|
347
|
+
};
|
|
348
|
+
//# sourceMappingURL=configureOrdersModule-processing.js.map
|