All files / middlewares outboundConsolidationValidations.js

100% Statements 23/23
100% Branches 22/22
100% Functions 2/2
100% Lines 23/23

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              6x   6x 1x       5x 1x           4x             4x 1x       3x 1x                     2x           2x 1x               5x   5x 1x           4x         4x 1x       3x 1x       2x 1x                      
import { Op } from 'sequelize';
import APIError from '../helpers/error';
import { t } from '../helpers/i18n';
import OutboundVolumesInformation from '../source/outboundVolumes/information';
import OutboundConsolidationInformation from '../source/outboundConsolidation/information';
 
async function validationLabelConsolidation(req) {
  const { outboundConsolidationId, outboundVolumeId } = req.body;
 
  if (!outboundConsolidationId) {
    throw new APIError(
      '',
      t('BEE2014' /* Etiqueta de consolidação não localizada! */)
    );
  } else if (!outboundVolumeId) {
    throw new APIError(
      '',
      t('BEE2015' /* Etiqueta de volume não localizada! */)
    );
  } else {
    const existConsolidation =
      await OutboundConsolidationInformation.getOutboundConsolidation(
        { id: outboundConsolidationId, outboundVolumeId: { [Op.is]: null } },
        {
          userBranches: req.userBranches,
        }
      );
 
    if (!existConsolidation) {
      throw new APIError(
        '',
        t('BEE2014' /* Etiqueta de consolidação não localizada! */)
      );
    } else if (existConsolidation.outboundVolumeId) {
      throw new APIError(
        '',
        t(
          'BEE3370',
          {
            0: outboundConsolidationId,
          } /* Etiqueta %{0} já vinculada a outra etiqueta de volume! */
        )
      );
    }
 
    const volume = await OutboundVolumesInformation.getOutboundVolume({
      id: outboundVolumeId,
      branchCode: req.userMainBranch,
      status: 3,
    });
 
    if (!volume)
      throw new APIError(
        '',
        t('BEE2015' /* Etiqueta de volume não localizada! */)
      );
  }
}
 
async function validationStatusConsolidation(req) {
  const { outboundVolumeId } = req.query;
 
  if (!outboundVolumeId) {
    throw new APIError(
      '',
      t('BEE2015' /* Etiqueta de volume não localizada! */)
    );
  }
 
  const volume = await OutboundVolumesInformation.getOutboundVolume({
    id: outboundVolumeId,
    branchCode: req.userMainBranch,
  });
 
  if (volume && volume.outboundOrder.status < 8) {
    throw new APIError(
      '',
      t('BEE2455' /* Volume informado consta em doca em aberto */)
    );
  } else if (volume && volume.outboundOrder.status > 8) {
    throw new APIError(
      '',
      t('BEE2082' /* Etiqueta de volume já consolidada */)
    );
  } else if (!volume) {
    throw new APIError(
      '',
      t('BEE2015' /* Etiqueta de volume não localizada! */)
    );
  }
}
 
export default {
  validationLabelConsolidation,
  validationStatusConsolidation,
};