@darkpos/pricing 1.0.49 → 1.0.51

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,27 @@
1
+ const usePricing = require('../index');
2
+
3
+ const pricingService = usePricing();
4
+
5
+ describe('Modifier actions', () => {
6
+ test('CU-86dv8hzxg modifier.isGroup fn', () => {
7
+ expect(
8
+ pricingService.modifier.isGroup({
9
+ attributes: ['group'],
10
+ })
11
+ ).toBe(true);
12
+
13
+ expect(
14
+ pricingService.modifier.isGroup({
15
+ properties: {
16
+ group: {},
17
+ },
18
+ })
19
+ ).toBe(false);
20
+ expect(
21
+ pricingService.modifier.isGroup({
22
+ atrributes: [],
23
+ properties: {},
24
+ })
25
+ ).toBe(false);
26
+ });
27
+ });
@@ -353,4 +353,46 @@ describe('Auto Split', () => {
353
353
  expect(subOrders[0].items[0].quantity).toEqual(1);
354
354
  expect(subOrders[1].items[0].quantity).toEqual(1);
355
355
  });
356
+
357
+ test('Auto split -> No departments. Should return items withouth joining them.', () => {
358
+ const { autoSplit } = pricingService.order;
359
+
360
+ const item1 = {
361
+ _id: '111',
362
+ itemId: '444',
363
+ modifiers: [{ _id: '123' }],
364
+ quantity: 1,
365
+ pieces: 2,
366
+ weight: 25,
367
+ };
368
+
369
+ const item2 = {
370
+ _id: '222',
371
+ itemId: '444',
372
+ modifiers: [{ _id: '234' }, { _id: '456' }],
373
+ quantity: 5,
374
+ pieces: 2,
375
+ weight: 25,
376
+ };
377
+
378
+ const subOrders = autoSplit({
379
+ parentOrder: {
380
+ items: [item1, item2],
381
+ status: {},
382
+ modifiers: [],
383
+ displayId: 1,
384
+ },
385
+ });
386
+
387
+ expect(subOrders.length).toEqual(1);
388
+ expect(subOrders[0].items.length).toEqual(2);
389
+ expect(subOrders[0].items[0].quantity).toEqual(1);
390
+ expect(subOrders[0].items[0].modifiers.length).toEqual(1);
391
+ expect(subOrders[0].items[0].modifiers[0]._id).toEqual('123');
392
+
393
+ expect(subOrders[0].items[1].quantity).toEqual(5);
394
+ expect(subOrders[0].items[1].modifiers.length).toEqual(2);
395
+ expect(subOrders[0].items[1].modifiers[0]._id).toEqual('234');
396
+ expect(subOrders[0].items[1].modifiers[1]._id).toEqual('456');
397
+ });
356
398
  });
@@ -11,7 +11,7 @@ module.exports = ({ modifierActions, _, settings }) =>
11
11
  !modifierActions.isAmountOverride(each) &&
12
12
  !modifierActions.isDepartment(each) &&
13
13
  modifierActions.isDirect(each) &&
14
- !modifierActions.isGroupOfModifiers(each)
14
+ !modifierActions.isGroup(each)
15
15
  )
