@darkpos/pricing 1.0.35 → 1.0.37

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.
@@ -2477,4 +2477,153 @@ describe('Order actions', () => {
2477
2477
  _actual: 20,
2478
2478
  });
2479
2479
  });
2480
+
2481
+ test('CU-86dueyuqj -> Merge Parent orders', () => {
2482
+ const item1 = {
2483
+ _id: 1,
2484
+ price: 10,
2485
+ quantity: 1,
2486
+ modifiers: [],
2487
+ };
2488
+ const item2 = {
2489
+ _id: 2,
2490
+ price: 20,
2491
+ quantity: 1,
2492
+ modifiers: [],
2493
+ };
2494
+
2495
+ const user = {
2496
+ _id: 'userId123',
2497
+ firstName: 'Esteban',
2498
+ lastName: 'Mero',
2499
+ };
2500
+
2501
+ const childOrder1 = pricingService.order.calculate({
2502
+ items: [item1],
2503
+ _id: 'abc',
2504
+ _parentId: 1,
2505
+ });
2506
+ const childOrder2 = pricingService.order.calculate({
2507
+ items: [item2],
2508
+ _id: 'bcd',
2509
+ _parentId: 2,
2510
+ });
2511
+
2512
+ const parentOrder1 = {
2513
+ items: [],
2514
+ orders: [childOrder1],
2515
+ _isParent: true,
2516
+ _id: 1,
2517
+ };
2518
+ const parentOrder2 = {
2519
+ items: [],
2520
+ orders: [childOrder2],
2521
+ _isParent: true,
2522
+ _id: 2,
2523
+ };
2524
+
2525
+ const [newParentOrder, voidedOrders, ordersToDelete, subOrders] =
2526
+ pricingService.order.merge({
2527
+ orders: [parentOrder1, parentOrder2],
2528
+ subOrdersToMerge: [childOrder1, childOrder2],
2529
+ aggregateItems: true,
2530
+ user,
2531
+ });
2532
+
2533
+ expect(newParentOrder).toHaveProperty('total', 30);
2534
+ expect(voidedOrders.length).toBe(1);
2535
+ expect(ordersToDelete.length).toBe(0);
2536
+ expect(subOrders.length).toBe(2);
2537
+ expect(newParentOrder.orders.length).toBe(2);
2538
+ });
2539
+
2540
+ test('CU-86dueyuqj -> Merge Orders that are not parents and not children', () => {
2541
+ const item1 = {
2542
+ _id: 1,
2543
+ price: 10,
2544
+ quantity: 1,
2545
+ modifiers: [],
2546
+ };
2547
+ const item2 = {
2548
+ _id: 2,
2549
+ price: 20,
2550
+ quantity: 1,
2551
+ modifiers: [],
2552
+ };
2553
+
2554
+ const user = {
2555
+ _id: 'userId123',
2556
+ firstName: 'Esteban',
2557
+ lastName: 'Mero',
2558
+ };
2559
+
2560
+ const order1 = pricingService.order.calculate({
2561
+ items: [item1],
2562
+ _id: 'abc',
2563
+ });
2564
+ const order2 = pricingService.order.calculate({
2565
+ items: [item2],
2566
+ _id: 'bcd',
2567
+ });
2568
+
2569
+ const [newOrder, voidedOrders, ordersToDelete, subOrders] =
2570
+ pricingService.order.merge({
2571
+ orders: [order1, order2],
2572
+ subOrdersToMerge: [],
2573
+ aggregateItems: true,
2574
+ user,
2575
+ });
2576
+
2577
+ expect(newOrder).toHaveProperty('total', 30);
2578
+ expect(voidedOrders.length).toBe(1);
2579
+ expect(ordersToDelete.length).toBe(0);
2580
+ expect(subOrders.length).toBe(0);
2581
+ expect(newOrder.orders).toBe(undefined);
2582
+ });
2583
+
2584
+ test('CU-86dueyuqj -> Merge Child orders', () => {
2585
+ const item1 = {
2586
+ _id: 1,
2587
+ price: 10,
2588
+ quantity: 1,
2589
+ modifiers: [],
2590
+ };
2591
+ const item2 = {
2592
+ _id: 2,
2593
+ price: 20,
2594
+ quantity: 1,
2595
+ modifiers: [],
2596
+ };
2597
+
2598
+ const user = {
2599
+ _id: 'userId123',
2600
+ firstName: 'Esteban',
2601
+ lastName: 'Mero',
2602
+ };
2603
+
2604
+ const childOrder1 = pricingService.order.calculate({
2605
+ items: [item1],
2606
+ _id: 'abc',
2607
+ _parentId: 1,
2608
+ });
2609
+ const childOrder2 = pricingService.order.calculate({
2610
+ items: [item2],
2611
+ _id: 'bcd',
2612
+ _parentId: 1,
2613
+ });
2614
+
2615
+ const [newParentOrder, voidedOrders, ordersToDelete, subOrders] =
2616
+ pricingService.order.merge({
2617
+ orders: [childOrder1, childOrder2],
2618
+ subOrdersToMerge: [],
2619
+ aggregateItems: true,
2620
+ user,
2621
+ });
2622
+
2623
+ expect(newParentOrder).toHaveProperty('total', 30);
2624
+ expect(voidedOrders.length).toBe(1);
2625
+ expect(ordersToDelete.length).toBe(0);
2626
+ expect(subOrders.length).toBe(0);
2627
+ expect(newParentOrder.orders).toBe(undefined);
2628
+ });
2480
2629
  });
