All files / controllers outboundPendingAllocationMonitor.js

100% Statements 27/27
56.25% Branches 9/16
100% Functions 5/5
100% Lines 25/25

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105                2x 2x   2x 2x 2x   2x       1x 1x                                                 1x                   1x   1x 1x 1x   1x   1x       1x           1x     1x       1x     1x 1x     1x           1x 1x     1x              
/* eslint-disable no-param-reassign */
/* eslint-disable import/no-extraneous-dependencies */
import moment from 'moment';
 
import OutboundOrderInformation from '../source/outboundOrders/information';
import UserInformation from '../source/users/information';
 
async function getDataGrid(req, res) {
  const { startDate, endDate, status } = req.body;
  const { userId } = req;
 
  try {
    const startDateF = moment(startDate).startOf('day').format();
    const endDateF = moment(endDate).endOf('day').format();
 
    const user = await UserInformation.getUser({
      id: userId,
    });
 
    const filtered = [];
    filtered.unshift(
      {
        id: 'dateRange',
        startDate: startDateF,
        endDate: endDateF,
      },
      {
        id: 'status',
        value: [status],
      },
      {
        id: 'branchCode',
        value: user ? user.mainBranch : '',
      },
      {
        id: 'status',
        value: [status],
      },
      {
        id: 'branchCode',
        value: user ? user.mainBranch : '',
      }
    );
 
    const ordersDb =
      await OutboundOrderInformation.getOutboundOrderPendingAllocationList(
        0,
        0,
        [],
        filtered,
        {
          userBranches: req.userBranches,
        }
      );
 
    const rows = ordersDb.rows.map((o) => o.get());
 
    rows.forEach((row) => {
      const products = row.products.filter((prod) => prod.status !== 9);
      let percentageAllocation = '';
 
      const noBalanceProducts = products.filter(
        (prod) =>
          parseFloat(prod.quantity) - (parseFloat(prod.pickedQuantity) || 0) >
          parseFloat(prod.dataValues.available)
      );
 
      percentageAllocation = noBalanceProducts.length
        ? `${(100 - (noBalanceProducts.length / products.length) * 100).toFixed(
            2
          )}%`
        : '100%';
 
      row.branchName = row.branch
        ? `${row.branch.code} - ${row.branch.name}`
        : row.branchCode;
      row.carrierName =
        row && row.carrier
          ? `${row.carrier.code} - ${row.carrier.name}`
          : row.carrierCode;
      row.customerName = row.customer
        ? `${row.customer.code} - ${row.customer.name}`
        : row.customerCode;
      row.products = products;
      row.percentageAllocation = percentageAllocation;
    });
 
    res.json({
      success: true,
      data: { rows },
    });
  } catch (err) {
    // eslint-disable-next-line no-console
    console.log(err);
    const response = {
      success: false,
    };
    res.json(response);
  }
}
 
export default {
  getDataGrid,
};