@darkpos/pricing 1.0.170 → 1.0.172

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.
@@ -373,4 +373,45 @@ describe('addItem function', () => {
373
373
  ],
374
374
  });
375
375
  });
376
+
377
+ test('should keep addModifiers of a required modifier (not toggle them off)', () => {
378
+ const { addItem } = pricingService.order;
379
+
380
+ const departmentModifier = {
381
+ _id: 'dept-dt',
382
+ name: 'D+T',
383
+ attributes: ['department'],
384
+ tags: ['default'],
385
+ };
386
+
387
+ const requiredGroupModifier = {
388
+ _id: 'repair-group',
389
+ name: 'Repair Blouse',
390
+ attributes: ['group'],
391
+ required: true,
392
+ tags: ['default'],
393
+ addModifiers: [departmentModifier],
394
+ delModifiers: [],
395
+ properties: { group: { modifiers: ['child-mod'], items: [] } },
396
+ };
397
+
398
+ const { updatedOrder } = addItem({
399
+ order: { items: [] },
400
+ item: {
401
+ _id: null,
402
+ itemId: 'blouse',
403
+ quantity: 1,
404
+ price: 4.5,
405
+ modifiers: [],
406
+ itemModifiers: [requiredGroupModifier],
407
+ },
408
+ itemIndex: -1,
409
+ });
410
+
411
+ const modifierIds = updatedOrder.items[0].modifiers.map(
412
+ mod => mod.modifierId
413
+ );
414
+
415
+ expect(modifierIds).toEqual(['repair-group', 'dept-dt']);
416
+ });
376
417
  });
@@ -18,63 +18,71 @@ const pricingServiceMethodLabel = usePricing({
18
18
  },
19
19
  });
20
20
 
