All files / middlewares industrialScaleValidations.js

100% Statements 118/118
100% Branches 112/112
100% Functions 18/18
100% Lines 118/118

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 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362              3x   3x 1x     2x       2x 1x                       4x   4x 1x 3x 1x           2x       2x 1x                 3x   3x 1x 2x 1x               4x   4x 1x       3x 1x       2x 1x               4x   4x 1x       3x 1x       2x 1x               4x   4x 1x 3x 1x       2x 1x               4x   4x 1x 3x 1x       2x 1x               4x   4x 1x 3x 1x       2x 1x               3x   3x 1x 2x 1x         4x   4x 1x 3x 1x               3x   3x 1x 2x 1x               4x   4x 1x 3x 1x       2x 1x               4x   4x 1x 3x 1x       2x 1x               4x   4x 1x 3x 1x       2x 1x               4x   4x 1x       3x 1x       2x 1x               4x   4x 1x 3x       1x       2x 1x               4x   4x 1x       3x 1x       2x 1x                   4x   4x 1x       3x 1x       2x 1x                                                          
import validator from 'validator';
import APIError from '../helpers/error';
import { t } from '../helpers/i18n';
import IndustrialScaleInformation from '../source/industrialScales/information';
 
async function validIndustrialScaleId(req) {
  const industrialScaleId =
    req.body.industrialScaleId || req.query.industrialScaleId;
 
  if (!industrialScaleId) {
    throw new APIError('', t('BEE3085' /* ID não pode ser vazio! */));
  } else {
    const existsIndustrialScale =
      await IndustrialScaleInformation.getIndustrialScale({
        id: industrialScaleId,
      });
 
    if (!existsIndustrialScale) {
      throw new APIError(
        'EXIST_INDUSTRIAL_SCALE_ID',
        t(
          'BEE3845',
          { 0: industrialScaleId } /* Balança Industrial %{0} não localizada */
        )
      );
    }
  }
}
 
async function validIdentification(req) {
  const { identification } = req.body;
 
  if (!identification) {
    throw new APIError('', t('BEE3813' /* Identificação é obrigatória. */));
  } else if (identification.toString().length > 30) {
    throw new APIError(
      '',
      t('BEE3908' /* Identificação deve ter no máximo 30 caracteres. */)
    );
  } else {
    const existIndustrialScale =
      await IndustrialScaleInformation.getIndustrialScale({
        identification,
      });
 
    if (existIndustrialScale) {
      throw new APIError(
        '',
        t('BEE1173', { 0: identification } /* Codigo %{0} já cadastrado  */)
      );
    }
  }
}
 
function validWeighingRoom(req) {
  const { weighingRoom } = req.body;
 
  if (!weighingRoom) {
    throw new APIError('', t('BEE3814' /* A Sala de Pesagem é obrigatória. */));
  } else if (weighingRoom.toString().length > 150) {
    throw new APIError(
      '',
      t('BEE3907' /* Sala de Pesagem deve ter no máximo 150 caracteres. */)
    );
  }
}
 
function validSequence(req) {
  const { sequence } = req.body;
 
  if (!sequence) {
    throw new APIError(
      '',
      t('BEE3815' /* Número de sequência é obrigatório. */)
    );
  } else if (!validator.isInt(sequence.toString(), { min: 0 })) {
    throw new APIError(
      '',
      t('BEE3816' /* Número de sequência deve ser um valor inteiro. */)
    );
  } else if (sequence.toString().length > 10) {
    throw new APIError(
      '',
      t('BEE3894' /* Número de sequência deve ter no máximo 10 caracteres. */)
    );
  }
}
 
function validPortNumber(req) {
  const { portNumber } = req.body;
 
  if (!portNumber) {
    throw new APIError(
      '',
      t('BEE3817' /* Número da porta de comunicação é obrigatório. */)
    );
  } else if (!validator.isInt(portNumber.toString(), { min: 0 })) {
    throw new APIError(
      '',
      t('BEE3818' /* Número da porta deve ser um valor inteiro. */)
    );
  } else if (portNumber.toString().length > 10) {
    throw new APIError(
      '',
      t('BEE3895' /* Número da porta deve ter no máximo 10 caracteres. */)
    );
  }
}
 
