@darkpos/pricing 1.0.91 → 1.0.93
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/__TEST__/order/conditionsNotMet.test.js +124 -0
- package/__TEST__/order/getParentTotals.test.js +79 -0
- package/__TEST__/order/order-payment-modifier.test.js +332 -17
- package/__TEST__/order/order.test.js +60 -6
- package/__TEST__/order/pickEndDate.test.js +111 -25
- package/lib/item/calculate.js +10 -1
- package/lib/item/getTotalsDifference.js +7 -1
- package/lib/item/hasPaymentMethodType.js +7 -1
- package/lib/modifier/areConditionsMet.js +3 -0
- package/lib/modifier/calculatePaymentModifier.js +2 -0
- package/lib/modifier/hasPaymentMethodType.js +21 -6
- package/lib/modifier/index.js +2 -0
- package/lib/modifier/isPaymentModifier.js +2 -1
- package/lib/modifier/isPercentage.js +1 -1
- package/lib/modifier/isPrepayModifier.js +11 -0
- package/lib/modifier/sort.js +16 -27
- package/lib/order/calculate.js +2 -7
- package/lib/order/getTotals.js +21 -22
- package/lib/order/index.js +0 -2
- package/lib/order/splitItems.js +2 -1
- package/lib/store/getScheduleByCustomer.js +25 -0
- package/lib/store/index.js +6 -4
- package/lib/store/pickEndDate.js +40 -6
- package/lib/store/pickEndDateByCustomer.js +5 -0
- package/package.json +3 -2
- package/__TEST__/order/getRecommendedEndDate.test.js +0 -96
- package/lib/order/getScheduleByCustomer.js +0 -19
- package/lib/store/getRecommendedEndDate.js +0 -25
|
@@ -884,4 +884,128 @@ describe('Conditions not met for the item', () => {
|
|
|
884
884
|
expect(order.items[1].total).toBe(20);
|
|
885
885
|
expect(order.items[1].modifiers.length).toBe(1);
|
|
886
886
|
});
|
|
887
|
+
|
|
888
|
+
test('Payment type condition is met (payment modifier and using prepay at the same time)', () => {
|
|
889
|
+
const prePayDiscount1Usd = {
|
|
890
|
+
_id: '6819114c06c23d37c1f19412',
|
|
891
|
+
name: '1 usd pre pay disc',
|
|
892
|
+
type: 'discount',
|
|
893
|
+
tags: ['default'],
|
|
894
|
+
direct: true,
|
|
895
|
+
conditions: {
|
|
896
|
+
valid: null,
|
|
897
|
+
rules: [
|
|
898
|
+
{
|
|
899
|
+
key: 'paymentMethods',
|
|
900
|
+
value: ['cash'],
|
|
901
|
+
operand: '$in',
|
|
902
|
+
},
|
|
903
|
+
{
|
|
904
|
+
key: 'paymentTypes',
|
|
905
|
+
value: ['cash'],
|
|
906
|
+
operand: '$in',
|
|
907
|
+
},
|
|
908
|
+
{
|
|
909
|
+
key: 'payment',
|
|
910
|
+
value: 'prepay',
|
|
911
|
+
operand: '$in',
|
|
912
|
+
},
|
|
913
|
+
],
|
|
914
|
+
},
|
|
915
|
+
compute: {
|
|
916
|
+
type: 'fixed',
|
|
917
|
+
action: 'subtract',
|
|
918
|
+
amount: 1,
|
|
919
|
+
},
|
|
920
|
+
};
|
|
921
|
+
|
|
922
|
+
const item = {
|
|
923
|
+
_id: 'abc',
|
|
924
|
+
price: 10,
|
|
925
|
+
quantity: 1,
|
|
926
|
+
modifiers: [prePayDiscount1Usd],
|
|
927
|
+
};
|
|
928
|
+
|
|
929
|
+
const resultedOrder = pricingService.order.calculate(
|
|
930
|
+
{
|
|
931
|
+
id: 'ord-123',
|
|
932
|
+
items: [item],
|
|
933
|
+
modifiers: [],
|
|
934
|
+
},
|
|
935
|
+
{
|
|
936
|
+
paymentMethod: 'cash',
|
|
937
|
+
paymentType: 'cash',
|
|
938
|
+
}
|
|
939
|
+
);
|
|
940
|
+
|
|
941
|
+
expect(resultedOrder.total).toBe(10);
|
|
942
|
+
expect(resultedOrder.items[0].total).toBe(10);
|
|
943
|
+
expect(resultedOrder.items[0].modifiers[0]._computed.amount).toBe(0);
|
|
944
|
+
|
|
945
|
+
const resultedOrderPrepay = pricingService.order.calculate(resultedOrder, {
|
|
946
|
+
paymentMethod: 'cash',
|
|
947
|
+
paymentType: 'cash',
|
|
948
|
+
isPrepay: true,
|
|
949
|
+
});
|
|
950
|
+
|
|
951
|
+
expect(resultedOrderPrepay.total).toBe(9);
|
|
952
|
+
expect(resultedOrderPrepay.items[0].total).toBe(9);
|
|
953
|
+
expect(resultedOrderPrepay.items[0].modifiers[0]._computed.amount).toBe(0);
|
|
954
|
+
expect(resultedOrderPrepay.items[0].modifiers[1]._computed.amount).toBe(-1);
|
|
955
|
+
});
|
|
956
|
+
|
|
957
|
+
test('Payment type condition is met (using only prepay rule)', () => {
|
|
958
|
+
const prePayDiscount1Usd = {
|
|
959
|
+
_id: '6819114c06c23d37c1f19412',
|
|
960
|
+
name: '1 usd pre pay disc',
|
|
961
|
+
type: 'discount',
|
|
962
|
+
tags: ['default'],
|
|
963
|
+
direct: true,
|
|
964
|
+
conditions: {
|
|
965
|
+
valid: null,
|
|
966
|
+
rules: [
|
|
967
|
+
{
|
|
968
|
+
key: 'payment',
|
|
969
|
+
value: 'prepay',
|
|
970
|
+
operand: '$in',
|
|
971
|
+
},
|
|
972
|
+
],
|
|
973
|
+
},
|
|
974
|
+
compute: {
|
|
975
|
+
type: 'fixed',
|
|
976
|
+
action: 'subtract',
|
|
977
|
+
amount: 1,
|
|
978
|
+
},
|
|
979
|
+
};
|
|
980
|
+
|
|
981
|
+
const item = {
|
|
982
|
+
_id: 'abc',
|
|
983
|
+
price: 10,
|
|
984
|
+
quantity: 1,
|
|
985
|
+
modifiers: [prePayDiscount1Usd],
|
|
986
|
+
};
|
|
987
|
+
|
|
988
|
+
const resultedOrder = pricingService.order.calculate({
|
|
989
|
+
id: 'ord-123',
|
|
990
|
+
items: [item],
|
|
991
|
+
modifiers: [],
|
|
992
|
+
});
|
|
993
|
+
|
|
994
|
+
expect(resultedOrder.total).toBe(10);
|
|
995
|
+
expect(resultedOrder.items[0].total).toBe(10);
|
|
996
|
+
expect(resultedOrder.items[0].modifiers[0]._computed.amount).toBe(0);
|
|
997
|
+
|
|
998
|
+
const resultedOrderPrepay = pricingService.order.calculate(resultedOrder, {
|
|
999
|
+
isPrepay: true,
|
|
1000
|
+
});
|
|
1001
|
+
|
|
1002
|
+
expect(resultedOrderPrepay.total).toBe(9);
|
|
1003
|
+
expect(resultedOrderPrepay.items[0].total).toBe(9);
|
|
1004
|
+
expect(resultedOrderPrepay.items[0].modifiers[0]._computed.amount).toBe(0);
|
|
1005
|
+
expect(resultedOrderPrepay.items[0].modifiers[1]._computed.amount).toBe(-1);
|
|
1006
|
+
expect(resultedOrderPrepay.items[0].modifiers[1].properties).toMatchObject({
|
|
1007
|
+
isCalculatedPaymentModifier: true,
|
|
1008
|
+
paymentId: undefined,
|
|
1009
|
+
});
|
|
1010
|
+
});
|
|
887
1011
|
});
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
const usePricing = require('../../index');
|
|
2
|
+
const mockStores = require('../mocks/stores');
|
|
3
|
+
|
|
4
|
+
const session = {
|
|
5
|
+
store: mockStores[0],
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
const pricingService = usePricing(session);
|
|
9
|
+
describe('Get parent totals', () => {
|
|
10
|
+
it('should return zero totals if input is not an array', () => {
|
|
11
|
+
expect(pricingService.order.getTotals(null)).toEqual({
|
|
12
|
+
total: 0,
|
|
13
|
+
subTotal: 0,
|
|
14
|
+
subTotals: {},
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it('should calculate totals for simple sub-orders', () => {
|
|
19
|
+
const orders = [
|
|
20
|
+
{
|
|
21
|
+
parentId: '123',
|
|
22
|
+
total: 10,
|
|
23
|
+
subTotal: 8,
|
|
24
|
+
subTotals: {
|
|
25
|
+
base: 8,
|
|
26
|
+
tax: 2,
|
|
27
|
+
},
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
parentId: '123',
|
|
31
|
+
total: 20,
|
|
32
|
+
subTotal: 18,
|
|
33
|
+
subTotals: {
|
|
34
|
+
base: 18,
|
|
35
|
+
tax: 2,
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
];
|
|
39
|
+
|
|
40
|
+
expect(pricingService.order.getTotals(orders)).toEqual({
|
|
41
|
+
total: 30,
|
|
42
|
+
subTotal: 26,
|
|
43
|
+
subTotals: {
|
|
44
|
+
base: 26,
|
|
45
|
+
tax: 4,
|
|
46
|
+
},
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it('should merge subtotals with missing keys', () => {
|
|
51
|
+
const orders = [
|
|
52
|
+
{
|
|
53
|
+
parentId: '1',
|
|
54
|
+
total: 5,
|
|
55
|
+
subTotal: 5,
|
|
56
|
+
subTotals: {
|
|
57
|
+
fee: 1,
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
parentId: '1',
|
|
62
|
+
total: 10,
|
|
63
|
+
subTotal: 10,
|
|
64
|
+
subTotals: {
|
|
65
|
+
tax: 2,
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
];
|
|
69
|
+
|
|
70
|
+
expect(pricingService.order.getTotals(orders)).toEqual({
|
|
71
|
+
total: 15,
|
|
72
|
+
subTotal: 15,
|
|
73
|
+
subTotals: {
|
|
74
|
+
fee: 1,
|
|
75
|
+
tax: 2,
|
|
76
|
+
},
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
});
|
|
@@ -44,40 +44,28 @@ describe('Order actions', () => {
|
|
|
44
44
|
total: 10,
|
|
45
45
|
totalPaid: 0,
|
|
46
46
|
};
|
|
47
|
-
|
|
48
47
|
let cashPercentageDiscountOrder = pricingService.order.calculate(
|
|
49
48
|
{ items: [orderItem] },
|
|
50
49
|
{ paymentType: 'cash' }
|
|
51
50
|
);
|
|
52
|
-
|
|
53
51
|
expect(cashPercentageDiscountOrder).toHaveProperty('total', 9);
|
|
54
52
|
expect(cashPercentageDiscountOrder).toHaveProperty('subTotal', 10);
|
|
55
53
|
expect(cashPercentageDiscountOrder).toHaveProperty('subTotals', {
|
|
56
54
|
discount: -1,
|
|
57
55
|
});
|
|
58
56
|
expect(cashPercentageDiscountOrder.items[0].modifiers).toHaveLength(2);
|
|
59
|
-
|
|
60
57
|
expect(cashPercentageDiscountOrder.items[0].modifiers[0]._computed).toEqual(
|
|
61
58
|
expect.objectContaining({
|
|
62
59
|
amount: 0,
|
|
63
60
|
description: 'CASH 10% DISCOUNT',
|
|
64
61
|
})
|
|
65
62
|
);
|
|
66
|
-
|
|
67
63
|
expect(cashPercentageDiscountOrder.items[0].modifiers[1]._computed).toEqual(
|
|
68
64
|
expect.objectContaining({
|
|
69
65
|
amount: -1,
|
|
70
66
|
description: '$1.00 discount (-$1.00)',
|
|
71
67
|
})
|
|
72
68
|
);
|
|
73
|
-
|
|
74
|
-
expect(cashPercentageDiscountOrder.items[0].modifiers[0]._computed).toEqual(
|
|
75
|
-
expect.objectContaining({
|
|
76
|
-
amount: 0,
|
|
77
|
-
description: 'CASH 10% DISCOUNT',
|
|
78
|
-
})
|
|
79
|
-
);
|
|
80
|
-
|
|
81
69
|
cashPercentageDiscountOrder = pricingService.order.calculate(
|
|
82
70
|
{ items: [orderItem] },
|
|
83
71
|
{
|
|
@@ -88,14 +76,12 @@ describe('Order actions', () => {
|
|
|
88
76
|
}
|
|
89
77
|
);
|
|
90
78
|
cashPercentageDiscountOrder.items[0].totalPaid = 1;
|
|
91
|
-
|
|
92
79
|
expect(cashPercentageDiscountOrder).toHaveProperty('total', 9.89);
|
|
93
80
|
expect(cashPercentageDiscountOrder).toHaveProperty('subTotal', 10);
|
|
94
81
|
expect(cashPercentageDiscountOrder).toHaveProperty('subTotals', {
|
|
95
82
|
discount: -0.1111111111111112,
|
|
96
83
|
});
|
|
97
84
|
expect(cashPercentageDiscountOrder.items[0].modifiers).toHaveLength(2);
|
|
98
|
-
|
|
99
85
|
cashPercentageDiscountOrder = pricingService.order.calculate(
|
|
100
86
|
cashPercentageDiscountOrder,
|
|
101
87
|
{
|
|
@@ -106,7 +92,6 @@ describe('Order actions', () => {
|
|
|
106
92
|
}
|
|
107
93
|
);
|
|
108
94
|
cashPercentageDiscountOrder.items[0].totalPaid = 3;
|
|
109
|
-
|
|
110
95
|
expect(cashPercentageDiscountOrder).toHaveProperty('total', 9.67);
|
|
111
96
|
expect(cashPercentageDiscountOrder).toHaveProperty('subTotal', 10);
|
|
112
97
|
expect(cashPercentageDiscountOrder).toHaveProperty('subTotals', {
|
|
@@ -141,7 +126,6 @@ describe('Order actions', () => {
|
|
|
141
126
|
paymentId: '111',
|
|
142
127
|
})
|
|
143
128
|
);
|
|
144
|
-
|
|
145
129
|
cashPercentageDiscountOrder = pricingService.order.calculate(
|
|
146
130
|
cashPercentageDiscountOrder,
|
|
147
131
|
{
|
|
@@ -151,7 +135,6 @@ describe('Order actions', () => {
|
|
|
151
135
|
paymentId: '333',
|
|
152
136
|
}
|
|
153
137
|
);
|
|
154
|
-
|
|
155
138
|
expect(cashPercentageDiscountOrder).toHaveProperty('total', 9);
|
|
156
139
|
expect(cashPercentageDiscountOrder).toHaveProperty('subTotal', 10);
|
|
157
140
|
expect(cashPercentageDiscountOrder).toHaveProperty('subTotals', {
|
|
@@ -1128,4 +1111,336 @@ describe('Order actions', () => {
|
|
|
1128
1111
|
})
|
|
1129
1112
|
);
|
|
1130
1113
|
});
|
|
1114
|
+
|
|
1115
|
+
test('Applies fixed and percentage discounts with fixed modifier added first (both payment modifiers)', () => {
|
|
1116
|
+
const prePayDiscount1Usd = {
|
|
1117
|
+
_id: '6819114c06c23d37c1f19412',
|
|
1118
|
+
name: '1 usd pre pay disc',
|
|
1119
|
+
type: 'discount',
|
|
1120
|
+
tags: ['default'],
|
|
1121
|
+
direct: true,
|
|
1122
|
+
conditions: {
|
|
1123
|
+
valid: null,
|
|
1124
|
+
rules: [
|
|
1125
|
+
{
|
|
1126
|
+
key: 'paymentTypes',
|
|
1127
|
+
value: ['cash'],
|
|
1128
|
+
operand: '$in',
|
|
1129
|
+
},
|
|
1130
|
+
],
|
|
1131
|
+
},
|
|
1132
|
+
compute: {
|
|
1133
|
+
type: 'fixed',
|
|
1134
|
+
action: 'subtract',
|
|
1135
|
+
amount: 1,
|
|
1136
|
+
},
|
|
1137
|
+
};
|
|
1138
|
+
|
|
1139
|
+
const cashDiscount10Percent = {
|
|
1140
|
+
_id: '6819114c06c23d37c1f19412',
|
|
1141
|
+
name: '10 % cash discount',
|
|
1142
|
+
type: 'discount',
|
|
1143
|
+
tags: ['default'],
|
|
1144
|
+
direct: true,
|
|
1145
|
+
conditions: {
|
|
1146
|
+
valid: null,
|
|
1147
|
+
rules: [
|
|
1148
|
+
{
|
|
1149
|
+
key: 'paymentTypes',
|
|
1150
|
+
value: ['cash'],
|
|
1151
|
+
operand: '$in',
|
|
1152
|
+
},
|
|
1153
|
+
],
|
|
1154
|
+
},
|
|
1155
|
+
compute: {
|
|
1156
|
+
type: 'percentage',
|
|
1157
|
+
action: 'subtract',
|
|
1158
|
+
amount: 10,
|
|
1159
|
+
},
|
|
1160
|
+
};
|
|
1161
|
+
|
|
1162
|
+
const item = {
|
|
1163
|
+
_id: 'abc',
|
|
1164
|
+
price: 10,
|
|
1165
|
+
quantity: 1,
|
|
1166
|
+
modifiers: [prePayDiscount1Usd, cashDiscount10Percent],
|
|
1167
|
+
};
|
|
1168
|
+
|
|
1169
|
+
const resultedOrderPrepay = pricingService.item.calculate(item, {
|
|
1170
|
+
paymentType: 'cash',
|
|
1171
|
+
paymentMethod: 'cash',
|
|
1172
|
+
isPrepay: true,
|
|
1173
|
+
lockPaymentModifiers: true,
|
|
1174
|
+
paymentId: 'abcd123',
|
|
1175
|
+
amountToPay: 8,
|
|
1176
|
+
});
|
|
1177
|
+
|
|
1178
|
+
expect(resultedOrderPrepay.total).toBe(8);
|
|
1179
|
+
|
|
1180
|
+
expect(resultedOrderPrepay.modifiers[0]._computed.amount).toBe(0);
|
|
1181
|
+
expect(resultedOrderPrepay.modifiers[1]._computed.amount).toBe(0);
|
|
1182
|
+
expect(resultedOrderPrepay.modifiers[2]._computed.amount).toBe(-1);
|
|
1183
|
+
expect(resultedOrderPrepay.modifiers[3]._computed.amount).toBe(-1);
|
|
1184
|
+
});
|
|
1185
|
+
|
|
1186
|
+
test('Applies percentage and fixed discounts with percentage modifier added first (both paymentModifiers)', () => {
|
|
1187
|
+
const prePayDiscount1Usd = {
|
|
1188
|
+
_id: '6819114c06c23d37c1f19412',
|
|
1189
|
+
name: '1 usd pre pay disc',
|
|
1190
|
+
type: 'discount',
|
|
1191
|
+
tags: ['default'],
|
|
1192
|
+
direct: true,
|
|
1193
|
+
conditions: {
|
|
1194
|
+
valid: null,
|
|
1195
|
+
rules: [
|
|
1196
|
+
{
|
|
1197
|
+
key: 'paymentTypes',
|
|
1198
|
+
value: ['cash'],
|
|
1199
|
+
operand: '$in',
|
|
1200
|
+
},
|
|
1201
|
+
],
|
|
1202
|
+
},
|
|
1203
|
+
compute: {
|
|
1204
|
+
type: 'fixed',
|
|
1205
|
+
action: 'subtract',
|
|
1206
|
+
amount: 1,
|
|
1207
|
+
},
|
|
1208
|
+
};
|
|
1209
|
+
|
|
1210
|
+
const cashDiscount10Percent = {
|
|
1211
|
+
_id: '6819114c06c23d37c1f19412',
|
|
1212
|
+
name: '10 % cash discount',
|
|
1213
|
+
type: 'discount',
|
|
1214
|
+
tags: ['default'],
|
|
1215
|
+
direct: true,
|
|
1216
|
+
conditions: {
|
|
1217
|
+
valid: null,
|
|
1218
|
+
rules: [
|
|
1219
|
+
{
|
|
1220
|
+
key: 'paymentTypes',
|
|
1221
|
+
value: ['cash'],
|
|
1222
|
+
operand: '$in',
|
|
1223
|
+
},
|
|
1224
|
+
],
|
|
1225
|
+
},
|
|
1226
|
+
compute: {
|
|
1227
|
+
type: 'percentage',
|
|
1228
|
+
action: 'subtract',
|
|
1229
|
+
amount: 10,
|
|
1230
|
+
},
|
|
1231
|
+
};
|
|
1232
|
+
|
|
1233
|
+
const item = {
|
|
1234
|
+
_id: 'abc',
|
|
1235
|
+
price: 10,
|
|
1236
|
+
quantity: 1,
|
|
1237
|
+
modifiers: [cashDiscount10Percent, prePayDiscount1Usd],
|
|
1238
|
+
};
|
|
1239
|
+
|
|
1240
|
+
const resultedOrderPrepay = pricingService.item.calculate(item, {
|
|
1241
|
+
paymentType: 'cash',
|
|
1242
|
+
paymentMethod: 'cash',
|
|
1243
|
+
isPrepay: true,
|
|
1244
|
+
lockPaymentModifiers: true,
|
|
1245
|
+
paymentId: 'abcd123',
|
|
1246
|
+
amountToPay: 8,
|
|
1247
|
+
});
|
|
1248
|
+
|
|
1249
|
+
expect(resultedOrderPrepay.total).toBe(8);
|
|
1250
|
+
|
|
1251
|
+
expect(resultedOrderPrepay.modifiers[0]._computed.amount).toBe(0);
|
|
1252
|
+
expect(resultedOrderPrepay.modifiers[1]._computed.amount).toBe(0);
|
|
1253
|
+
expect(resultedOrderPrepay.modifiers[2]._computed.amount).toBe(-1);
|
|
1254
|
+
expect(resultedOrderPrepay.modifiers[3]._computed.amount).toBe(-1);
|
|
1255
|
+
});
|
|
1256
|
+
|
|
1257
|
+
test('Applies one payment discount and one regular fixed discount (percentage first)', () => {
|
|
1258
|
+
const prePayDiscount1Usd = {
|
|
1259
|
+
_id: '6819114c06c23d37c1f19412',
|
|
1260
|
+
name: '1 usd pre pay disc',
|
|
1261
|
+
type: 'discount',
|
|
1262
|
+
tags: ['default'],
|
|
1263
|
+
direct: true,
|
|
1264
|
+
conditions: {
|
|
1265
|
+
valid: null,
|
|
1266
|
+
rules: [],
|
|
1267
|
+
},
|
|
1268
|
+
compute: {
|
|
1269
|
+
type: 'fixed',
|
|
1270
|
+
action: 'subtract',
|
|
1271
|
+
amount: 1,
|
|
1272
|
+
},
|
|
1273
|
+
};
|
|
1274
|
+
|
|
1275
|
+
const cashDiscount10Percent = {
|
|
1276
|
+
_id: '6819114c06c23d37c1f19412',
|
|
1277
|
+
name: '10 % cash discount',
|
|
1278
|
+
type: 'discount',
|
|
1279
|
+
tags: ['default'],
|
|
1280
|
+
direct: true,
|
|
1281
|
+
conditions: {
|
|
1282
|
+
valid: null,
|
|
1283
|
+
rules: [
|
|
1284
|
+
{
|
|
1285
|
+
key: 'paymentTypes',
|
|
1286
|
+
value: ['cash'],
|
|
1287
|
+
operand: '$in',
|
|
1288
|
+
},
|
|
1289
|
+
],
|
|
1290
|
+
},
|
|
1291
|
+
compute: {
|
|
1292
|
+
type: 'percentage',
|
|
1293
|
+
action: 'subtract',
|
|
1294
|
+
amount: 10,
|
|
1295
|
+
},
|
|
1296
|
+
};
|
|
1297
|
+
|
|
1298
|
+
const item = {
|
|
1299
|
+
_id: 'abc',
|
|
1300
|
+
price: 10,
|
|
1301
|
+
quantity: 1,
|
|
1302
|
+
modifiers: [cashDiscount10Percent, prePayDiscount1Usd],
|
|
1303
|
+
};
|
|
1304
|
+
|
|
1305
|
+
const resultedOrderPrepay = pricingService.item.calculate(item, {
|
|
1306
|
+
paymentType: 'cash',
|
|
1307
|
+
paymentMethod: 'cash',
|
|
1308
|
+
isPrepay: true,
|
|
1309
|
+
lockPaymentModifiers: true,
|
|
1310
|
+
paymentId: 'abcd123',
|
|
1311
|
+
amountToPay: 8,
|
|
1312
|
+
});
|
|
1313
|
+
|
|
1314
|
+
expect(resultedOrderPrepay.total).toBe(8);
|
|
1315
|
+
|
|
1316
|
+
expect(resultedOrderPrepay.modifiers[0]._computed.amount).toBe(0);
|
|
1317
|
+
expect(resultedOrderPrepay.modifiers[1]._computed.amount).toBe(-1);
|
|
1318
|
+
expect(resultedOrderPrepay.modifiers[2]._computed.amount).toBe(-1);
|
|
1319
|
+
});
|
|
1320
|
+
|
|
1321
|
+
test('Applies one payment discount and one regular percentage discount (fixed first)', () => {
|
|
1322
|
+
const prePayDiscount1Usd = {
|
|
1323
|
+
_id: '6819114c06c23d37c1f19412',
|
|
1324
|
+
name: '1 usd pre pay disc',
|
|
1325
|
+
type: 'discount',
|
|
1326
|
+
tags: ['default'],
|
|
1327
|
+
direct: true,
|
|
1328
|
+
conditions: {
|
|
1329
|
+
valid: null,
|
|
1330
|
+
rules: [],
|
|
1331
|
+
},
|
|
1332
|
+
compute: {
|
|
1333
|
+
type: 'fixed',
|
|
1334
|
+
action: 'subtract',
|
|
1335
|
+
amount: 1,
|
|
1336
|
+
},
|
|
1337
|
+
};
|
|
1338
|
+
|
|
1339
|
+
const cashDiscount10Percent = {
|
|
1340
|
+
_id: '6819114c06c23d37c1f19412',
|
|
1341
|
+
name: '10 % cash discount',
|
|
1342
|
+
type: 'discount',
|
|
1343
|
+
tags: ['default'],
|
|
1344
|
+
direct: true,
|
|
1345
|
+
conditions: {
|
|
1346
|
+
valid: null,
|
|
1347
|
+
rules: [
|
|
1348
|
+
{
|
|
1349
|
+
key: 'paymentTypes',
|
|
1350
|
+
value: ['cash'],
|
|
1351
|
+
operand: '$in',
|
|
1352
|
+
},
|
|
1353
|
+
],
|
|
1354
|
+
},
|
|
1355
|
+
compute: {
|
|
1356
|
+
type: 'percentage',
|
|
1357
|
+
action: 'subtract',
|
|
1358
|
+
amount: 10,
|
|
1359
|
+
},
|
|
1360
|
+
};
|
|
1361
|
+
|
|
1362
|
+
const item = {
|
|
1363
|
+
_id: 'abc',
|
|
1364
|
+
price: 10,
|
|
1365
|
+
quantity: 1,
|
|
1366
|
+
modifiers: [prePayDiscount1Usd, cashDiscount10Percent],
|
|
1367
|
+
};
|
|
1368
|
+
|
|
1369
|
+
const resultedOrderPrepay = pricingService.item.calculate(item, {
|
|
1370
|
+
paymentType: 'cash',
|
|
1371
|
+
paymentMethod: 'cash',
|
|
1372
|
+
isPrepay: true,
|
|
1373
|
+
lockPaymentModifiers: true,
|
|
1374
|
+
paymentId: 'abcd123',
|
|
1375
|
+
amountToPay: 8,
|
|
1376
|
+
});
|
|
1377
|
+
|
|
1378
|
+
expect(resultedOrderPrepay.total).toBe(8);
|
|
1379
|
+
|
|
1380
|
+
expect(resultedOrderPrepay.modifiers[0]._computed.amount).toBe(0);
|
|
1381
|
+
expect(resultedOrderPrepay.modifiers[1]._computed.amount).toBe(-1);
|
|
1382
|
+
expect(resultedOrderPrepay.modifiers[2]._computed.amount).toBe(-1);
|
|
1383
|
+
});
|
|
1384
|
+
|
|
1385
|
+
test('Applies one payment discount and one regular percentage discount (fixed first) withouth sending amountToPay', () => {
|
|
1386
|
+
const prePayDiscount1Usd = {
|
|
1387
|
+
_id: '6819114c06c23d37c1f19412',
|
|
1388
|
+
name: '1 usd pre pay disc',
|
|
1389
|
+
type: 'discount',
|
|
1390
|
+
tags: ['default'],
|
|
1391
|
+
direct: true,
|
|
1392
|
+
conditions: {
|
|
1393
|
+
valid: null,
|
|
1394
|
+
rules: [],
|
|
1395
|
+
},
|
|
1396
|
+
compute: {
|
|
1397
|
+
type: 'fixed',
|
|
1398
|
+
action: 'subtract',
|
|
1399
|
+
amount: 1,
|
|
1400
|
+
},
|
|
1401
|
+
};
|
|
1402
|
+
|
|
1403
|
+
const cashDiscount10Percent = {
|
|
1404
|
+
_id: '6819114c06c23d37c1f19412',
|
|
1405
|
+
name: '10 % cash discount',
|
|
1406
|
+
type: 'discount',
|
|
1407
|
+
tags: ['default'],
|
|
1408
|
+
direct: true,
|
|
1409
|
+
conditions: {
|
|
1410
|
+
valid: null,
|
|
1411
|
+
rules: [
|
|
1412
|
+
{
|
|
1413
|
+
key: 'paymentTypes',
|
|
1414
|
+
value: ['cash'],
|
|
1415
|
+
operand: '$in',
|
|
1416
|
+
},
|
|
1417
|
+
],
|
|
1418
|
+
},
|
|
1419
|
+
compute: {
|
|
1420
|
+
type: 'percentage',
|
|
1421
|
+
action: 'subtract',
|
|
1422
|
+
amount: 10,
|
|
1423
|
+
},
|
|
1424
|
+
};
|
|
1425
|
+
|
|
1426
|
+
const item = {
|
|
1427
|
+
_id: 'abc',
|
|
1428
|
+
price: 10,
|
|
1429
|
+
quantity: 1,
|
|
1430
|
+
modifiers: [prePayDiscount1Usd, cashDiscount10Percent],
|
|
1431
|
+
};
|
|
1432
|
+
|
|
1433
|
+
const resultedOrderPrepay = pricingService.item.calculate(item, {
|
|
1434
|
+
paymentType: 'cash',
|
|
1435
|
+
paymentMethod: 'cash',
|
|
1436
|
+
isPrepay: true,
|
|
1437
|
+
paymentId: 'abcd123',
|
|
1438
|
+
});
|
|
1439
|
+
|
|
1440
|
+
expect(resultedOrderPrepay.total).toBe(8);
|
|
1441
|
+
|
|
1442
|
+
expect(resultedOrderPrepay.modifiers[0]._computed.amount).toBe(0);
|
|
1443
|
+
expect(resultedOrderPrepay.modifiers[1]._computed.amount).toBe(-1);
|
|
1444
|
+
expect(resultedOrderPrepay.modifiers[2]._computed.amount).toBe(-1);
|
|
1445
|
+
});
|
|
1131
1446
|
});
|