16
16
  .map(modifier => {
17
17
  const { name, _id, compute = {} } = modifier;
@@ -148,6 +148,7 @@ const isCalculatedPaymentModifier = require('./isCalculatedPaymentModifier');
148
148
  const isChild = require('./isChild');
149
149
  const isExtractCalculatedValue = require('./isExtractCalculatedValue');
150
150
  const getPriceWithoutModifier = require('./getPriceWithoutModifier');
151
+ const isGroup = require('./isGroup');
151
152
 
152
153
  const modifierActions = (deps = {}) => {
153
154
  const actions = {};
@@ -309,6 +310,7 @@ const modifierActions = (deps = {}) => {
309
310
  isChild: isChild(innerDeps),
310
311
  isExtractCalculatedValue: isExtractCalculatedValue(innerDeps),
311
312
  getPriceWithoutModifier: getPriceWithoutModifier(innerDeps),
313
+ isGroup: isGroup(innerDeps),
312
314
  });
313
315
 
314
316
  Object.keys(freezedActions).forEach(actionName => {
@@ -0,0 +1,10 @@
1
+ module.exports = ({ actions }) =>
2
+ function isGroup(modifier) {
3
+ if (!modifier) return false;
4
+ return !!(
5
+ actions.hasAttribute(modifier, 'group') ||
6
+ (modifier.properties &&
7
+ modifier.properties.group &&
8
+ Object.keys(modifier.properties.group) > 0)
9
+ );
10
+ };
@@ -1,24 +1,19 @@
1
1
  module.exports = ({ utils, _, actions, itemActions, modifierActions }) => {
2
2
  const { helpers } = utils;
3
3
 
4
- const joinItemQuantity = orders =>
4
+ const getDepartmentName = item => {
5
+ const deps = itemActions.getDepartmentModifiers(item);
6
+ return (deps.length && deps[0].name) || 'other';
7
+ };
8
+
9
+ const isSplitByPieces = splitUnit => splitUnit === 'pieces';
10
+ const isSplitByWeight = splitUnit => splitUnit === 'weight';
11
+
12
+ const joinItemQuantityById = orders =>
5
13
  orders.map(order => {
6
14
  const items = order.items.reduce((arr, item) => {
7
15
  const _items = arr;
8
- const index = _items.findIndex(each => {
9
- const preSubs = modifierActions.getSubscriptionModifiers(
10
- each.modifiers
11
- );
12
- const curSubs = modifierActions.getSubscriptionModifiers(
13
- item.modifiers
14
- );
15
- return (
16
- each.itemId === item.itemId &&
17
- ((!each.serial && !item.serial) || each.serial === item.serial) &&
18
- ((preSubs.length < 1 && curSubs.length < 1) ||
19
- (preSubs.length > 0 && curSubs.length > 0))
20
- );
21
- });
16
+ const index = _items.findIndex(each => each._id === item._id);
22
17
  if (index !== -1)
23
18
  _items[index] = {
24
19
  ..._items[index],
@@ -30,14 +25,6 @@ module.exports = ({ utils, _, actions, itemActions, modifierActions }) => {
30
25
  return { ...order, items };
31
26
  });
32
27
 
33
- const getDepartmentName = item => {
34
- const deps = itemActions.getDepartmentModifiers(item);
35
- return (deps.length && deps[0].name) || 'other';
36
- };
37
-
38
- const isSplitByPieces = splitUnit => splitUnit === 'pieces';
39
- const isSplitByWeight = splitUnit => splitUnit === 'weight';
40
-
41
28
  return function splitByDepartments(order) {
42
29
  const { items } = order;
43
30
  const itemsByDepartments = _.groupBy(items, getDepartmentName);
@@ -83,12 +70,14 @@ module.exports = ({ utils, _, actions, itemActions, modifierActions }) => {
83
70
  const totalCount = getItemsTotalCount(newItems);
84
71
 
85
72
  if (department && autoSplit && maxItems && totalCount > maxItems) {
73
+ let newItemid = helpers.getObjectID();
74
+
86
75
  newItems.forEach(newItem => {
87
76
  for (let j = 0; j < newItem.quantity; j += 1) {
88
77
  const item = {
89
78
  ...newItem,
90
79
  quantity: 1,
91
- _id: helpers.getObjectID(),
80
+ _id: newItemid,
92
81
  };
93
82
  const totalCountPerItem = getItemTotalCount(item);
94
83
 
@@ -99,14 +88,20 @@ module.exports = ({ utils, _, actions, itemActions, modifierActions }) => {
99
88
  maxItems &&
100
89
  departmentName === getDepartmentName(newOrder.items[0])
101
90
  );
102
- if (index > -1) splitOrders[index].items.push(item);
103
- else splitOrders.push(create([item], splitOrders.length + 1));
91
+ if (index > -1) {
92
+ splitOrders[index].items.push(item);
93
+ } else {
94
+ newItemid = helpers.getObjectID();
95
+ splitOrders.push(
96
+ create([{ ...item, _id: newItemid }], splitOrders.length + 1)
97
+ );
98
+ }
104
99
  }
105
100
  });
106
101
  } else splitOrders.push(create(newItems, splitOrders.length + 1));
107
102
  });
108
103
 
109
- splitOrders = joinItemQuantity(splitOrders);
104
+ splitOrders = joinItemQuantityById(splitOrders);
110
105
 
111
106
  return splitOrders;
112
107
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@darkpos/pricing",
3
- "version": "1.0.49",
3
+ "version": "1.0.51",
4
4
  "description": "Pricing calculator",
5
5
  "author": "Dark POS",
6
6
  "license": "ISC",
@@ -17,6 +17,7 @@
17
17
  "test:validateConditions": "jest --runInBand --detectOpenHandles --logHeapUsage --forceExit ./__TEST__/order/validateConditionsCalculate.test.js",
18
18
  "test:hasModifier": "jest --runInBand --detectOpenHandles --logHeapUsage --forceExit ./__TEST__/modifier/hasModifier.test.js",
19
19
  "test:item": "jest --runInBand --detectOpenHandles --logHeapUsage --forceExit ./__TEST__/item.test.js",
20
+ "test:split": "jest --runInBand --detectOpenHandles --logHeapUsage --forceExit ./__TEST__/order/split.test.js",
20
21
  "lint": "eslint --quiet lib/"
21
22
  },
22
23
  "publishConfig": {
@@ -40,5 +41,5 @@
40
41
  "supertest": "^6.2.3",
41
42
  "supervisor": "^0.12.0"
42
43
  },
43
- "gitHead": "7f4c199f3f67a3d9d3c2d4b01fe4f04c7113f5ad"
44
+ "gitHead": "256cf9e7f41cbdf88d38713f8edae725ec5e855e"
44
45
  }