21
- describe('getMaxAmountToRefund tests', () => {
22
- test('Returns 0 when payment is undefined', () => {
23
- expect(
24
- pricingService.payment.getMaxAmountToRefund({ payment: undefined })
25
- ).toBe(0);
21
+ describe('getMaxAmountsToRefund tests', () => {
22
+ const getMax = options =>
23
+ pricingService.payment.getMaxAmountsToRefund(options);
24
+
25
+ test('Returns zeros when payment is undefined', () => {
26
+ expect(getMax({ payment: undefined })).toEqual({
27
+ amount: 0,
28
+ credit: 0,
29
+ total: 0,
30
+ });
26
31
  });
27
32
 
28
33
  test('Returns full payment amount when no deductions', () => {
29
- const payment = { amount: 100 };
30
- expect(pricingService.payment.getMaxAmountToRefund({ payment })).toBe(100);
31
- });
32
-
33
- test('Subtracts credit only', () => {
34
- const payment = { amount: 100, credit: 20 };
35
- expect(pricingService.payment.getMaxAmountToRefund({ payment })).toBe(80);
36
- });
37
-
38
- test('Subtracts change only', () => {
39
- const payment = { amount: 100, change: 10 };
40
- expect(pricingService.payment.getMaxAmountToRefund({ payment })).toBe(90);
41
- });
42
-
43
- test('Subtracts refund only', () => {
44
- const payment = { amount: 100, refund: 25 };
45
- expect(pricingService.payment.getMaxAmountToRefund({ payment })).toBe(75);
34
+ expect(getMax({ payment: { amount: 100 } })).toEqual({
35
+ amount: 100,
36
+ credit: 0,
37
+ total: 100,
38
+ });
46
39
  });
47
40
 
48
- test('Subtracts credit, change, and refund', () => {
49
- const payment = {
50
- amount: 100,
51
- credit: 10,
52
- change: 5,
53
- refund: 20,
54
- };
55
- expect(pricingService.payment.getMaxAmountToRefund({ payment })).toBe(65);
41
+ test('Splits credit out of the order amount and includes it in total', () => {
42
+ expect(getMax({ payment: { amount: 40, credit: 10.6 } })).toEqual({
43
+ amount: 29.4,
44
+ credit: 10.6,
45
+ total: 40,
46
+ });
56
47
  });
57
48
 
58
- test('Handles all fields missing gracefully', () => {
59
- const payment = { amount: 50 };
60
- expect(pricingService.payment.getMaxAmountToRefund({ payment })).toBe(50);
49
+ test('Subtracts change and refund from the order amount', () => {
50
+ const payment = { amount: 100, credit: 10, change: 5, refund: 20 };
51
+ expect(getMax({ payment })).toEqual({ amount: 65, credit: 10, total: 75 });
61
52
  });
62
53
 
63
- test('Subtracts tip from refundable amount', () => {
64
- const payment = { amount: 100, tip: 15 };
65
- expect(pricingService.payment.getMaxAmountToRefund({ payment })).toBe(85);
54
+ test('Subtracts already refunded credit', () => {
55
+ const payment = { amount: 40, credit: 10.6, creditRefund: 4 };
56
+ expect(getMax({ payment })).toEqual({
57
+ amount: 29.4,
58
+ credit: 6.6,
59
+ total: 36,
60
+ });
66
61
  });
67
- });
68
62
 
69
- describe('getMaxAmountToVoid tests', () => {
70
- test('Does not subtract tip from voidable amount', () => {
71
- const payment = { amount: 100, tip: 15 };
72
- expect(pricingService.payment.getMaxAmountToVoid({ payment })).toBe(100);
63
+ test('Caps the credit to what the customer still has', () => {
64
+ const payment = { amount: 40, credit: 10.6 };
65
+ expect(getMax({ payment, availableCredit: 4 })).toEqual({
66
+ amount: 29.4,
67
+ credit: 4,
68
+ total: 33.4,
69
+ });
70
+ expect(getMax({ payment, availableCredit: -5 })).toEqual({
71
+ amount: 29.4,
72
+ credit: 0,
73
+ total: 29.4,
74
+ });
75
+ expect(getMax({ payment, availableCredit: 50 }).credit).toBe(10.6);
73
76
  });
74
77
 
75
- test('Subtracts credit, change, and refund', () => {
76
- const payment = { amount: 100, credit: 10, change: 5, refund: 20, tip: 15 };
77
- expect(pricingService.payment.getMaxAmountToVoid({ payment })).toBe(65);
78
+ test('Subtracts tip on refund but not on void', () => {
79
+ const payment = { amount: 100, credit: 10, tip: 15 };
80
+ expect(getMax({ payment })).toEqual({ amount: 75, credit: 10, total: 85 });
81
+ expect(getMax({ payment, isVoid: true })).toEqual({
82
+ amount: 90,
83
+ credit: 10,
84
+ total: 100,
85
+ });
78
86
  });
79
87
  });
80
88
 
@@ -239,3 +247,27 @@ describe('getMethodLabel tests', () => {
239
247
  ).toBe('');
240
248
  });
241
249
  });
250
+
251
+ describe('getCustomerStoreCredit', () => {
252
+ test('does not count gift cards or external wallet balances as store credit', () => {
253
+ expect(
254
+ pricingService.order.getCustomerStoreCredit({
255
+ customer: {
256
+ wallet: [
257
+ {
258
+ provider: 'darkpos',
259
+ cards: [
260
+ { type: 'store-credit', amount: 4 },
261
+ { type: 'gift-card', amount: 100 },
262
+ ],
263
+ },
264
+ {
265
+ provider: 'external',
266
+ cards: [{ type: 'store-credit', amount: 100 }],
267
+ },
268
+ ],
269
+ },
270
+ })
271
+ ).toBe(4);
272
+ });
273
+ });
@@ -240,16 +240,17 @@ module.exports = ({ actions, itemActions, modifierActions, settings, _ }) => {
240
240
  const [, , nextItem] = params;
241
241
 
242
242
  if (modifiersToAdd.length) {
243
- nextOrder = modifiersToAdd.reduce(
244
- (acc, modifier) =>
245
- actions.addItemModifier({
246
- itemIndex: nextItemIndex,
247
- order: acc,
248
- modifier,
249
- originalItem: item,
250
- }),
251
- nextOrder
252
- );
243
+ nextOrder = modifiersToAdd.reduce((acc, modifier) => {
244
+ const { modifiers } = acc.items[nextItemIndex];
245
+ if (modifierActions.contains(modifiers, modifier)) return acc;
246
+
247
+ return actions.addItemModifier({
248
+ itemIndex: nextItemIndex,
249
+ order: acc,
250
+ modifier,
251
+ originalItem: item,
252
+ });
253
+ }, nextOrder);
253
254
  }
254
255
 
255
256
  const { reArrangedOrder, newIndex } = reArrangeNewItem({
@@ -9,6 +9,7 @@ module.exports = ({ utils }) => {
9
9
  each && each.provider === 'darkpos' && each.cards && each.cards.length
10
10
  )
11
11
  .reduce((acc, each) => [...acc, ...each.cards], [])
12
+ .filter(each => each && each.type === 'store-credit')
12
13
  .reduce((acc, each) => math.add(acc, each.amount || 0), 0);
13
14
  };
14
15
  };
@@ -0,0 +1,31 @@
1
+ module.exports = ({ utils }) => {
2
+ const { math } = utils;
3
+
4
+ // amount: refundable to the orders, credit: store credit that can be taken back
5
+ // availableCredit: what the customer still has, credit already spent can't come back
6
+ return function getMaxAmountsToRefund({
7
+ payment,
8
+ isVoid = false,
9
+ availableCredit,
10
+ }) {
11
+ if (!payment) return { amount: 0, credit: 0, total: 0 };
12
+
13
+ const amount = math.sub(
14
+ payment.amount,
15
+ payment.credit || 0,
16
+ payment.change || 0,
17
+ isVoid ? 0 : payment.tip || 0,
18
+ payment.refund || 0
19
+ );
20
+ const paymentCredit = math.sub(
21
+ payment.credit || 0,
22
+ payment.creditRefund || 0
23
+ );
24
+ const credit =
25
+ availableCredit === undefined
26
+ ? paymentCredit
27
+ : Math.max(Math.min(paymentCredit, availableCredit), 0);
28
+
29
+ return { amount, credit, total: math.add(amount, credit) };
30
+ };
31
+ };
@@ -1,6 +1,5 @@
1
1
  //
2
- const getMaxAmountToRefund = require('./getMaxAmountToRefund');
3
- const getMaxAmountToVoid = require('./getMaxAmountToVoid');
2
+ const getMaxAmountsToRefund = require('./getMaxAmountsToRefund');
4
3
  const getRefundableOrderAmount = require('./getRefundableOrderAmount');
5
4
  const getMethodLabel = require('./getMethodLabel');
6
5
  const getOverPaidOrders = require('./getOverPaidOrders');
@@ -16,8 +15,7 @@ const orderActions = (deps = {}) => {
16
15
  };
17
16
 
18
17
  const freezedActions = Object.freeze({
19
- getMaxAmountToRefund: getMaxAmountToRefund(innerDeps),
20
- getMaxAmountToVoid: getMaxAmountToVoid(innerDeps),
18
+ getMaxAmountsToRefund: getMaxAmountsToRefund(innerDeps),
21
19
  getRefundableOrderAmount: getRefundableOrderAmount(innerDeps),
22
20
  getMethodLabel: getMethodLabel(innerDeps),
23
21
  getOverPaidOrders: getOverPaidOrders(innerDeps),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@darkpos/pricing",
3
- "version": "1.0.170",
3
+ "version": "1.0.172",
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": "46c6ec41cb87af79e5d160f6226c74ae4191d2f1"
57
+ "gitHead": "bc90d2ed9dcb3bde861ce9174d2f69fff3bb3691"
58
58
  }
@@ -1,14 +0,0 @@
1
- module.exports = ({ utils }) => {
2
- const { math } = utils;
3
-
4
- return function getMaxAmountToRefund({ payment }) {
5
- if (!payment) return 0;
6
- return math.sub(
7
- payment.amount,
8
- payment.credit || 0,
9
- payment.change || 0,
10
- payment.tip || 0,
11
- payment.refund || 0
12
- );
13
- };
14
- };
@@ -1,14 +0,0 @@
1
- module.exports = ({ utils }) => {
2
- const { math } = utils;
3
-
4
- return function getMaxAmountToVoid({ payment }) {
5
- if (!payment) return 0;
6
-
7
- return math.sub(
8
- payment.amount,
9
- payment.credit || 0,
10
- payment.change || 0,
11
- payment.refund || 0
12
- );
13
- };
14
- };