@darkpos/pricing 1.0.63 → 1.0.65

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.
@@ -1,6 +1,5 @@
1
1
  const usePricing = require('../../index');
2
2
  const mockStores = require('../mocks/stores');
3
- const addItemObj = require('../mocks/addItemMock');
4
3
 
5
4
  const session = {
6
5
  store: mockStores[0],
@@ -3170,4 +3170,68 @@ describe('Order actions', () => {
3170
3170
  },
3171
3171
  });
3172
3172
  });
3173
+
3174
+ test('should return the latest location= tracker', () => {
3175
+ const status = {
3176
+ assembled: { location: '1', date: '2024-02-25T12:00:00Z' },
3177
+ tracker: { location: 'Truck', date: '2024-02-26T12:00:00Z' },
3178
+ racked: { location: '%AB', date: '2024-02-24T15:00:00Z' },
3179
+ };
3180
+ expect(pricingService.order.getLastLocation({ status })).toEqual({
3181
+ location: 'Truck',
3182
+ date: '2024-02-26T12:00:00Z',
3183
+ isTracker: true,
3184
+ });
3185
+ });
3186
+
3187
+ test('should return the latest location= assembled', () => {
3188
+ const status = {
3189
+ assembled: { location: '1', date: '2024-02-27T12:00:00Z' },
3190
+ tracker: { location: 'Truck', date: '2024-02-26T12:00:00Z' },
3191
+ racked: { location: '%AB', date: '2024-02-25T15:00:00Z' },
3192
+ };
3193
+ expect(pricingService.order.getLastLocation({ status })).toEqual({
3194
+ location: '1',
3195
+ date: '2024-02-27T12:00:00Z',
3196
+ isAssembled: true,
3197
+ prefix: 'spot',
3198
+ });
3199
+ });
3200
+
3201
+ test('should return the latest location= racked', () => {
3202
+ const status = {
3203
+ assembled: { location: '1', date: '2024-02-25T12:00:00Z' },
3204
+ tracker: { location: 'Truck', date: '2024-02-26T12:00:00Z' },
3205
+ racked: { location: '%AB', date: '2024-02-27T15:00:00Z' },
3206
+ };
3207
+ expect(pricingService.order.getLastLocation({ status })).toEqual({
3208
+ location: '%AB',
3209
+ date: '2024-02-27T15:00:00Z',
3210
+ isRacked: true,
3211
+ prefix: 'rack',
3212
+ });
3213
+ });
3214
+
3215
+ test('should handle null values correctly and still return the latest location', () => {
3216
+ const status = {
3217
+ assembled: null,
3218
+ tracker: { location: 'Truck', date: '2024-02-26T12:00:00Z' },
3219
+ racked: { location: '%AB', date: '2024-02-27T15:00:00Z' },
3220
+ };
3221
+ expect(pricingService.order.getLastLocation({ status })).toEqual({
3222
+ location: '%AB',
3223
+ date: '2024-02-27T15:00:00Z',
3224
+ isRacked: true,
3225
+ prefix: 'rack',
3226
+ });
3227
+ });
3228
+
3229
+ test('should return null if all locations are missing or null', () => {
3230
+ const status = {
3231
+ assembled: null,
3232
+ tracker: null,
3233
+ racked: null,
3234
+ };
3235
+ expect(pricingService.order.getLastLocation({ status })).toEqual(null);
3236
+ });
3173
3237
  });
@@ -154,16 +154,16 @@ describe('Conditions not met for the item', () => {
154
154
  expect(calculatedOrder).toHaveProperty('total', 70);
155
155
  expect(calculatedOrder).toHaveProperty('subTotal', 100);
156
156
  });
