@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.
Files changed (137) hide show
  1. package/.npm/package/README +7 -0
  2. package/.npm/package/npm-shrinkwrap.json +190 -0
  3. package/.versions +54 -0
  4. package/README.md +5 -0
  5. package/lib/db/OrderDeliveriesCollection.d.ts +3 -0
  6. package/lib/db/OrderDeliveriesCollection.js +8 -0
  7. package/lib/db/OrderDeliveriesCollection.js.map +1 -0
  8. package/lib/db/OrderDeliveriesSchema.d.ts +1 -0
  9. package/lib/db/OrderDeliveriesSchema.js +20 -0
  10. package/lib/db/OrderDeliveriesSchema.js.map +1 -0
  11. package/lib/db/OrderDiscountTrigger.d.ts +4 -0
  12. package/lib/db/OrderDiscountTrigger.js +6 -0
  13. package/lib/db/OrderDiscountTrigger.js.map +1 -0
  14. package/lib/db/OrderDiscountsCollection.d.ts +3 -0
  15. package/lib/db/OrderDiscountsCollection.js +11 -0
  16. package/lib/db/OrderDiscountsCollection.js.map +1 -0
  17. package/lib/db/OrderDiscountsSchema.d.ts +1 -0
  18. package/lib/db/OrderDiscountsSchema.js +12 -0
  19. package/lib/db/OrderDiscountsSchema.js.map +1 -0
  20. package/lib/db/OrderPaymentsCollection.d.ts +3 -0
  21. package/lib/db/OrderPaymentsCollection.js +8 -0
  22. package/lib/db/OrderPaymentsCollection.js.map +1 -0
  23. package/lib/db/OrderPaymentsSchema.d.ts +1 -0
  24. package/lib/db/OrderPaymentsSchema.js +20 -0
  25. package/lib/db/OrderPaymentsSchema.js.map +1 -0
  26. package/lib/db/OrderPositionsCollection.d.ts +3 -0
  27. package/lib/db/OrderPositionsCollection.js +11 -0
  28. package/lib/db/OrderPositionsCollection.js.map +1 -0
  29. package/lib/db/OrderPositionsSchema.d.ts +1 -0
  30. package/lib/db/OrderPositionsSchema.js +37 -0
  31. package/lib/db/OrderPositionsSchema.js.map +1 -0
  32. package/lib/db/OrdersCollection.d.ts +3 -0
  33. package/lib/db/OrdersCollection.js +33 -0
  34. package/lib/db/OrdersCollection.js.map +1 -0
  35. package/lib/db/OrdersSchema.d.ts +1 -0
  36. package/lib/db/OrdersSchema.js +30 -0
  37. package/lib/db/OrdersSchema.js.map +1 -0
  38. package/lib/director/OrderDiscountAdapter.d.ts +2 -0
  39. package/lib/director/OrderDiscountAdapter.js +3 -0
  40. package/lib/director/OrderDiscountAdapter.js.map +1 -0
  41. package/lib/director/OrderDiscountDirector.d.ts +2 -0
  42. package/lib/director/OrderDiscountDirector.js +3 -0
  43. package/lib/director/OrderDiscountDirector.js.map +1 -0
  44. package/lib/director/OrderPricingAdapter.d.ts +3 -0
  45. package/lib/director/OrderPricingAdapter.js +25 -0
  46. package/lib/director/OrderPricingAdapter.js.map +1 -0
  47. package/lib/director/OrderPricingDirector.d.ts +2 -0
  48. package/lib/director/OrderPricingDirector.js +48 -0
  49. package/lib/director/OrderPricingDirector.js.map +1 -0
  50. package/lib/director/OrderPricingSheet.d.ts +3 -0
  51. package/lib/director/OrderPricingSheet.js +113 -0
  52. package/lib/director/OrderPricingSheet.js.map +1 -0
  53. package/lib/module/configureOrderDeliveriesModule.d.ts +7 -0
  54. package/lib/module/configureOrderDeliveriesModule.js +164 -0
  55. package/lib/module/configureOrderDeliveriesModule.js.map +1 -0
  56. package/lib/module/configureOrderDiscountsModule.d.ts +7 -0
  57. package/lib/module/configureOrderDiscountsModule.js +183 -0
  58. package/lib/module/configureOrderDiscountsModule.js.map +1 -0
  59. package/lib/module/configureOrderPaymentsModule.d.ts +7 -0
  60. package/lib/module/configureOrderPaymentsModule.js +229 -0
  61. package/lib/module/configureOrderPaymentsModule.js.map +1 -0
  62. package/lib/module/configureOrderPositionsModule.d.ts +7 -0
  63. package/lib/module/configureOrderPositionsModule.js +213 -0
  64. package/lib/module/configureOrderPositionsModule.js.map +1 -0
  65. package/lib/module/configureOrdersModule-mutations.d.ts +12 -0
  66. package/lib/module/configureOrdersModule-mutations.js +162 -0
  67. package/lib/module/configureOrdersModule-mutations.js.map +1 -0
  68. package/lib/module/configureOrdersModule-processing.d.ts +13 -0
  69. package/lib/module/configureOrdersModule-processing.js +343 -0
  70. package/lib/module/configureOrdersModule-processing.js.map +1 -0
  71. package/lib/module/configureOrdersModule-queries.d.ts +5 -0
  72. package/lib/module/configureOrdersModule-queries.js +55 -0
  73. package/lib/module/configureOrdersModule-queries.js.map +1 -0
  74. package/lib/module/configureOrdersModule-transformations.d.ts +5 -0
  75. package/lib/module/configureOrdersModule-transformations.js +108 -0
  76. package/lib/module/configureOrdersModule-transformations.js.map +1 -0
  77. package/lib/module/configureOrdersModule.d.ts +3 -0
  78. package/lib/module/configureOrdersModule.js +239 -0
  79. package/lib/module/configureOrdersModule.js.map +1 -0
  80. package/lib/orders-index.d.ts +11 -0
  81. package/lib/orders-index.js +12 -0
  82. package/lib/orders-index.js.map +1 -0
  83. package/lib/orders-settings.d.ts +20 -0
  84. package/lib/orders-settings.js +17 -0
  85. package/lib/orders-settings.js.map +1 -0
  86. package/lib/service/createUserCartService.d.ts +2 -0
  87. package/lib/service/createUserCartService.js +23 -0
  88. package/lib/service/createUserCartService.js.map +1 -0
  89. package/lib/service/migrateOrderCartService.d.ts +2 -0
  90. package/lib/service/migrateOrderCartService.js +10 -0
  91. package/lib/service/migrateOrderCartService.js.map +1 -0
  92. package/lib/service/orderServices.d.ts +2 -0
  93. package/lib/service/orderServices.js +7 -0
  94. package/lib/service/orderServices.js.map +1 -0
  95. package/package.js +33 -0
  96. package/package.json +43 -0
  97. package/plugins/discount-100-off.ts +44 -0
  98. package/plugins/discount-half-price-manual.ts +43 -0
  99. package/plugins/discount-half-price.ts +46 -0
  100. package/plugins/order-delivery.ts +45 -0
  101. package/plugins/order-discount.ts +158 -0
  102. package/plugins/order-items-discount.ts +116 -0
  103. package/plugins/order-items.ts +61 -0
  104. package/plugins/order-payment.ts +46 -0
  105. package/src/db/OrderDeliveriesCollection.ts +12 -0
  106. package/src/db/OrderDeliveriesSchema.js +24 -0
  107. package/src/db/OrderDiscountTrigger.ts +4 -0
  108. package/src/db/OrderDiscountsCollection.ts +15 -0
  109. package/src/db/OrderDiscountsSchema.js +15 -0
  110. package/src/db/OrderPaymentsCollection.ts +12 -0
  111. package/src/db/OrderPaymentsSchema.js +24 -0
  112. package/src/db/OrderPositionsCollection.ts +15 -0
  113. package/src/db/OrderPositionsSchema.js +41 -0
  114. package/src/db/OrdersCollection.ts +37 -0
  115. package/src/db/OrdersSchema.js +34 -0
  116. package/src/director/OrderDiscountAdapter.ts +5 -0
  117. package/src/director/OrderDiscountDirector.ts +4 -0
  118. package/src/director/OrderPricingAdapter.ts +41 -0
  119. package/src/director/OrderPricingDirector.ts +68 -0
  120. package/src/director/OrderPricingSheet.ts +138 -0
  121. package/src/module/configureOrderDeliveriesModule.ts +240 -0
  122. package/src/module/configureOrderDiscountsModule.ts +245 -0
  123. package/src/module/configureOrderPaymentsModule.ts +331 -0
  124. package/src/module/configureOrderPositionsModule.ts +329 -0
  125. package/src/module/configureOrdersModule-mutations.ts +230 -0
  126. package/src/module/configureOrdersModule-processing.ts +506 -0
  127. package/src/module/configureOrdersModule-queries.ts +71 -0
  128. package/src/module/configureOrdersModule-transformations.ts +145 -0
  129. package/src/module/configureOrdersModule.ts +359 -0
  130. package/src/orders-index.ts +15 -0
  131. package/src/orders-settings.ts +23 -0
  132. package/src/service/createUserCartService.ts +38 -0
  133. package/src/service/migrateOrderCartService.ts +22 -0
  134. package/src/service/orderServices.ts +8 -0
  135. package/tests/orders-index.test.ts +50 -0
  136. package/tsconfig.build.json +26 -0
  137. package/tsconfig.json +8 -0
