@darkpos/pricing 1.0.159 → 1.0.162

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,39 @@
1
+ const usePricing = require('../index');
2
+
3
+ const pricingService = usePricing({});
4
+
5
+ describe('invoice.distributeTip tests', () => {
6
+ test('distributes tip proportionally across invoice options', () => {
7
+ const invoicesOptions = [{ amount: 1 }, { amount: 1 }, { amount: 1 }];
8
+
9
+ const result = pricingService.invoice.distributeTip({
10
+ invoicesOptions,
11
+ tip: 1,
12
+ totalAmountToPay: 3,
13
+ });
14
+
15
+ expect(result).not.toBe(invoicesOptions);
16
+ expect(result).toEqual([
17
+ { amount: 1, tip: 0.33 },
18
+ { amount: 1, tip: 0.33 },
19
+ { amount: 1, tip: 0.34 },
20
+ ]);
21
+ expect(invoicesOptions).toEqual([{ amount: 1 }, { amount: 1 }, { amount: 1 }]);
22
+ });
23
+
24
+ test('assigns undistributed tip to the last invoice option when total amount is zero', () => {
25
+ const invoicesOptions = [{ amount: 0 }, { amount: 0 }];
26
+
27
+ const result = pricingService.invoice.distributeTip({
28
+ invoicesOptions,
29
+ tip: 5,
30
+ totalAmountToPay: 0,
31
+ });
32
+
33
+ expect(result).toEqual([
34
+ { amount: 0, tip: 0 },
35
+ { amount: 0, tip: 5 },
36
+ ]);
37
+ expect(invoicesOptions).toEqual([{ amount: 0 }, { amount: 0 }]);
38
+ });
39
+ });
@@ -0,0 +1,125 @@
1
+ const moment = require('moment-timezone');
2
+ const usePricing = require('../../index');
3
+
4
+ const getPricingService = () =>
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
+ },
19
+ },
20
+ },
21
+ });
22
+
23
+ describe('normalizeEndDate', () => {
24
+ afterEach(() => {
25
+ jest.restoreAllMocks();
26
+ });
27
+
28
+ test('keeps end.requestDate sent in the payload', () => {
29
+ const pricingService = getPricingService();
30
+ const requestDate = '2030-01-18T22:00:00.000Z';
31
+ const order = {
32
+ end: {
33
+ requestDate,
34
+ },
35
+ };
36
+
37
+ const result = pricingService.order.normalizeEndDate({
38
+ data: order,
39
+ order,
40
+ });
41
+
42
+ expect(result.end.requestDate).toBe(requestDate);
43
+ });
44
+
45
+ test('picks end.requestDate when it is not sent in the payload', () => {
46
+ const pricingService = getPricingService();
47
+ const now = moment('2025-05-13T15:00:00Z').tz('America/New_York');
48
+ jest.spyOn(moment, 'now').mockImplementation(() => now.valueOf());
49
+
50
+ const result = pricingService.order.normalizeEndDate({
51
+ data: {},
52
+ order: {},
53
+ });
54
+
55
+ expect(result.end.requestDate).toBe('2025-05-13T21:00:00.000Z');
56
+ });
57
+
58
+ test('picks end.requestDate when requestDate is empty string', () => {
59
+ const pricingService = getPricingService();
60
+ const now = moment('2025-05-13T15:00:00Z').tz('America/New_York');
61
+ jest.spyOn(moment, 'now').mockImplementation(() => now.valueOf());
62
+
63
+ const result = pricingService.order.normalizeEndDate({
64
+ data: {
65
+ end: {
66
+ requestDate: '',
67
+ },
68
+ },
69
+ order: {
70
+ end: {
71
+ requestDate: '',
72
+ },
73
+ },
74
+ });
75
+
76
+ expect(result.end.requestDate).toBe('2025-05-13T21:00:00.000Z');
77
+ });
78
+
79
+ test('preserves previous end.requestDate when an update sends null', () => {
80
+ const pricingService = getPricingService();
81
+ const prevRequestDate = '2030-01-18T22:00:00.000Z';
82
+
83
+ const result = pricingService.order.normalizeEndDate({
84
+ data: {
85
+ end: {
86
+ requestDate: null,
87
+ },
88
+ },
89
+ order: {
90
+ end: {
91
+ requestDate: null,
92
+ },
93
+ },
94
+ prevOrder: {
95
+ end: {
96
+ requestDate: prevRequestDate,
97
+ },
98
+ },
99
+ });
100
+
101
+ expect(result.end.requestDate).toBe(prevRequestDate);
102
+ });
103
+
104
+ test('does not touch previous end.requestDate when an update omits end', () => {
105
+ const pricingService = getPricingService();
106
+ const prevRequestDate = '2030-01-18T22:00:00.000Z';
107
+ const order = {
108
+ end: {
109
+ requestDate: prevRequestDate,
110
+ },
111
+ };
112
+
113
+ const result = pricingService.order.normalizeEndDate({
114
+ data: {},
115
+ order,
116
+ prevOrder: {
117
+ end: {
118
+ requestDate: prevRequestDate,
119
+ },
120
+ },
121
+ });
122
+
123
+ expect(result).toBe(order);
124
+ });
125
+ });
@@ -243,28 +243,39 @@ describe('pickEndDate function', () => {
243
243
  expect(result).toBe('2025-05-14T15:00:00Z');
244
244
  });