157
- test('#5: endDateDays condition is not met, item price remains the same', () => {
157
+ test('#5: endDateHours condition is not met, item price remains the same', () => {
158
158
  const order = {
159
159
  id: 'ord-123',
160
160
  items: [],
161
161
  modifiers: [],
162
162
  start: {
163
- requestDate: new Date('2020-01-01'),
163
+ requestDate: '2025-02-14T17:00:00.000',
164
164
  },
165
165
  end: {
166
- requestDate: new Date('2020-01-02'),
166
+ requestDate: '2025-02-14T20:00:00.000',
167
167
  },
168
168
  };
169
169
  const item = {
@@ -177,7 +177,7 @@ describe('Conditions not met for the item', () => {
177
177
  conditions: {
178
178
  rules: [
179
179
  {
180
- key: 'endDateDays',
180
+ key: 'endDateHours',
181
181
  value: 5,
182
182
  operand: '$gt',
183
183
  },
@@ -196,16 +196,16 @@ describe('Conditions not met for the item', () => {
196
196
  expect(calculatedOrder).toHaveProperty('total', 100);
197
197
  expect(calculatedOrder).toHaveProperty('subTotal', 100);
198
198
  });
199
- test('#6: endDateDays condition is met, item total is updated', () => {
199
+ test('#6: endDateHours condition is met, item total is updated', () => {
200
200
  const order = {
201
201
  id: 'ord-123',
202
202
  items: [],
203
203
  modifiers: [],
204
204
  start: {
205
- requestDate: new Date('2020-01-01'),
205
+ requestDate: '2025-02-14T17:00:00.000',
206
206
  },
207
207
  end: {
208
- requestDate: new Date('2020-01-08'),
208
+ requestDate: '2025-02-14T23:00:00.000',
209
209
  },
210
210
  };
211
211
  const item = {
@@ -224,7 +224,7 @@ describe('Conditions not met for the item', () => {
224
224
  conditions: {
225
225
  rules: [
226
226
  {
227
- key: 'endDateDays',
227
+ key: 'endDateHours',
228
228
  value: 5,
229
229
  operand: '$gt',
230
230
  },
@@ -235,6 +235,50 @@ describe('Conditions not met for the item', () => {
235
235
  const { calculate } = pricingService.order;
236
236
  // Calculate the order
237
237
  const calculatedOrder = calculate(order);
238
+
239
+ expect(calculatedOrder).toHaveProperty('total', 70);
240
+ expect(calculatedOrder).toHaveProperty('subTotal', 100);
241
+ });
242
+ test('#6.5: endDateHours condition is met with gte, item total is updated', () => {
243
+ const order = {
244
+ id: 'ord-123',
245
+ items: [],
246
+ modifiers: [],
247
+ start: {
248
+ requestDate: '2025-02-14T17:00:00.000',
249
+ },
250
+ end: {
251
+ requestDate: '2025-02-14T22:00:00.000',
252
+ },
253
+ };
254
+ const item = {
255
+ quantity: 1,
256
+ itemId: '123',
257
+ price: 100,
258
+ modifiers: [],
259
+ };
260
+ order.items.push(item);
261
+ const modifier = {
262
+ compute: {
263
+ amount: 30,
264
+ type: 'fixed',
265
+ action: 'subtract',
266
+ },
267
+ conditions: {
268
+ rules: [
269
+ {
270
+ key: 'endDateHours',
271
+ value: 5,
272
+ operand: '$gte',
273
+ },
274
+ ],
275
+ },
276
+ };
277
+ item.modifiers.push(modifier);
278
+ const { calculate } = pricingService.order;
279
+ // Calculate the order
280
+ const calculatedOrder = calculate(order);
281
+
238
282
  expect(calculatedOrder).toHaveProperty('total', 70);
239
283
  expect(calculatedOrder).toHaveProperty('subTotal', 100);
240
284
  });
@@ -0,0 +1,30 @@
1
+ module.exports = () =>
2
+ function getLastLocation(item) {
3
+ if (!item || !item.status) return null;
4
+
5
+ const { assembled = {}, tracker = [], racked = {} } = item.status;
6
+
7
+ let latestLocation =
8
+ Array.isArray(tracker) && tracker.length > 0
9
+ ? { ...tracker[tracker.length - 1], isTracker: true }
10
+ : {};
11
+
12
+ if (
13
+ assembled &&
14
+ assembled.date &&
15
+ (!latestLocation.date ||
16
+ new Date(assembled.date) > new Date(latestLocation.date))
17
+ ) {
18
+ latestLocation = { ...assembled, isAssembled: true, prefix: 'spot' };
19
+ }
20
+ if (
21
+ racked &&
22
+ racked.date &&
23
+ (!latestLocation.date ||
24
+ new Date(racked.date) > new Date(latestLocation.date))
25
+ ) {
26
+ latestLocation = { ...racked, isRacked: true, prefix: 'rack' };
27
+ }
28
+
29
+ return Object.keys(latestLocation).length !== 0 ? latestLocation : null;
30
+ };
package/lib/item/index.js CHANGED
@@ -47,6 +47,7 @@ const getNoteTags = require('./getNoteTags');
47
47
  const getBasePrice = require('./getBasePrice');
48
48
  const removeModifiersByQuantity = require('./removeModifiersByQuantity');
49
49
  const getModifiersBySingleValueId = require('./getModifiersBySingleValueId');
50
+ const getLastLocation = require('./getLastLocation');
50
51
 
51
52
  const itemActions = (deps = {}) => {
52
53
  const actions = {};
@@ -107,6 +108,7 @@ const itemActions = (deps = {}) => {
107
108
  getBasePrice: getBasePrice(innerDeps),
108
109
  removeModifiersByQuantity: removeModifiersByQuantity(innerDeps),
109
110
  getModifiersBySingleValueId: getModifiersBySingleValueId(innerDeps),
111
+ getLastLocation: getLastLocation(innerDeps),
110
112
  });
111
113
 
112
114
  Object.keys(freezedActions).forEach(actionName => {
@@ -24,8 +24,8 @@ module.exports = ({ actions }) => {
24
24
  condition.value,
25
25
  condition.operand
26
26
  );
27
- case 'endDateDays':
28
- return actions.validateDateDaysDiff(
27
+ case 'endDateHours':
28
+ return actions.validateDateHoursDiff(
29
29
  startRequestDate,
30
30
  endRequestDate,
31
31
  condition.value,
@@ -121,7 +121,7 @@ const getGroupedModifierLabels = require('./getGroupedModifierLabels');
121
121
  const validate = require('./validate');
122
122
  const validateNumberCondition = require('./validateNumberCondition');
123
123
  const validateRequiredModifiers = require('./validateRequiredModifiers');
124
- const validateDateDaysDiff = require('./validateDateDaysDiff');
124
+ const validateDateHoursDiff = require('./validateDateHoursDiff');
125
125
  const validateInArr = require('./validateInArr');
126
126
  const isPercentage = require('./isPercentage');
127
127
  const getChildren = require('./getChildren');
@@ -272,7 +272,7 @@ const modifierActions = (deps = {}) => {
272
272
  validate: validate(innerDeps),
273
273
  validateNumberCondition: validateNumberCondition(innerDeps),
274
274
  validateRequiredModifiers: validateRequiredModifiers(innerDeps),
275
- validateDateDaysDiff: validateDateDaysDiff(innerDeps),
275
+ validateDateHoursDiff: validateDateHoursDiff(innerDeps),
276
276
  validateInArr: validateInArr(innerDeps),
277
277
  areConditionsMet: areConditionsMet(innerDeps),
278
278
  isPercentage: isPercentage(innerDeps),
@@ -2,7 +2,7 @@ module.exports = () =>
2
2
  function validateDateDaysDiff(
3
3
  startRequestDate,
4
4
  endRequestDate,
5
- conditionNumber,
5
+ conditionNumber, // in hours
6
6
  operand
7
7
  ) {
8
8
  if (!conditionNumber && !operand) return true;
@@ -10,20 +10,21 @@ module.exports = () =>
10
10
  const diff = Math.abs(
11
11
  new Date(endRequestDate) - new Date(startRequestDate)
12
12
  );
13
- const diffDays = Math.ceil(diff / (1000 * 60 * 60 * 24));
13
+ const diffHours = Math.ceil(diff / (1000 * 60 * 60));
14
+
14
15
  switch (operand) {
15
16
  case '$eq':
16
- return diffDays === conditionNumber;
17
+ return diffHours === conditionNumber;
17
18
  case '$ne':
18
- return diffDays !== conditionNumber;
19
+ return diffHours !== conditionNumber;
19
20
  case '$gt':
20
- return diffDays > conditionNumber;
21
+ return diffHours > conditionNumber;
21
22
  case '$gte':
22
- return diffDays >= conditionNumber;
23
+ return diffHours >= conditionNumber;
23
24
  case '$lt':
24
- return diffDays < conditionNumber;
25
+ return diffHours < conditionNumber;
25
26
  case '$lte':
26
- return diffDays <= conditionNumber;
27
+ return diffHours <= conditionNumber;
27
28
  default:
28
29
  return false;
29
30
  }
@@ -0,0 +1,28 @@
1
+ module.exports = () =>
2
+ function getLastLocation(order) {
3
+ if (!order || !order.status) return null;
4
+
5
+ const { assembled = {}, tracker = {}, racked = {} } = order.status;
6
+
7
+ let latestLocation =
8
+ tracker && tracker.date ? { ...tracker, isTracker: true } : {};
9
+
10
+ if (
11
+ assembled &&
12
+ assembled.date &&
13
+ (!latestLocation.date ||
14
+ new Date(assembled.date) > new Date(latestLocation.date))
15
+ ) {
16
+ latestLocation = { ...assembled, isAssembled: true, prefix: 'spot' };
17
+ }
18
+ if (
19
+ racked &&
20
+ racked.date &&
21
+ (!latestLocation.date ||
22
+ new Date(racked.date) > new Date(latestLocation.date))
23
+ ) {
24
+ latestLocation = { ...racked, isRacked: true, prefix: 'rack' };
25
+ }
26
+
27
+ return Object.keys(latestLocation).length !== 0 ? latestLocation : null;
28
+ };
@@ -1,14 +1,7 @@
1
1
  module.exports = ({ utils, _, settings }) => {
2
2
  const orderSettings = _.get(settings, 'order');
3
- const { date, helpers } = utils;
4
- const noteDefaults = {
5
- message: '',
6
- attributes: [],
7
- date: date.toISOString(),
8
- user: null,
9
- url: '',
10
- __typename: 'Note',
11
- };
3
+ const { helpers } = utils;
4
+
12
5
  const getSubOrder =
13
6
  ({ order }) =>
14
7
  (items, index) => {
@@ -35,13 +28,7 @@ module.exports = ({ utils, _, settings }) => {
35
28
  parentId: orderSettings.allowSuborder ? order._id : null,
36
29
  items,
37
30
  modifiers: [],
38
- notes: [
39
- ...(order.notes || []),
40
- {
41
- ...noteDefaults,
42
- message: `Suborder from ${order.displayId}`, // ask about this message
43
- },
44
- ],
31
+ notes: order.notes || [],
45
32
  };
46
33
  };
47
34
 
@@ -88,6 +88,7 @@ const manualSplit = require('./manualSplit');
88
88
  const manualSplitByQuantity = require('./manualSplitByQuantity');
89
89
  const applyPayment = require('./applyPayment');
90
90
  const getBalance = require('./getBalance');
91
+ const getLastLocation = require('./getLastLocation');
91
92
 
92
93
  const orderActions = (deps = {}) => {
93
94
  const actions = {};
@@ -187,6 +188,7 @@ const orderActions = (deps = {}) => {
187
188
  manualSplitByQuantity: manualSplitByQuantity(innerDeps),
188
189
  applyPayment: applyPayment(innerDeps),
189
190
  getOrdersBalance: getOrdersBalance(innerDeps),
191
+ getLastLocation: getLastLocation(innerDeps),
190
192
  });
191
193
 
192
194
  Object.keys(freezedActions).forEach(actionName => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@darkpos/pricing",
3
- "version": "1.0.63",
3
+ "version": "1.0.65",
4
4
  "description": "Pricing calculator",
5
5
  "author": "Dark POS",
6
6
  "license": "ISC",
@@ -46,5 +46,5 @@
46
46
  "supertest": "^6.2.3",
47
47
  "supervisor": "^0.12.0"
48
48
  },
49
- "gitHead": "0d2bce7580b5ba1078a16390ae63680af8b27d66"
49
+ "gitHead": "f80b944b57b9f03791606ca809fb55590bea5436"
50
50
  }