All files / middlewares collectorValidations.js

100% Statements 15/15
100% Branches 16/16
100% Functions 3/3
100% Lines 15/15

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          4x   4x 1x   3x       3x 2x               4x   4x 2x         2x       2x 1x           3x   3x 2x                        
import APIError from '../helpers/error';
import I18n from '../helpers/i18n';
import CollectorInformation from '../source/collectors/information';
 
async function validCollectorId(req) {
  const collectorId = req.body.collectorId || req.query.collectorId;
 
  if (!collectorId) {
    throw new APIError('INVALID_COLLECTOR_ID');
  } else {
    const existCollector = await CollectorInformation.getCollector({
      id: collectorId,
    });
 
    if (!existCollector) {
      throw new APIError(
        I18n.t('BEE1695', { 0: collectorId } /* Coletor %{0} não localizado */),
        'NOT_EXIST_COLLECTOR_ID'
      );
    }
  }
}
async function validCode(req) {
  const code = req.body.code || req.query.code;
 
  if (!code) {
    throw new APIError(
      I18n.t('BEE1713' /* Informe o código do coletor (obrigatório) */),
      'INVALID_COLLECTOR_CODE'
    );
  } else {
    const existCollector = await CollectorInformation.getCollector({
      code,
    });
 
    if (existCollector) {
      throw new APIError('EXIST_COLLECTOR_CODE');
    }
  }
}
 
function validName(req) {
  const name = req.body.name || req.query.name;
 
  if (!name) {
    throw new APIError(
      I18n.t('BEE1714' /* Informe o nome do coletor (obrigatório) */),
      'INVALID_COLLECTOR_NAME'
    );
  }
}
 
export default {
  validCollectorId,
  validCode,
  validName,
};