245
245
 
246
- test('pickEndDate - filters out schedules with departments', () => {
246
+ test('pickEndDate - returns latest date across schedules with departments', () => {
247
247
  const schedulesWithDepartments = [
248
248
  {
249
- _id: 'schedule-with-departments',
249
+ _id: 'tailoring-schedule',
250
250
  dayOfWeek: [2],
251
251
  recommended: 'auto_recommended',
252
- addDays: 2,
252
+ addDays: 4,
253
253
  cutDay: 1,
254
- skipDays: [1],
255
- readyHour: { hour: 18, minute: 0 },
256
- cutHour: { hour: 16, minute: 0 },
257
- departments: [{ _id: 'department-id' }],
254
+ skipDays: [],
255
+ readyHour: { hour: 17, minute: 0 },
256
+ cutHour: { hour: 10, minute: 0 },
257
+ departments: [{ _id: 'tailoring-department-id' }],
258
258
  },
259
259
  {
260
- _id: 'schedule-without-departments',
260
+ _id: 'default-schedule',
261
261
  dayOfWeek: [2],
262
262
  recommended: 'auto_recommended',
263
263
  addDays: 1,
264
264
  cutDay: 1,
265
- skipDays: [1],
266
- readyHour: { hour: 17, minute: 0 },
267
- cutHour: { hour: 16, minute: 0 },
265
+ skipDays: [],
266
+ readyHour: { hour: 15, minute: 0 },
267
+ cutHour: { hour: 0, minute: 0 },
268
+ },
269
+ {
270
+ _id: 'dt-schedule',
271
+ dayOfWeek: [2],
272
+ recommended: 'auto_recommended',
273
+ addDays: 10,
274
+ cutDay: 1,
275
+ skipDays: [],
276
+ readyHour: { hour: 0, minute: 0 },
277
+ cutHour: { hour: 0, minute: 0 },
278
+ departments: [{ _id: 'dt-department-id' }],
268
279
  },
269
280
  ];
270
281
 
@@ -276,7 +287,7 @@ describe('pickEndDate function', () => {
276
287
 
277
288
  const result = pricingService.store.pickEndDate();
278
289
 
279
- expect(result).toBe('2025-05-14T21:00:00Z');
290
+ expect(result).toBe('2025-05-24T04:00:00Z');
280
291
  });
281
292
 
282
293
  test('pickEndDate - uses provided schedules even with departments', () => {
@@ -1,3 +1,4 @@
1
+ const moment = require('moment-timezone');
1
2
  const usePricing = require('../../index');
2
3
  const mockStores = require('../mocks/stores');
3
4
  const orderSplit1 = require('../mocks/order/split-1.json');
@@ -80,6 +81,140 @@ describe('Auto Split', () => {
80
81
  expect(subOrders[0].items[2].quantity).toEqual(1000);
81
82
  });
82
83
 
84
+ test('Auto split keeps incoming end date when department has its own schedule', () => {
85
+ const defaultSchedule = {
86
+ addDays: 0,
87
+ readyHour: { hour: 17, minute: 0 },
88
+ skipDays: [],
89
+ cutHour: { hour: 16, minute: 0 },
90
+ cutDay: 1,
91
+ };
92
+ const departmentSchedule = {
93
+ addDays: 2,
94
+ readyHour: { hour: 18, minute: 0 },
95
+ skipDays: [],
96
+ cutHour: { hour: 16, minute: 0 },
97
+ cutDay: 1,
98
+ departments: [{ _id: 'laundry-department-id' }],
99
+ };
100
+ 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
+ },
109
+ },
110
+ },
111
+ });
112
+ const now = moment('2025-05-13T15:00:00Z').tz('America/New_York');
113
+ const momentNowSpy = jest
114
+ .spyOn(moment, 'now')
115
+ .mockImplementation(() => now.valueOf());
116
+
117
+ const incomingEndDate = '2025-05-14T21:00:00Z';
118
+ const departmentEndDate = pricingServiceWithSchedules.store.pickEndDate([
119
+ departmentSchedule,
120
+ ]);
121
+ const laundryModifier = {
122
+ ...laundryModNoAutoSplit,
123
+ modifierId: 'laundry-department-id',
124
+ };
125
+
126
+ const subOrders = pricingServiceWithSchedules.order.autoSplit({
127
+ parentOrder: {
128
+ items: [
129
+ {
130
+ _id: '123',
131
+ itemId: '234',
132
+ modifiers: [laundryModifier],
133
+ quantity: 1,
134
+ },
135
+ ],
136
+ end: {
137
+ requestDate: incomingEndDate,
138
+ },
139
+ status: {},
140
+ modifiers: [],
141
+ displayId: 1,
142
+ },
143
+ });
144
+
145
+ momentNowSpy.mockRestore();
146
+
147
+ expect(departmentEndDate).not.toEqual(incomingEndDate);
148
+ expect(subOrders[0].end.requestDate).toEqual(incomingEndDate);
149
+ });
150
+
151
+ test('Auto split picks department end date when incoming end date is empty', () => {
152
+ const defaultSchedule = {
153
+ addDays: 0,
154
+ readyHour: { hour: 17, minute: 0 },
155
+ skipDays: [],
156
+ cutHour: { hour: 16, minute: 0 },
157
+ cutDay: 1,
158
+ };
159
+ const departmentSchedule = {
160
+ addDays: 2,
161
+ readyHour: { hour: 18, minute: 0 },
162
+ skipDays: [],
163
+ cutHour: { hour: 16, minute: 0 },
164
+ cutDay: 1,
165
+ departments: [{ _id: 'laundry-department-id' }],
166
+ };
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
+ },
176
+ },
177
+ },
178
+ });
179
+ const now = moment('2025-05-13T15:00:00Z').tz('America/New_York');
180
+ const momentNowSpy = jest
181
+ .spyOn(moment, 'now')
182
+ .mockImplementation(() => now.valueOf());
183
+
184
+ const departmentEndDate = pricingServiceWithSchedules.store.pickEndDate([
185
+ departmentSchedule,
186
+ ]);
187
+ const laundryModifier = {
188
+ ...laundryModNoAutoSplit,
189
+ modifierId: 'laundry-department-id',
190
+ };
191
+
192
+ const subOrders = pricingServiceWithSchedules.order.autoSplit({
193
+ parentOrder: {
194
+ items: [
195
+ {
196
+ _id: '123',
197
+ itemId: '234',
198
+ modifiers: [laundryModifier],
199
+ quantity: 1,
200
+ },
201
+ ],
202
+ end: {
203
+ requestDate: '',
204
+ },
205
+ status: {},
206
+ modifiers: [],
207
+ displayId: 1,
208
+ },
209
+ });
210
+
211
+ momentNowSpy.mockRestore();
212
+
213
+ expect(subOrders[0].end.requestDate).toEqual(
214
+ new Date(departmentEndDate).toISOString()
215
+ );
216
+ });
217
+
83
218
  test('Auto split by department and Items Quantity', () => {
84
219
  const { autoSplit } = pricingService.order;
85
220
 
@@ -59,6 +59,23 @@ describe('getMaxAmountToRefund tests', () => {
59
59
  const payment = { amount: 50 };
60
60
  expect(pricingService.payment.getMaxAmountToRefund({ payment })).toBe(50);
61
61
  });
62
+
63
+ test('Subtracts tip from refundable amount', () => {
64
+ const payment = { amount: 100, tip: 15 };
65
+ expect(pricingService.payment.getMaxAmountToRefund({ payment })).toBe(85);
66
+ });
67
+ });
68
+
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);
73
+ });
74
+
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
+ });
62
79
  });
