@darkpos/pricing 1.0.171 → 1.0.173

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.
Files changed (40) hide show
  1. package/README.md +2 -5
  2. package/__TEST__/invoice.test.js +5 -1
  3. package/__TEST__/item/getModifierTags.test.js +1 -1
  4. package/__TEST__/item/unpick.item.test.js +2 -0
  5. package/__TEST__/item.test.js +2 -0
  6. package/__TEST__/mocks/storeSettings.js +427 -0
  7. package/__TEST__/mocks/stores.js +0 -429
  8. package/__TEST__/modifier/addItemModifier.test.js +2 -0
  9. package/__TEST__/modifier/hasModifier.test.js +2 -0
  10. package/__TEST__/modifier/overrideModifiers.test.js +2 -0
  11. package/__TEST__/modifier.test.js +2 -0
  12. package/__TEST__/order/addItem.test.js +2 -0
  13. package/__TEST__/order/addItemsToSubOrder.js +9 -3
  14. package/__TEST__/order/applyRefund.test.js +2 -0
  15. package/__TEST__/order/conditionsNotMet.test.js +2 -0
  16. package/__TEST__/order/createSubOrder.test.js +10 -2
  17. package/__TEST__/order/getGiftModifierSources.test.js +8 -2
  18. package/__TEST__/order/getPaidGiftModifiers.test.js +3 -3
  19. package/__TEST__/order/getParentTotals.test.js +2 -0
  20. package/__TEST__/order/getSameItems.test.js +2 -0
  21. package/__TEST__/order/hasSerial.test.js +2 -0
  22. package/__TEST__/order/manualSplit.test.js +2 -0
  23. package/__TEST__/order/normalizeEndDate.test.js +11 -13
  24. package/__TEST__/order/order-payment-modifier.test.js +2 -0
  25. package/__TEST__/order/order.test.js +8 -6
  26. package/__TEST__/order/pickEndDate.test.js +48 -54
  27. package/__TEST__/order/setPieces.test.js +2 -0
  28. package/__TEST__/order/split.test.js +14 -16
  29. package/__TEST__/order/unpick.order.test.js +2 -0
  30. package/__TEST__/payment/getPaymentItemIds.test.js +33 -9
  31. package/__TEST__/payment/getPaymentOrderIds.test.js +24 -8
  32. package/__TEST__/payment.test.js +79 -53
  33. package/lib/index.js +7 -2
  34. package/lib/order/getCustomerStoreCredit.js +1 -0
  35. package/lib/payment/getMaxAmountsToRefund.js +31 -0
  36. package/lib/payment/getMethodLabel.js +1 -1
  37. package/lib/payment/index.js +2 -4
  38. package/package.json +2 -2
  39. package/lib/payment/getMaxAmountToRefund.js +0 -14
  40. package/lib/payment/getMaxAmountToVoid.js +0 -14
@@ -3,19 +3,17 @@ const usePricing = require('../../index');
3
3
 
4
4
  const getPricingService = () =>
