@darkpos/pricing 1.0.29 → 1.0.31
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.
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
const usePricing = require('../../index');
|
|
2
|
+
const mockStores = require('../mocks/stores');
|
|
3
|
+
const addItemObj = require('../mocks/addItemMock');
|
|
4
|
+
|
|
5
|
+
const session = {
|
|
6
|
+
store: mockStores[0],
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
const pricingService = usePricing(session);
|
|
10
|
+
|
|
11
|
+
describe('addItem function', () => {
|
|
12
|
+
test('should add a new item to an empty order', () => {
|
|
13
|
+
const addItem = pricingService.order.addItem;
|
|
14
|
+
|
|
15
|
+
// Mock an empty order
|
|
16
|
+
const order = {
|
|
17
|
+
items: [],
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
// Mock item to add
|
|
21
|
+
const item = {
|
|
22
|
+
itemId: '123',
|
|
23
|
+
quantity: 1,
|
|
24
|
+
price: 100,
|
|
25
|
+
modifiers: [],
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
// Call the function
|
|
29
|
+
const [updatedOrder, itemIndex, addedItem] = addItem({
|
|
30
|
+
order,
|
|
31
|
+
item,
|
|
32
|
+
itemIndex: -1, // No existing item in order
|
|
33
|
+
cache: addItemObj.cache, // Assuming some cache object
|
|
34
|
+
overridenQuantity: -1, // No overridden quantity
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
// Assertions
|
|
38
|
+
expect(updatedOrder.items).toHaveLength(1); // Should have one item
|
|
39
|
+
expect(updatedOrder.items[0].itemId).toBe('123'); // Item ID should match
|
|
40
|
+
expect(updatedOrder.items[0].quantity).toBe(1); // Quantity should be 1
|
|
41
|
+
expect(updatedOrder.items[0].price).toBe(100); // Price should be 100
|
|
42
|
+
expect(itemIndex).toBe(0); // Index of the added item should be 0
|
|
43
|
+
expect(addedItem).toEqual(updatedOrder.items[0]); // Added item should match the one in the order
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
test('should combine with an existing item if aggregation is enabled', () => {
|
|
47
|
+
const addItem = pricingService.order.addItem;
|
|
48
|
+
|
|
49
|
+
// Mock order with one existing item
|
|
50
|
+
const order = {
|
|
51
|
+
items: [
|
|
52
|
+
{
|
|
53
|
+
itemId: '123',
|
|
54
|
+
quantity: 1,
|
|
55
|
+
price: 100,
|
|
56
|
+
modifiers: [],
|
|
57
|
+
},
|
|
58
|
+
],
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
// Mock item to add (same as existing item)
|
|
62
|
+
const item = {
|
|
63
|
+
itemId: '123',
|
|
64
|
+
quantity: 1,
|
|
65
|
+
price: 100,
|
|
66
|
+
modifiers: [],
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
// Call the function
|
|
70
|
+
const [updatedOrder, itemIndex, addedItem] = addItem({
|
|
71
|
+
order,
|
|
72
|
+
item,
|
|
73
|
+
itemIndex: -1, // Assuming no explicit index
|
|
74
|
+
cache: addItemObj.cache, // Assuming some cache object
|
|
75
|
+
overridenQuantity: -1, // No overridden quantity
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
// Assertions
|
|
79
|
+
expect(updatedOrder.items).toHaveLength(1); // Should still have one item
|
|
80
|
+
expect(updatedOrder.items[0].quantity).toBe(2); // Quantity should be 2 (aggregated)
|
|
81
|
+
expect(itemIndex).toBe(0); // Index should be 0
|
|
82
|
+
expect(addedItem.quantity).toBe(2); // Added item should have combined quantity
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
test('should add item with overridden quantity', () => {
|
|
86
|
+
const addItem = pricingService.order.addItem;
|
|
87
|
+
|
|
88
|
+
// Mock order with one existing item
|
|
89
|
+
const order = {
|
|
90
|
+
items: [],
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
// Mock item to add
|
|
94
|
+
const item = {
|
|
95
|
+
itemId: '456',
|
|
96
|
+
quantity: 1,
|
|
97
|
+
price: 200,
|
|
98
|
+
modifiers: [],
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
// Call the function with an overridden quantity
|
|
102
|
+
const [updatedOrder, itemIndex, addedItem] = addItem({
|
|
103
|
+
order,
|
|
104
|
+
item,
|
|
105
|
+
itemIndex: -1, // Assuming no explicit index
|
|
106
|
+
cache: addItemObj.cache, // Assuming some cache object
|
|
107
|
+
overridenQuantity: 5, // Override quantity to 5
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
// Assertions
|
|
111
|
+
expect(updatedOrder.items).toHaveLength(1); // Should have one item
|
|
112
|
+
expect(updatedOrder.items[0].quantity).toBe(5); // Quantity should be overridden to 5
|
|
113
|
+
expect(addedItem.quantity).toBe(5); // Added item should reflect overridden quantity
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
test('should handle adding an item with modifiers', () => {
|
|
117
|
+
const addItem = pricingService.order.addItem;
|
|
118
|
+
|
|
119
|
+
// Mock order with one existing item
|
|
120
|
+
const order = {
|
|
121
|
+
items: [],
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
// Mock item to add with modifiers
|
|
125
|
+
const item = {
|
|
126
|
+
itemId: '789',
|
|
127
|
+
quantity: 1,
|
|
128
|
+
price: 150,
|
|
129
|
+
modifiers: [
|
|
130
|
+
{
|
|
131
|
+
"_id": "66cdf18a1e48455e128a4f63",
|
|
132
|
+
"modifierId": 'mod1',
|
|
133
|
+
"_parentId": null,
|
|
134
|
+
"name": null,
|
|
135
|
+
"sku": null,
|
|
136
|
+
"description": null,
|
|
137
|
+
"group": null,
|
|
138
|
+
"type": null,
|
|
139
|
+
"attributes": [],
|
|
140
|
+
"color": null,
|
|
141
|
+
"backgroundColor": null,
|
|
142
|
+
"icon": null,
|
|
143
|
+
"url": null,
|
|
144
|
+
"tags": [],
|
|
145
|
+
"order": null,
|
|
146
|
+
"included": false,
|
|
147
|
+
"direct": true,
|
|
148
|
+
"hidden": false,
|
|
149
|
+
"required": false,
|
|
150
|
+
"code": null,
|
|
151
|
+
"properties": null,
|
|
152
|
+
"_computed": null
|
|
153
|
+
},
|
|
154
|
+
{
|
|
155
|
+
"_id": "66cdf18a1e48455e128a4f64",
|
|
156
|
+
"modifierId": 'mod2',
|
|
157
|
+
"_parentId": null,
|
|
158
|
+
"name": null,
|
|
159
|
+
"sku": null,
|
|
160
|
+
"description": null,
|
|
161
|
+
"group": null,
|
|
162
|
+
"type": null,
|
|
163
|
+
"attributes": [],
|
|
164
|
+
"color": null,
|
|
165
|
+
"backgroundColor": null,
|
|
166
|
+
"icon": null,
|
|
167
|
+
"url": null,
|
|
168
|
+
"tags": [],
|
|
169
|
+
"order": null,
|
|
170
|
+
"included": false,
|
|
171
|
+
"direct": true,
|
|
172
|
+
"hidden": false,
|
|
173
|
+
"required": false,
|
|
174
|
+
"code": null,
|
|
175
|
+
"properties": null,
|
|
176
|
+
"_computed": null
|
|
177
|
+
},
|
|
178
|
+
],
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
// Call the function
|
|
182
|
+
const [updatedOrder, itemIndex, addedItem] = addItem({
|
|
183
|
+
order,
|
|
184
|
+
item,
|
|
185
|
+
itemIndex: -1, // Assuming no explicit index
|
|
186
|
+
cache: addItemObj.cache, // Assuming some cache object
|
|
187
|
+
overridenQuantity: -1, // No overridden quantity
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
// Assertions
|
|
191
|
+
expect(updatedOrder.items).toHaveLength(1); // Should have one item
|
|
192
|
+
expect(updatedOrder.items[0].modifiers).toHaveLength(2); // Item should have two modifiers
|
|
193
|
+
expect(updatedOrder.items[0].modifiers[0].modifierId).toBe('66cdf18a1e48455e128a4f63'); // Check first modifier
|
|
194
|
+
expect(updatedOrder.items[0].modifiers[1].modifierId).toBe('66cdf18a1e48455e128a4f64'); // Check second modifier
|
|
195
|
+
expect(updatedOrder.items[0].modifiers).toHaveLength(2); // Added item should have modifiers
|
|
196
|
+
});
|
|
197
|
+
});
|
|
@@ -0,0 +1,101 @@
|
|
|
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
|
+
|
|
10
|
+
describe('getSameItems function', () => {
|
|
11
|
+
test('Get calculated Order, one item', () => {
|
|
12
|
+
const { getSameItems } = pricingService.order;
|
|
13
|
+
|
|
14
|
+
// Mock order with one item
|
|
15
|
+
const order = {
|
|
16
|
+
items: [
|
|
17
|
+
{
|
|
18
|
+
itemId: '123',
|
|
19
|
+
menuId: 'menu1',
|
|
20
|
+
path: [1, 2, 3],
|
|
21
|
+
serial: null,
|
|
22
|
+
},
|
|
23
|
+
],
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
// Mock item to compare
|
|
27
|
+
const item = {
|
|
28
|
+
itemId: '123',
|
|
29
|
+
menuId: 'menu1',
|
|
30
|
+
path: [1, 2, 3],
|
|
31
|
+
serial: null,
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
// Call the function
|
|
35
|
+
const result = getSameItems({ order, item });
|
|
36
|
+
|
|
37
|
+
// Assertions
|
|
38
|
+
expect(result).toHaveLength(1); // Expecting one match
|
|
39
|
+
expect(result[0]).toEqual(order.items[0]); // The matched item should be the same as the one in the order
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
test('No match due to different itemId', () => {
|
|
43
|
+
const { getSameItems } = pricingService.order;
|
|
44
|
+
|
|
45
|
+
// Mock order with one item
|
|
46
|
+
const order = {
|
|
47
|
+
items: [
|
|
48
|
+
{
|
|
49
|
+
itemId: '123',
|
|
50
|
+
menuId: 'menu1',
|
|
51
|
+
path: [1, 2, 3],
|
|
52
|
+
serial: null,
|
|
53
|
+
},
|
|
54
|
+
],
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
// Mock item to compare with a different itemId
|
|
58
|
+
const item = {
|
|
59
|
+
itemId: '456',
|
|
60
|
+
menuId: 'menu1',
|
|
61
|
+
path: [1, 2, 3],
|
|
62
|
+
serial: null,
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
// Call the function
|
|
66
|
+
const result = getSameItems({ order, item });
|
|
67
|
+
|
|
68
|
+
// Assertions
|
|
69
|
+
expect(result).toHaveLength(0); // Expecting no matches
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
test('No match due to different path', () => {
|
|
73
|
+
const { getSameItems } = pricingService.order;
|
|
74
|
+
|
|
75
|
+
// Mock order with one item
|
|
76
|
+
const order = {
|
|
77
|
+
items: [
|
|
78
|
+
{
|
|
79
|
+
itemId: '123',
|
|
80
|
+
menuId: 'menu1',
|
|
81
|
+
path: [1, 2, 3],
|
|
82
|
+
serial: null,
|
|
83
|
+
},
|
|
84
|
+
],
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
// Mock item to compare with a different path
|
|
88
|
+
const item = {
|
|
89
|
+
itemId: '123',
|
|
90
|
+
menuId: 'menu1',
|
|
91
|
+
path: [4, 5, 6],
|
|
92
|
+
serial: null,
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
// Call the function
|
|
96
|
+
const result = getSameItems({ order, item });
|
|
97
|
+
|
|
98
|
+
// Assertions
|
|
99
|
+
expect(result).toHaveLength(0); // Expecting no matches
|
|
100
|
+
});
|
|
101
|
+
});
|
|
@@ -11,7 +11,7 @@ module.exports = ({ _, utils, constants, actions }) => {
|
|
|
11
11
|
) {
|
|
12
12
|
const maxAmount = actions.getProperty(modifier, 'maxAmount');
|
|
13
13
|
|
|
14
|
-
const
|
|
14
|
+
const compute = modifier.compute || {};
|
|
15
15
|
const { type, amount = 0 } = compute;
|
|
16
16
|
let modifierAmount = amount;
|
|
17
17
|
|
|
@@ -4,8 +4,8 @@ module.exports = () =>
|
|
|
4
4
|
return items.filter(each => {
|
|
5
5
|
let check =
|
|
6
6
|
each.itemId &&
|
|
7
|
-
item.
|
|
8
|
-
each.itemId === item.
|
|
7
|
+
item.itemId &&
|
|
8
|
+
each.itemId === item.itemId &&
|
|
9
9
|
!each.serial &&
|
|
10
10
|
!item.serial;
|
|
11
11
|
if (check && each.menuId && item.menuId) {
|
|
@@ -13,7 +13,6 @@ module.exports = () =>
|
|
|
13
13
|
each.menuId === item.menuId &&
|
|
14
14
|
each.path.toString() === item.path.toString();
|
|
15
15
|
}
|
|
16
|
-
|
|
17
16
|
return check;
|
|
18
17
|
});
|
|
19
18
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@darkpos/pricing",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.31",
|
|
4
4
|
"description": "Pricing calculator",
|
|
5
5
|
"author": "Dark POS",
|
|
6
6
|
"license": "ISC",
|
|
@@ -36,5 +36,5 @@
|
|
|
36
36
|
"supertest": "^6.2.3",
|
|
37
37
|
"supervisor": "^0.12.0"
|
|
38
38
|
},
|
|
39
|
-
"gitHead": "
|
|
39
|
+
"gitHead": "a864feac96c47884de2da753f60ea69856bcb914"
|
|
40
40
|
}
|