@@ -2,14 +2,18 @@
2
2
  /**
3
3
  * return calculated Order
4
4
  */
5
- module.exports = ({ _, itemActions, modifierActions }) =>
5
+ module.exports = ({ _, actions, itemActions, modifierActions }) =>
6
6
  function calculateorder(inputOrder) {
7
7
  if (!inputOrder) return inputOrder;
8
8
  const order = _.cloneDeep(inputOrder);
9
- const { items = [] } = order;
9
+ const { items = [], orders = [] } = order;
10
10
 
11
- if (!items.length)
12
- return { ...order, subTotals: {}, subTotal: 0, total: 0 };
11
+ if (!items.length) {
12
+ if (!orders.length)
13
+ return { ...order, subTotals: {}, subTotal: 0, total: 0 };
14
+
15
+ return { ...order, ...actions.getTotals(order.orders) };
16
+ }
13
17
 
14
18
  const sortedOrderModifiers = modifierActions.sort(
15
19
  modifierActions.removeLocked(order.modifiers)
@@ -54,15 +54,6 @@ module.exports = ({ _, actions, utils }) => {
54
54
  };
55
55
  }
56
56
 
57
- newOrder = actions.calculate(newOrder);
58
- newOrder = actions.addNote({
59
- order: newOrder,
60
- message: `This order was merged with ${
61
- mOrders.length === 2 ? ` order ` : ` orders `
62
- } # ${mOrders.slice(1).map(order => ` ${order.displayId}`)}`,
63
- user,
64
- });
65
-
66
57
  const ordersToDelete = [];
67
58
  const voidedOrders = [];
68
59
  for (let i = 1; i < mOrders.length; i += 1) {
@@ -89,8 +80,18 @@ module.exports = ({ _, actions, utils }) => {
89
80
  parentId: newOrder._id,
90
81
  customer: newOrder.customer,
91
82
  }));
83
+ newOrder.orders = subOrders;
92
84
  }
93
85
 
86
+ newOrder = actions.calculate(newOrder);
87
+ newOrder = actions.addNote({
88
+ order: newOrder,
89
+ message: `This order was merged with ${
90
+ mOrders.length === 2 ? ` order ` : ` orders `
91
+ } # ${mOrders.slice(1).map(order => ` ${order.displayId}`)}`,
92
+ user,
93
+ });
94
+
94
95
  return [newOrder, voidedOrders, ordersToDelete, subOrders];
95
96
  };
96
97
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@darkpos/pricing",
3
- "version": "1.0.35",
3
+ "version": "1.0.37",
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": "00a27ca75fa0c5b5a2e0dba0f9446487f7c1ac63"
39
+ "gitHead": "880122bbb47242484095cd2db3f68036c95c569a"
40
40
  }