function validBaudRate(req) {
  const { baudRate } = req.body;
 
  if (!baudRate) {
    throw new APIError('', t('BEE3839' /* Taxa de baud é obrigatória. */));
  } else if (baudRate && !validator.isInt(baudRate.toString(), { min: 0 })) {
    throw new APIError(
      '',
      t('BEE3819' /* Taxa de baud deve ser um valor inteiro. */)
    );
  } else if (baudRate.toString().length > 10) {
    throw new APIError(
      '',
      t('BEE3896' /* Taxa de baud deve ter no máximo 10 caracteres. */)
    );
  }
}
 
function validDataBits(req) {
  const { dataBits } = req.body;
 
  if (!dataBits) {
    throw new APIError('', t('BEE3840' /* Data bits é obrigatório. */));
  } else if (dataBits && !validator.isInt(dataBits.toString(), { min: 0 })) {
    throw new APIError(
      '',
      t('BEE3820' /* Data bits deve ser um valor inteiro. */)
    );
  } else if (dataBits.toString().length > 10) {
    throw new APIError(
      '',
      t('BEE3897' /* Data bits deve ter no máximo 10 caracteres. */)
    );
  }
}
 
function validStopBits(req) {
  const { stopBits } = req.body;
 
  if (!stopBits) {
    throw new APIError('', t('BEE3841' /* Stop bits é obrigatório. */));
  } else if (stopBits && !validator.isInt(stopBits.toString(), { min: 0 })) {
    throw new APIError(
      '',
      t('BEE3821' /* Stop bits deve ser um valor inteiro. */)
    );
  } else if (stopBits.toString().length > 10) {
    throw new APIError(
      '',
      t('BEE3898' /* Stop bits deve ter no máximo 10 caracteres. */)
    );
  }
}
 
function validParity(req) {
  const { parity } = req.body;
 
  if (!parity) {
    throw new APIError('', t('BEE3842' /* Paridade é obrigatório. */));
  } else if (parity && !['Even'].includes(parity)) {
    throw new APIError('', t('BEE3822' /* Paridade deve ser 'Even'. */));
  }
}
 
function validFlowControl(req) {
  const { flowControl } = req.body;
 
  if (!flowControl) {
    throw new APIError('', t('BEE3843' /* Controle de fluxo é obrigatório. */));
  } else if (flowControl && !['Xon', 'Xoff'].includes(flowControl)) {
    throw new APIError(
      '',
      t('BEE3823' /* Controle de fluxo deve ser 'Xon' ou 'Xoff'. */)
    );
  }
}
 
function validSeparator(req) {
  const { separator } = req.body;
 
  if (!separator) {
    throw new APIError('', t('BEE3844' /* Separador é obrigatório. */));
  } else if (separator.toString().length > 30) {
    throw new APIError(
      '',
      t('BEE3899' /* Separador deve ter no máximo 30 caracteres. */)
    );
  }
}
 
function validPacketSize(req) {
  const { packetSize } = req.body;
 
  if (!packetSize) {
    throw new APIError('', t('BEE3825' /* Tamanho do pacote é obrigatório. */));
  } else if (!validator.isInt(packetSize.toString(), { min: 0 })) {
    throw new APIError(
      '',
      t('BEE3826' /* Tamanho do pacote deve ser um valor inteiro. */)
    );
  } else if (packetSize.toString().length > 10) {
    throw new APIError(
      '',
      t('BEE3900' /* Tamanho do pacote deve ter no máximo 10 caracteres. */)
    );
  }
}
 
