@darkpos/pricing 1.0.27 → 1.0.29

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,284 @@
1
+ const moment = require('moment-timezone');
2
+ const _ = require('lodash');
3
+ const pickEndDateFunction = require('../../lib/order/pickEndDate');
4
+
5
+ describe('pickEndDate function', () => {
6
+ test('Get calculated Order, one item', () => {
7
+ const settings = {
8
+ localization: {
9
+ locale: 'en-US',
10
+ hour12: true,
11
+ currency: 'USD',
12
+ phoneFormat: 'NATIONAL',
13
+ timezone: 'America/New_York',
14
+ defaultCountry: {
15
+ label: 'United States',
16
+ value: 'US',
17
+ },
18
+ dateFormat: 'default',
19
+ },
20
+ schedule: {
21
+ open: [
22
+ {
23
+ groupId: '666b542d5ed0ff34672d9c14',
24
+ days: [6, 4, 3, 1, 2, 5],
25
+ startTime: '04:00',
26
+ endTime: '17:05',
27
+ },
28
+ {
29
+ startTime: '07:00',
30
+ endTime: '22:04',
31
+ days: [1, 2],
32
+ },
33
+ ],
34
+ close: [
35
+ {
36
+ date: '2024-08-27',
37
+ },
38
+ ],
39
+ },
40
+ };
41
+
42
+ const pickEndDate = pickEndDateFunction({ settings, _, moment });
43
+ const schedule = {
44
+ addDays: 1,
45
+ hour: 17,
46
+ minute: 0,
47
+ skipDays: [7],
48
+ cutHour: { hour: 16, minute: 0 },
49
+ cutDay: 2,
50
+ };
51
+
52
+ const now = moment('2024-08-25T15:00:00Z').tz('America/New_York');
53
+
54
+ jest.spyOn(moment, 'now').mockImplementation(() => now.valueOf());
55
+ const result = pickEndDate(schedule);
56
+
57
+ const expectedDate = now
58
+ .clone()
59
+ .add(2, 'days')
60
+ .set({ hour: 17, minute: 0, second: 0 })
61
+ .utc()
62
+ .format();
63
+
64
+ expect(result).toBe(expectedDate);
65
+ });
66
+
67
+ test('Order falls on a closed day', () => {
68
+ const settings = {
69
+ localization: {
70
+ locale: 'en-US',
71
+ hour12: true,
72
+ currency: 'USD',
73
+ phoneFormat: 'NATIONAL',
74
+ timezone: 'America/New_York',
75
+ defaultCountry: {
76
+ label: 'United States',
77
+ value: 'US',
78
+ },
79
+ dateFormat: 'default',
80
+ },
81
+ schedule: {
82
+ open: [
83
+ {
84
+ groupId: '666b542d5ed0ff34672d9c14',
85
+ days: [1, 2, 3, 4, 5], // Monday to Friday
86
+ startTime: '04:00',
87
+ endTime: '17:05',
88
+ },
89
+ ],
90
+ close: [
91
+ {
92
+ date: '2024-08-28', // Wednesday is closed
93
+ },
94
+ ],
95
+ },
96
+ };
97
+
98
+ const pickEndDate = pickEndDateFunction({ settings, _, moment });
99
+ const schedule = {
100
+ addDays: 1,
101
+ hour: 17,
102
+ minute: 0,
103
+ skipDays: [7], // Skip Sunday
104
+ cutHour: { hour: 16, minute: 0 },
105
+ cutDay: 2,
106
+ };
107
+
108
+ const now = moment('2024-08-27T15:00:00Z').tz('America/New_York'); // Tuesday
109
+
110
+ jest.spyOn(moment, 'now').mockImplementation(() => now.valueOf());
111
+ const result = pickEndDate(schedule);
112
+
113
+ const expectedDate = now
114
+ .clone()
115
+ .add(2, 'days') // Skips the closed Wednesday, goes to Thursday
116
+ .set({ hour: 17, minute: 0, second: 0 })
117
+ .utc()
118
+ .format();
119
+
120
+ expect(result).toBe(expectedDate);
121
+ });
122
+
123
+ test('Order calculated on a weekend', () => {
124
+ const settings = {
125
+ localization: {
126
+ locale: 'en-US',
127
+ hour12: true,
128
+ currency: 'USD',
129
+ phoneFormat: 'NATIONAL',
130
+ timezone: 'America/New_York',
131
+ defaultCountry: {
132
+ label: 'United States',
133
+ value: 'US',
134
+ },
135
+ dateFormat: 'default',
136
+ },
137
+ schedule: {
138
+ open: [
139
+ {
140
+ groupId: '666b542d5ed0ff34672d9c14',
141
+ days: [1, 2, 3, 4, 5], // Monday to Friday
142
+ startTime: '04:00',
143
+ endTime: '17:05',
144
+ },
145
+ ],
146
+ close: [],
147
+ },
148
+ };
149
+
150
+ const pickEndDate = pickEndDateFunction({ settings, _, moment });
151
+ const schedule = {
152
+ addDays: 1,
153
+ hour: 17,
154
+ minute: 0,
155
+ skipDays: [6, 7], // Skip Saturday and Sunday
156
+ cutHour: { hour: 16, minute: 0 },
157
+ cutDay: 5,
158
+ };
159
+
160
+ const now = moment('2024-08-23T15:00:00Z').tz('America/New_York'); // Friday
161
+
162
+ jest.spyOn(moment, 'now').mockImplementation(() => now.valueOf());
163
+ const result = pickEndDate(schedule);
164
+
165
+ const expectedDate = now
166
+ .clone()
167
+ .add(3, 'days') // Skips weekend, goes to Monday
168
+ .set({ hour: 17, minute: 0, second: 0 })
169
+ .utc()
170
+ .format();
171
+
172
+ expect(result).toBe(expectedDate);
173
+ });
174
+
175
+ test('Order time before cut-off hour', () => {
176
+ const settings = {
177
+ localization: {
178
+ locale: 'en-US',
179
+ hour12: true,
180
+ currency: 'USD',
181
+ phoneFormat: 'NATIONAL',
182
+ timezone: 'America/New_York',
183
+ defaultCountry: {
184
+ label: 'United States',
185
+ value: 'US',
186
+ },
187
+ dateFormat: 'default',
188
+ },
189
+ schedule: {
190
+ open: [
191
+ {
192
+ groupId: '666b542d5ed0ff34672d9c14',
193
+ days: [1, 2, 3, 4, 5], // Monday to Friday
194
+ startTime: '04:00',
195
+ endTime: '17:05',
196
+ },
197
+ ],
198
+ close: [],
199
+ },
200
+ };
201
+
202
+ const pickEndDate = pickEndDateFunction({ settings, _, moment });
203
+ const schedule = {
204
+ addDays: 0, // Same day
205
+ hour: 17,
206
+ minute: 0,
207
+ skipDays: [],
208
+ cutHour: { hour: 16, minute: 0 }, // Cut-off at 4 PM
209
+ cutDay: 5,
210
+ };
211
+
212
+ const now = moment('2024-08-25T15:00:00Z').tz('America/New_York'); // Friday, before cut-off
213
+
214
+ jest.spyOn(moment, 'now').mockImplementation(() => now.valueOf());
215
+ const result = pickEndDate(schedule);
216
+
217
+ const expectedDate = now
218
+ .clone()
219
+ .set({ hour: 17, minute: 0, second: 0 })
220
+ .utc()
221
+ .format();
222
+
223
+ expect(result).toBe(expectedDate);
224
+ });
225
+ test('Order time after cut-off hour', () => {
226
+ const settings = {
227
+ localization: {
228
+ locale: 'en-US',
229
+ hour12: true,
230
+ currency: 'USD',
231
+ phoneFormat: 'NATIONAL',
232
+ timezone: 'America/New_York',
233
+ defaultCountry: {
234
+ label: 'United States',
235
+ value: 'US',
236
+ },
237
+ dateFormat: 'default',
238
+ },
239
+ schedule: {
240
+ open: [
241
+ {
242
+ groupId: '666b542d5ed0ff34672d9c14',
243
+ days: [1, 2, 3, 4, 5], // Monday to Friday
244
+ startTime: '04:00',
245
+ endTime: '17:05',
246
+ },
247
+ ],
248
+ close: [],
249
+ },
250
+ };
251
+
252
+ const pickEndDate = pickEndDateFunction({ settings, _, moment });
253
+ const schedule = {
254
+ hour: 17,
255
+ minute: 0,
256
+ cutDay: 5,
257
+ skipDays: [],
258
+ cutHour: {
259
+ hour: 0,
260
+ minute: 0,
261
+ },
262
+ time: {
263
+ hour: 16,
264
+ minute: 0,
265
+ },
266
+ addDays: 1,
267
+ recommended: 'nothing_recommended',
268
+ };
269
+
270
+ const now = moment().tz('America/New_York'); // Friday, after cut-off
271
+
272
+ jest.spyOn(moment, 'now').mockImplementation(() => now.valueOf());
273
+ const result = pickEndDate(schedule);
274
+
275
+ const expectedDate = now
276
+ .clone()
277
+ .add(6, 'days') // Skips weekend, goes to Monday
278
+ .set({ hour: 17, minute: 0, second: 0 })
279
+ .utc()
280
+ .format();
281
+
282
+ expect(result).toBe(expectedDate);
283
+ });
284
+ });
@@ -7,13 +7,11 @@ module.exports = ({ settings, _, moment }) => {
7
7
 
8
8
  if (!Array.isArray(close)) return [];
9
9
  return close.map(closedDate => ({
10
- isoWeekDay: moment(closedDate.date).tz(timezone).isoWeekday(),
11
- date: moment(closedDate.date).tz(timezone)
10
+ date: moment(closedDate.date), // the date is saved with the user timezone, no need to convert it back here (ex: date: '2024-08-27' would become date:'2024-08-26 23:00 -4:00')
12
11
  }));
13
- }
12
+ };
14
13
 
