All files / middlewares labelsValidations.js

100% Statements 45/45
100% Branches 58/58
100% Functions 4/4
100% Lines 45/45

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                              8x   8x 1x 7x 1x 6x 1x 5x 1x 4x           1x             3x             3x 1x 2x 1x                                     8x   8x 1x 7x 1x 6x 1x 5x 1x 4x           1x         3x 1x         2x               2x 1x                         2x 2x 2x   2x   2x 1x                 4x   4x 3x         3x 2x             2x 1x                                        
import APIError from '../helpers/error';
import { t } from '../helpers/i18n';
import LabelInformation from '../source/labels/information';
import ProductEansInformation from '../source/productEans/information';
 
async function validEditLabel(req) {
  const {
    version,
    labelZPL,
    labelDPL,
    labelId,
    trigger,
    labelImageSimulation,
    labelWidthSimulation,
    labelHeightSimulation,
  } = req.body;
 
  if (!labelId) {
    throw new APIError(t('BEE2982' /* Etiqueta não informada */));
  } else if (!version) {
    throw new APIError(t('BEE4047' /* Versão não preenchida */));
  } else if (!labelZPL && !labelDPL) {
    throw new APIError(t('BEE4048' /* Código da etiqueta não preenchido */));
  } else if (!trigger) {
    throw new APIError(t('BEE4049' /* Gatilho não preenchido */));
  } else if (
    labelZPL &&
    (!labelImageSimulation ||
      labelWidthSimulation <= 0 ||
      labelHeightSimulation <= 0)
  ) {
    throw new APIError(
      t(
        'BEE4050' /* Para efetivar a gravação da etiqueta, é necessário realizar a simulação da etiqueta. */
      )
    );
  }
 
  const labelExist = await LabelInformation.getLabel(
    {
      id: labelId,
    },
    { attributes: ['isMainLabel'], raw: true, nest: true }
  );
 
  if (!labelExist) {
    throw new APIError(t('BEE2857' /* Etiqueta não encontrada. */));
  } else if (labelExist.isMainLabel) {
    throw new APIError(
      t(
        'BEE4044' /* Etiquetas que são padrão do BeeStock não podem ser alteradas! */
      )
    );
  }
}
 
async function validNewLabel(req) {
  const {
    code,
    version,
    labelZPL,
    labelDPL,
    trigger,
    labelImageSimulation,
    labelWidthSimulation,
    labelHeightSimulation,
    isCopy,
  } = req.body;
 
  if (!code) {
    throw new APIError(t('BEE1746' /* Código não preenchido */));
  } else if (!version) {
    throw new APIError(t('BEE4047' /* Versão não preenchida */));
  } else if (!labelZPL && !labelDPL) {
    throw new APIError(t('BEE4048' /* Código da etiqueta não preenchido */));
  } else if (!trigger) {
    throw new APIError(t('BEE4049' /* Gatilho não preenchido */));
  } else if (
    labelZPL &&
    (!labelImageSimulation ||
      labelWidthSimulation <= 0 ||
      labelHeightSimulation <= 0)
  ) {
    throw new APIError(
      t(
        'BEE4050' /* Para efetivar a gravação da etiqueta, é necessário realizar a simulação da etiqueta. */
      )
    );
  } else if (isCopy && version !== 1) {
    throw new APIError(
      t('BEE4052' /* Versão inválida do modelo de etiqueta. */)
    );
  }
 
  const existLabel = await LabelInformation.getLabel(
    {
      code,
      ...(!isCopy ? { version } : {}),
    },
    { attributes: ['id'], raw: true, nest: true }
  );
 
  if (existLabel) {
    throw new APIError(
      t(
        'BEE4051',
        {
          0: code,
          1: version,
        } /* O código %{0} da versão %{1} já está cadastrado */
      )
    );
  }
}
 
function forbiddenWordInLabel(req) {
  const { labelZPL, labelDPL, zpl } = req.body;
  const labelZPLTemp = labelZPL || zpl;
  const labelDPLTemp = labelDPL || zpl;
  const forbiddenRegex =
    /require\(['"].*?['"]\);?|async\s+|await\s+|new\s+Promise|Promise|import\s|import\(|eval|Function|process\.|process\[|Buffer|setTimeout|setInterval|XMLHttpRequest|fetch|WebSocket/gi;
 
  if (forbiddenRegex.test(labelZPLTemp) || forbiddenRegex.test(labelDPLTemp)) {
    throw new APIError(
      t(
        'BEE4055' /* Seu modelo de etiqueta contém palavras proibidas que precisam ser removidas */
      )
    );
  }
}
 
async function validLabelDisable(req) {
  const { labelId, active } = req.body;
 
  if (!active) {
    const label = await LabelInformation.getLabel(
      { id: labelId },
      { raw: true, nest: true, include: [], attributes: ['code', 'trigger'] }
    );
 
    if (label.trigger === 'ean') {
      const productEans = await ProductEansInformation.getProductEan(
        {
          labelCode: label.code,
        },
        { include: [], attributes: ['ean'], raw: true, nest: true }
      );
 
      if (productEans) {
        throw new APIError(
          t(
            'BEE4111',
            {
              0: t('BEE2734' /* Código de Barras */),
              1: productEans.ean,
            } /* O %{0} %{1} está vinculado a este modelo, remova o vínculo para efetivar a desativação. */
          )
        );
      }
    }
  }
}
 
export default {
  validNewLabel,
  validEditLabel,
  forbiddenWordInLabel,
  validLabelDisable,
};