@unchainedshop/core-orders 1.1.3
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/.npm/package/README +7 -0
- package/.npm/package/npm-shrinkwrap.json +190 -0
- package/.versions +54 -0
- package/README.md +5 -0
- package/lib/db/OrderDeliveriesCollection.d.ts +3 -0
- package/lib/db/OrderDeliveriesCollection.js +8 -0
- package/lib/db/OrderDeliveriesCollection.js.map +1 -0
- package/lib/db/OrderDeliveriesSchema.d.ts +1 -0
- package/lib/db/OrderDeliveriesSchema.js +20 -0
- package/lib/db/OrderDeliveriesSchema.js.map +1 -0
- package/lib/db/OrderDiscountTrigger.d.ts +4 -0
- package/lib/db/OrderDiscountTrigger.js +6 -0
- package/lib/db/OrderDiscountTrigger.js.map +1 -0
- package/lib/db/OrderDiscountsCollection.d.ts +3 -0
- package/lib/db/OrderDiscountsCollection.js +11 -0
- package/lib/db/OrderDiscountsCollection.js.map +1 -0
- package/lib/db/OrderDiscountsSchema.d.ts +1 -0
- package/lib/db/OrderDiscountsSchema.js +12 -0
- package/lib/db/OrderDiscountsSchema.js.map +1 -0
- package/lib/db/OrderPaymentsCollection.d.ts +3 -0
- package/lib/db/OrderPaymentsCollection.js +8 -0
- package/lib/db/OrderPaymentsCollection.js.map +1 -0
- package/lib/db/OrderPaymentsSchema.d.ts +1 -0
- package/lib/db/OrderPaymentsSchema.js +20 -0
- package/lib/db/OrderPaymentsSchema.js.map +1 -0
- package/lib/db/OrderPositionsCollection.d.ts +3 -0
- package/lib/db/OrderPositionsCollection.js +11 -0
- package/lib/db/OrderPositionsCollection.js.map +1 -0
- package/lib/db/OrderPositionsSchema.d.ts +1 -0
- package/lib/db/OrderPositionsSchema.js +37 -0
- package/lib/db/OrderPositionsSchema.js.map +1 -0
- package/lib/db/OrdersCollection.d.ts +3 -0
- package/lib/db/OrdersCollection.js +33 -0
- package/lib/db/OrdersCollection.js.map +1 -0
- package/lib/db/OrdersSchema.d.ts +1 -0
- package/lib/db/OrdersSchema.js +30 -0
- package/lib/db/OrdersSchema.js.map +1 -0
- package/lib/director/OrderDiscountAdapter.d.ts +2 -0
- package/lib/director/OrderDiscountAdapter.js +3 -0
- package/lib/director/OrderDiscountAdapter.js.map +1 -0
- package/lib/director/OrderDiscountDirector.d.ts +2 -0
- package/lib/director/OrderDiscountDirector.js +3 -0
- package/lib/director/OrderDiscountDirector.js.map +1 -0
- package/lib/director/OrderPricingAdapter.d.ts +3 -0
- package/lib/director/OrderPricingAdapter.js +25 -0
- package/lib/director/OrderPricingAdapter.js.map +1 -0
- package/lib/director/OrderPricingDirector.d.ts +2 -0
- package/lib/director/OrderPricingDirector.js +48 -0
- package/lib/director/OrderPricingDirector.js.map +1 -0
- package/lib/director/OrderPricingSheet.d.ts +3 -0
- package/lib/director/OrderPricingSheet.js +113 -0
- package/lib/director/OrderPricingSheet.js.map +1 -0
- package/lib/module/configureOrderDeliveriesModule.d.ts +7 -0
- package/lib/module/configureOrderDeliveriesModule.js +164 -0
- package/lib/module/configureOrderDeliveriesModule.js.map +1 -0
- package/lib/module/configureOrderDiscountsModule.d.ts +7 -0
- package/lib/module/configureOrderDiscountsModule.js +183 -0
- package/lib/module/configureOrderDiscountsModule.js.map +1 -0
- package/lib/module/configureOrderPaymentsModule.d.ts +7 -0
- package/lib/module/configureOrderPaymentsModule.js +229 -0
- package/lib/module/configureOrderPaymentsModule.js.map +1 -0
- package/lib/module/configureOrderPositionsModule.d.ts +7 -0
- package/lib/module/configureOrderPositionsModule.js +213 -0
- package/lib/module/configureOrderPositionsModule.js.map +1 -0
- package/lib/module/configureOrdersModule-mutations.d.ts +12 -0
- package/lib/module/configureOrdersModule-mutations.js +162 -0
- package/lib/module/configureOrdersModule-mutations.js.map +1 -0
- package/lib/module/configureOrdersModule-processing.d.ts +13 -0
- package/lib/module/configureOrdersModule-processing.js +343 -0
- package/lib/module/configureOrdersModule-processing.js.map +1 -0
- package/lib/module/configureOrdersModule-queries.d.ts +5 -0
- package/lib/module/configureOrdersModule-queries.js +55 -0
- package/lib/module/configureOrdersModule-queries.js.map +1 -0
- package/lib/module/configureOrdersModule-transformations.d.ts +5 -0
- package/lib/module/configureOrdersModule-transformations.js +108 -0
- package/lib/module/configureOrdersModule-transformations.js.map +1 -0
- package/lib/module/configureOrdersModule.d.ts +3 -0
- package/lib/module/configureOrdersModule.js +239 -0
- package/lib/module/configureOrdersModule.js.map +1 -0
- package/lib/orders-index.d.ts +11 -0
- package/lib/orders-index.js +12 -0
- package/lib/orders-index.js.map +1 -0
- package/lib/orders-settings.d.ts +20 -0
- package/lib/orders-settings.js +17 -0
- package/lib/orders-settings.js.map +1 -0
- package/lib/service/createUserCartService.d.ts +2 -0
- package/lib/service/createUserCartService.js +23 -0
- package/lib/service/createUserCartService.js.map +1 -0
- package/lib/service/migrateOrderCartService.d.ts +2 -0
- package/lib/service/migrateOrderCartService.js +10 -0
- package/lib/service/migrateOrderCartService.js.map +1 -0
- package/lib/service/orderServices.d.ts +2 -0
- package/lib/service/orderServices.js +7 -0
- package/lib/service/orderServices.js.map +1 -0
- package/package.js +33 -0
- package/package.json +43 -0
- package/plugins/discount-100-off.ts +44 -0
- package/plugins/discount-half-price-manual.ts +43 -0
- package/plugins/discount-half-price.ts +46 -0
- package/plugins/order-delivery.ts +45 -0
- package/plugins/order-discount.ts +158 -0
- package/plugins/order-items-discount.ts +116 -0
- package/plugins/order-items.ts +61 -0
- package/plugins/order-payment.ts +46 -0
- package/src/db/OrderDeliveriesCollection.ts +12 -0
- package/src/db/OrderDeliveriesSchema.js +24 -0
- package/src/db/OrderDiscountTrigger.ts +4 -0
- package/src/db/OrderDiscountsCollection.ts +15 -0
- package/src/db/OrderDiscountsSchema.js +15 -0
- package/src/db/OrderPaymentsCollection.ts +12 -0
- package/src/db/OrderPaymentsSchema.js +24 -0
- package/src/db/OrderPositionsCollection.ts +15 -0
- package/src/db/OrderPositionsSchema.js +41 -0
- package/src/db/OrdersCollection.ts +37 -0
- package/src/db/OrdersSchema.js +34 -0
- package/src/director/OrderDiscountAdapter.ts +5 -0
- package/src/director/OrderDiscountDirector.ts +4 -0
- package/src/director/OrderPricingAdapter.ts +41 -0
- package/src/director/OrderPricingDirector.ts +68 -0
- package/src/director/OrderPricingSheet.ts +138 -0
- package/src/module/configureOrderDeliveriesModule.ts +240 -0
- package/src/module/configureOrderDiscountsModule.ts +245 -0
- package/src/module/configureOrderPaymentsModule.ts +331 -0
- package/src/module/configureOrderPositionsModule.ts +329 -0
- package/src/module/configureOrdersModule-mutations.ts +230 -0
- package/src/module/configureOrdersModule-processing.ts +506 -0
- package/src/module/configureOrdersModule-queries.ts +71 -0
- package/src/module/configureOrdersModule-transformations.ts +145 -0
- package/src/module/configureOrdersModule.ts +359 -0
- package/src/orders-index.ts +15 -0
- package/src/orders-settings.ts +23 -0
- package/src/service/createUserCartService.ts +38 -0
- package/src/service/migrateOrderCartService.ts +22 -0
- package/src/service/orderServices.ts +8 -0
- package/tests/orders-index.test.ts +50 -0
- package/tsconfig.build.json +26 -0
- package/tsconfig.json +8 -0
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import {
|
|
2
|
+
IOrderPricingSheet,
|
|
3
|
+
OrderPricingAdapterContext,
|
|
4
|
+
OrderPricingCalculation,
|
|
5
|
+
} from '@unchainedshop/types/orders.pricing';
|
|
6
|
+
import { IPricingAdapter } from '@unchainedshop/types/pricing';
|
|
7
|
+
import { BasePricingAdapter } from '@unchainedshop/utils';
|
|
8
|
+
import { OrderPricingSheet } from './OrderPricingSheet';
|
|
9
|
+
|
|
10
|
+
const basePricingAdapter = BasePricingAdapter<OrderPricingAdapterContext, OrderPricingCalculation>();
|
|
11
|
+
|
|
12
|
+
export const OrderPricingAdapter: IPricingAdapter<
|
|
13
|
+
OrderPricingAdapterContext,
|
|
14
|
+
OrderPricingCalculation,
|
|
15
|
+
IOrderPricingSheet
|
|
16
|
+
> = {
|
|
17
|
+
...basePricingAdapter,
|
|
18
|
+
|
|
19
|
+
isActivatedFor: () => {
|
|
20
|
+
return false;
|
|
21
|
+
},
|
|
22
|
+
|
|
23
|
+
actions: (params) => {
|
|
24
|
+
const { context } = params;
|
|
25
|
+
const { currency } = context;
|
|
26
|
+
const baseActions = basePricingAdapter.actions(params);
|
|
27
|
+
const resultSheet = OrderPricingSheet({ currency });
|
|
28
|
+
|
|
29
|
+
return {
|
|
30
|
+
...baseActions,
|
|
31
|
+
calculate: async () => {
|
|
32
|
+
const resultRaw = resultSheet.getRawPricingSheet();
|
|
33
|
+
resultRaw.forEach(({ amount, category }) =>
|
|
34
|
+
basePricingAdapter.log(`Order Pricing Calculation -> ${category} ${amount}`),
|
|
35
|
+
);
|
|
36
|
+
return resultRaw;
|
|
37
|
+
},
|
|
38
|
+
resultSheet: () => resultSheet,
|
|
39
|
+
};
|
|
40
|
+
},
|
|
41
|
+
};
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import {
|
|
2
|
+
IOrderPricingAdapter,
|
|
3
|
+
IOrderPricingDirector,
|
|
4
|
+
OrderPricingAdapterContext,
|
|
5
|
+
OrderPricingCalculation,
|
|
6
|
+
OrderPricingContext,
|
|
7
|
+
} from '@unchainedshop/types/orders.pricing';
|
|
8
|
+
import { BasePricingDirector } from '@unchainedshop/utils';
|
|
9
|
+
import { OrderPricingSheet } from './OrderPricingSheet';
|
|
10
|
+
|
|
11
|
+
const baseDirector = BasePricingDirector<
|
|
12
|
+
OrderPricingContext,
|
|
13
|
+
OrderPricingAdapterContext,
|
|
14
|
+
OrderPricingCalculation,
|
|
15
|
+
IOrderPricingAdapter
|
|
16
|
+
>('OrderPricingDirector');
|
|
17
|
+
|
|
18
|
+
export const OrderPricingDirector: IOrderPricingDirector = {
|
|
19
|
+
...baseDirector,
|
|
20
|
+
|
|
21
|
+
buildPricingContext: async ({ order, ...context }, requestContext) => {
|
|
22
|
+
const { modules } = requestContext;
|
|
23
|
+
const user = await modules.users.findUserById(order.userId);
|
|
24
|
+
|
|
25
|
+
const orderDelivery = await modules.orders.deliveries.findDelivery({
|
|
26
|
+
orderDeliveryId: order.deliveryId,
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
const orderPayment = await modules.orders.payments.findOrderPayment({
|
|
30
|
+
orderPaymentId: order.paymentId,
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
const orderPositions = await modules.orders.positions.findOrderPositions({
|
|
34
|
+
orderId: order._id,
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
const discounts = await modules.orders.discounts.findOrderDiscounts({
|
|
38
|
+
orderId: order._id,
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
return {
|
|
42
|
+
country: order.countryCode,
|
|
43
|
+
currency: order.currency,
|
|
44
|
+
...context,
|
|
45
|
+
...requestContext,
|
|
46
|
+
discounts,
|
|
47
|
+
order,
|
|
48
|
+
orderDelivery,
|
|
49
|
+
orderPayment,
|
|
50
|
+
orderPositions,
|
|
51
|
+
user,
|
|
52
|
+
};
|
|
53
|
+
},
|
|
54
|
+
|
|
55
|
+
async actions(pricingContext, requestContext) {
|
|
56
|
+
const actions = await baseDirector.actions(pricingContext, requestContext, this.buildPricingContext);
|
|
57
|
+
return {
|
|
58
|
+
...actions,
|
|
59
|
+
calculationSheet() {
|
|
60
|
+
const context = actions.getContext();
|
|
61
|
+
return OrderPricingSheet({
|
|
62
|
+
calculation: actions.getCalculation(),
|
|
63
|
+
currency: context.currency,
|
|
64
|
+
});
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
},
|
|
68
|
+
};
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { BasePricingSheet } from '@unchainedshop/utils';
|
|
2
|
+
import {
|
|
3
|
+
OrderPricingCalculation,
|
|
4
|
+
OrderPricingRowCategory,
|
|
5
|
+
IOrderPricingSheet,
|
|
6
|
+
} from '@unchainedshop/types/orders.pricing';
|
|
7
|
+
import { IBasePricingSheet, PricingSheetParams } from '@unchainedshop/types/pricing';
|
|
8
|
+
|
|
9
|
+
export const OrderPricingSheet = (
|
|
10
|
+
params: PricingSheetParams<OrderPricingCalculation>,
|
|
11
|
+
): IOrderPricingSheet => {
|
|
12
|
+
const basePricingSheet: IBasePricingSheet<OrderPricingCalculation> = BasePricingSheet(params);
|
|
13
|
+
|
|
14
|
+
const pricingSheet: IOrderPricingSheet = {
|
|
15
|
+
...basePricingSheet,
|
|
16
|
+
|
|
17
|
+
addItems({ amount, meta }) {
|
|
18
|
+
basePricingSheet.calculation.push({
|
|
19
|
+
category: OrderPricingRowCategory.Items,
|
|
20
|
+
amount,
|
|
21
|
+
meta,
|
|
22
|
+
});
|
|
23
|
+
},
|
|
24
|
+
|
|
25
|
+
addDiscount({ amount, discountId, meta }: { amount: number; discountId: string; meta?: any }) {
|
|
26
|
+
basePricingSheet.calculation.push({
|
|
27
|
+
category: OrderPricingRowCategory.Discounts,
|
|
28
|
+
amount,
|
|
29
|
+
discountId,
|
|
30
|
+
meta,
|
|
31
|
+
});
|
|
32
|
+
},
|
|
33
|
+
|
|
34
|
+
addTax({ amount, meta }) {
|
|
35
|
+
basePricingSheet.calculation.push({
|
|
36
|
+
category: OrderPricingRowCategory.Taxes,
|
|
37
|
+
amount,
|
|
38
|
+
meta,
|
|
39
|
+
});
|
|
40
|
+
},
|
|
41
|
+
|
|
42
|
+
addDelivery({ amount, meta }) {
|
|
43
|
+
basePricingSheet.calculation.push({
|
|
44
|
+
category: OrderPricingRowCategory.Delivery,
|
|
45
|
+
amount,
|
|
46
|
+
meta,
|
|
47
|
+
});
|
|
48
|
+
},
|
|
49
|
+
|
|
50
|
+
addPayment({ amount, meta }) {
|
|
51
|
+
basePricingSheet.calculation.push({
|
|
52
|
+
category: OrderPricingRowCategory.Payment,
|
|
53
|
+
amount,
|
|
54
|
+
meta,
|
|
55
|
+
});
|
|
56
|
+
},
|
|
57
|
+
|
|
58
|
+
gross() {
|
|
59
|
+
// tax is included 2 times, this is only true for Order Pricing!
|
|
60
|
+
return basePricingSheet.sum() - pricingSheet.taxSum();
|
|
61
|
+
},
|
|
62
|
+
|
|
63
|
+
taxSum() {
|
|
64
|
+
return basePricingSheet.sum({
|
|
65
|
+
category: OrderPricingRowCategory.Taxes,
|
|
66
|
+
});
|
|
67
|
+
},
|
|
68
|
+
|
|
69
|
+
itemsSum() {
|
|
70
|
+
return basePricingSheet.sum({
|
|
71
|
+
category: OrderPricingRowCategory.Items,
|
|
72
|
+
});
|
|
73
|
+
},
|
|
74
|
+
|
|
75
|
+
discountSum(discountId) {
|
|
76
|
+
return basePricingSheet.sum({
|
|
77
|
+
category: OrderPricingRowCategory.Discounts,
|
|
78
|
+
discountId,
|
|
79
|
+
});
|
|
80
|
+
},
|
|
81
|
+
|
|
82
|
+
discountPrices(explicitDiscountId) {
|
|
83
|
+
const discountIds = pricingSheet
|
|
84
|
+
.getDiscountRows(explicitDiscountId)
|
|
85
|
+
.map(({ discountId }) => discountId);
|
|
86
|
+
|
|
87
|
+
return [...new Set(discountIds)]
|
|
88
|
+
.map((discountId) => {
|
|
89
|
+
const amount = basePricingSheet.sum({
|
|
90
|
+
category: OrderPricingRowCategory.Discounts,
|
|
91
|
+
discountId,
|
|
92
|
+
});
|
|
93
|
+
if (!amount) {
|
|
94
|
+
return null;
|
|
95
|
+
}
|
|
96
|
+
return {
|
|
97
|
+
discountId,
|
|
98
|
+
amount: Math.round(amount),
|
|
99
|
+
currency: basePricingSheet.currency,
|
|
100
|
+
};
|
|
101
|
+
})
|
|
102
|
+
.filter(Boolean);
|
|
103
|
+
},
|
|
104
|
+
|
|
105
|
+
getDiscountRows(discountId) {
|
|
106
|
+
return basePricingSheet.filterBy({
|
|
107
|
+
category: OrderPricingRowCategory.Discounts,
|
|
108
|
+
discountId,
|
|
109
|
+
});
|
|
110
|
+
},
|
|
111
|
+
|
|
112
|
+
getItemsRows() {
|
|
113
|
+
return basePricingSheet.filterBy({
|
|
114
|
+
category: OrderPricingRowCategory.Items,
|
|
115
|
+
});
|
|
116
|
+
},
|
|
117
|
+
|
|
118
|
+
getTaxRows() {
|
|
119
|
+
return basePricingSheet.filterBy({
|
|
120
|
+
category: OrderPricingRowCategory.Taxes,
|
|
121
|
+
});
|
|
122
|
+
},
|
|
123
|
+
|
|
124
|
+
getDeliveryRows() {
|
|
125
|
+
return basePricingSheet.filterBy({
|
|
126
|
+
category: OrderPricingRowCategory.Delivery,
|
|
127
|
+
});
|
|
128
|
+
},
|
|
129
|
+
|
|
130
|
+
getPaymentRows() {
|
|
131
|
+
return basePricingSheet.filterBy({
|
|
132
|
+
category: OrderPricingRowCategory.Payment,
|
|
133
|
+
});
|
|
134
|
+
},
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
return pricingSheet;
|
|
138
|
+
};
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
import { Collection, Filter, ModuleMutations, Update } from '@unchainedshop/types/common';
|
|
2
|
+
import { OrdersModule } from '@unchainedshop/types/orders';
|
|
3
|
+
import {
|
|
4
|
+
OrderDeliveriesModule,
|
|
5
|
+
OrderDelivery,
|
|
6
|
+
OrderDeliveryStatus,
|
|
7
|
+
} from '@unchainedshop/types/orders.deliveries';
|
|
8
|
+
import { emit, registerEvents } from '@unchainedshop/events';
|
|
9
|
+
import { log } from '@unchainedshop/logger';
|
|
10
|
+
import { generateDbFilterById, generateDbMutations } from '@unchainedshop/utils';
|
|
11
|
+
import { OrderDeliveriesSchema } from '../db/OrderDeliveriesSchema';
|
|
12
|
+
|
|
13
|
+
const ORDER_DELIVERY_EVENTS: string[] = ['ORDER_DELIVER', 'ORDER_UPDATE_DELIVERY'];
|
|
14
|
+
|
|
15
|
+
const buildFindByIdSelector = (orderDeliveryId: string) =>
|
|
16
|
+
generateDbFilterById(orderDeliveryId) as Filter<OrderDelivery>;
|
|
17
|
+
|
|
18
|
+
export const configureOrderDeliveriesModule = ({
|
|
19
|
+
OrderDeliveries,
|
|
20
|
+
updateCalculation,
|
|
21
|
+
}: {
|
|
22
|
+
OrderDeliveries: Collection<OrderDelivery>;
|
|
23
|
+
updateCalculation: OrdersModule['updateCalculation'];
|
|
24
|
+
}): OrderDeliveriesModule => {
|
|
25
|
+
registerEvents(ORDER_DELIVERY_EVENTS);
|
|
26
|
+
|
|
27
|
+
const mutations = generateDbMutations<OrderDelivery>(OrderDeliveries, OrderDeliveriesSchema, {
|
|
28
|
+
permanentlyDeleteByDefault: true,
|
|
29
|
+
hasCreateOnly: false,
|
|
30
|
+
}) as ModuleMutations<OrderDelivery>;
|
|
31
|
+
|
|
32
|
+
const normalizedStatus: OrderDeliveriesModule['normalizedStatus'] = (orderDelivery) => {
|
|
33
|
+
return orderDelivery.status === null
|
|
34
|
+
? OrderDeliveryStatus.OPEN
|
|
35
|
+
: (orderDelivery.status as OrderDeliveryStatus);
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
const updateStatus: OrderDeliveriesModule['updateStatus'] = async (
|
|
39
|
+
orderDeliveryId,
|
|
40
|
+
{ status, info },
|
|
41
|
+
userId,
|
|
42
|
+
) => {
|
|
43
|
+
log(`OrderDelivery ${orderDeliveryId} -> New Status: ${status}`);
|
|
44
|
+
|
|
45
|
+
const date = new Date();
|
|
46
|
+
const modifier: Update<OrderDelivery> = {
|
|
47
|
+
$set: { status, updated: new Date(), updatedBy: userId },
|
|
48
|
+
$push: {
|
|
49
|
+
log: {
|
|
50
|
+
date,
|
|
51
|
+
status,
|
|
52
|
+
info,
|
|
53
|
+
},
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
if (status === OrderDeliveryStatus.DELIVERED) {
|
|
57
|
+
modifier.$set.delivered = date;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const selector = buildFindByIdSelector(orderDeliveryId);
|
|
61
|
+
await OrderDeliveries.updateOne(selector, modifier);
|
|
62
|
+
return OrderDeliveries.findOne(selector, {});
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
return {
|
|
66
|
+
// Queries
|
|
67
|
+
findDelivery: async ({ orderDeliveryId }, options) => {
|
|
68
|
+
return OrderDeliveries.findOne(buildFindByIdSelector(orderDeliveryId), options);
|
|
69
|
+
},
|
|
70
|
+
|
|
71
|
+
// Transformations
|
|
72
|
+
discounts: (orderDelivery, { order, orderDiscount }, context) => {
|
|
73
|
+
const { modules } = context;
|
|
74
|
+
if (!orderDelivery) return [];
|
|
75
|
+
|
|
76
|
+
const pricingSheet = modules.orders.deliveries.pricingSheet(
|
|
77
|
+
orderDelivery,
|
|
78
|
+
order.currency,
|
|
79
|
+
context,
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
return pricingSheet.discountPrices(orderDiscount._id).map((discount) => ({
|
|
83
|
+
delivery: orderDelivery,
|
|
84
|
+
...discount,
|
|
85
|
+
}));
|
|
86
|
+
},
|
|
87
|
+
|
|
88
|
+
isBlockingOrderConfirmation: async (orderDelivery, requestContext) => {
|
|
89
|
+
const provider = await requestContext.modules.delivery.findProvider({
|
|
90
|
+
deliveryProviderId: orderDelivery.deliveryProviderId,
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
const isAutoReleaseAllowed = await requestContext.modules.delivery.isAutoReleaseAllowed(
|
|
94
|
+
provider,
|
|
95
|
+
requestContext,
|
|
96
|
+
);
|
|
97
|
+
|
|
98
|
+
return !isAutoReleaseAllowed;
|
|
99
|
+
},
|
|
100
|
+
isBlockingOrderFullfillment: (orderDelivery) => {
|
|
101
|
+
if (orderDelivery.status === OrderDeliveryStatus.DELIVERED) return false;
|
|
102
|
+
return true;
|
|
103
|
+
},
|
|
104
|
+
|
|
105
|
+
normalizedStatus,
|
|
106
|
+
|
|
107
|
+
pricingSheet: (orderDelivery, currency, { modules }) => {
|
|
108
|
+
return modules.delivery.pricingSheet({
|
|
109
|
+
calculation: orderDelivery.calculation,
|
|
110
|
+
currency,
|
|
111
|
+
});
|
|
112
|
+
},
|
|
113
|
+
|
|
114
|
+
// Mutations
|
|
115
|
+
|
|
116
|
+
create: async (doc, userId) => {
|
|
117
|
+
const orderDeliveryId = await mutations.create(
|
|
118
|
+
{ ...doc, context: doc.context || {}, status: null },
|
|
119
|
+
userId,
|
|
120
|
+
);
|
|
121
|
+
|
|
122
|
+
const orderDelivery = await OrderDeliveries.findOne(buildFindByIdSelector(orderDeliveryId));
|
|
123
|
+
return orderDelivery;
|
|
124
|
+
},
|
|
125
|
+
|
|
126
|
+
delete: async (orderDeliveryId, userId) => {
|
|
127
|
+
const deletedCount = await mutations.delete(orderDeliveryId, userId);
|
|
128
|
+
return deletedCount;
|
|
129
|
+
},
|
|
130
|
+
|
|
131
|
+
markAsDelivered: async (orderDelivery, userId) => {
|
|
132
|
+
if (normalizedStatus(orderDelivery) !== OrderDeliveryStatus.OPEN) return;
|
|
133
|
+
const updatedOrderDelivery = await updateStatus(
|
|
134
|
+
orderDelivery._id,
|
|
135
|
+
{
|
|
136
|
+
status: OrderDeliveryStatus.DELIVERED,
|
|
137
|
+
info: 'mark delivered manually',
|
|
138
|
+
},
|
|
139
|
+
userId,
|
|
140
|
+
);
|
|
141
|
+
emit('ORDER_DELIVER', { orderDelivery: updatedOrderDelivery });
|
|
142
|
+
},
|
|
143
|
+
|
|
144
|
+
send: async (orderDelivery, { order, deliveryContext }, requestContext) => {
|
|
145
|
+
if (normalizedStatus(orderDelivery) !== OrderDeliveryStatus.OPEN) return orderDelivery;
|
|
146
|
+
|
|
147
|
+
const deliveryProvider = await requestContext.modules.delivery.findProvider({
|
|
148
|
+
deliveryProviderId: orderDelivery.deliveryProviderId,
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
const deliveryProviderId = deliveryProvider._id;
|
|
152
|
+
|
|
153
|
+
const address = orderDelivery.context?.address || order || order.billingAddress || {};
|
|
154
|
+
|
|
155
|
+
const arbitraryResponseData = await requestContext.modules.delivery.send(
|
|
156
|
+
deliveryProviderId,
|
|
157
|
+
{
|
|
158
|
+
order,
|
|
159
|
+
orderDelivery,
|
|
160
|
+
transactionContext: {
|
|
161
|
+
...(deliveryContext || {}),
|
|
162
|
+
...(orderDelivery.context || {}),
|
|
163
|
+
address,
|
|
164
|
+
},
|
|
165
|
+
},
|
|
166
|
+
requestContext,
|
|
167
|
+
);
|
|
168
|
+
|
|
169
|
+
if (arbitraryResponseData) {
|
|
170
|
+
return updateStatus(
|
|
171
|
+
orderDelivery._id,
|
|
172
|
+
{
|
|
173
|
+
status: OrderDeliveryStatus.DELIVERED,
|
|
174
|
+
info: JSON.stringify(arbitraryResponseData),
|
|
175
|
+
},
|
|
176
|
+
requestContext.userId,
|
|
177
|
+
);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
return orderDelivery;
|
|
181
|
+
},
|
|
182
|
+
|
|
183
|
+
updateContext: async (orderDeliveryId, context, requestContext) => {
|
|
184
|
+
const selector = buildFindByIdSelector(orderDeliveryId);
|
|
185
|
+
const orderDelivery = await OrderDeliveries.findOne(selector, {});
|
|
186
|
+
const { orderId } = orderDelivery;
|
|
187
|
+
|
|
188
|
+
log(`OrderDelivery ${orderDeliveryId} -> Update Context`, {
|
|
189
|
+
orderId,
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
const result = await OrderDeliveries.updateOne(selector, {
|
|
193
|
+
$set: {
|
|
194
|
+
context: { ...(orderDelivery.context || {}), ...context },
|
|
195
|
+
updated: new Date(),
|
|
196
|
+
updatedBy: requestContext.userId,
|
|
197
|
+
},
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
if (result.modifiedCount) {
|
|
201
|
+
await updateCalculation(orderId, requestContext);
|
|
202
|
+
emit('ORDER_UPDATE_DELIVERY', {
|
|
203
|
+
orderDelivery: {
|
|
204
|
+
...orderDelivery,
|
|
205
|
+
context: { ...(orderDelivery.context || {}), ...context },
|
|
206
|
+
},
|
|
207
|
+
});
|
|
208
|
+
return true;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
return false;
|
|
212
|
+
},
|
|
213
|
+
|
|
214
|
+
updateStatus,
|
|
215
|
+
|
|
216
|
+
updateCalculation: async (orderDelivery, requestContext) => {
|
|
217
|
+
log(`OrderDelivery ${orderDelivery._id} -> Update Calculation`, {
|
|
218
|
+
orderId: orderDelivery.orderId,
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
const calculation = await requestContext.modules.delivery.calculate(
|
|
222
|
+
{
|
|
223
|
+
item: orderDelivery,
|
|
224
|
+
},
|
|
225
|
+
requestContext,
|
|
226
|
+
);
|
|
227
|
+
|
|
228
|
+
const selector = buildFindByIdSelector(orderDelivery._id);
|
|
229
|
+
await OrderDeliveries.updateOne(selector, {
|
|
230
|
+
$set: {
|
|
231
|
+
calculation,
|
|
232
|
+
updated: new Date(),
|
|
233
|
+
updatedBy: requestContext.userId,
|
|
234
|
+
},
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
return OrderDeliveries.findOne(selector);
|
|
238
|
+
},
|
|
239
|
+
};
|
|
240
|
+
};
|