@@ -0,0 +1,45 @@
1
+ import { IOrderPricingAdapter } from '@unchainedshop/types/orders.pricing';
2
+ import { OrderPricingDirector, OrderPricingAdapter } from 'meteor/unchained:core-orders';
3
+
4
+ const OrderDelivery: IOrderPricingAdapter = {
5
+ ...OrderPricingAdapter,
6
+
7
+ key: 'shop.unchained.pricing.order-delivery',
8
+ version: '1.0',
9
+ label: 'Add Delivery Fees to Order',
10
+ orderIndex: 10,
11
+
12
+ isActivatedFor: () => {
13
+ return true;
14
+ },
15
+
16
+ actions: (params) => {
17
+ const pricingAdapter = OrderPricingAdapter.actions(params);
18
+ const { order, orderDelivery, modules } = params.context;
19
+
20
+ return {
21
+ ...pricingAdapter,
22
+
23
+ calculate: async () => {
24
+ // just add tax + net price to order pricing
25
+ if (!orderDelivery) return null;
26
+ const pricing = modules.orders.deliveries.pricingSheet(
27
+ orderDelivery,
28
+ order.currency,
29
+ params.context,
30
+ );
31
+ const tax = pricing.taxSum();
32
+ const shipping = pricing.gross();
33
+
34
+ pricingAdapter.resultSheet().addDelivery({ amount: shipping });
35
+ if (tax !== 0) {
36
+ pricingAdapter.resultSheet().addTax({ amount: tax });
37
+ }
38
+
39
+ return pricingAdapter.calculate();
40
+ },
41
+ };
42
+ },
43
+ };
44
+
45
+ OrderPricingDirector.registerAdapter(OrderDelivery);
@@ -0,0 +1,158 @@
1
+ import { IOrderPricingAdapter, OrderPricingRowCategory } from '@unchainedshop/types/orders.pricing';
2
+ import { OrderPricingDirector, OrderPricingAdapter } from 'meteor/unchained:core-orders';
3
+
4
+ const resolveRatioAndTaxDivisorForPricingSheet = (pricing, total) => {
5
+ if (total === 0 || !pricing) {
6
+ return {
7
+ ratio: 1,
8
+ taxDivisor: 1,
9
+ };
10
+ }
11
+ const tax = pricing.taxSum();
12
+ const gross = pricing.gross();
13
+ if (gross - tax === 0) {
14
+ return {
15
+ ratio: 0,
16
+ taxDivisor: 0,
17
+ };
18
+ }
19
+ return {
20
+ ratio: gross / total,
21
+ taxDivisor: gross / (gross - tax),
22
+ };
23
+ };
24
+
25
+ const resolveAmountAndTax = ({ ratio, taxDivisor }, amount) => {
26
+ const shareAmount = Number.isFinite(ratio) ? amount * ratio : 0;
27
+ const shareTaxAmount =
28
+ Number.isFinite(taxDivisor) && taxDivisor !== 0 ? shareAmount - shareAmount / taxDivisor : 0;
29
+ return [shareAmount, shareTaxAmount];
30
+ };
31
+
32
+ const applyDiscountToMultipleShares = (shares, amount) => {
33
+ return shares.reduce(
34
+ ([currentDiscountAmount, currentTaxAmount], share) => {
35
+ const [shareAmount, shareTaxAmount] = resolveAmountAndTax(share, amount);
36
+ return [currentDiscountAmount + shareAmount, currentTaxAmount + shareTaxAmount];
37
+ },
38
+ [0, 0],
39
+ );
40
+ };
41
+
42
+ const calculateAmountToSplit = (configuration, amount) => {
43
+ const deductionAmount = configuration.rate ? amount * configuration.rate : configuration.fixedRate;
44
+
45
+ const leftInDiscount = Math.max(0, deductionAmount - (configuration.alreadyDeductedForDiscount || 0));
46
+ const leftToDeduct = Math.min(configuration.amountLeft, leftInDiscount);
47
+ return Math.max(0, leftToDeduct);
48
+ };
49
+
50
+ const OrderDiscount: IOrderPricingAdapter = {
51
+ ...OrderPricingAdapter,
52
+
53
+ key: 'shop.unchained.pricing.order-discount',
54
+ version: '1.0',
55
+ label: 'Apply Discounts on Total Order Value',
56
+ orderIndex: 90,
57
+
58
+ isActivatedFor: () => {
59
+ return true;
60
+ },
61
+
62
+ actions: (params) => {
63
+ const pricingAdapter = OrderPricingAdapter.actions(params);
64
+ const { order, orderDelivery, orderPositions, orderPayment, modules } = params.context;
65
+
66
+ return {
67
+ ...pricingAdapter,
68
+
69
+ calculate: async () => {
70
+ // discounts need to provide a *fixedRate*
71
+ // if you want to add percentual discounts,
72
+ // add it to the order item calculation
73
+ const totalAmountOfItems = params.calculationSheet.sum({
74
+ category: OrderPricingRowCategory.Items,
75
+ });
76
+ const totalAmountOfPaymentAndDelivery =
77
+ params.calculationSheet.sum({
78
+ category: OrderPricingRowCategory.Payment,
79
+ }) +
80
+ params.calculationSheet.sum({
81
+ category: OrderPricingRowCategory.Delivery,
82
+ });
83
+
84
+ const itemShares = orderPositions.map((orderPosition) =>
85
+ resolveRatioAndTaxDivisorForPricingSheet(
86
+ modules.orders.positions.pricingSheet(orderPosition, order.currency, params.context),
87
+ totalAmountOfItems,
88
+ ),
89
+ );
90
+
91
+ const deliveryShare = resolveRatioAndTaxDivisorForPricingSheet(
92
+ orderDelivery &&
93
+ modules.orders.deliveries.pricingSheet(orderDelivery, order.currency, params.context),
94
+ totalAmountOfPaymentAndDelivery,
95
+ );
96
+ const paymentShare = resolveRatioAndTaxDivisorForPricingSheet(
97
+ orderPayment &&
98
+ modules.orders.payments.pricingSheet(orderPayment, order.currency, params.context),
99
+ totalAmountOfPaymentAndDelivery,
100
+ );
101
+
102
+ let amountLeft = totalAmountOfPaymentAndDelivery + totalAmountOfItems;
103
+
104
+ params.discounts.forEach(({ configuration, discountId }) => {
105
+ // First, we deduce the discount from the items
106
+ let alreadyDeductedForDiscount = 0;
107
+ const [itemsDiscountAmount, itemsTaxAmount] = applyDiscountToMultipleShares(
108
+ itemShares,
109
+ calculateAmountToSplit(
110
+ { ...configuration, amountLeft, alreadyDeductedForDiscount },
111
+ totalAmountOfItems,
112
+ ),
113
+ );
114
+ amountLeft -= itemsDiscountAmount;
115
+ alreadyDeductedForDiscount += itemsDiscountAmount;
116
+
117
+ // After the items, we deduct the remaining discount from payment & delivery fees
118
+ const [deliveryAndPaymentDiscountAmount, deliveryAndPaymentTaxAmount] =
119
+ applyDiscountToMultipleShares(
120
+ [deliveryShare, paymentShare],
121
+ calculateAmountToSplit(
122
+ { ...configuration, amountLeft, alreadyDeductedForDiscount },
123
+ totalAmountOfPaymentAndDelivery,
124
+ ),
125
+ );
126
+ amountLeft -= deliveryAndPaymentDiscountAmount;
127
+ alreadyDeductedForDiscount += itemsDiscountAmount;
128
+
129
+ const discountAmount = itemsDiscountAmount + deliveryAndPaymentDiscountAmount;
130
+ const taxAmount = itemsTaxAmount + deliveryAndPaymentTaxAmount;
131
+
132
+ if (discountAmount) {
133
+ pricingAdapter.resultSheet().addDiscount({
134
+ amount: discountAmount * -1,
135
+ discountId,
136
+ meta: {
137
+ adapter: OrderDiscount.key,
138
+ },
139
+ });
140
+ if (taxAmount !== 0) {
141
+ pricingAdapter.resultSheet().addTax({
142
+ amount: taxAmount * -1,
143
+ meta: {
144
+ discountId,
145
+ adapter: OrderDiscount.key,
146
+ },
147
+ });
148
+ }
149
+ }
150
+ });
151
+
152
+ return pricingAdapter.calculate();
153
+ },
154
+ };
155
+ },
156
+ };
157
+
158
+ OrderPricingDirector.registerAdapter(OrderDiscount);
@@ -0,0 +1,116 @@
1
+ import { IOrderPricingAdapter, OrderPricingRowCategory } from '@unchainedshop/types/orders.pricing';
2
+ import { OrderPricingDirector, OrderPricingAdapter } from 'meteor/unchained:core-orders';
3
+
4
+ const resolveRatioAndTaxDivisorForPricingSheet = (pricing, total) => {
5
+ if (total === 0 || !pricing) {
6
+ return {
7
+ ratio: 1,
8
+ taxDivisor: 1,
9
+ };
10
+ }
11
+ const tax = pricing.taxSum();
12
+ const gross = pricing.gross();
13
+ return {
14
+ ratio: gross / total,
15
+ taxDivisor: gross / (gross - tax),
16
+ };
17
+ };
18
+
19
+ const resolveAmountAndTax = ({ ratio, taxDivisor }, amount) => {
20
+ const shareAmount = Number.isFinite(ratio) ? amount * ratio : 0;
21
+ const shareTaxAmount = Number.isFinite(taxDivisor) ? shareAmount - shareAmount / taxDivisor : 0;
22
+ return [shareAmount, shareTaxAmount];
23
+ };
24
+
25
+ const applyDiscountToMultipleShares = (shares, amount) => {
26
+ return shares.reduce(
27
+ ([currentDiscountAmount, currentTaxAmount], share) => {
28
+ const [shareAmount, shareTaxAmount] = resolveAmountAndTax(share, amount);
29
+ return [currentDiscountAmount + shareAmount, currentTaxAmount + shareTaxAmount];
30
+ },
31
+ [0, 0],
32
+ );
33
+ };
34
+
35
+ const calculateAmountToSplit = (configuration, amount) => {
36
+ const deductionAmount = configuration.rate
37
+ ? amount * configuration.rate
38
+ : Math.min(configuration.fixedRate, amount);
39
+
40
+ return Math.max(0, deductionAmount - (configuration.alreadyDeducted || 0));
41
+ };
42
+
43
+ const OrderItemsDiscount: IOrderPricingAdapter = {
44
+ ...OrderPricingAdapter,
45
+
46
+ key: 'shop.unchained.pricing.order-items-discount',
47
+ version: '1.0',
48
+ label: 'Apply Discounts on Total Value Of Goods',
49
+ orderIndex: 89,
50
+
51
+ isActivatedFor: () => {
52
+ return true;
53
+ },
54
+
55
+ actions: (params) => {
56
+ const pricingAdapter = OrderPricingAdapter.actions(params);
57
+ const { order, orderPositions, modules } = params.context;
58
+
59
+ return {
60
+ ...pricingAdapter,
61
+
62
+ calculate: async () => {
63
+ // discounts need to provide a *fixedRate*
64
+ // if you want to add percentual discounts,
65
+ // add it to the order item calculation
66
+
67
+ const totalAmountOfItems = params.calculationSheet.sum({
68
+ category: OrderPricingRowCategory.Items,
69
+ });
70
+
71
+ const itemShares = orderPositions.map((orderPosition) =>
72
+ resolveRatioAndTaxDivisorForPricingSheet(
73
+ modules.orders.positions.pricingSheet(orderPosition, order.currency, params.context),
74
+ totalAmountOfItems,
75
+ ),
76
+ );
77
+
78
+ let alreadyDeducted = 0;
79
+
80
+ params.discounts.forEach(({ configuration, discountId }) => {
81
+ // First, we deduce the discount from the items
82
+ const [itemsDiscountAmount, itemsTaxAmount] = applyDiscountToMultipleShares(
83
+ itemShares,
84
+ calculateAmountToSplit({ ...configuration, alreadyDeducted }, totalAmountOfItems),
85
+ );
86
+ alreadyDeducted = +itemsDiscountAmount;
87
+
88
+ const discountAmount = itemsDiscountAmount;
89
+ const taxAmount = itemsTaxAmount;
90
+ if (discountAmount) {
91
+ pricingAdapter.resultSheet().addDiscount({
92
+ amount: discountAmount * -1,
93
+ discountId,
94
+ meta: {
95
+ adapter: OrderItemsDiscount.key,
96
+ },
97
+ });
98
+ if (taxAmount !== 0) {
99
+ pricingAdapter.resultSheet().addTax({
100
+ amount: taxAmount * -1,
101
+ meta: {
102
+ discountId,
103
+ adapter: OrderItemsDiscount.key,
104
+ },
105
+ });
106
+ }
107
+ }
108
+ });
109
+
110
+ return pricingAdapter.calculate();
111
+ },
112
+ };
113
+ },
114
+ };
115
+
116
+ OrderPricingDirector.registerAdapter(OrderItemsDiscount);
@@ -0,0 +1,61 @@
1
+ import { IOrderPricingAdapter } from '@unchainedshop/types/orders.pricing';
2
+ import { OrderPricingDirector, OrderPricingAdapter } from 'meteor/unchained:core-orders';
3
+
4
+ const OrderItems: IOrderPricingAdapter = {
5
+ ...OrderPricingAdapter,
6
+
7
+ key: 'shop.unchained.pricing.order-items',
8
+ version: '1.0',
9
+ label: 'Add Total Value Of Goods to Order',
10
+ orderIndex: 0,
11
+
12
+ isActivatedFor: () => {
13
+ return true;
14
+ },
15
+
16
+ actions: (params) => {
17
+ const pricingAdapter = OrderPricingAdapter.actions(params);
18
+ const { order, orderPositions, modules } = params.context;
19
+
20
+ return {
21
+ ...pricingAdapter,
22
+
23
+ calculate: async () => {
24
+ // just sum up all products items prices, taxes & fees
25
+ const totalAndTaxesOfAllItems = orderPositions.reduce(
26
+ (current, orderPosition) => {
27
+ const pricing = modules.orders.positions.pricingSheet(
28
+ orderPosition,
29
+ order.currency,
30
+ params.context,
31
+ );
32
+ const tax = pricing.taxSum();
33
+ const items = pricing.gross();
34
+ return {
35
+ taxes: current.taxes + tax,
36
+ items: current.items + items,
37
+ };
38
+ },
39
+ {
40
+ taxes: 0,
41
+ items: 0,
42
+ },
43
+ );
44
+
45
+ pricingAdapter.resultSheet().addItems({
46
+ amount: totalAndTaxesOfAllItems.items,
47
+ });
48
+
49
+ if (totalAndTaxesOfAllItems.taxes !== 0) {
50
+ pricingAdapter.resultSheet().addTax({
51
+ amount: totalAndTaxesOfAllItems.taxes,
52
+ });
53
+ }
54
+
55
+ return pricingAdapter.calculate();
56
+ },
57
+ };
58
+ },
59
+ };
60
+
61
+ OrderPricingDirector.registerAdapter(OrderItems);
@@ -0,0 +1,46 @@
1
+ import { IOrderPricingAdapter } from '@unchainedshop/types/orders.pricing';
2
+ import { OrderPricingDirector, OrderPricingAdapter } from 'meteor/unchained:core-orders';
3
+
4
+ const OrderPayment: IOrderPricingAdapter = {
5
+ ...OrderPricingAdapter,
6
+
7
+ key: 'shop.unchained.pricing.order-payment',
8
+ version: '1.0',
9
+ label: 'Add Payment Fees to Order',
10
+ orderIndex: 20,
11
+
12
+ isActivatedFor: () => {
13
+ return true;
14
+ },
15
+
16
+ actions: (params) => {
17
+ const pricingAdapter = OrderPricingAdapter.actions(params);
18
+ const { order, orderPayment, modules } = params.context;
19
+
20
+ return {
21
+ ...pricingAdapter,
22
+
23
+ calculate: async () => {
24
+ // just add tax + net price to order pricing
25
+ if (!orderPayment) return null;
26
+
27
+ const pricing = modules.orders.payments.pricingSheet(
28
+ orderPayment,
29
+ order.currency,
30
+ params.context,
31
+ );
32
+ const tax = pricing.taxSum();
33
+ const paymentFees = pricing.gross();
34
+
35
+ pricingAdapter.resultSheet().addPayment({ amount: paymentFees });
36
+ if (tax !== 0) {
37
+ pricingAdapter.resultSheet().addTax({ amount: tax });
38
+ }
39
+
40
+ return pricingAdapter.calculate();
41
+ },
42
+ };
43
+ },
44
+ };
45
+
46
+ OrderPricingDirector.registerAdapter(OrderPayment);
@@ -0,0 +1,12 @@
1
+ import { Db } from '@unchainedshop/types/common';
2
+ import { OrderDelivery } from '@unchainedshop/types/orders.deliveries';
3
+ import { buildDbIndexes } from '@unchainedshop/utils';
4
+
5
+ export const OrderDeliveriesCollection = async (db: Db) => {
6
+ const OrderDeliveries = db.collection<OrderDelivery>('order_deliveries');
7
+
8
+ // Order Indexes
9
+ await buildDbIndexes<OrderDelivery>(OrderDeliveries, [{ index: { orderId: 1 } }]);
10
+
11
+ return OrderDeliveries;
12
+ };
@@ -0,0 +1,24 @@
1
+ import { Schemas } from '@unchainedshop/utils';
2
+ import SimpleSchema from 'simpl-schema';
3
+
4
+ const calculationFields = {
5
+ calculation: Array,
6
+ 'calculation.$': {
7
+ type: Object,
8
+ blackbox: true,
9
+ },
10
+ };
11
+
12
+ export const OrderDeliveriesSchema = new SimpleSchema(
13
+ {
14
+ delivered: Date,
15
+ deliveryProviderId: String,
16
+ orderId: String,
17
+ status: String,
18
+ ...calculationFields,
19
+ ...Schemas.contextFields,
20
+ ...Schemas.logFields,
21
+ ...Schemas.timestampFields,
22
+ },
23
+ { requiredByDefault: false },
24
+ );
@@ -0,0 +1,4 @@
1
+ export enum OrderDiscountTrigger {
2
+ USER = 'USER',
3
+ SYSTEM = 'SYSTEM',
4
+ }
@@ -0,0 +1,15 @@
1
+ import { Db } from '@unchainedshop/types/common';
2
+ import { OrderDiscount } from '@unchainedshop/types/orders.discounts';
3
+ import { buildDbIndexes } from '@unchainedshop/utils';
4
+
5
+ export const OrderDiscountsCollection = async (db: Db) => {
6
+ const OrderDiscounts = db.collection<OrderDiscount>('order_discounts');
7
+
8
+ // Order Indexes
9
+ await buildDbIndexes<OrderDiscount>(OrderDiscounts, [
10
+ { index: { orderId: 1 } },
11
+ { index: { trigger: 1 } },
12
+ ]);
13
+
14
+ return OrderDiscounts;
15
+ };
@@ -0,0 +1,15 @@
1
+ import { Schemas } from '@unchainedshop/utils';
2
+ import SimpleSchema from 'simpl-schema';
3
+
4
+ export const OrderDiscountsSchema = new SimpleSchema(
5
+ {
6
+ orderId: String,
7
+ code: String,
8
+ trigger: { type: String, required: true },
9
+ discountKey: { type: String, required: true },
10
+ reservation: { type: Object, blackbox: true },
11
+ ...Schemas.contextFields,
12
+ ...Schemas.timestampFields,
13
+ },
14
+ { requiredByDefault: false },
15
+ );
@@ -0,0 +1,12 @@
1
+ import { Db } from '@unchainedshop/types/common';
2
+ import { OrderPayment } from '@unchainedshop/types/orders.payments';
3
+ import { buildDbIndexes } from '@unchainedshop/utils';
4
+
5
+ export const OrderPaymentsCollection = async (db: Db) => {
6
+ const OrderPayments = db.collection<OrderPayment>('order_payments');
7
+
8
+ // Order Indexes
9
+ await buildDbIndexes<OrderPayment>(OrderPayments, [{ index: { orderId: 1 } }]);
10
+
11
+ return OrderPayments;
12
+ };
@@ -0,0 +1,24 @@
1
+ import { Schemas } from '@unchainedshop/utils';
2
+ import SimpleSchema from 'simpl-schema';
3
+
4
+ const calculationFields = {
5
+ calculation: Array,
6
+ 'calculation.$': {
7
+ type: Object,
8
+ blackbox: true,
9
+ },
10
+ };
11
+
12
+ export const OrderPaymentsSchema = new SimpleSchema(
13
+ {
14
+ orderId: String,
15
+ paymentProviderId: String,
16
+ paid: Date,
17
+ status: String,
18
+ ...calculationFields,
19
+ ...Schemas.contextFields,
20
+ ...Schemas.logFields,
21
+ ...Schemas.timestampFields,
22
+ },
23
+ { requiredByDefault: false },
24
+ );
@@ -0,0 +1,15 @@
1
+ import { Db } from '@unchainedshop/types/common';
2
+ import { OrderPosition } from '@unchainedshop/types/orders.positions';
3
+ import { buildDbIndexes } from '@unchainedshop/utils';
4
+
5
+ export const OrderPositionsCollection = async (db: Db) => {
6
+ const OrderPositions = db.collection<OrderPosition>('order_positions');
7
+
8
+ // Order Indexes
9
+ await buildDbIndexes<OrderPosition>(OrderPositions, [
10
+ { index: { productId: 1 } },
11
+ { index: { orderId: 1 } },
12
+ ]);
13
+
14
+ return OrderPositions;
15
+ };
@@ -0,0 +1,41 @@
1
+ import { Schemas } from '@unchainedshop/utils';
2
+ import SimpleSchema from 'simpl-schema';
3
+
4
+ const calculationFields = {
5
+ calculation: Array,
6
+ 'calculation.$': {
7
+ type: Object,
8
+ blackbox: true,
9
+ },
10
+ scheduling: Array,
11
+ 'scheduling.$': {
12
+ type: Object,
13
+ blackbox: true,
14
+ },
15
+ };
16
+
17
+ export const OrderPositionsSchema = new SimpleSchema(
18
+ {
19
+ productId: String,
20
+ orderId: String,
21
+ originalProductId: { type: String },
22
+ quotationId: { type: String },
23
+ quantity: Number,
24
+ configuration: Array,
25
+ 'configuration.$': {
26
+ type: Object,
27
+ required: true,
28
+ },
29
+ 'configuration.$.key': {
30
+ type: String,
31
+ required: true,
32
+ },
33
+ 'configuration.$.value': {
34
+ type: String,
35
+ },
36
+ ...calculationFields,
37
+ ...Schemas.contextFields,
38
+ ...Schemas.timestampFields,
39
+ },
40
+ { requiredByDefault: false },
41
+ );
@@ -0,0 +1,37 @@
1
+ import { Db } from '@unchainedshop/types/common';
2
+ import { buildDbIndexes } from '@unchainedshop/utils';
3
+ import { Order } from '@unchainedshop/types/orders';
4
+
5
+ export const OrdersCollection = async (db: Db) => {
6
+ const Orders = db.collection<Order>('orders');
7
+
8
+ // Order Indexes
9
+ await buildDbIndexes<Order>(Orders, [
10
+ { index: { userId: 1 } },
11
+ { index: { status: 1 } },
12
+ { index: { orderNumber: 1 } },
13
+ {
14
+ index: {
15
+ _id: 'text',
16
+ userId: 'text',
17
+ orderNumber: 'text',
18
+ status: 'text',
19
+ 'contact.emailAddress': 'text',
20
+ 'contact.telNumber': 'text',
21
+ } as any,
22
+ options: {
23
+ weights: {
24
+ _id: 8,
25
+ userId: 3,
26
+ orderNumber: 6,
27
+ 'contact.telNumber': 5,
28
+ 'contact.emailAddress': 4,
29
+ status: 1,
30
+ },
31
+ name: 'order_fulltext_search',
32
+ },
33
+ },
34
+ ]);
35
+
36
+ return Orders;
37
+ };
@@ -0,0 +1,34 @@
1
+ import { Schemas } from '@unchainedshop/utils';
2
+ import SimpleSchema from 'simpl-schema';
3
+
4
+ const calculationFields = {
5
+ calculation: Array,
6
+ 'calculation.$': {
7
+ type: Object,
8
+ blackbox: true,
9
+ },
10
+ };
11
+
12
+ export const OrdersSchema = new SimpleSchema(
13
+ {
14
+ billingAddress: Schemas.Address,
15
+ confirmed: Date,
16
+ rejected: Date,
17
+ contact: Schemas.Contact,
18
+ countryCode: String,
19
+ currency: String,
20
+ deliveryId: String,
21
+ fullfilled: Date,
22
+ ordered: Date,
23
+ orderNumber: String,
24
+ originEnrollmentId: String,
25
+ paymentId: String,
26
+ status: String,
27
+ userId: String,
28
+ ...calculationFields,
29
+ ...Schemas.contextFields,
30
+ ...Schemas.logFields,
31
+ ...Schemas.timestampFields,
32
+ },
33
+ { requiredByDefault: false },
34
+ );
@@ -0,0 +1,5 @@
1
+ import { IDiscountAdapter } from '@unchainedshop/types/discount';
2
+ import { BaseDiscountAdapter } from '@unchainedshop/utils';
3
+
4
+ export const OrderDiscountAdapter: Omit<IDiscountAdapter, 'key' | 'label' | 'version'> =
5
+ BaseDiscountAdapter;
@@ -0,0 +1,4 @@
1
+ import { IDiscountDirector } from '@unchainedshop/types/discount';
2
+ import { BaseDiscountDirector } from '@unchainedshop/utils';
3
+
4
+ export const OrderDiscountDirector: IDiscountDirector = BaseDiscountDirector('OrderDiscountDirector');