5
5
  usePricing({
6
- store: {
7
- _settings: {
8
- order: {
9
- schedules: [
10
- {
11
- addDays: 0,
12
- readyHour: { hour: 17, minute: 0 },
13
- skipDays: [],
14
- cutHour: { hour: 16, minute: 0 },
15
- cutDay: 1,
16
- },
17
- ],
18
- },
6
+ settings: {
7
+ order: {
8
+ schedules: [
9
+ {
10
+ addDays: 0,
11
+ readyHour: { hour: 17, minute: 0 },
12
+ skipDays: [],
13
+ cutHour: { hour: 16, minute: 0 },
14
+ cutDay: 1,
15
+ },
16
+ ],
19
17
  },
20
18
  },
21
19
  });
@@ -1,8 +1,10 @@
1
1
  const usePricing = require('../../index');
2
2
  const mockStores = require('../mocks/stores');
3
+ const mockStoreSettings = require('../mocks/storeSettings');
3
4
 
4
5
  const session = {
5
6
  store: mockStores[0],
7
+ settings: mockStoreSettings,
6
8
  };
7
9
 
8
10
  const pricingService = usePricing(session);
@@ -1,11 +1,13 @@
1
1
  const usePricing = require('../../index');
2
2
  const mockStores = require('../mocks/stores');
3
+ const mockStoreSettings = require('../mocks/storeSettings');
3
4
 
4
5
  const orderCredit = require('../mocks/order-credit.json');
5
6
  const { Types } = require('../../lib/constants/Modifier/Compute');
6
7
 
7
8
  const session = {
8
9
  store: mockStores[0],
10
+ settings: mockStoreSettings,
9
11
  };
10
12
 
11
13
  const pricingService = usePricing(session);
@@ -2448,7 +2450,7 @@ describe('Order actions', () => {
2448
2450
 
2449
2451
  test('Get calculated Order, multiple items and indirect modifiers #2 toNeareastMultiple', () => {
2450
2452
  const pricingService2 = usePricing({
2451
- store: { _settings: { localization: { nearestMultiple: '0.5' } } },
2453
+ settings: { localization: { nearestMultiple: '0.5' } },
2452
2454
  });
2453
2455
 
2454
2456
  const item1 = {
@@ -2502,7 +2504,7 @@ describe('Order actions', () => {
2502
2504
 
2503
2505
  test('CU-86dvd3dbp Get calculated Order, multiple items and indirect modifiers #2 toNeareastMultiple (No Order Modifiers)', () => {
2504
2506
  const pricingService2 = usePricing({
2505
- store: { _settings: { localization: { nearestMultiple: '0.5' } } },
2507
+ settings: { localization: { nearestMultiple: '0.5' } },
2506
2508
  });
2507
2509
 
2508
2510
  const item1 = {
@@ -3746,7 +3748,7 @@ describe('Order actions', () => {
3746
3748
  };
3747
3749
  const order = { items: [orderItem] };
3748
3750
  const pricing = usePricing({
3749
- store: { _settings: { order: { autoMarkAsPaid: true } } },
3751
+ settings: { order: { autoMarkAsPaid: true } },
3750
3752
  });
3751
3753
  const newOrder = pricing.order.calculate(order);
3752
3754
 
@@ -3784,7 +3786,7 @@ describe('Order actions', () => {
3784
3786
  },
3785
3787
  };
3786
3788
  const pricing = usePricing({
3787
- store: { _settings: { order: { autoMarkAsPaid: true } } },
3789
+ settings: { order: { autoMarkAsPaid: true } },
3788
3790
  });
3789
3791
  const order = { items: [orderItem], status: { paid: true } };
3790
3792
  const newOrder = pricing.order.calculate(order);
@@ -3816,7 +3818,7 @@ describe('Order actions', () => {
3816
3818
  totalPaid: 30,
3817
3819
  };
3818
3820
  const pricing = usePricing({
3819
- store: { _settings: { order: { autoMarkAsPaid: false } } },
3821
+ settings: { order: { autoMarkAsPaid: false } },
3820
3822
  });
3821
3823
  const order = { items: [orderItem], status: {}, totalPaid: 30 };
3822
3824
  const newOrder = pricing.order.calculate(order);
@@ -3835,7 +3837,7 @@ describe('Order actions', () => {
3835
3837
  quantity: 1,
3836
3838
  };
3837
3839
  const pricing = usePricing({
3838
- store: { _settings: { order: { autoMarkAsPaid: true } } },
3840
+ settings: { order: { autoMarkAsPaid: true } },
3839
3841
  });
3840
3842
  const order = { items: [orderItem] };
3841
3843
  const newOrder = pricing.order.calculate(order);
@@ -2,31 +2,29 @@ const moment = require('moment-timezone');
2
2
  const usePricing = require('../../lib/index');
3
3
 
4
4
  const getDefaultSettings = (schedules, readyDateCapacityDays = []) => ({
5
- store: {
6
- _settings: {
7
- schedule: {
8
- closed: [
9
- {
10
- date: '2024-09-03',
11
- },
12
- ],
13
- },
14
- order: {
15
- schedules: schedules || [
16
- {
17
- _id: '682373ef8256034ae1f2c195',
18
- dayOfWeek: [2],
19
- recommended: 'auto_recommended',
20
- addDays: 1,
21
- cutDay: 1,
22
- skipDays: undefined,
23
- readyHour: null,
24
- cutHour: null,
25
- },
26
- ],
27
- readyDateCapacity: {
28
- days: readyDateCapacityDays,
5
+ settings: {
6
+ schedule: {
7
+ closed: [
8
+ {
9
+ date: '2024-09-03',
10
+ },
11
+ ],
12
+ },
13
+ order: {
14
+ schedules: schedules || [
15
+ {
16
+ _id: '682373ef8256034ae1f2c195',
17
+ dayOfWeek: [2],
18
+ recommended: 'auto_recommended',
19
+ addDays: 1,
20
+ cutDay: 1,
21
+ skipDays: undefined,
22
+ readyHour: null,
23
+ cutHour: null,
29
24
  },
25
+ ],
26
+ readyDateCapacity: {
27
+ days: readyDateCapacityDays,
30
28
  },
31
29
  },
32
30
  },
@@ -191,23 +189,21 @@ describe('pickEndDate function', () => {
191
189
 
192
190
  test('pickEndDate - getRecommendedEndDate, skipDays with dayOfWeek undefined', () => {
193
191
  const pricingServiceSkipDays2 = usePricing({
194
- store: {
195
- _settings: {
196
- order: {
197
- endDate: {
198
- readySchedule: [
199
- {
200
- _id: '682373ef8256034ae1f2c195',
201
- dayOfWeek: null,
202
- recommended: 'auto_recommended',
203
- addDays: 1,
204
- cutDay: 1,
205
- skipDays: [1],
206
- readyHour: null,
207
- cutHour: null,
208
- },
209
- ],
210
- },
192
+ settings: {
193
+ order: {
194
+ endDate: {
195
+ readySchedule: [
196
+ {
197
+ _id: '682373ef8256034ae1f2c195',
198
+ dayOfWeek: null,
199
+ recommended: 'auto_recommended',
200
+ addDays: 1,
201
+ cutDay: 1,
202
+ skipDays: [1],
203
+ readyHour: null,
204
+ cutHour: null,
205
+ },
206
+ ],
211
207
  },
212
208
  },
213
209
  },
@@ -421,19 +417,17 @@ describe('pickEndDate function', () => {
421
417
  };
422
418
 
423
419
  const pricingService = usePricing({
424
- store: {
425
- _settings: {
426
- schedule: {
427
- closed: [{ date: '2026-03-01' }],
428
- },
429
- order: {
430
- schedules: [schedule],
431
- readyDateCapacity: {
432
- days: [
433
- { day: 6, maxOrders: 2, maxItems: 10 },
434
- { day: 1, maxOrders: 10, maxItems: 10 },
435
- ],
436
- },
420
+ settings: {
421
+ schedule: {
422
+ closed: [{ date: '2026-03-01' }],
423
+ },
424
+ order: {
425
+ schedules: [schedule],
426
+ readyDateCapacity: {
427
+ days: [
428
+ { day: 6, maxOrders: 2, maxItems: 10 },
429
+ { day: 1, maxOrders: 10, maxItems: 10 },
430
+ ],
437
431
  },
438
432
  },
439
433
  },
@@ -1,10 +1,12 @@
1
1
  const utils = require('@darkpos/utils');
2
2
  const usePricing = require('../../index');
3
3
  const mockStores = require('../mocks/stores');
4
+ const mockStoreSettings = require('../mocks/storeSettings');
4
5
  const orders = require('../mocks/setPieces/ordersMock');
5
6
 
6
7
  const session = {
7
8
  store: mockStores[0],
9
+ settings: mockStoreSettings,
8
10
  };
9
11
 
10
12
  const pricingService = usePricing(session);
@@ -1,10 +1,12 @@
1
1
  const moment = require('moment-timezone');
2
2
  const usePricing = require('../../index');
3
3
  const mockStores = require('../mocks/stores');
4
+ const mockStoreSettings = require('../mocks/storeSettings');
4
5
  const orderSplit1 = require('../mocks/order/split-1.json');
5
6
 
6
7
  const session = {
7
8
  store: mockStores[0],
9
+ settings: mockStoreSettings,
8
10
  };
9
11
 
10
12
  const pricingService = usePricing(session);
@@ -98,14 +100,12 @@ describe('Auto Split', () => {
98
100
  departments: [{ _id: 'laundry-department-id' }],
99
101
  };
100
102
  const pricingServiceWithSchedules = usePricing({
101
- store: {
102
- ...mockStores[0],
103
- _settings: {
104
- ...mockStores[0]._settings,
105
- order: {
106
- ...mockStores[0]._settings.order,
107
- schedules: [defaultSchedule, departmentSchedule],
108
- },
103
+ store: mockStores[0],
104
+ settings: {
105
+ ...mockStoreSettings,
106
+ order: {
107
+ ...mockStoreSettings.order,
108
+ schedules: [defaultSchedule, departmentSchedule],
109
109
  },
110
110
  },
111
111
  });
@@ -165,14 +165,12 @@ describe('Auto Split', () => {
165
165
  departments: [{ _id: 'laundry-department-id' }],
166
166
  };
167
167
  const pricingServiceWithSchedules = usePricing({
168
- store: {
169
- ...mockStores[0],
170
- _settings: {
171
- ...mockStores[0]._settings,
172
- order: {
173
- ...mockStores[0]._settings.order,
174
- schedules: [defaultSchedule, departmentSchedule],
175
- },
168
+ store: mockStores[0],
169
+ settings: {
170
+ ...mockStoreSettings,
171
+ order: {
172
+ ...mockStoreSettings.order,
173
+ schedules: [defaultSchedule, departmentSchedule],
176
174
  },
177
175
  },
178
176
  });
@@ -1,8 +1,10 @@
1
1
  const usePricing = require('../../index');
2
2
  const mockStores = require('../mocks/stores');
3
+ const mockStoreSettings = require('../mocks/storeSettings');
3
4
 
4
5
  const session = {
5
6
  store: mockStores[0],
7
+ settings: mockStoreSettings,
6
8
  };
7
9
 
8
10
  const pricingService = usePricing(session);
@@ -2,9 +2,9 @@ const usePricing = require('../../index');
2
2
 
3
3
  const pricingService = usePricing();
4
4
 
5
- const makePayment = (invoices) => ({ paymentInvoices: invoices });
5
+ const makePayment = invoices => ({ paymentInvoices: invoices });
6
6
 
7
- const makeInvoice = (itemIds) => ({
7
+ const makeInvoice = itemIds => ({
8
8
  paymentOrderItems: itemIds.map(orderItemId => ({ orderItemId })),
9
9
  });
10
10
 
@@ -18,26 +18,50 @@ describe('getPaymentItemIds', () => {
18
18
  });
19
19
 
20
20
  test('returns empty array when paymentInvoices is empty', () => {
21
- expect(pricingService.payment.getPaymentItemIds(makePayment([]))).toEqual([]);
21
+ expect(pricingService.payment.getPaymentItemIds(makePayment([]))).toEqual(
22
+ []
23
+ );
22
24
  });
23
25
 
24
26
  test('extracts itemIds from single invoice', () => {
25
27
  const payment = makePayment([makeInvoice(['item1', 'item2'])]);
26
- expect(pricingService.payment.getPaymentItemIds(payment)).toEqual(['item1', 'item2']);
28
+ expect(pricingService.payment.getPaymentItemIds(payment)).toEqual([
29
+ 'item1',
30
+ 'item2',
31
+ ]);
27
32
  });
28
33
 
29
34
  test('extracts itemIds from multiple invoices', () => {
30
- const payment = makePayment([makeInvoice(['item1']), makeInvoice(['item2', 'item3'])]);
31
- expect(pricingService.payment.getPaymentItemIds(payment)).toEqual(['item1', 'item2', 'item3']);
35
+ const payment = makePayment([
36
+ makeInvoice(['item1']),
37
+ makeInvoice(['item2', 'item3']),
38
+ ]);
39
+ expect(pricingService.payment.getPaymentItemIds(payment)).toEqual([
40
+ 'item1',
41
+ 'item2',
42
+ 'item3',
43
+ ]);
32
44
  });
33
45
 
34
46
  test('filters out null/undefined itemIds', () => {
35
- const payment = makePayment([{ paymentOrderItems: [{ orderItemId: 'item1' }, { orderItemId: null }, {}] }]);
36
- expect(pricingService.payment.getPaymentItemIds(payment)).toEqual(['item1']);
47
+ const payment = makePayment([
48
+ {
49
+ paymentOrderItems: [
50
+ { orderItemId: 'item1' },
51
+ { orderItemId: null },
52
+ {},
53
+ ],
54
+ },
55
+ ]);
56
+ expect(pricingService.payment.getPaymentItemIds(payment)).toEqual([
57
+ 'item1',
58
+ ]);
37
59
  });
38
60
 
39
61
  test('coerces itemIds to strings', () => {
40
- const payment = makePayment([{ paymentOrderItems: [{ orderItemId: 123 }] }]);
62
+ const payment = makePayment([
63
+ { paymentOrderItems: [{ orderItemId: 123 }] },
64
+ ]);
41
65
  expect(pricingService.payment.getPaymentItemIds(payment)).toEqual(['123']);
42
66
  });
43
67
 
@@ -2,9 +2,9 @@ const usePricing = require('../../index');
2
2
 
3
3
  const pricingService = usePricing();
4
4
 
5
- const makePayment = (invoices) => ({ paymentInvoices: invoices });
5
+ const makePayment = invoices => ({ paymentInvoices: invoices });
6
6
 
7
- const makeInvoice = (orderIds) => ({
7
+ const makeInvoice = orderIds => ({
8
8
  paymentOrderItems: orderIds.map(orderId => ({ orderId })),
9
9
  });
10
10
 
@@ -18,22 +18,38 @@ describe('getPaymentOrderIds', () => {
18
18
  });
19
19
 
20
20
  test('returns empty array when paymentInvoices is empty', () => {
21
- expect(pricingService.payment.getPaymentOrderIds(makePayment([]))).toEqual([]);
21
+ expect(pricingService.payment.getPaymentOrderIds(makePayment([]))).toEqual(
22
+ []
23
+ );
22
24
  });
23
25
 
24
26
  test('extracts orderIds from single invoice', () => {
25
27
  const payment = makePayment([makeInvoice(['order1', 'order2'])]);
26
- expect(pricingService.payment.getPaymentOrderIds(payment)).toEqual(['order1', 'order2']);
28
+ expect(pricingService.payment.getPaymentOrderIds(payment)).toEqual([
29
+ 'order1',
30
+ 'order2',
31
+ ]);
27
32
  });
28
33
 
29
34
  test('extracts orderIds from multiple invoices', () => {
30
- const payment = makePayment([makeInvoice(['order1']), makeInvoice(['order2', 'order3'])]);
31
- expect(pricingService.payment.getPaymentOrderIds(payment)).toEqual(['order1', 'order2', 'order3']);
35
+ const payment = makePayment([
36
+ makeInvoice(['order1']),
37
+ makeInvoice(['order2', 'order3']),
38
+ ]);
39
+ expect(pricingService.payment.getPaymentOrderIds(payment)).toEqual([
40
+ 'order1',
41
+ 'order2',
42
+ 'order3',
43
+ ]);
32
44
  });
33
45
 
34
46
  test('filters out null/undefined orderIds', () => {
35
- const payment = makePayment([{ paymentOrderItems: [{ orderId: 'order1' }, { orderId: null }, {}] }]);
36
- expect(pricingService.payment.getPaymentOrderIds(payment)).toEqual(['order1']);
47
+ const payment = makePayment([
48
+ { paymentOrderItems: [{ orderId: 'order1' }, { orderId: null }, {}] },
49
+ ]);
50
+ expect(pricingService.payment.getPaymentOrderIds(payment)).toEqual([
51
+ 'order1',
52
+ ]);
37
53
  });
38
54
 
39
55
  test('coerces orderIds to strings', () => {
@@ -1,80 +1,82 @@
1
1
  const usePricing = require('../index');
2
2
  const mockStores = require('./mocks/stores');
3
+ const mockStoreSettings = require('./mocks/storeSettings');
3
4
 
4
5
  const session = {
5
6
  store: mockStores[0],
7
+ settings: mockStoreSettings,
6
8
  };
7
9
 
8
10
  const pricingService = usePricing(session);
9
11
  const pricingServiceMethodLabel = usePricing({
10
- store: {
11
- _settings: {
12
- _settings: {
13
- payment: {
14
- methods: mockStores[0]._settings.payment.methods,
15
- },
16
- },
17
- },
18
- },
12
+ settings: { payment: { methods: mockStoreSettings.payment.methods } },
19
13
  });
20
14
 
21
- describe('getMaxAmountToRefund tests', () => {
22
- test('Returns 0 when payment is undefined', () => {
23
- expect(
24
- pricingService.payment.getMaxAmountToRefund({ payment: undefined })
25
- ).toBe(0);
26
- });
27
-
28
- 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
- });
15
+ describe('getMaxAmountsToRefund tests', () => {
16
+ const getMax = options =>
17
+ pricingService.payment.getMaxAmountsToRefund(options);
37
18
 
38
- test('Subtracts change only', () => {
39
- const payment = { amount: 100, change: 10 };
40
- expect(pricingService.payment.getMaxAmountToRefund({ payment })).toBe(90);
19
+ test('Returns zeros when payment is undefined', () => {
20
+ expect(getMax({ payment: undefined })).toEqual({
21
+ amount: 0,
22
+ credit: 0,
23
+ total: 0,
24
+ });
41
25
  });
42
26
 
43
- test('Subtracts refund only', () => {
44
- const payment = { amount: 100, refund: 25 };
45
- expect(pricingService.payment.getMaxAmountToRefund({ payment })).toBe(75);
27
+ test('Returns full payment amount when no deductions', () => {
28
+ expect(getMax({ payment: { amount: 100 } })).toEqual({
29
+ amount: 100,
30
+ credit: 0,
31
+ total: 100,
32
+ });
46
33
  });
47
34
 
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);
35
+ test('Splits credit out of the order amount and includes it in total', () => {
36
+ expect(getMax({ payment: { amount: 40, credit: 10.6 } })).toEqual({
37
+ amount: 29.4,
38
+ credit: 10.6,
39
+ total: 40,
40
+ });
56
41
  });
57
42
 
58
- test('Handles all fields missing gracefully', () => {
59
- const payment = { amount: 50 };
60
- expect(pricingService.payment.getMaxAmountToRefund({ payment })).toBe(50);
43
+ test('Subtracts change and refund from the order amount', () => {
44
+ const payment = { amount: 100, credit: 10, change: 5, refund: 20 };
45
+ expect(getMax({ payment })).toEqual({ amount: 65, credit: 10, total: 75 });
61
46
  });
62
47
 
63
- test('Subtracts tip from refundable amount', () => {
64
- const payment = { amount: 100, tip: 15 };
65
- expect(pricingService.payment.getMaxAmountToRefund({ payment })).toBe(85);
48
+ test('Subtracts already refunded credit', () => {
49
+ const payment = { amount: 40, credit: 10.6, creditRefund: 4 };
50
+ expect(getMax({ payment })).toEqual({
51
+ amount: 29.4,
52
+ credit: 6.6,
53
+ total: 36,
54
+ });
66
55
  });
67
- });
68
56
 
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);
57
+ test('Caps the credit to what the customer still has', () => {
58
+ const payment = { amount: 40, credit: 10.6 };
59
+ expect(getMax({ payment, availableCredit: 4 })).toEqual({
60
+ amount: 29.4,
61
+ credit: 4,
62
+ total: 33.4,
63
+ });
64
+ expect(getMax({ payment, availableCredit: -5 })).toEqual({
65
+ amount: 29.4,
66
+ credit: 0,
67
+ total: 29.4,
68
+ });
69
+ expect(getMax({ payment, availableCredit: 50 }).credit).toBe(10.6);
73
70
  });
74
71
 
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);
72
+ test('Subtracts tip on refund but not on void', () => {
73
+ const payment = { amount: 100, credit: 10, tip: 15 };
74
+ expect(getMax({ payment })).toEqual({ amount: 75, credit: 10, total: 85 });
75
+ expect(getMax({ payment, isVoid: true })).toEqual({
76
+ amount: 90,
77
+ credit: 10,
78
+ total: 100,
79
+ });
78
80
  });
79
81
  });
80
82
 
@@ -239,3 +241,27 @@ describe('getMethodLabel tests', () => {
239
241
  ).toBe('');
240
242
  });
241
243
  });
244
+
245
+ describe('getCustomerStoreCredit', () => {
246
+ test('does not count gift cards or external wallet balances as store credit', () => {
247
+ expect(
248
+ pricingService.order.getCustomerStoreCredit({
249
+ customer: {
250
+ wallet: [
251
+ {
252
+ provider: 'darkpos',
253
+ cards: [
254
+ { type: 'store-credit', amount: 4 },
255
+ { type: 'gift-card', amount: 100 },
256
+ ],
257
+ },
258
+ {
259
+ provider: 'external',
260
+ cards: [{ type: 'store-credit', amount: 100 }],
261
+ },
262
+ ],
263
+ },
264
+ })
265
+ ).toBe(4);
266
+ });
267
+ });
package/lib/index.js CHANGED
@@ -14,12 +14,17 @@ const makeStatementActions = require('./statement');
14
14
 
15
15
  const constants = require('./constants');
16
16
 
17
- module.exports = session => {
17
+ /**
18
+ * The pricing actions of a store:
19
+ * store: the store itself, its entity information
20
+ * settings: its settings by group, e.g. { localization: {...}, order: {...}, payment: {...} }
21
+ */
22
+ module.exports = ({ store, settings = {} } = {}) => {
18
23
  const { useLocalization } = utils;
19
- const settings = _.get(session, 'store._settings', {});
20
24
  const localization = useLocalization(settings.localization);
21
25
 
22
26
  const deps = {
27
+ store,
23
28
  settings,
24
29
  utils,
25
30
  localization,
@@ -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
+ };