@darkpos/pricing 1.0.84 → 1.0.86

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.
@@ -1855,4 +1855,74 @@ describe('Modifier actions', () => {
1855
1855
 
1856
1856
  expect(updatedOrder2.items[0].modifiers.length).toEqual(1);
1857
1857
  });
1858
+
1859
+ test('Should not add modifiers if tags do not match', () => {
1860
+ const order = {
1861
+ id: 'ord-123',
1862
+ items: [],
1863
+ modifiers: [],
1864
+ };
1865
+ const modifier = {
1866
+ _id: 1,
1867
+ compute: {
1868
+ amount: 10,
1869
+ action: 'subtract',
1870
+ type: 'fixed',
1871
+ },
1872
+ tags: ['tagA', 'tagB'],
1873
+ };
1874
+ order.items.push({
1875
+ quantity: 2,
1876
+ itemId: '123',
1877
+ price: 100,
1878
+ modifiers: [],
1879
+ tags: ['otherTag'],
1880
+ });
1881
+
1882
+ const updatedOrder = pricingService.order.addItemModifier({
1883
+ order,
1884
+ modifier,
1885
+ itemIndex: 0,
1886
+ });
1887
+
1888
+ expect(updatedOrder.items[0].modifiers.length).toEqual(0);
1889
+ });
1890
+
1891
+ test('Should add modifier if tags match', () => {
1892
+ const order = {
1893
+ id: 'ord-123',
1894
+ items: [],
1895
+ modifiers: [],
1896
+ };
1897
+ const modifier = {
1898
+ _id: 1,
1899
+ compute: {
1900
+ amount: 10,
1901
+ action: 'subtract',
1902
+ type: 'fixed',
1903
+ },
1904
+ tags: ['tagA', 'tagB'],
1905
+ };
1906
+ order.items.push({
1907
+ quantity: 2,
1908
+ itemId: '123',
1909
+ price: 100,
1910
+ modifiers: [],
1911
+ tags: ['tagA'],
1912
+ });
1913
+
1914
+ let error = '';
1915
+ const updatedOrder = pricingService.order.addItemModifier({
1916
+ order,
1917
+ modifier,
1918
+ itemIndex: 0,
1919
+ onError: err => {
1920
+ error = err;
1921
+ },
1922
+ });
1923
+
1924
+ expect(error).toEqual('');
1925
+
1926
+ expect(updatedOrder.items[0].modifiers.length).toEqual(1);
1927
+ });
1858
1928
  });
@@ -13,24 +13,23 @@ const pricingService = usePricing(session);
13
13
 
