All files / middlewares outboundWaveValidations.js

100% Statements 48/48
100% Branches 48/48
100% Functions 7/7
100% Lines 48/48

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 176 177 178 179 180 181 182 183 184 185 186                        2x   2x   2x                   2x 1x                         4x   4x   4x                   4x 3x 3x 3x     3x       3x       1x                             5x   5x 4x             4x 1x           3x 1x       2x 1x         1x         5x 5x   5x 2x           3x 3x 1x           2x 2x 1x               2x   2x 1x       5x   5x   2x       2x 1x       3x   2x       2x 1x   1x                        
import moment from 'moment-timezone';
import { Op } from 'sequelize';
 
import APIError from '../helpers/error';
import { t } from '../helpers/i18n';
 
import UserInformation from '../source/users/information';
import BranchInformation from '../source/branches/information';
import OutboundWaveInformation from '../source/outboundWaves/information';
import OutboundOrderRangeInformation from '../source/outboundRange/information';
 
async function validPendingWave(req) {
  const { userId } = req;
 
  const user = await UserInformation.getUser({ id: userId });
 
  const outboundWave = await OutboundWaveInformation.getOutboundWave(
    {
      branchCode: user.mainBranch,
      pickedUser: userId,
      status: 2,
    },
    false,
    { userBranches: req.userBranches }
  );
 
  if (outboundWave) {
    throw new APIError(
      '',
      t(
        'BEE3133',
        {
          0: outboundWave.waveNumber,
        } /* Usuário com a Onda %{0} em separação! */
      )
    );
  }
}
 
async function validQuantityProductWave(req) {
  const { userId } = req;
 
  const user = await UserInformation.getUser({ id: userId });
 
  const outboundWave = await OutboundWaveInformation.getOutboundWave(
    {
      branchCode: user.mainBranch,
      pickedUser: userId,
      status: 1,
    },
    false,
    { userBranches: req.userBranches }
  );
 
  if (outboundWave && outboundWave.outboundRanges.length > 0) {
    const totalProduct = outboundWave.outboundRanges.reduce((accum, range) => {
      accum += range.products.length;
      return accum;
    }, 0);
 
    const branch = await BranchInformation.getBranchSettings({
      code: user.mainBranch,
    });
 
    if (
      branch.limitProductsWave > 0 &&
      totalProduct > branch.limitProductsWave
    ) {
      throw new APIError(
        '',
        t(
          'BEE3134',
          {
            0: outboundWave.waveNumber,
            1: branch.limitProductsWave,
          } /* Onda de Separação %{0} já atingiu o limite de Produtos: %{1} */
        )
      );
    }
  }
}
 
async function validStatusWave(req) {
  const { outboundWaveId } = req.body;
 
  if (outboundWaveId) {
    const outboundWave = await OutboundWaveInformation.outboundWave(
      {
        id: outboundWaveId,
      },
      { userBranches: req.userBranches }
    );
 
    if (!outboundWave) {
      throw new APIError(
        '',
        t('BEE1838' /* não foi possível fazer a inicialização da onda */)
      );
    }
 
    if (outboundWave.status === 2)
      throw new APIError(
        '',
        t('BEE3135' /* Onda de Separação já foi Iniciada! */)
      );
    if (outboundWave.status === 3)
      throw new APIError(
        '',
        t('BEE3136' /* Onda de Separação já foi Finalizada! */)
      );
  } else {
    throw new APIError('', t('BEE1839' /* código da onda não localizado */));
  }
}
 
function validFilterDates(req) {
  const filterStartDate = req.query.filterStartDate || req.body.filterStartDate;
  const filterEndDate = req.query.filterEndDate || req.body.filterEndDate;
 
  if (!filterStartDate || !filterEndDate) {
    throw new APIError(
      'DATE_MISSING',
      t('BEE1212' /* Não foi informada a data inicial ou final */)
    );
  }
 
  const validStartDate = moment(filterStartDate).isValid();
  if (!validStartDate) {
    throw new APIError(
      'INVALID_START_DATE',
      t('BEE1213' /* A data inicial informada não é válida */)
    );
  }
 
  const validEndDate = moment(filterEndDate).isValid();
  if (!validEndDate) {
    throw new APIError(
      'INVALID_END_DATE',
      t('BEE1214' /* A data final informada não é válida */)
    );
  }
}
 
async function validOutboundWaveId(req) {
  const { outboundWaveId } = req.query;
 
  if (!outboundWaveId)
    throw new APIError('', t('BEE1839' /* código da onda não localizado */));
}
 
async function validOutboundWaveIdOutboundRangeId(req) {
  const { outboundRangeId, outboundWaveId } = req.body;
 
  if (!outboundRangeId && outboundWaveId) {
    const outboundRanges =
      await OutboundOrderRangeInformation.getOutboundRanges({
        outboundWaveId,
        status: { [Op.gt]: 2 },
      });
    if (outboundRanges && outboundRanges.length)
      throw new APIError(
        '',
        t('BEE1893' /* Existem documentos já finalizados */)
      );
  } else if (outboundWaveId && outboundRangeId) {
    const outboundRanges =
      await OutboundOrderRangeInformation.getOutboundRanges({
        id: outboundRangeId,
        status: { [Op.gt]: 2 },
      });
    if (outboundRanges && outboundRanges.length)
      throw new APIError('', t('BEE1894' /* Este documento já foi separado */));
  } else {
    throw new APIError('', t('BEE1895' /* Erro dados não localizados */));
  }
}
 
export default {
  validPendingWave,
  validQuantityProductWave,
  validStatusWave,
  validFilterDates,
  validOutboundWaveId,
  validOutboundWaveIdOutboundRangeId,
};