All files / controllers outboundConsolidation.js

94.73% Statements 36/38
66.66% Branches 8/12
100% Functions 8/8
94.73% Lines 36/38

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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175                          2x 2x 2x         1x                 1x 1x     1x 1x   1x                   1x       1x     1x   1x       1x         1x 1x 1x 1x                 1x                 1x                       1x                 1x 1x       1x                       1x     1x                 2x   2x       2x 1x       1x 1x 1x                   1x     1x 1x                        
import moment from 'moment-timezone';
import { Op } from 'sequelize';
 
import OutboundVolumesInformation from '../source/outboundVolumes/information';
import OutboundVolumesUpdate from '../source/outboundVolumes/update';
import OutboundOrderUpdate from '../source/outboundOrders/update';
import OutboundConsolidationInformation from '../source/outboundConsolidation/information';
import OutboundConsolidationCreate from '../source/outboundConsolidation/create';
import OutboundConsolidationUpdate from '../source/outboundConsolidation/update';
 
import { sequelize } from '../../config/database';
 
async function getVolumes(req, res) {
  const { outboundVolumeId } = req.query;
  try {
    const volume = await OutboundVolumesInformation.getOutboundVolume({
      id: outboundVolumeId,
      branchCode: req.userMainBranch,
    });
 
    const volumes = await OutboundVolumesInformation.getOutboundVolumes(
      {
        outboundOrderId: volume.outboundOrderId,
        branchCode: req.userMainBranch,
        status: [3, 4],
      },
      { order: ['id'] }
    );
 
    const consolidatedCount = volumes.filter(
      (item) => item.status === 4
    ).length;
 
    const outboundVolumes = volumes
      .filter((item) => item.status === 3)
      .map((item) => {
        return {
          outboundVolumeId: item.id,
          sequence: item.sequence,
          orderNumber: item.outboundOrder.orderNumber,
          printedConsolidationLabel: item.printedConsolidationLabel,
          pendingCount: volumes.length - consolidatedCount,
          consolidatedCount: consolidatedCount || 0,
        };
      });
 
    const response = {
      success: true,
      data: outboundVolumes,
    };
    res.json(response);
  } catch (err) {
    // eslint-disable-next-line no-console
    console.log(err);
 
    const response = {
      success: false,
    };
 
    res.json(response);
  }
}
 
async function doConsolidation(req, res) {
  const { outboundVolumeId, outboundConsolidationId } = req.body;
  try {
    await sequelize.transaction(async (transaction) => {
      const volume = await OutboundVolumesInformation.getOutboundVolume(
        {
          id: outboundVolumeId,
          branchCode: req.userMainBranch,
          status: 3,
        },
        { transaction }
      );
 
      await OutboundConsolidationUpdate.updateOutboundOrderConsolidation(
        outboundConsolidationId,
        {
          outboundVolumeId,
          updatedUser: req.userId,
        },
        { transaction }
      );
 
      await OutboundVolumesUpdate.updateOutboundVolume(
        outboundVolumeId,
        {
          status: 4,
          consolidationAt: moment().format(),
          consolidationUser: req.userId,
          updatedUser: req.userId,
        },
        { transaction }
      );
 
      const volumes =
        await OutboundConsolidationInformation.getOutboundConsolidations(
          {
            outboundOrderId: volume.outboundOrderId,
            branchCode: req.userMainBranch,
            outboundVolumeId: { [Op.is]: null },
          },
          { transaction }
        );
 
      Eif (!volumes.length) {
        const isRequestDeliveryConfirmation = volume.outboundOrder.typeOrders
          ? volume.outboundOrder.typeOrders.requestDeliveryConfirmation
          : false;
 
        await OutboundOrderUpdate.updateOutboundOrder(
          volume.outboundOrderId,
          {
            status: isRequestDeliveryConfirmation ? 14 : 10, // Pendente Retirada ou Finalizado
            consolidationAt: moment().format(),
            consolidationUser: req.userId,
            updatedUser: req.userId,
          },
          { transaction }
        );
      }
    });
    const response = {
      success: true,
    };
    res.json(response);
  } catch (err) {
    // eslint-disable-next-line no-console
    console.log('err', err);
    throw err;
  }
}
 
async function createOutboundConsolidation(outboundOrderId, userId) {
  const outboundConsolidations = [];
  const existConsolidation =
    await OutboundConsolidationInformation.getOutboundConsolidation({
      outboundOrderId,
    });
 
  if (!existConsolidation) {
    const volumes = await OutboundVolumesInformation.getOutboundVolumes({
      outboundOrderId,
    });
 
    let sequence = 1;
    volumes.forEach((volume) => {
      outboundConsolidations.push({
        branchCode: volume.outboundOrder.branchCode,
        sequence,
        outboundOrderId,
        createdUser: userId,
        createdAt: moment().format(),
        updatedUser: userId,
        updatedAt: moment().format(),
      });
 
      sequence++;
    });
 
    Eif (outboundConsolidations.length > 0) {
      await OutboundConsolidationCreate.bulkOutboundConsolidations(
        outboundConsolidations
      );
    }
  }
}
 
export default {
  getVolumes,
  doConsolidation,
  createOutboundConsolidation,
};