async function validMaxWeight(req) {
  const { maxWeight } = req.body;
 
  if (!maxWeight) {
    throw new APIError('', t('BEE3827' /* Peso máximo é obrigatório. */));
  } else if (!validator.isInt(maxWeight.toString(), { min: 0 })) {
    throw new APIError(
      '',
      t('BEE3828' /* Peso máximo deve ser um valor inteiro. */)
    );
  } else if (maxWeight.toString().length > 10) {
    throw new APIError(
      '',
      t('BEE3901' /* Peso máximo deve ter no máximo 10 caracteres. */)
    );
  }
}
 
async function validDivisionFactor(req) {
  const { divisionFactor } = req.body;
 
  if (!divisionFactor) {
    throw new APIError('', t('BEE3829' /* Fator de divisão é obrigatório. */));
  } else if (!validator.isDecimal(divisionFactor.toString(), { min: 0 })) {
    throw new APIError(
      '',
      t('BEE3830' /* Fator de divisão deve ser um valor decimal. */)
    );
  } else if (divisionFactor.toString().length > 15) {
    throw new APIError(
      '',
      t('BEE3902' /* Fator de divisão deve ter no máximo 15 caracteres. */)
    );
  }
}
 
async function validReadTimeout(req) {
  const { readTimeout } = req.body;
 
  if (!readTimeout) {
    throw new APIError(
      '',
      t('BEE3831' /* Tempo de leitura de divisão é obrigatório. */)
    );
  } else if (!validator.isInt(readTimeout.toString(), { min: 0 })) {
    throw new APIError(
      '',
      t('BEE3832' /* Tempo de leitura deve ser um valor inteiro. */)
    );
  } else if (readTimeout.toString().length > 6) {
    throw new APIError(
      '',
      t('BEE3903' /* Tempo de leitura deve ter no máximo 6 caracteres. */)
    );
  }
}
 
async function validWriteTimeout(req) {
  const { writeTimeout } = req.body;
 
  if (!writeTimeout) {
    throw new APIError('', t('BEE3833' /* Tempo de escrita é obrigatório. */));
  } else if (
    writeTimeout &&
    !validator.isInt(writeTimeout.toString(), { min: 0 })
  ) {
    throw new APIError(
      '',
      t('BEE3834' /* Tempo de gravação deve ser um valor inteiro. */)
    );
  } else if (writeTimeout.toString().length > 6) {
    throw new APIError(
      '',
      t('BEE3904' /* Tempo de gravação deve ter no máximo 6 caracteres. */)
    );
  }
}
 
async function validStartPosRead(req) {
  const { startPosRead } = req.body;
 
  if (!startPosRead) {
    throw new APIError(
      '',
      t('BEE3835' /* Posição inicial de leitura é obrigatória. */)
    );
  } else if (!validator.isInt(startPosRead.toString(), { min: 0 })) {
    throw new APIError(
      '',
      t('BEE3836' /* Posição inicial de leitura deve ser um valor inteiro. */)
    );
  } else if (startPosRead.toString().length > 6) {
    throw new APIError(
      '',
      t(
        'BEE3905' /* Posição inicial de leitura deve ter no máximo 6 caracteres. */
      )
    );
  }
}
 
async function validNumCharacters(req) {
  const { numCharacters } = req.body;
 
  if (!numCharacters) {
    throw new APIError(
      '',
      t('BEE3837' /* Quantidade de caracteres é obrigatória. */)
    );
  } else if (!validator.isInt(numCharacters.toString(), { min: 0 })) {
    throw new APIError(
      '',
      t('BEE3838' /* Quantidade de caracteres deve ser um valor inteiro. */)
    );
  } else if (numCharacters.toString().length > 5) {
    throw new APIError(
      '',
      t(
        'BEE3906' /* Quantidade de caracteres deve ter no máximo 5 caracteres. */
      )
    );
  }
}
 
export default {
  validIdentification,
  validWeighingRoom,
  validSequence,
  validPortNumber,
  validBaudRate,
  validDataBits,
  validStopBits,
  validParity,
  validFlowControl,
  validSeparator,
  validPacketSize,
  validMaxWeight,
  validDivisionFactor,
  validReadTimeout,
  validWriteTimeout,
  validStartPosRead,
  validNumCharacters,
  validIndustrialScaleId,
};