63
80
 
64
81
  describe('getRefundableOrderAmount tests', () => {
@@ -0,0 +1,44 @@
1
+ module.exports = ({ utils }) =>
2
+ function distributeTip({ invoicesOptions, tip = 0, totalAmountToPay }) {
3
+ const { math } = utils;
4
+
5
+ if (
6
+ tip <= 0 ||
7
+ !Array.isArray(invoicesOptions) ||
8
+ !invoicesOptions.length
9
+ ) {
10
+ return invoicesOptions;
11
+ }
12
+
13
+ const amountToPay =
14
+ totalAmountToPay !== undefined
15
+ ? totalAmountToPay
16
+ : invoicesOptions.reduce(
17
+ (acc, option) => math.add(acc, option.amount),
18
+ 0
19
+ );
20
+
21
+ let distributed = 0;
22
+ return invoicesOptions.map((option, idx) => {
23
+ const isLast = idx === invoicesOptions.length - 1;
24
+ if (isLast) {
25
+ return {
26
+ ...option,
27
+ tip: math.sub(tip, distributed),
28
+ };
29
+ }
30
+
31
+ const share =
32
+ amountToPay > 0
33
+ ? math.toDecimalPlaces(
34
+ math.mul(tip, math.div(option.amount, amountToPay))
35
+ )
36
+ : 0;
37
+ distributed = math.add(distributed, share);
38
+
39
+ return {
40
+ ...option,
41
+ tip: share,
42
+ };
43
+ });
44
+ };
@@ -1,5 +1,6 @@
1
1
  const getStatusByItems = require('./getStatusByItems');
2
2
  const getTotalByItems = require('./getTotalByItems');
3
+ const distributeTip = require('./distributeTip');
3
4
 
4
5
  const invoiceActions = (deps = {}) => {
5
6
  const actions = {};
@@ -12,6 +13,7 @@ const invoiceActions = (deps = {}) => {
12
13
  const freezedActions = Object.freeze({
13
14
  getStatusByItems: getStatusByItems(innerDeps),
14
15
  getTotalByItems: getTotalByItems(innerDeps),
16
+ distributeTip: distributeTip(innerDeps),
15
17
  });
16
18
 
17
19
  Object.keys(freezedActions).forEach(actionName => {
@@ -11,12 +11,12 @@ module.exports = ({ settings, _ }) =>
11
11
  null
12
12
  );
13
13
 
14
- const serialLenght = serial ? serial.length : 0;
14
+ const serialLength = serial ? serial.length : 0;
15
15
 
16
16
  const minLen = typeof minSerialLength === 'number' ? minSerialLength : null;
17
17
  const maxLen = typeof maxSerialLength === 'number' ? maxSerialLength : null;
18
18
 
19
- if (minLen != null && serialLenght < minLen) return false;
20
- if (maxLen != null && serialLenght > maxLen) return false;
19
+ if (minLen != null && serialLength < minLen) return false;
20
+ if (maxLen != null && serialLength > maxLen) return false;
21
21
  return true;
22
22
  };
@@ -105,6 +105,7 @@ const hasSerial = require('./hasSerial');
105
105
  const getOverPaidAmount = require('./getOverPaidAmount');
106
106
  const getTotalPaidToRedistribute = require('./getTotalPaidToRedistribute');
107
107
  const refundOrderItem = require('./refundOrderItem');
108
+ const normalizeEndDate = require('./normalizeEndDate');
108
109
 
109
110
  const orderActions = (deps = {}) => {
110
111
  const actions = {};
@@ -221,6 +222,7 @@ const orderActions = (deps = {}) => {
221
222
  getOverPaidAmount: getOverPaidAmount(innerDeps),
222
223
  getTotalPaidToRedistribute: getTotalPaidToRedistribute(innerDeps),
223
224
  refundOrderItem: refundOrderItem(innerDeps),
225
+ normalizeEndDate: normalizeEndDate(innerDeps),
224
226
  });
225
227
 
226
228
  Object.keys(freezedActions).forEach(actionName => {
@@ -0,0 +1,38 @@
1
+ module.exports = ({ _, actions, storeActions }) => {
2
+ const isEmptyEndRequestDate = requestDate =>
3
+ requestDate === undefined || requestDate === null || requestDate === '';
4
+
5
+ const shouldPreservePrevEndDate = ({ data, prevOrder }) =>
6
+ prevOrder &&
7
+ _.has(data, 'end') &&
8
+ isEmptyEndRequestDate(_.get(data, 'end.requestDate')) &&
9
+ !isEmptyEndRequestDate(_.get(prevOrder, 'end.requestDate'));
10
+
11
+ const preservePrevEndDate = ({ order, prevOrder }) => ({
12
+ ...order,
13
+ end: {
14
+ ...(order.end || {}),
15
+ requestDate: _.get(prevOrder, 'end.requestDate'),
16
+ },
17
+ });
18
+
19
+ const shouldPickEndDate = ({ data, order, prevOrder }) => {
20
+ if (prevOrder && !_.has(data, 'end')) return false;
21
+
22
+ const endRequestDate = _.get(order, 'end.requestDate');
23
+ return isEmptyEndRequestDate(endRequestDate);
24
+ };
25
+
26
+ return function normalizeEndDate({ data = {}, order, prevOrder }) {
27
+ if (shouldPreservePrevEndDate({ data, prevOrder })) {
28
+ return preservePrevEndDate({ order, prevOrder });
29
+ }
30
+
31
+ if (!shouldPickEndDate({ data, order, prevOrder })) return order;
32
+
33
+ return actions.changeEndDate({
34
+ date: storeActions.pickEndDate(),
35
+ order,
36
+ });
37
+ };
38
+ };
@@ -59,11 +59,8 @@ module.exports = ({
59
59
  );
60
60
  const itemGroups = Object.values(itemsByDepartments);
61
61
  let splitOrders = [];
62
- const defaultEndDate = storeActions.pickEndDate();
63
- const isDefaultEndDate =
64
- !parentOrderParam.end ||
65
- !parentOrderParam.end.requestDate ||
66
- utils.date.isEqual(defaultEndDate, parentOrderParam.end.requestDate);
62
+ const parentEndDate = _.get(parentOrderParam, 'end.requestDate');
63
+ const shouldPickDepartmentEndDate = !parentEndDate;
67
64
 
68
65
  itemGroups.forEach(items => {
69
66
  // Assuming there is one department in the item
@@ -79,7 +76,7 @@ module.exports = ({
79
76
  if (
80
77
  departmentSchedules &&
81
78
  departmentSchedules.length &&
82
- isDefaultEndDate
79
+ shouldPickDepartmentEndDate
83
80
  ) {
84
81
  parentOrder = actions.changeEndDate({
85
82
  date: storeActions.pickEndDate(departmentSchedules),
@@ -7,6 +7,7 @@ module.exports = ({ utils }) => {
7
7
  payment.amount,
8
8
  payment.credit || 0,
9
9
  payment.change || 0,
10
+ payment.tip || 0,
10
11
  payment.refund || 0
11
12
  );
12
13
  };
@@ -0,0 +1,14 @@
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
+ };
@@ -1,5 +1,6 @@
1
1
  //
2
2
  const getMaxAmountToRefund = require('./getMaxAmountToRefund');
3
+ const getMaxAmountToVoid = require('./getMaxAmountToVoid');
3
4
  const getRefundableOrderAmount = require('./getRefundableOrderAmount');
4
5
  const getMethodLabel = require('./getMethodLabel');
5
6
  const getOverPaidOrders = require('./getOverPaidOrders');
@@ -14,6 +15,7 @@ const orderActions = (deps = {}) => {
14
15
 
15
16
  const freezedActions = Object.freeze({
16
17
  getMaxAmountToRefund: getMaxAmountToRefund(innerDeps),
18
+ getMaxAmountToVoid: getMaxAmountToVoid(innerDeps),
17
19
  getRefundableOrderAmount: getRefundableOrderAmount(innerDeps),
18
20
  getMethodLabel: getMethodLabel(innerDeps),
19
21
  getOverPaidOrders: getOverPaidOrders(innerDeps),
@@ -1,6 +1,6 @@
1
1
  const { MAX_ADD_DAYS } = require('../constants/Store');
2
2
 
3
- module.exports = ({ _, moment, actions }) =>
3
+ module.exports = ({ _, moment, settings, actions }) =>
4
4
  function pickEndDate(
5
5
  schedules,
6
6
  isoWeekday,
@@ -8,75 +8,89 @@ module.exports = ({ _, moment, actions }) =>
8
8
  readyDateCapacityCounts = []
9
9
  ) {
10
10
  const timezone = actions.getStoreTimezone();
11
+ const schedulesToEvaluate =
12
+ schedules && schedules.length
13
+ ? schedules
14
+ : _.get(settings, 'order.schedules', []);
15
+
16
+ const getCandidateDate = schedule => {
17
+ const candidateSchedules = schedule ? [schedule] : undefined;
18
+ let candidateDate = actions.pickBaseEndDate(
19
+ candidateSchedules,
20
+ isoWeekday,
21
+ fromDate
22
+ );
11
23
 
12
- let candidateDate = actions.pickBaseEndDate(
13
- schedules,
14
- isoWeekday,
15
- fromDate
16
- );
24
+ const readyDateCapacityDays = actions.getReadyDateCapacityDays();
25
+ if (!actions.hasReadyDateCapacitySettings(readyDateCapacityDays))
26
+ return candidateDate;
17
27
 
18
- const readyDateCapacityDays = actions.getReadyDateCapacityDays();
19
- if (!actions.hasReadyDateCapacitySettings(readyDateCapacityDays))
20
- return candidateDate;
28
+ const countsByDate = actions.getReadyDateCountsByDate(
29
+ readyDateCapacityCounts
30
+ );
31
+ if (!countsByDate.size) return candidateDate;
21
32
 
22
- const countsByDate = actions.getReadyDateCountsByDate(
23
- readyDateCapacityCounts
24
- );
25
- if (!countsByDate.size) return candidateDate;
33
+ const inspectedDays = new Set();
26
34
 
27
- const inspectedDays = new Set();
35
+ for (let index = 0; index < MAX_ADD_DAYS; index += 1) {
36
+ const candidateMomentTZ = moment(candidateDate).tz(timezone);
37
+ const candidateLocalDateKey = candidateMomentTZ.format('YYYY-MM-DD');
28
38
 
29
- for (let index = 0; index < MAX_ADD_DAYS; index += 1) {
30
- const candidateMomentTZ = moment(candidateDate).tz(timezone);
31
- const candidateLocalDateKey = candidateMomentTZ.format('YYYY-MM-DD');
39
+ if (inspectedDays.has(candidateLocalDateKey)) break;
40
+ inspectedDays.add(candidateLocalDateKey);
32
41
 
33
- if (inspectedDays.has(candidateLocalDateKey)) break;
34
- inspectedDays.add(candidateLocalDateKey);
42
+ const capacitySettingsForDay = readyDateCapacityDays.find(
43
+ day => Number(_.get(day, 'day')) === candidateMomentTZ.isoWeekday()
44
+ );
35
45
 
36
- const capacitySettingsForDay = readyDateCapacityDays.find(
37
- day => Number(_.get(day, 'day')) === candidateMomentTZ.isoWeekday()
38
- );
46
+ if (!capacitySettingsForDay) return candidateDate;
39
47
 
40
- if (!capacitySettingsForDay) return candidateDate;
48
+ const maxOrders = actions.normalizeLimit(
49
+ _.get(capacitySettingsForDay, 'maxOrders')
50
+ );
51
+ const maxItems = actions.normalizeLimit(
52
+ _.get(capacitySettingsForDay, 'maxItems')
53
+ );
41
54
 
42
- const maxOrders = actions.normalizeLimit(
43
- _.get(capacitySettingsForDay, 'maxOrders')
44
- );
45
- const maxItems = actions.normalizeLimit(
46
- _.get(capacitySettingsForDay, 'maxItems')
47
- );
55
+ if (maxOrders <= 0 && maxItems <= 0) return candidateDate;
48
56
 
49
- if (maxOrders <= 0 && maxItems <= 0) return candidateDate;
57
+ const candidateUTCDateKey = moment
58
+ .utc(candidateDate)
59
+ .format('YYYY-MM-DD');
60
+ const dayCount = countsByDate.get(candidateUTCDateKey) || {
61
+ totalOrders: 0,
62
+ totalItems: 0,
63
+ };
50
64
 
51
- const candidateUTCDateKey = moment
52
- .utc(candidateDate)
53
- .format('YYYY-MM-DD');
54
- const dayCount = countsByDate.get(candidateUTCDateKey) || {
55
- totalOrders: 0,
56
- totalItems: 0,
57
- };
65
+ const isUnavailable =
66
+ (maxOrders > 0 && dayCount.totalOrders >= maxOrders) ||
67
+ (maxItems > 0 && dayCount.totalItems >= maxItems);
58
68
 
59
- const isUnavailable =
60
- (maxOrders > 0 && dayCount.totalOrders >= maxOrders) ||
61
- (maxItems > 0 && dayCount.totalItems >= maxItems);
69
+ if (!isUnavailable) return candidateDate;
62
70
 
63
- if (!isUnavailable) return candidateDate;
71
+ const nextDateSeed = candidateMomentTZ
72
+ .clone()
73
+ .add(1, 'day')
74
+ .startOf('day')
75
+ .toISOString();
64
76
 
65
- const nextDateSeed = candidateMomentTZ
66
- .clone()
67
- .add(1, 'day')
68
- .startOf('day')
69
- .toISOString();
77
+ const { readyHour, cutHour, cutDay, skipDays } =
78
+ actions.getSchedule(candidateSchedules, undefined, candidateDate) ||
79
+ {};
70
80
 
71
- const { readyHour, cutHour, cutDay, skipDays } =
72
- actions.getSchedule(schedules, undefined, candidateDate) || {};
81
+ candidateDate = actions.pickBaseEndDate(
82
+ [{ readyHour, cutHour, cutDay, skipDays }],
83
+ undefined,
84
+ nextDateSeed
85
+ );
86
+ }
73
87
 
74
- candidateDate = actions.pickBaseEndDate(
75
- [{ readyHour, cutHour, cutDay, skipDays }],
76
- undefined,
77
- nextDateSeed
78
- );
79
- }
88
+ return candidateDate;
89
+ };
80
90
 
81
- return candidateDate;
91
+ return (schedulesToEvaluate.length ? schedulesToEvaluate : [undefined])
92
+ .map(getCandidateDate)
93
+ .reduce((latestDate, candidateDate) =>
94
+ moment(candidateDate).isAfter(latestDate) ? candidateDate : latestDate
95
+ );
82
96
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@darkpos/pricing",
3
- "version": "1.0.159",
3
+ "version": "1.0.162",
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": "5d00359c0a4c0bac4d5668fdd4450ff9f5223dcb"
57
+ "gitHead": "abe424aaf5ba78958cf76d520a8d2307d75b957d"
58
58
  }