@darkpos/pricing 1.0.176 → 1.0.177

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,31 @@
1
+ const usePricing = require('../../index');
2
+ const mockStores = require('../mocks/stores');
3
+ const mockStoreSettings = require('../mocks/storeSettings');
4
+
5
+ const pricingService = usePricing({
6
+ store: mockStores[0],
7
+ settings: mockStoreSettings,
8
+ });
9
+
10
+ describe('createDescription', () => {
11
+ const describe4 = (name, amount) =>
12
+ pricingService.modifier.createDescription({
13
+ modifier: {
14
+ name,
15
+ compute: { type: 'fixed', action: 'add', amount: 4 },
16
+ _computed: { amount },
17
+ },
18
+ });
19
+
20
+ test('A modifier is its name and the amount it adds', () => {
21
+ expect(describe4('Silk', 4)).toBe('Silk ($4.00)');
22
+ });
23
+
24
+ test('A name that already ends in that amount does not repeat it', () => {
25
+ expect(describe4('Silk ($4.00)', 4)).toBe('Silk ($4.00)');
26
+ });
27
+
28
+ test('A name with another amount still shows the amount it adds', () => {
29
+ expect(describe4('Silk ($4.00)', 8)).toBe('Silk ($4.00) ($8.00)');
30
+ });
31
+ });
@@ -0,0 +1,48 @@
1
+ const usePricing = require('../../index');
2
+ const mockStores = require('../mocks/stores');
3
+ const mockStoreSettings = require('../mocks/storeSettings');
4
+
5
+ const pricingService = usePricing({
6
+ store: mockStores[0],
7
+ settings: mockStoreSettings,
8
+ });
9
+
10
+ describe('getName', () => {
11
+ const silk = {
12
+ _id: '6afe20d9b6d053fb1883c001',
13
+ name: 'Silk',
14
+ compute: { type: 'fixed', action: 'add', amount: 4 },
15
+ };
16
+
17
+ test('A modifier with one amount is its name', () => {
18
+ expect(pricingService.modifier.getName(silk)).toBe('Silk');
19
+ expect(pricingService.modifier.getName({ name: 'Starch' })).toBe('Starch');
20
+ expect(pricingService.modifier.getName(null)).toBe('');
21
+ });
22
+
23
+ test('A modifier with several amounts and its copies show their amount', () => {
24
+ const copy = {
25
+ ...silk,
26
+ _parentId: silk._id,
27
+ compute: { ...silk.compute, amount: 8 },
28
+ };
29
+
30
+ expect(
31
+ pricingService.modifier.getName({
32
+ ...silk,
33
+ computeAmounts: [{ _id: '1' }],
34
+ })
35
+ ).toBe('Silk ($4.00)');
36
+ expect(pricingService.modifier.getName(copy)).toBe('Silk ($8.00)');
37
+ });
38
+
39
+ test('A percentage amount shows as a percentage', () => {
40
+ const discount = {
41
+ name: 'Silk',
42
+ _parentId: silk._id,
43
+ compute: { type: 'percentage', action: 'subtract', amount: 10 },
44
+ };
45
+
46
+ expect(pricingService.modifier.getName(discount)).toBe('Silk (10%)');
47
+ });
48
+ });
@@ -0,0 +1,82 @@
1
+ const usePricing = require('../../index');
2
+ const mockStores = require('../mocks/stores');
3
+ const mockStoreSettings = require('../mocks/storeSettings');
4
+
5
+ const pricingService = usePricing({
6
+ store: mockStores[0],
7
+ settings: mockStoreSettings,
8
+ });
9
+
10
+ describe('A catalog amount copy of a modifier on an order', () => {
11
+ // The $8 copy of Silk: its _parentId links it to Silk in the catalog
12
+ const silkCopy = {
13
+ _id: '6afe20d9b6d053fb1883c006',
14
+ _parentId: '6afe20d9b6d053fb1883c001',
15
+ name: 'Silk',
16
+ tags: ['default'],
17
+ compute: { type: 'fixed', action: 'add', amount: 8 },
18
+ };
19
+
20
+ const item = {
21
+ _id: '66ec759cd57e32c3455724a0',
22
+ name: 'Shirt',
23
+ price: 10,
24
+ quantity: 1,
25
+ pieces: 1,
26
+ modifiers: [],
27
+ };
28
+
29
+ const order = {
30
+ _id: '66ec7594d57e32c34557249f',
31
+ customer: { _id: '66bb8d1e38ebaee653177a92', tags: ['default'] },
32
+ };
33
+
34
+ test('Added to an item, it is the copy itself with no parent', () => {
35
+ let nextOrder = pricingService.order.addItem({ order, item }).updatedOrder;
36
+ nextOrder = pricingService.order.addItemModifier({
37
+ order: nextOrder,
38
+ modifier: silkCopy,
39
+ itemIndex: 0,
40
+ });
41
+
42
+ const [modifier] = nextOrder.items[0].modifiers;
43
+ expect(modifier.modifierId).toBe(silkCopy._id);
44
+ expect(modifier._parentId).toBeNull();
45
+ });
46
+
47
+ test('Added with its item, it is the copy itself with no parent', () => {
48
+ const { updatedOrder } = pricingService.order.addItem({
49
+ order,
50
+ item: { ...item, modifiers: [silkCopy] },
51
+ });
52
+
53
+ const [modifier] = updatedOrder.items[0].modifiers;
54
+ expect(modifier.modifierId).toBe(silkCopy._id);
55
+ expect(modifier._parentId).toBeNull();
56
+ });
57
+
58
+ test('Added to the order, it has no parent', () => {
59
+ const nextOrder = pricingService.order.addModifier({
60
+ order: { ...order, modifiers: [] },
61
+ modifier: silkCopy,
62
+ });
63
+
64
+ expect(nextOrder.modifiers[0]._parentId).toBeNull();
65
+ });
66
+
67
+ test('A modifier already on an order keeps its parent', () => {
68
+ const child = {
69
+ ...silkCopy,
70
+ modifierId: silkCopy._id,
71
+ _parentId: '66ec3e3efe02e7ee35f595be',
72
+ };
73
+
74
+ expect(pricingService.modifier.duplicate(child)._parentId).toBe(
75
+ child._parentId
76
+ );
77
+ expect(
78
+ pricingService.modifier.getEntityModifiers({ modifiers: [child] })[0]
79
+ ._parentId
80
+ ).toBe(child._parentId);
81
+ });
82
+ });
@@ -45,5 +45,8 @@ module.exports = ({ actions, localization, _, utils }) =>
45
45
  ? localization.formatAmount(computedAmount)