15
14
  return function pickEndDate(schedule = {}) {
16
-
17
15
  const {
18
16
  addDays = 1,
19
17
  hour,
@@ -29,24 +27,31 @@ module.exports = ({ settings, _, moment }) => {
29
27
  const closedDays = getClosedDays();
30
28
 
31
29
  if (
32
- cutHour &&
33
- todayTZ.isAfter(moment(cutHour, 'hh:mm:ss').tz(timezone))
30
+ (cutHour && todayTZ.get('hours') > cutHour.hour) ||
31
+ (todayTZ.get('hours') >= cutHour.hour &&
32
+ todayTZ.get('minutes') > cutHour.minute)
34
33
  ) {
35
- endDateTZ = endDateTZ.add(cutDay, 'days')
34
+ endDateTZ = endDateTZ.add(cutDay, 'days');
36
35
  }
37
36
 
38
37
  let addDaysCounter = 0;
39
-
40
- while (addDaysCounter < addDays) {
38
+ // adding the skip days to the loop
39
+ const loop = addDays + skipDays.length;
40
+ while (addDaysCounter < loop) {
41
41
  endDateTZ = endDateTZ.add(1, 'days');
42
- const endDateISOWeekday = endDateTZ.isoWeekday() === 7 ? 0 : endDateTZ.isoWeekday();
43
-
44
- if (!skipDays.includes(endDateISOWeekday) && !closedDays.some(closedDay => {
45
- return closedDay.date.isSame(endDateTZ, 'day');
46
- })) {
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
+ !closedDays.some(closedDay => closedDay.date.isSame(endDateTZ, 'day'))
52
+ ) {
47
53
  addDaysCounter++;
48
- }
49
-
54
+ }
50
55
  }
51
56
 
52
57
  if (hour !== undefined) endDateTZ.set('hour', hour);
@@ -55,4 +60,4 @@ module.exports = ({ settings, _, moment }) => {
55
60
 
56
61
  return moment.utc(endDateTZ).format();
57
62
  };
58
- };
63
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@darkpos/pricing",
3
- "version": "1.0.27",
3
+ "version": "1.0.29",
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": "575378174e6efda3e4bffa65d1226e55eb09725e"
39
+ "gitHead": "2b7284fc85592dc13f7026d605c62aaa00e18459"
40
40
  }