@darkpos/pricing 1.0.41 → 1.0.43

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,141 @@
1
+ const _ = require('lodash');
2
+ const getItemModifiersDescriptionFunction = require('../../lib/item/getItemModifiersDescription');
3
+
4
+ describe('getItemModifiersDescription Function', () => {
5
+ const modifierActions = {
6
+ isHidden: jest.fn(),
7
+ isAmountOverride: jest.fn(),
8
+ isDepartment: jest.fn(),
9
+ isDirect: jest.fn(),
10
+ isGroupOfValues: jest.fn(),
11
+ getSelectedValues: jest.fn(),
12
+ getGroupedModifiers: jest.fn(),
13
+ getNotesToModifierTags: jest.fn(),
14
+ };
15
+
16
+ const settings = {
17
+ order: {
18
+ groupModifiers: false,
19
+ },
20
+ };
21
+
22
+ const getItemModifiersDescription = getItemModifiersDescriptionFunction({
23
+ modifierActions,
24
+ _,
25
+ settings,
26
+ });
27
+
28
+ beforeEach(() => {
29
+ jest.clearAllMocks();
30
+ });
31
+
32
+ test('should return an empty array if item or modifiers are not provided', () => {
33
+ const result = getItemModifiersDescription(null);
34
+ expect(result).toEqual([]);
35
+ });
36
+
37
+ test('should filter and map modifiers correctly', () => {
38
+ const item = {
39
+ modifiers: [
40
+ {
41
+ _id: 'mod1',
42
+ name: 'Modifier 1',
43
+ _computed: { description: 'Desc 1' },
44
+ },
45
+ { _id: 'mod2', name: 'Modifier 2' },
46
+ ],
47
+ };
48
+
49
+ modifierActions.isHidden
50
+ .mockReturnValueOnce(false)
51
+ .mockReturnValueOnce(false);
52
+ modifierActions.isAmountOverride
53
+ .mockReturnValueOnce(false)
54
+ .mockReturnValueOnce(false);
55
+ modifierActions.isDepartment
56
+ .mockReturnValueOnce(false)
57
+ .mockReturnValueOnce(false);
58
+ modifierActions.isDirect
59
+ .mockReturnValueOnce(true)
60
+ .mockReturnValueOnce(true);
61
+ modifierActions.isGroupOfValues
62
+ .mockReturnValueOnce(false)
63
+ .mockReturnValueOnce(false);
64
+ modifierActions.getNotesToModifierTags.mockReturnValueOnce([
65
+ { label: 'Desc 1', value: 'mod1' },
66
+ ]);
67
+
68
+ const result = getItemModifiersDescription(item);
69
+
70
+ expect(result).toEqual([{ label: 'Desc 1', value: 'mod1' }]);
71
+
72
+ expect(modifierActions.isHidden).toHaveBeenCalledTimes(2);
73
+ expect(modifierActions.isAmountOverride).toHaveBeenCalledTimes(2);
74
+ expect(modifierActions.isDepartment).toHaveBeenCalledTimes(2);
75
+ expect(modifierActions.isDirect).toHaveBeenCalledTimes(2);
76
+ });
77
+
78
+ test('should handle selected override options in the modifier', () => {
79
+ const item = {
80
+ modifiers: [
81
+ {
82
+ _id: 'mod1',
83
+ name: 'Modifier 1',
84
+ compute: { amount: 100 },
85
+ properties: { override: { selected: { selectedUnit: 'kg' } } },
86
+ },
87
+ ],
88
+ };
89
+
90
+ modifierActions.isHidden.mockReturnValueOnce(false);
91
+ modifierActions.isAmountOverride.mockReturnValueOnce(false);
92
+ modifierActions.isDepartment.mockReturnValueOnce(false);
93
+ modifierActions.isDirect.mockReturnValueOnce(true);
94
+ modifierActions.getNotesToModifierTags.mockReturnValueOnce([
95
+ {
96
+ label: 'Modifier 1 100 kg',
97
+ value: 'mod1',
98
+ data: item.modifiers[0],
99
+ quantity: 0,
100
+ },
101
+ ]);
102
+ const result = getItemModifiersDescription(item);
103
+
104
+ expect(result).toEqual([
105
+ {
106
+ label: 'Modifier 1 100 kg',
107
+ value: 'mod1',
108
+ data: item.modifiers[0],
109
+ quantity: 0,
110
+ },
111
+ ]);
112
+ });
113
+
114
+ test('should group modifiers when groupModifiers setting is true', () => {
115
+ const item = {
116
+ modifiers: [
117
+ {
118
+ _id: 'mod1',
119
+ name: 'Modifier 1',
120
+ _computed: { description: 'Desc 1' },
121
+ },
122
+ ],
123
+ };
124
+
125
+ settings.order.groupModifiers = true;
126
+ modifierActions.isHidden.mockReturnValueOnce(false);
127
+ modifierActions.isAmountOverride.mockReturnValueOnce(false);
128
+ modifierActions.isDepartment.mockReturnValueOnce(false);
129
+ modifierActions.isDirect.mockReturnValueOnce(true);
130
+ modifierActions.getGroupedModifiers.mockReturnValueOnce([
131
+ { label: 'Grouped Modifier', value: 'mod1' },
132
+ ]);
133
+ modifierActions.getNotesToModifierTags.mockReturnValueOnce([
134
+ { label: 'Grouped Modifier', value: 'mod1' },
135
+ ]);
136
+
137
+ const result = getItemModifiersDescription(item);
138
+
139
+ expect(result).toEqual([{ label: 'Grouped Modifier', value: 'mod1' }]);
140
+ });
141
+ });
@@ -0,0 +1,95 @@
1
+ const getGroupedModifierLabelsFunction = require('../../lib/modifier/getGroupedModifierLabels');
2
+
3
+ describe('getGroupedModifierLabels Function', () => {
4
+ const actions = {
5
+ getGroupedModifiers: jest.fn(),
6
+ };
7
+
8
+ const getGroupedModifierLabels = getGroupedModifierLabelsFunction({
9
+ actions,
10
+ });
11
+
12
+ beforeEach(() => {
13
+ jest.clearAllMocks();
14
+ });
15
+
16
+ test('should return an empty array if no modifiers are provided', () => {
17
+ actions.getGroupedModifiers.mockReturnValueOnce([]);
18
+
19
+ const result = getGroupedModifierLabels([]);
20
+
21
+ expect(result).toEqual([]);
22
+ expect(actions.getGroupedModifiers).toHaveBeenCalledWith([]);
23
+ });
24
+
25
+ test('should map modifiers to labels and group them by modifierId', () => {
26
+ const modifiers = [
27
+ { name: 'Modifier 1', modifierId: 'mod1' },
28
+ { name: 'Modifier 2', modifierId: 'mod2' },
29
+ ];
30
+
31
+ const groupedModifiers = [
32
+ { label: '1 Modifier 1', value: 'mod1', data: modifiers[0], quantity: 1 },
33
+ { label: '1 Modifier 2', value: 'mod2', data: modifiers[1], quantity: 1 },
34
+ ];
35
+
36
+ actions.getGroupedModifiers.mockReturnValueOnce(groupedModifiers);
37
+
38
+ const result = getGroupedModifierLabels(modifiers);
39
+
40
+ expect(result).toEqual(['1 Modifier 1', '1 Modifier 2']);
41
+ expect(actions.getGroupedModifiers).toHaveBeenCalledWith([
42
+ { label: 'Modifier 1', value: 'mod1', data: modifiers[0], quantity: 1 },
43
+ { label: 'Modifier 2', value: 'mod2', data: modifiers[1], quantity: 1 },
44
+ ]);
45
+ });
46
+
47
+ test('should group modifiers with the same modifierId and increment quantity', () => {
48
+ const modifiers = [
49
+ { name: 'Modifier 1', modifierId: 'mod1' },
50
+ { name: 'Modifier 1', modifierId: 'mod1' }, // Same modifierId
51
+ ];
52
+
53
+ const groupedModifiers = [
54
+ { label: '2 Modifier 1', value: 'mod1', data: modifiers[0], quantity: 2 },
55
+ ];
56
+
57
+ actions.getGroupedModifiers.mockReturnValueOnce(groupedModifiers);
58
+
59
+ const result = getGroupedModifierLabels(modifiers);
60
+
61
+ expect(result).toEqual(['2 Modifier 1']);
62
+ expect(actions.getGroupedModifiers).toHaveBeenCalledWith([
63
+ { label: 'Modifier 1', value: 'mod1', data: modifiers[0], quantity: 1 },
64
+ { label: 'Modifier 1', value: 'mod1', data: modifiers[1], quantity: 1 },
65
+ ]);
66
+ });
67
+
68
+ test('should handle a single modifier correctly', () => {
69
+ const modifiers = [{ name: 'Modifier 1', modifierId: 'mod1' }];
70
+
71
+ const groupedModifiers = [
72
+ { label: '1 Modifier 1', value: 'mod1', data: modifiers[0], quantity: 1 },
73
+ ];
74
+
75
+ actions.getGroupedModifiers.mockReturnValueOnce(groupedModifiers);
76
+
77
+ const result = getGroupedModifierLabels(modifiers);
78
+
79
+ expect(result).toEqual(['1 Modifier 1']);
80
+ expect(actions.getGroupedModifiers).toHaveBeenCalledWith([
81
+ { label: 'Modifier 1', value: 'mod1', data: modifiers[0], quantity: 1 },
82
+ ]);
83
+ });
84
+
85
+ test('should correctly handle an empty modifiers array', () => {
86
+ const modifiers = [];
87
+
88
+ actions.getGroupedModifiers.mockReturnValueOnce([]);
89
+
90
+ const result = getGroupedModifierLabels(modifiers);
91
+
92
+ expect(result).toEqual([]);
93
+ expect(actions.getGroupedModifiers).toHaveBeenCalledWith([]);
94
+ });
95
+ });
@@ -0,0 +1,65 @@
1
+ const getGroupedModifiersFunction = require('../../lib/modifier/getGroupedModifiers');
2
+
3
+ describe('getGroupedModifiers Function', () => {
4
+ const getGroupedModifiers = getGroupedModifiersFunction();
5
+
6
+ test('should return an empty array if no tags are provided', () => {
7
+ const result = getGroupedModifiers([]);
8
+ expect(result).toEqual([]);
9
+ });
10
+
11
+ test('should group modifiers by modifierId and increment quantity', () => {
12
+ const tags = [
13
+ { label: 'Modifier 1', data: { modifierId: 'mod1' }, quantity: 1 },
14
+ { label: 'Modifier 2', data: { modifierId: 'mod2' }, quantity: 1 },
15
+ { label: 'Modifier 1', data: { modifierId: 'mod1' }, quantity: 1 },
16
+ ];
17
+
18
+ const result = getGroupedModifiers(tags);
19
+
20
+ expect(result).toEqual([
21
+ { label: '2 Modifier 1', data: { modifierId: 'mod1' }, quantity: 2 },
22
+ { label: '1 Modifier 2', data: { modifierId: 'mod2' }, quantity: 1 },
23
+ ]);
24
+ });
25
+
26
+ test('should correctly handle tags with different modifierIds', () => {
27
+ const tags = [
28
+ { label: 'Modifier 1', data: { modifierId: 'mod1' }, quantity: 1 },
29
+ { label: 'Modifier 2', data: { modifierId: 'mod2' }, quantity: 1 },
30
+ ];
31
+
32
+ const result = getGroupedModifiers(tags);
33
+
34
+ expect(result).toEqual([
35
+ { label: '1 Modifier 1', data: { modifierId: 'mod1' }, quantity: 1 },
36
+ { label: '1 Modifier 2', data: { modifierId: 'mod2' }, quantity: 1 },
37
+ ]);
38
+ });
39
+
40
+ test('should not modify original tags array', () => {
41
+ const tags = [
42
+ { label: 'Modifier 1', data: { modifierId: 'mod1' }, quantity: 1 },
43
+ { label: 'Modifier 1', data: { modifierId: 'mod1' }, quantity: 1 },
44
+ ];
45
+
46
+ const originalTagsCopy = [...tags];
47
+
48
+ getGroupedModifiers(tags);
49
+
50
+ expect(tags).toEqual(originalTagsCopy); // Ensure original array is not mutated
51
+ });
52
+
53
+ test('should format the label to include the quantity', () => {
54
+ const tags = [
55
+ { label: 'Modifier 1', data: { modifierId: 'mod1' }, quantity: 1 },
56
+ { label: 'Modifier 1', data: { modifierId: 'mod1' }, quantity: 1 },
57
+ ];
58
+
59
+ const result = getGroupedModifiers(tags);
60
+
61
+ expect(result).toEqual([
62
+ { label: '2 Modifier 1', data: { modifierId: 'mod1' }, quantity: 2 },
63
+ ]);
64
+ });
65
+ });
@@ -0,0 +1,78 @@
1
+ const getNotesToModifierTagsFunction = require('../../lib/modifier/getNotesToModifierTags');
2
+
3
+ describe('getNotesToModifierTags Function', () => {
4
+ const getNotesToModifierTags = getNotesToModifierTagsFunction();
5
+
6
+ test('should return modifierTags if no notes are provided', () => {
7
+ const modifierTags = [
8
+ { label: 'Modifier 1', value: 'mod1', data: {}, quantity: 1 },
9
+ ];
10
+
11
+ const result = getNotesToModifierTags({ notes: null, modifierTags });
12
+ expect(result).toEqual(modifierTags);
13
+ });
14
+
15
+ test('should return modifierTags if notes array is empty', () => {
16
+ const modifierTags = [
17
+ { label: 'Modifier 1', value: 'mod1', data: {}, quantity: 1 },
18
+ ];
19
+
20
+ const result = getNotesToModifierTags({ notes: [], modifierTags });
21
+ expect(result).toEqual(modifierTags);
22
+ });
23
+
24
+ test('should filter out notes that are null or have a url', () => {
25
+ const notes = [
26
+ { message: 'Note 1' },
27
+ { message: 'Note 2', url: 'http://example.com' }, // Should be filtered out
28
+ null, // Should be filtered out
29
+ ];
30
+
31
+ const modifierTags = [
32
+ { label: 'Modifier 1', value: 'mod1', data: {}, quantity: 1 },
33
+ ];
34
+
35
+ const result = getNotesToModifierTags({ notes, modifierTags });
36
+
37
+ expect(result).toEqual([
38
+ { label: 'Modifier 1', value: 'mod1', data: {}, quantity: 1 },
39
+ { label: 'Note 1', value: '0', data: { message: 'Note 1' }, quantity: 1 },
40
+ ]);
41
+ });
42
+
43
+ test('should map notes to noteTags and append to modifierTags', () => {
44
+ const notes = [{ message: 'Note 1' }, { message: 'Note 2' }];
45
+
46
+ const modifierTags = [
47
+ { label: 'Modifier 1', value: 'mod1', data: {}, quantity: 1 },
48
+ ];
49
+
50
+ const result = getNotesToModifierTags({ notes, modifierTags });
51
+
52
+ expect(result).toEqual([
53
+ { label: 'Modifier 1', value: 'mod1', data: {}, quantity: 1 },
54
+ { label: 'Note 1', value: '0', data: { message: 'Note 1' }, quantity: 1 },
55
+ { label: 'Note 2', value: '1', data: { message: 'Note 2' }, quantity: 1 },
56
+ ]);
57
+ });
58
+
59
+ test('should handle case where notes contain falsy values', () => {
60
+ const notes = [null, undefined, { message: 'Valid Note' }];
61
+
62
+ const modifierTags = [
63
+ { label: 'Modifier 1', value: 'mod1', data: {}, quantity: 1 },
64
+ ];
65
+
66
+ const result = getNotesToModifierTags({ notes, modifierTags });
67
+
68
+ expect(result).toEqual([
69
+ { label: 'Modifier 1', value: 'mod1', data: {}, quantity: 1 },
70
+ {
71
+ label: 'Valid Note',
72
+ value: '0',
73
+ data: { message: 'Valid Note' },
74
+ quantity: 1,
75
+ },
76
+ ]);
77
+ });
78
+ });
@@ -0,0 +1,46 @@
1
+ const isOptionsOverrideFunction = require('../../lib/modifier/isOptionsOverride');
2
+
3
+ describe('isOptionsOverride Function', () => {
4
+ const isOptionsOverride = isOptionsOverrideFunction();
5
+
6
+ test('should return false when no modifier is provided', () => {
7
+ const result = isOptionsOverride(null);
8
+ expect(result).toBe(false);
9
+ });
10
+
11
+ test('should return false when modifier does not have properties', () => {
12
+ const modifier = { someKey: 'someValue' };
13
+ const result = isOptionsOverride(modifier);
14
+ expect(result).toBe(false);
15
+ });
16
+
17
+ test('should return false when modifier properties do not have override', () => {
18
+ const modifier = { properties: {} };
19
+ const result = isOptionsOverride(modifier);
20
+ expect(result).toBe(false);
21
+ });
22
+
23
+ test('should return false when override has no options', () => {
24
+ const modifier = {
25
+ properties: {
26
+ override: {
27
+ options: [],
28
+ },
29
+ },
30
+ };
31
+ const result = isOptionsOverride(modifier);
32
+ expect(result).toBe(false);
33
+ });
34
+
35
+ test('should return true when override has options', () => {
36
+ const modifier = {
37
+ properties: {
38
+ override: {
39
+ options: ['option1', 'option2'],
40
+ },
41
+ },
42
+ };
43
+ const result = isOptionsOverride(modifier);
44
+ expect(result).toBe(true);
45
+ });
46
+ });
@@ -56,8 +56,8 @@ describe('pickEndDate function', () => {
56
56
 
57
57
  const expectedDate = now
58
58
  .clone()
59
- .add(2, 'days')
60
- .set({ hour: 17, minute: 0, second: 0 })
59
+ .add(3, 'days')
60
+ .set({ hour: 0, minute: 0, second: 0 })
61
61
  .utc()
62
62
  .format();
63
63
 
@@ -113,7 +113,7 @@ describe('pickEndDate function', () => {
113
113
  const expectedDate = now
114
114
  .clone()
115
115
  .add(2, 'days') // Skips the closed Wednesday, goes to Thursday
116
- .set({ hour: 17, minute: 0, second: 0 })
116
+ .set({ hour: 0, minute: 0, second: 0 })
117
117
  .utc()
118
118
  .format();
119
119
 
@@ -1,22 +1,54 @@
1
- module.exports = ({ modifierActions }) =>
1
+ module.exports = ({ modifierActions, _, settings }) =>
2
2
  function getItemModifiersDescription(item) {
3
+ const groupModifiers =
4
+ settings && settings.order && settings.order.groupModifiers;
3
5
  if (!item || !Array.isArray(item.modifiers)) return [];
4
- const description = item.modifiers
6
+ let description = item.modifiers
5
7
  .filter(
6
8
  each =>
7
9
  !modifierActions.isHidden(each) &&
8
10
  !modifierActions.isAmountOverride(each) &&
11
+ !modifierActions.isDepartment(each) &&
9
12
  modifierActions.isDirect(each)
10
13
  )
11
14
  .map(modifier => {
12
- const { name } = modifier;
15
+ const { name, _id, compute = {} } = modifier;
13
16
  let label = name;
14
17
  if (modifierActions.isGroupOfValues(modifier))
15
18
  label = modifierActions.getSelectedValues(modifier) || label;
19
+ const selectedOverrideOptions = _.get(
20
+ modifier,
21
+ 'properties.override.selected'
22
+ );
23
+ if (
24
+ modifier &&
25
+ !modifier.included &&
26
+ modifier._computed &&
27
+ modifier._computed.description
28
+ ) {
29
+ label = modifier._computed.description;
30
+ }
16
31
 
17
- return label;
18
- })
19
- .join(', ');
32
+ const modifierValue = {
33
+ label,
34
+ value: _id,
35
+ data: modifier,
36
+ quantity: 1,
37
+ };
20
38
 
21
- return description;
39
+ if (selectedOverrideOptions) {
40
+ modifierValue.label = `${name} ${compute.amount} ${selectedOverrideOptions.selectedUnit}`;
41
+ modifierValue.quantity = 0;
42
+ }
43
+ return modifierValue;
44
+ });
45
+ if (groupModifiers) {
46
+ description = modifierActions.getGroupedModifiers(description);
47
+ }
48
+
49
+ const descriptions = modifierActions.getNotesToModifierTags({
50
+ notes: item.notes,
51
+ modifierTags: description,
52
+ });
53
+ return descriptions;
22
54
  };
@@ -0,0 +1,10 @@
1
+ module.exports = ({ actions }) =>
2
+ function getGroupedModifierLabels(modifiers) {
3
+ const gModifiers = modifiers.map(modifier => ({
4
+ label: modifier.name,
5
+ value: modifier.modifierId,
6
+ data: modifier,
7
+ quantity: 1,
8
+ }));
9
+ return actions.getGroupedModifiers(gModifiers).map(tag => tag.label);
10
+ };
@@ -0,0 +1,21 @@
1
+ module.exports = () =>
2
+ function getGroupedModifiers(tags) {
3
+ return tags
4
+ .reduce((acc, curr) => {
5
+ const found = acc.find(
6
+ item => item.data.modifierId === curr.data.modifierId
7
+ );
8
+
9
+ if (found) {
10
+ found.quantity += 1;
11
+ } else {
12
+ acc.push({ ...curr });
13
+ }
14
+
15
+ return acc;
16
+ }, [])
17
+ .map(tag => ({
18
+ ...tag,
19
+ label: `${tag.quantity} ${tag.label}`,
20
+ }));
21
+ };
@@ -0,0 +1,21 @@
1
+ module.exports = () =>
2
+ function getNotesToModifierTags({ notes, modifierTags }) {
3
+ let noteTags = [];
4
+ if (notes && notes.length > 0) {
5
+ noteTags =
6
+ notes
7
+ .filter(Boolean)
8
+ .filter(item => item && !item.url)
9
+ .map((note, index) => {
10
+ const { message } = note;
11
+ return {
12
+ label: message,
13
+ value: index.toString(),
14
+ data: note,
15
+ quantity: 1,
16
+ };
17
+ }) || [];
18
+ }
19
+
20
+ return modifierTags.concat(noteTags);
21
+ };
@@ -129,6 +129,10 @@ const isFixedDiscount = require('./isFixedDiscount');
129
129
  const removeLocked = require('./removeLocked');
130
130
  const hasItems = require('./hasItems');
131
131
  const getLockedModifiers = require('./getLockedModifiers');
132
+ const getGroupedModifiers = require('./getGroupedModifiers');
133
+ const getNotesToModifierTags = require('./getNotesToModifierTags');
134
+ const isOptionsOverride = require('./isOptionsOverride');
135
+ const getGroupedModifierLabels = require('./getGroupedModifierLabels');
132
136
 
133
137
  const modifierActions = (deps = {}) => {
134
138
  const actions = {};
@@ -269,6 +273,10 @@ const modifierActions = (deps = {}) => {
269
273
  isFee: isFee(innerDeps),
270
274
  isAdd: isAdd(innerDeps),
271
275
  mutateModifier: mutateModifier(innerDeps),
276
+ getGroupedModifiers: getGroupedModifiers(innerDeps),
277
+ getNotesToModifierTags: getNotesToModifierTags(innerDeps),
278
+ isOptionsOverride: isOptionsOverride(innerDeps),
279
+ getGroupedModifierLabels: getGroupedModifierLabels(innerDeps),
272
280
  });
273
281
 
274
282
  Object.keys(freezedActions).forEach(actionName => {
@@ -0,0 +1,9 @@
1
+ module.exports = () =>
2
+ function isOptionsOverride(modifier) {
3
+ if (!modifier || !modifier.properties || !modifier.properties.override)
4
+ return false;
5
+ return (
6
+ modifier.properties.override.options &&
7
+ modifier.properties.override.options.length > 0
8
+ );
9
+ };
@@ -23,42 +23,39 @@ module.exports = ({ settings, _, moment }) => {
23
23
 
24
24
  const todayTZ = moment().tz(timezone);
25
25
  let endDateTZ = todayTZ.clone();
26
-
27
26
  const closedDays = getClosedDays();
28
-
27
+ const todayHours = todayTZ.get('hours');
28
+ const todayMinutes = todayTZ.get('minutes');
29
29
  if (
30
- (cutHour && todayTZ.get('hours') > cutHour.hour) ||
31
- (todayTZ.get('hours') >= cutHour.hour &&
32
- todayTZ.get('minutes') > cutHour.minute)
30
+ (cutHour && todayHours > cutHour.hour) ||
31
+ (todayHours >= cutHour.hour && todayMinutes > cutHour.minute)
33
32
  ) {
34
- endDateTZ = endDateTZ.add(cutDay, 'days');
33
+ endDateTZ = endDateTZ.add(cutDay, 'days'); // if the cut day is zero should we add a day based on hours? ask Nabil/Vicky
35
34
  }
36
35
 
37
- let addDaysCounter = 0;
38
- // adding the skip days to the loop
39
- const loop = addDays + skipDays.length;
40
- while (addDaysCounter < loop) {
41
- endDateTZ = endDateTZ.add(1, 'days');
42
- const endDateISOWeekday = endDateTZ.isoWeekday();
43
- // does the current endDateISOWeekday match with any skip day?
44
- // is endDateISOWeekday a sunday?
45
- const shouldSkip =
46
- skipDays.includes(endDateISOWeekday) || endDateISOWeekday === 7;
47
- // if addDaysCounter is greater than loop, than we shouldn't add more days
48
- // day condition stays working until addDaysCounter is greater than loop
49
- if (
50
- !shouldSkip ||
51
- // eslint-disable-next-line no-loop-func
52
- !closedDays.some(closedDay => closedDay.date.isSame(endDateTZ, 'day'))
53
- ) {
54
- // eslint-disable-next-line no-plusplus
55
- addDaysCounter++;
36
+ endDateTZ.add(addDays, 'days');
37
+ // do the logic related to skipdays
38
+ // if the date already pass the skip day, do nothing
39
+ // if the date is the same as one of the skip day, check the next skip day if any, then add one more day
40
+ skipDays.forEach(day => {
41
+ if (todayTZ.isoWeekday() <= day) {
42
+ endDateTZ.add(1, 'days');
56
43
  }
57
- }
44
+ });
58
45
 
46
+ /**
47
+ * setting the hour and minute so we can compare with closedDays
48
+ */
59
49
  if (hour !== undefined) endDateTZ.set('hour', hour);
60
50
  if (minute !== undefined) endDateTZ.set('minute', minute);
61
- endDateTZ.set('second', 0);
51
+ closedDays.forEach(closed => {
52
+ const closedDay = moment(closed.date);
53
+ closedDay.set('hour', hour);
54
+ closedDay.set('minute', minute);
55
+ if (endDateTZ.startOf('day').isSame(closedDay, 'day')) {
56
+ endDateTZ.add(1, 'days');
57
+ }
58
+ });
62
59
 
63
60
  return moment.utc(endDateTZ).format();
64
61
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@darkpos/pricing",
3
- "version": "1.0.41",
3
+ "version": "1.0.43",
4
4
  "description": "Pricing calculator",
5
5
  "author": "Dark POS",
6
6
  "license": "ISC",
@@ -36,5 +36,5 @@
36
36
  "supertest": "^6.2.3",
37
37
  "supervisor": "^0.12.0"
38
38
  },
39
- "gitHead": "0ab9e8f6bf59ef671ffb2005712a5ec6f998b36e"
39
+ "gitHead": "60a1eef3eb458c750cda95b701904c8ea7e68157"
40
40
  }