@darkpos/pricing 1.0.109 → 1.0.110

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,51 @@
1
+ const usePricing = require('../index');
2
+ const mockStores = require('./mocks/stores');
3
+
4
+ const session = {
5
+ store: mockStores[0],
6
+ };
7
+
8
+ const pricingService = usePricing(session);
9
+
10
+ describe('getMaxAmountToRefund tests', () => {
11
+ test('Returns 0 when payment is undefined', () => {
12
+ expect(
13
+ pricingService.payment.getMaxAmountToRefund({ payment: undefined })
14
+ ).toBe(0);
15
+ });
16
+
17
+ test('Returns full payment amount when no deductions', () => {
18
+ const payment = { amount: 100 };
19
+ expect(pricingService.payment.getMaxAmountToRefund({ payment })).toBe(100);
20
+ });
21
+
22
+ test('Subtracts credit only', () => {
23
+ const payment = { amount: 100, credit: 20 };
24
+ expect(pricingService.payment.getMaxAmountToRefund({ payment })).toBe(80);
25
+ });
26
+
27
+ test('Subtracts change only', () => {
28
+ const payment = { amount: 100, change: 10 };
29
+ expect(pricingService.payment.getMaxAmountToRefund({ payment })).toBe(90);
30
+ });
31
+
32
+ test('Subtracts refundedAmount only', () => {
33
+ const payment = { amount: 100, refundedAmount: 25 };
34
+ expect(pricingService.payment.getMaxAmountToRefund({ payment })).toBe(75);
35
+ });
36
+
37
+ test('Subtracts credit, change, and refundedAmount', () => {
38
+ const payment = {
39
+ amount: 100,
40
+ credit: 10,
41
+ change: 5,
42
+ refundedAmount: 20,
43
+ };
44
+ expect(pricingService.payment.getMaxAmountToRefund({ payment })).toBe(65);
45
+ });
46
+
47
+ test('Handles all fields missing gracefully', () => {
48
+ const payment = { amount: 50 };
49
+ expect(pricingService.payment.getMaxAmountToRefund({ payment })).toBe(50);
50
+ });
51
+ });
package/lib/index.js CHANGED
@@ -8,6 +8,8 @@ const makeStoreActions = require('./store');
8
8
  const makeItemActions = require('./item');
9
9
  const makeOrderActions = require('./order');
10
10
  const makeModifierActions = require('./modifier');
11
+ const makePaymentActions = require('./payment');
12
+
11
13
  const constants = require('./constants');
12
14
 
13
15
  module.exports = session => {
@@ -40,12 +42,20 @@ module.exports = session => {
40
42
  modifierActions,
41
43
  storeActions,
42
44
  });
45
+ const paymentActions = makePaymentActions({
46
+ ...deps,
47
+ itemActions,
48
+ modifierActions,
49
+ orderActions,
50
+ storeActions,
51
+ });
43
52
 
44
53
  return {
45
54
  item: itemActions,
46
55
  order: orderActions,
47
56
  modifier: modifierActions,
48
57
  store: storeActions,
58
+ payment: paymentActions,
49
59
  constants,
50
60
  };
51
61
  };
@@ -0,0 +1,13 @@
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.refundedAmount || 0
11
+ );
12
+ };
13
+ };
@@ -0,0 +1,23 @@
1
+ //
2
+ const getMaxAmountToRefund = require('./getMaxAmountToRefund');
3
+
4
+ const orderActions = (deps = {}) => {
5
+ const actions = {};
6
+
7
+ const innerDeps = {
8
+ ...deps,
9
+ actions,
10
+ };
11
+
12
+ const freezedActions = Object.freeze({
13
+ getMaxAmountToRefund: getMaxAmountToRefund(innerDeps),
14
+ });
15
+
16
+ Object.keys(freezedActions).forEach(actionName => {
17
+ actions[actionName] = freezedActions[actionName];
18
+ });
19
+
20
+ return freezedActions;
21
+ };
22
+
23
+ module.exports = orderActions;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@darkpos/pricing",
3
- "version": "1.0.109",
3
+ "version": "1.0.110",
4
4
  "description": "Pricing calculator",
5
5
  "author": "Dark POS",
6
6
  "license": "ISC",
@@ -30,6 +30,7 @@
30
30
  "test:getParentTotals": "jest --runInBand --detectOpenHandles --logHeapUsage --forceExit ./__TEST__/order/getParentTotals.test.js",
31
31
  "test:overrideModifiers": "jest --runInBand --detectOpenHandles --logHeapUsage --forceExit ./__TEST__/modifier/overrideModifiers.test.js",
32
32
  "test:addItemModifier": "jest --runInBand --detectOpenHandles --logHeapUsage --forceExit ./__TEST__/modifier/addItemModifier.test.js",
33
+ "test:payment": "jest --runInBand --detectOpenHandles --logHeapUsage --forceExit ./__TEST__/payment.test.js",
33
34
  "lint": "eslint --quiet lib/"
34
35
  },
35
36
  "publishConfig": {
@@ -54,5 +55,5 @@
54
55
  "supertest": "^6.2.3",
55
56
  "supervisor": "^0.12.0"
56
57
  },
57
- "gitHead": "a971788284c326a00e19c914ffbfdc22f1069c72"
58
+ "gitHead": "3e47ebde1cb164b785b001a37853316ec3c8e352"
58
59
  }