All files / middlewares priorityValidations.js

100% Statements 37/37
100% Branches 40/40
100% Functions 6/6
100% Lines 37/37

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            3x   3x 1x   2x       2x 1x               3x   3x 1x   2x       2x 1x                 2x   2x 1x         6x 6x   6x 3x     3x 1x         7x     7x 3x       3x 2x       2x         1x       4x 2x       2x 1x           2x   2x       2x       2x 1x                                  
import APIError from '../helpers/error';
import { t } from '../helpers/i18n';
import PriorityInformation from '../source/priorities/information';
import OutboundOrderInformation from '../source/outboundOrders/information';
 
async function validPriorityId(req) {
  const { priorityId } = req.body;
 
  if (!priorityId) {
    throw new APIError('INVALID_PRIORITY_ID');
  } else {
    const existPriority = await PriorityInformation.getPriority({
      id: priorityId,
    });
 
    if (!existPriority) {
      throw new APIError(
        'NOT_EXIST_PRIORITY_ID',
        t('BEE1227', { 0: priorityId } /* Prioridade %{0} não localizada */)
      );
    }
  }
}
async function validCode(req) {
  const { code } = req.body;
 
  if (!code) {
    throw new APIError('INVALID_CODE', t('BEE3430' /* Código inválido */));
  } else {
    const existPriority = await PriorityInformation.getPriority({
      code,
    });
 
    if (existPriority) {
      throw new APIError(
        'EXIST_PRIORITY_CODE',
        t('BEE1173', { 0: code } /* Codigo %{0} já cadastrado  */)
      );
    }
  }
}
 
function validPriorityName(req) {
  const { name } = req.body;
 
  if (!name) {
    throw new APIError('INVALID_NAME', t('BEE3562' /* Nome inválido */));
  }
}
 
async function validSequence(req) {
  const { sequence } = req.body;
  const { status } = req.body;
 
  if (sequence === undefined || sequence === null || sequence < 0) {
    throw new APIError('', t('BEE3054' /* Sequência inválida! */));
  }
 
  if (status === 0 && sequence !== 0) {
    throw new APIError('', t('BEE3054' /* Sequência inválida! */));
  }
}
 
async function validExistSequence(req) {
  const { sequence, priorityId } = req.body;
 
  // Update
  if (priorityId) {
    const oldPriorityData = await PriorityInformation.getPriority({
      id: priorityId,
    });
 
    if (sequence && sequence > 0) {
      const existPriority = await PriorityInformation.getPriority({
        sequence,
      });
 
      if (
        existPriority &&
        oldPriorityData &&
        sequence !== oldPriorityData.sequence
      ) {
        throw new APIError('', t('BEE4038' /* Sequência já cadastrada. */));
      }
    }
    // Insert
  } else if (sequence && sequence > 0) {
    const existPriority = await PriorityInformation.getPriority({
      sequence,
    });
 
    if (existPriority) {
      throw new APIError('', t('BEE4038' /* Sequência já cadastrada. */));
    }
  }
}
 
async function validDeleteHasAssociatedOutboundOrder(req) {
  const priorityId = req.body.priorityId || req.query.priorityId;
 
  const priorityData = await PriorityInformation.getPriority({
    id: priorityId,
  });
 
  const existOutboundOrder = await OutboundOrderInformation.getOutboundOrder({
    priorityCode: priorityData.code,
  });
 
  if (existOutboundOrder) {
    throw new APIError(
      '',
      t(
        'BEE4243' /* Não é possível excluir esta prioridade, pois ela está vinculada a outros registros no sistema */
      )
    );
  }
}
 
export default {
  validPriorityId,
  validCode,
  validPriorityName,
  validSequence,
  validExistSequence,
  validDeleteHasAssociatedOutboundOrder,
};