All files / middlewares scheduleValidations.js

100% Statements 25/25
100% Branches 22/22
100% Functions 4/4
100% Lines 25/25

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            3x   3x 1x   2x             2x 1x                     3x   3x 1x         2x       2x 1x                 2x   2x 1x               4x   4x 4x 1x                 3x 3x 1x                 2x       2x 1x                          
import APIError from '../helpers/error';
import I18n from '../helpers/i18n';
import ScheduleInformation from '../source/schedules/information';
import TimeStringValidator from '../helpers/validators/timeString';
 
async function validScheduleId(req) {
  const scheduleId = req.body.scheduleId || req.query.scheduleId;
 
  if (!scheduleId) {
    throw new APIError('INVALID_SCHEDULE_ID');
  } else {
    const existSchedule = await ScheduleInformation.getSchedule(
      {
        id: scheduleId,
      },
      { userBranches: req.userBranches }
    );
 
    if (!existSchedule) {
      throw new APIError(
        'NOT_EXIST_SCHEDULE_ID',
        I18n.t(
          'BEE1238',
          { 0: scheduleId } /* Agendamento %{0} não localizado */
        )
      );
    }
  }
}
async function validCode(req) {
  const code = req.body.code || req.query.code;
 
  if (!code) {
    throw new APIError(
      'INVALID_SCHEDULE_CODE',
      I18n.t('BEE1237' /* Deve ser informado um código para o agendamento ! */)
    );
  } else {
    const existSchedule = await ScheduleInformation.getSchedule({
      code,
    });
 
    if (existSchedule) {
      throw new APIError(
        'EXIST_SCHEDULE_CODE',
        I18n.t('BEE1173', { 0: code } /* Codigo %{0} já cadastrado  */)
      );
    }
  }
}
 
function validScheduleName(req) {
  const name = req.body.name || req.query.name;
 
  if (!name) {
    throw new APIError(
      'INVALID_SCHEDULE_NAME',
      I18n.t('BEE1239' /* Deve ser informado um nome para o agendamento ! */)
    );
  }
}
 
function validTimes(req) {
  const { startTime, endTime } = req.body;
 
  const validStartTime = TimeStringValidator.validateTimeString(startTime);
  if (!validStartTime) {
    throw new APIError(
      'INVALID_START_TIME',
      I18n.t(
        'BEE1240',
        { 0: startTime } /* %{0} não é uma hora de início válida ! */
      )
    );
  }
 
  const validEndTime = TimeStringValidator.validateTimeString(endTime);
  if (!validEndTime) {
    throw new APIError(
      'INVALID_END_TIME',
      I18n.t(
        'BEE1241',
        { 0: endTime } /* %{0} não é uma hora de término válida ! */
      )
    );
  }
 
  const validSequence = TimeStringValidator.validateTimeSequence(
    startTime,
    endTime
  );
  if (!validSequence) {
    throw new APIError(
      'INVALID_TIME_SEQUENCE',
      I18n.t('BEE1242' /* A hora inicial deve anteceder a hora final */)
    );
  }
}
 
export default {
  validScheduleId,
  validCode,
  validScheduleName,
  validTimes,
};