46
46
  : '';
47
47
 
48
- return `${name}${localAmount ? ` (${localAmount})` : ''}`;
48
+ const amountLabel = localAmount ? ` (${localAmount})` : '';
49
+ if (amountLabel && name.endsWith(amountLabel)) return name;
50
+
51
+ return `${name}${amountLabel}`;
49
52
  };
@@ -1,10 +1,10 @@
1
- module.exports = ({ utils }) => {
1
+ module.exports = ({ utils, actions }) => {
2
2
  const { helpers } = utils;
3
3
  return function duplicate(modifier) {
4
4
  if (!modifier) return null;
5
5
 
6
6
  return {
7
- ...modifier,
7
+ ...actions.removeCatalogParent(modifier),
8
8
  modifierId: modifier.modifierId || modifier._id,
9
9
  _id: helpers.getObjectID(),
10
10
  compute: {
@@ -1,10 +1,10 @@
1
- module.exports = () =>
1
+ module.exports = ({ actions }) =>
2
2
  function getEntityModifiers(entity) {
3
3
  if (!entity || !entity.modifiers || !entity.modifiers.length) return [];
4
4
  return entity.modifiers.map(each => {
5
5
  const modifierId = each.modifierId || each._id;
6
6
  return {
7
- ...each,
7
+ ...actions.removeCatalogParent(each),
8
8
  modifierId,
9
9
  };
10
10
  });
@@ -20,7 +20,7 @@ module.exports = ({ actions }) =>
20
20
  const itemModifiers = [...enabledModifiers, ...relatedModifiers];
21
21
 
22
22
  return itemModifiers.map(each => ({
23
- ...each,
23
+ ...actions.removeCatalogParent(each),
24
24
  modifierId: each.modifierId || each._id,
25
25
  }));
26
26
  };
@@ -0,0 +1,19 @@
1
+ // The name to show for a modifier. One with several amounts shares its name with its copies, so
2
+ // each one shows its own amount, e.g. Silk ($4.00), Silk (10%)
3
+ module.exports = ({ actions, localization }) =>
4
+ function getName(modifier) {
5
+ const name = (modifier && modifier.name) || '';
6
+ const hasAmounts =
7
+ !!modifier &&
8
+ (!!modifier._parentId ||
9
+ (Array.isArray(modifier.computeAmounts) &&
10
+ modifier.computeAmounts.length > 0));
11
+ if (!hasAmounts || !modifier.compute) return name;
12
+
13
+ const { amount } = modifier.compute;
14
+ const formattedAmount = actions.isPercentage(modifier)
15
+ ? `${amount}%`
16
+ : localization.formatAmount(amount);
17
+
18
+ return `${name} (${formattedAmount})`;
19
+ };
@@ -139,6 +139,8 @@ const isFixedOverride = require('./isFixedOverride');
139
139
  const isLocked = require('./isLocked');
140
140
  const hasAddModifier = require('./hasAddModifier');
141
141
  const removeGroupRelations = require('./removeGroupRelations');
142
+ const removeCatalogParent = require('./removeCatalogParent');
143
+ const getName = require('./getName');
142
144
  const getManualModifiers = require('./getManualModifiers');
143
145
  const shouldSpreadModifier = require('./shouldSpreadModifier');
144
146
  const spreadSingleGroupModifiers = require('./spreadSingleGroupModifiers');
@@ -335,6 +337,8 @@ const modifierActions = (deps = {}) => {
335
337
  isLocked: isLocked(innerDeps),
336
338
  hasAddModifier: hasAddModifier(innerDeps),
337
339
  removeGroupRelations: removeGroupRelations(innerDeps),
340
+ removeCatalogParent: removeCatalogParent(innerDeps),
341
+ getName: getName(innerDeps),
338
342
  getManualModifiers: getManualModifiers(innerDeps),
339
343
  shouldSpreadModifier: shouldSpreadModifier(innerDeps),
340
344
  spreadSingleGroupModifiers: spreadSingleGroupModifiers(innerDeps),
@@ -0,0 +1,8 @@
1
+ // A catalog modifier has no modifierId yet: its _parentId links an amount copy to its modifier,
2
+ // which means nothing on an order
3
+ module.exports = () =>
4
+ function removeCatalogParent(modifier) {
5
+ if (!modifier || modifier.modifierId) return modifier;
6
+
7
+ return { ...modifier, _parentId: null };
8
+ };
@@ -10,6 +10,9 @@ module.exports = ({ modifierActions }) =>
10
10
 
11
11
  return {
12
12
  ...order,
13
- modifiers: [...orderModifiers, { ...modifier }],
13
+ modifiers: [
14
+ ...orderModifiers,
15
+ { ...modifierActions.removeCatalogParent(modifier) },
16
+ ],
14
17
  };
15
18
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@darkpos/pricing",
3
- "version": "1.0.176",
3
+ "version": "1.0.177",
4
4
  "description": "Pricing calculator",
5
5
  "author": "Dark POS",
6
6
  "license": "ISC",
@@ -54,5 +54,5 @@
54
54
  "supertest": "^6.2.3",
55
55
  "supervisor": "^0.12.0"
56
56
  },
57
- "gitHead": "f81d174d40b1fb5314afb5430b387ceaf50bdcc1"
57
+ "gitHead": "1892f9cf59dbdc18b4e59822bbf23cff6eba91ea"
58
58
  }