14
14
  describe('Create suborder', () => {
15
15
  test('Create a suborder with one order', () => {
16
- const subOrders = pricingService.order.createSuborder(orderMock);
17
16
  const order = {
18
17
  ...orderMock,
19
- orders: subOrders,
18
+ orders: []
20
19
  };
21
- expect(order.orders).toHaveLength(1);
20
+ const subOrders = pricingService.order.addItemsToSubOrder({ parentOrder: order });
21
+
22
22
  expect(subOrders).toHaveLength(1);
23
23
  });
24
24
 
25
25
  test('Create suborder with multiple items in the order', () => {
26
- const multipleItemsOrderMock = {
26
+ const order = {
27
27
  ...orderMock,
28
+ orders: [],
28
29
  items: [item1Mock, item2Mock, item3Mock], // assuming items is an array of items
29
30
  };
30
- const subOrders = pricingService.order.createSuborder(
31
- multipleItemsOrderMock
32
- );
31
+ const subOrders = pricingService.order.createSubOrder({ parentOrder: order });
33
32
 
34
- expect(subOrders[0].items).toHaveLength(3); // Suborders should match number of items
33
+ expect(subOrders).toHaveLength(1); // Suborders should match number of items
35
34
  });
36
35
  });
@@ -0,0 +1,33 @@
1
+ const usePricing = require('../../index');
2
+ const mockStores = require('../mocks/stores');
3
+ const orderMock = require('../modifier/mocks/orderMock.json');
4
+ const item1Mock = require('../mocks/items/item1Mock.json');
5
+ const item2Mock = require('../mocks/items/item2Mock.json');
6
+ const item3Mock = require('../mocks/items/item3Mock.json');
7
+
8
+ const session = {
9
+ store: mockStores[0],
10
+ };
11
+
12
+ const pricingService = usePricing(session);
13
+
14
+ describe('Create suborder', () => {
15
+ test('Create a suborder with one order', () => {
16
+ const order = {
17
+ ...orderMock,
18
+ };
19
+ const subOrder = pricingService.order.createSubOrder({ parentOrder: order, items: order.items });
20
+
21
+ expect(subOrder.items).toHaveLength(1);
22
+ });
23
+
24
+ test('Create suborder with multiple items in the order', () => {
25
+ const order = {
26
+ ...orderMock,
27
+ items: [item1Mock, item2Mock, item3Mock], // assuming items is an array of items
28
+ };
29
+ const subOrder = pricingService.order.createSubOrder({ parentOrder: order, items: order.items });
30
+
31
+ expect(subOrder.items).toHaveLength(3); // Suborders should match number of items
32
+ });
33
+ });
package/lib/item/index.js CHANGED
@@ -59,6 +59,7 @@ const adjustFixedModifiersDifference = require('./adjustFixedModifiersDifference
59
59
  const validateCreditModifiersTotal = require('./validateCreditModifiersTotal');
60
60
  const adjustCreditModifiersDifference = require('./adjustCreditModifiersDifference');
61
61
  const getSubtotal = require('./getSubtotal');
62
+ const isSomeTagsMatch = require('./isSomeTagsMatch');
62
63
  const getTotals = require('./getTotals');
63
64
  const patchItem = require('./patchItem');
64
65
 
@@ -133,6 +134,7 @@ const itemActions = (deps = {}) => {
133
134
  validateCreditModifiersTotal: validateCreditModifiersTotal(innerDeps),
134
135
  adjustCreditModifiersDifference: adjustCreditModifiersDifference(innerDeps),
135
136
  getSubtotal: getSubtotal(innerDeps),
137
+ isSomeTagsMatch: isSomeTagsMatch(innerDeps),
136
138
  getTotals: getTotals(innerDeps),
137
139
  patchItem: patchItem(innerDeps),
138
140
  });
@@ -0,0 +1,13 @@
1
+ module.exports = () =>
2
+ function isSomeTagsMatch({ item, tags }) {
3
+ if (
4
+ !item ||
5
+ !Array.isArray(item.tags) ||
6
+ item.tags.length === 0 ||
7
+ !Array.isArray(tags) ||
8
+ tags.length === 0
9
+ )
10
+ return true;
11
+
12
+ return item.tags.some(tag => tags.includes(tag));
13
+ };
@@ -180,6 +180,11 @@ module.exports = ({ actions, itemActions, modifierActions, utils, _ }) => {
180
180
  if (!order || itemIndex < 0 || !modifier) return order;
181
181
 
182
182
  let item = actions.getSelectedItem({ order, itemIndex });
183
+
184
+ if (!itemActions.isSomeTagsMatch({ item, tags: modifier.tags })) {
185
+ return order;
186
+ }
187
+
183
188
  const customer = actions.getCustomer(order);
184
189
  const contains =
185
190
  !_.isEmpty(item.modifiers) &&
@@ -0,0 +1,15 @@
1
+ module.exports = ({ actions }) =>
2
+ function addItemsToSubOrder({ parentOrder, index }) {
3
+ let subOrders = parentOrder.orders || [];
4
+ const newSubOrders = [];
5
+ const items = actions.getNewItems({ parentOrder });
6
+
7
+ if (items.length > 0) {
8
+ if (subOrders.length === 0 || index === -1)
9
+ newSubOrders.push(actions.createSubOrder({ parentOrder, items }));
10
+ else subOrders[index || subOrders.length - 1].items.push(...items);
11
+ }
12
+
13
+ subOrders = actions.mapSubOrders({ parentOrder, newSubOrders });
14
+ return subOrders;
15
+ };
@@ -1,34 +1,12 @@
1
1
  module.exports = ({ actions }) =>
2
2
  function autoSplit({ parentOrder }) {
3
- let subOrders = parentOrder.orders || [];
4
3
  let newSubOrders = [];
5
- const existingItems = subOrders
6
- .map(subOrder => subOrder.items || [])
7
- .flat();
8
- const newItems = parentOrder.items.filter(item => {
9
- const existingItem = existingItems.find(
10
- eItem => String(eItem._id) === String(item._id)
11
- );
12
- return !existingItem;
13
- });
14
4
 
5
+ const newItems = actions.getNewItems({ parentOrder });
15
6
  if (newItems.length > 0) {
16
- newSubOrders = actions.splitByDepartments({
17
- ...parentOrder,
18
- items: newItems,
19
- });
7
+ newSubOrders = actions.splitByDepartments({ parentOrder, newItems });
20
8
  }
21
9
 
22
- // reassign displayIds
23
- subOrders = [...subOrders, ...newSubOrders].map((subOrder, idx) => ({
24
- ...subOrder,
25
- displayId: `${parentOrder.displayId}-${idx + 1}`,
26
- }));
27
-
28
- subOrders = actions.spreadModifiers({
29
- parentOrder,
30
- subOrders,
31
- });
32
-
10
+ const subOrders = actions.mapSubOrders({ parentOrder, newSubOrders });
33
11
  return subOrders;
34
12
  };
@@ -0,0 +1,24 @@
1
+ module.exports = ({ actions }) =>
2
+ function createSubOrder({ parentOrder, items }) {
3
+ const subOrder = actions.create({
4
+ // ...order,
5
+ displayId: null,
6
+ discount: 0,
7
+ tax: 0,
8
+ totalPaid: 0,
9
+ total: 0,
10
+ status: {
11
+ order: 'open',
12
+ delivery: parentOrder.status.delivery,
13
+ detailed: parentOrder.status.detailed,
14
+ fullyPaid: false,
15
+ fullyPicked: false,
16
+ },
17
+ parentId: parentOrder._id,
18
+ items,
19
+ modifiers: [],
20
+ notes: parentOrder.notes || [],
21
+ });
22
+
23
+ return subOrder;
24
+ };
@@ -0,0 +1,15 @@
1
+ module.exports = () =>
2
+ function getNewItems({ parentOrder }) {
3
+ const subOrders = parentOrder.orders || [];
4
+ const existingItems = subOrders
5
+ .map(subOrder => subOrder.items || [])
6
+ .flat();
7
+ const newItems = parentOrder.items.filter(item => {
8
+ const existingItem = existingItems.find(
9
+ eItem => String(eItem._id) === String(item._id)
10
+ );
11
+ return !existingItem;
12
+ });
13
+
14
+ return newItems;
15
+ };
@@ -42,7 +42,8 @@ const unpickOrder = require('./unpick');
42
42
  const isDetailed = require('./isDetailed');
43
43
  const splitByDepartments = require('./splitByDepartments');
44
44
  const autoSplit = require('./autoSplit');
45
- const syncSubOrderItemsFromParent = require('./syncSubOrderItemsFromParent');
45
+ const addItemsToSubOrder = require('./addItemsToSubOrder');
46
+ const mergeItemsWithSubOrders = require('./mergeItemsWithSubOrders');
46
47
  const createParent = require('./createParent');
47
48
  const getOrdersPieces = require('./getOrdersPieces');
48
49
  const getTotals = require('./getTotals');
@@ -79,8 +80,7 @@ const validateCreditModifiersTotal = require('./validateCreditModifiersTotal');
79
80
  const validateFixedModifiersTotal = require('./validateFixedModifiersTotal');
80
81
  const removeModifiers = require('./removeModifiers');
81
82
  const addModifiers = require('./addModifiers');
82
- const createSuborder = require('./createSuborder');
83
- const getSuborder = require('./getSuborder');
83
+ const createSubOrder = require('./createSubOrder');
84
84
  const manualSplit = require('./manualSplit');
85
85
  const manualSplitByQuantity = require('./manualSplitByQuantity');
86
86
  const applyPayment = require('./applyPayment');
@@ -89,6 +89,8 @@ const getLastLocation = require('./getLastLocation');
89
89
  const getModifierRelations = require('./getModifierRelations');
90
90
  const resetItem = require('./resetItem');
91
91
  const splitItems = require('./splitItems');
92
+ const getNewItems = require('./getNewItems');
93
+ const mapSubOrders = require('./mapSubOrders');
92
94
 
93
95
  const orderActions = (deps = {}) => {
94
96
  const actions = {};
@@ -142,7 +144,8 @@ const orderActions = (deps = {}) => {
142
144
  isDetailed: isDetailed(innerDeps),
143
145
  splitByDepartments: splitByDepartments(innerDeps),
144
146
  autoSplit: autoSplit(innerDeps),
145
- syncSubOrderItemsFromParent: syncSubOrderItemsFromParent(innerDeps),
147
+ addItemsToSubOrder: addItemsToSubOrder(innerDeps),
148
+ mergeItemsWithSubOrders: mergeItemsWithSubOrders(innerDeps),
146
149
  createParent: createParent(innerDeps),
147
150
  getOrdersPieces: getOrdersPieces(innerDeps),
148
151
  getTotals: getTotals(innerDeps),
@@ -179,8 +182,7 @@ const orderActions = (deps = {}) => {
179
182
  validateFixedModifiersTotal: validateFixedModifiersTotal(innerDeps),
180
183
  removeModifiers: removeModifiers(innerDeps),
181
184
  addModifiers: addModifiers(innerDeps),
182
- createSuborder: createSuborder(innerDeps),
183
- getSuborder: getSuborder(innerDeps),
185
+ createSubOrder: createSubOrder(innerDeps),
184
186
  manualSplit: manualSplit(innerDeps),
185
187
  manualSplitByQuantity: manualSplitByQuantity(innerDeps),
186
188
  applyPayment: applyPayment(innerDeps),
@@ -189,6 +191,8 @@ const orderActions = (deps = {}) => {
189
191
  getModifierRelations: getModifierRelations(innerDeps),
190
192
  resetItem: resetItem(innerDeps),
191
193
  splitItems: splitItems(innerDeps),
194
+ getNewItems: getNewItems(innerDeps),
195
+ mapSubOrders: mapSubOrders(innerDeps),
192
196
  });
193
197
 
194
198
  Object.keys(freezedActions).forEach(actionName => {
@@ -0,0 +1,17 @@
1
+ module.exports = ({ actions }) =>
2
+ function mapSubOrders({ parentOrder, newSubOrders }) {
3
+ let subOrders = parentOrder.orders || [];
4
+
5
+ // reassign displayIds
6
+ subOrders = [...subOrders, ...newSubOrders].map((subOrder, index) => ({
7
+ ...subOrder,
8
+ displayId: `${parentOrder.displayId}-${index + 1}`,
9
+ }));
10
+
11
+ subOrders = actions.spreadModifiers({
12
+ parentOrder,
13
+ subOrders,
14
+ });
15
+
16
+ return subOrders;
17
+ };
@@ -1,5 +1,5 @@
1
1
  module.exports = () =>
2
- function syncSubOrderItemsFromParent({ parentOrder }) {
2
+ function mergeItemsWithSubOrders({ parentOrder }) {
3
3
  const parentItems = parentOrder.items || [];
4
4
  const subOrders = [...(parentOrder.orders || [])].map(subOrder => {
5
5
  const items = subOrder.items
@@ -1,14 +1,15 @@
1
1
  module.exports = ({ utils, _, actions, itemActions, modifierActions }) => {
2
2
  const { helpers } = utils;
3
3
 
4
+ const isSplitByPieces = splitUnit => splitUnit === 'pieces';
5
+
6
+ const isSplitByWeight = splitUnit => splitUnit === 'weight';
7
+
4
8
  const getDepartmentName = item => {
5
9
  const deps = itemActions.getDepartmentModifiers(item);
6
10
  return (deps.length && deps[0].name) || 'other';
7
11
  };
8
12
 
9
- const isSplitByPieces = splitUnit => splitUnit === 'pieces';
10
- const isSplitByWeight = splitUnit => splitUnit === 'weight';
11
-
12
13
  const joinItemQuantityById = orders =>
13
14
  orders.map(order => {
14
15
  const items = order.items.reduce((arr, item) => {
@@ -25,18 +26,15 @@ module.exports = ({ utils, _, actions, itemActions, modifierActions }) => {
25
26
  return { ...order, items };
26
27
  });
27
28
 
28
- return function splitByDepartments(order) {
29
- const { items } = order;
30
- const itemsByDepartments = _.groupBy(items, getDepartmentName);
31
- const orders = Object.entries(itemsByDepartments);
29
+ return function splitByDepartments({ parentOrder, newItems }) {
30
+ const itemsByDepartments = _.groupBy(newItems, getDepartmentName);
31
+ const itemGroups = Object.values(itemsByDepartments);
32
32
  let splitOrders = [];
33
33
 
34
- orders.forEach(each => {
35
- const [name, newItems] = each;
36
- const create = actions.getSuborder({ order, name });
34
+ itemGroups.forEach(items => {
37
35
  // Assuming there is one department in the item
38
- const department = itemActions.getDepartmentModifiers(newItems[0])[0];
39
- const departmentName = getDepartmentName(newItems[0]);
36
+ const department = itemActions.getDepartmentModifiers(items[0])[0];
37
+ const departmentName = getDepartmentName(items[0]);
40
38
  const maxItems = modifierActions.getDepartmentMaxItems(department);
41
39
 
42
40
  const autoSplit = _.get(
@@ -67,12 +65,12 @@ module.exports = ({ utils, _, actions, itemActions, modifierActions }) => {
67
65
  return itemActions.getTotalQuantity(itemParam);
68
66
  };
69
67
 
70
- const totalCount = getItemsTotalCount(newItems);
68
+ const totalCount = getItemsTotalCount(items);
71
69
 
72
70
  if (department && autoSplit && maxItems && totalCount > maxItems) {
73
71
  let newItemid = helpers.getObjectID();
74
72
 
75
- newItems.forEach(newItem => {
73
+ items.forEach(newItem => {
76
74
  newItemid = helpers.getObjectID();
77
75
 
78
76
  for (let j = 0; j < newItem.quantity; j += 1) {
@@ -95,12 +93,15 @@ module.exports = ({ utils, _, actions, itemActions, modifierActions }) => {
95
93
  } else {
96
94
  newItemid = helpers.getObjectID();
97
95
  splitOrders.push(
98
- create([{ ...item, _id: newItemid }], splitOrders.length + 1)
96
+ actions.createSubOrder({
97
+ parentOrder,
98
+ items: [{ ...item, _id: newItemid }],
99
+ })
99
100
  );
100
101
  }
101
102
  }
102
103
  });
103
- } else splitOrders.push(create(newItems, splitOrders.length + 1));
104
+ } else splitOrders.push(actions.createSubOrder({ parentOrder, items }));
104
105
  });
105
106
 
106
107
  splitOrders = joinItemQuantityById(splitOrders);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@darkpos/pricing",
3
- "version": "1.0.84",
3
+ "version": "1.0.86",
4
4
  "description": "Pricing calculator",
5
5
  "author": "Dark POS",
6
6
  "license": "ISC",
@@ -51,5 +51,5 @@
51
51
  "supertest": "^6.2.3",
52
52
  "supervisor": "^0.12.0"
53
53
  },
54
- "gitHead": "d15a98cdefcf27cdaf41a8096a6c6c7dcdfcc13f"
54
+ "gitHead": "feab9f47beb2312e3b82bae4eb2ec63f18d64be3"
55
55
  }
@@ -1,89 +0,0 @@
1
- const usePricing = require('../../index');
2
- const mockStores = require('../mocks/stores');
3
- const orderMock = require('../modifier/mocks/orderMock.json');
4
-
5
- const session = {
6
- store: mockStores[0],
7
- };
8
-
9
- const pricingService = usePricing(session);
10
-
11
- const mockItem = {
12
- location: {},
13
- status: {
14
- picked: {
15
- value: false,
16
- date: '',
17
- },
18
- paid: {
19
- value: false,
20
- date: '',
21
- },
22
- tracker: [],
23
- },
24
- name: 'Andre Pants 2',
25
- description: 'Andre Pants 2',
26
- pieces: 1,
27
- modifiersTotalAmount: 0,
28
- total: 2,
29
- totalPaid: 0,
30
- price: 2,
31
- quantity: 1,
32
- path: ',66da070aac2d4ae39904050f,66da131a81cf1303bc32a2c7,',
33
- notes: [],
34
- serial: null,
35
- sku: 'AND-PAN2',
36
- modifiers: [],
37
- _id: '66ec759cd57e32c3455724a0',
38
- weight: null,
39
- properties: null,
40
- hasInventory: true,
41
- inventoryType: 'pull',
42
- category: null,
43
- priceLevels: [
44
- {
45
- value: 10,
46
- tags: ['default'],
47
- },
48
- {
49
- value: 2,
50
- tags: ['vip'],
51
- },
52
- {
53
- value: 10,
54
- tags: ['default', 'press only'],
55
- },
56
- {
57
- value: 2,
58
- tags: ['vip', 'press only'],
59
- },
60
- ],
61
- costLevels: [],
62
- itemId: '66da11fdac2d4ae39904465e',
63
- menuRuleId: '66da132381cf1303bc32a2c8',
64
- __typename: 'OrderItem',
65
- subTotals: {
66
- _included: 0,
67
- _xincluded: 0,
68
- _direct: 0,
69
- _xdirect: 0,
70
- _simple: 2,
71
- _actual: 2,
72
- },
73
- };
74
-
75
- describe('Get sub order', () => {
76
- test('Get a suborder with one item', () => {
77
- const order = orderMock;
78
- const create = pricingService.order.getSuborder({ order });
79
- const newSubOrders = [create(order.items, 1)];
80
- expect(newSubOrders).toHaveLength(1);
81
- });
82
- test('Get a suborder with multiple items', () => {
83
- const order = orderMock;
84
- const create = pricingService.order.getSuborder({ order });
85
- order.items = [...order.items, mockItem];
86
- const newSubOrders = [create(order.items, 2)];
87
- expect(newSubOrders).toHaveLength(1);
88
- });
89
- });
@@ -1,17 +0,0 @@
1
- module.exports = ({ actions }) =>
2
- function createSuborder(order) {
3
- const create = actions.getSuborder({ order });
4
- let subOrders = order.orders || [];
5
- const newSubOrders = [create(order.items, 1)];
6
- subOrders = [...subOrders, ...newSubOrders].map((subOrder, idx) => ({
7
- ...subOrder,
8
- displayId: `${order.displayId}-${idx + 1}`,
9
- }));
10
-
11
- subOrders = actions.spreadModifiers({
12
- parentOrder: order,
13
- subOrders,
14
- });
15
-
16
- return subOrders;
17
- };
@@ -1,36 +0,0 @@
1
- module.exports = ({ utils, _, settings }) => {
2
- const orderSettings = _.get(settings, 'order');
3
- const { helpers } = utils;
4
-
5
- const getSubOrder =
6
- ({ order }) =>
7
- (items, index) => {
8
- let displayId = '';
9
- if (orderSettings.allowSuborder)
10
- displayId = `${order.displayId}-${index}`;
11
- else if (index === 1) displayId = order.displayId;
12
-
13
- return {
14
- ...order,
15
- _id: helpers.getObjectID(),
16
- displayId,
17
- discount: 0,
18
- tax: 0,
19
- totalPaid: 0,
20
- total: 0,
21
- status: {
22
- order: 'open',
23
- delivery: order.status.delivery,
24
- detailed: order.status.detailed,
25
- fullyPaid: false,
26
- fullyPicked: false,
27
- },
28
- parentId: orderSettings.allowSuborder ? order._id : null,
29
- items,
30
- modifiers: [],
31
- notes: order.notes || [],
32
- };
33
- };
34
-
35
- return getSubOrder;
36
- };