All files / middlewares printerValidations.js

100% Statements 64/64
100% Branches 70/70
100% Functions 13/13
100% Lines 64/64

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                        3x   3x 1x   2x       2x 1x                   2x   2x         2x       2x 2x   2x 2x 1x                 3x   3x 1x   2x       2x 1x                 2x   2x 1x         2x   2x 1x         2x   2x 1x               3x   3x 1x         3x   3x 1x         4x 4x   4x 1x           3x                     3x 1x           2x       3x   3x 1x           2x                 2x 1x           1x                   1x       3x   3x 2x       2x 1x                 3x   3x 1x               2x   2x       2x       2x 1x                                                
import { Op } from 'sequelize';
import APIError from '../helpers/error';
import { t } from '../helpers/i18n';
import PrinterInformation from '../source/printers/information';
import SerialGroupInformation from '../source/serialGroup/information';
import inboundOrdersInformation from '../source/inboundOrders/information';
import inboundOrderProductsInformation from '../source/inboundOrderProducts/information';
import SerialLabelInformation from '../source/serialLabel/information';
import AgentInformation from '../source/agents/information';
import UserPrinterInformation from '../source/userPrinters/information';
 
async function validPrinterId(req) {
  const printerId = req.body.printerId || req.query.printerId;
 
  if (!printerId) {
    throw new APIError('INVALID_ID', t('BEE3421' /* ID inválido */));
  } else {
    const existPrinter = await PrinterInformation.getPrinter({
      id: printerId,
    });
 
    if (!existPrinter) {
      throw new APIError(
        'NOT_EXIST_PRINTER_ID',
        t('BEE1226', { 0: printerId } /* Impressora %{0} não localizada */)
      );
    }
  }
}
 
async function validProductBranch(req) {
  const preConferenceLabelId =
    req.body.preConferenceLabelId || req.query.preConferenceLabelId;
 
  const inboundOrder = await inboundOrdersInformation.getInboundOrdersLabel({
    id: preConferenceLabelId,
  });
 
  const InboundOrderProducts =
    await inboundOrderProductsInformation.getInboundOrderProductsLabels({
      inboundOrderId: inboundOrder.id,
    });
 
  req.inboundOrder = inboundOrder;
  req.InboundOrderProducts = InboundOrderProducts;
 
  for (const element of InboundOrderProducts) {
    if (!element.product.productBranch) {
      throw new APIError(
        '',
        t('BEE3314' /* Produto X Filial não cadastrada */)
      );
    }
  }
}
 
async function validCode(req) {
  const code = req.body.code || req.query.code;
 
  if (!code) {
    throw new APIError('INVALID_PRINTER_CODE');
  } else {
    const existPrinter = await PrinterInformation.getPrinter({
      code,
    });
 
    if (existPrinter) {
      throw new APIError(
        'EXIST_PRINTER_CODE',
        t('BEE1173', { 0: code } /* Codigo %{0} já cadastrado  */)
      );
    }
  }
}
 
function validPrinterName(req) {
  const name = req.body.name || req.query.name;
 
  if (!name) {
    throw new APIError('INVALID_PRINTER_NAME');
  }
}
 
function validPrinterLocation(req) {
  const location = req.body.location || req.query.location;
 
  if (!location) {
    throw new APIError('INVALID_PRINTER_LOCATION');
  }
}
 
function validPrinterType(req) {
  const printerType = req.body.type || req.query.type;
 
  if (!printerType) {
    throw new APIError(
      '',
      t('BEE1225' /* Tipo da Impressora deve ser informado ! */)
    );
  }
}
 
function validOutboundOrderId(req) {
  const outboundOrderId = req.body.outboundOrderId || req.query.outboundOrderId;
 
  if (!outboundOrderId) {
    throw new APIError('', t('BEE1915' /* Documento não localizada */));
  }
}
 
function validPrinterCode(req) {
  const printerCode = req.body.printerCode || req.query.printerCode;
 
  if (!printerCode) {
    throw new APIError('', t('BEE1919' /* Impressora não localizada */));
  }
}
 
async function validSerialGroup(req) {
  const serialGroupId = req.body.serialGroupId || req.query.serialGroupId;
  const code = req.body.code || req.query.code;
 
  if (!serialGroupId && !code) {
    throw new APIError(
      'BAD_REQUEST',
      t('BEE2857' /* Etiqueta não encontrada. */)
    );
  }
 
  const serial = await SerialGroupInformation.getSerialGroup(
    serialGroupId
      ? {
          id: serialGroupId,
        }
      : {
          code,
        },
    { userBranches: req.userBranches }
  );
 
  if (!serial) {
    throw new APIError(
      'BAD_REQUEST',
      t('BEE2857' /* Etiqueta não encontrada. */)
    );
  }
 
  req.serialGroup = serial;
}
 
async function validSerialLabel(req) {
  const serialGroupCode = req.body.serialGroupCode || req.query.serialGroupCode;
 
  if (!serialGroupCode) {
    throw new APIError(
      'BAD_REQUEST',
      t('BEE2857' /* Etiqueta não encontrada. */)
    );
  }
 
  const serialGroup = await SerialGroupInformation.getSerialGroup(
    {
      code: serialGroupCode,
    },
    {
      userBranches: req.userBranches,
    }
  );
 
  if (!serialGroup || !serialGroup.id) {
    throw new APIError(
      'BAD_REQUEST',
      t('BEE2857' /* Etiqueta não encontrada. */)
    );
  }
 
  const serialLabels = await SerialLabelInformation.getAllSerialLabelWithEAN(
    {
      serialGroupId: serialGroup.id,
      quantity: { [Op.gt]: 0 },
    },
    {
      attributes: ['code', 'productCode', 'lotNumber', 'expirationDate'],
    }
  );
 
  req.serialLabels = serialLabels;
}
 
async function validPrinterAgent(req) {
  const agentCode = req.body.agentCode || req.query.agentCode;
 
  if (agentCode) {
    const existAgent = await AgentInformation.getAgent({
      code: agentCode,
    });
 
    if (!existAgent) {
      throw new APIError(
        'BAD_REQUEST',
        t('BEE3509' /* Agente não localizado! */)
      );
    }
  }
}
 
async function validPrinterLanguageCode(req) {
  const languageCode = req.body.languageCode || req.query.languageCode;
 
  if (!languageCode) {
    throw new APIError(
      'BAD_REQUEST',
      t('BEE4210' /* A linguagem da impressora não foi informada. */)
    );
  }
}
 
async function validDeleteHasAssociatedUser(req) {
  const printerId = req.body.printerId || req.query.printerId;
 
  const printerData = await PrinterInformation.getPrinter({
    id: printerId,
  });
 
  const userPrinters = await UserPrinterInformation.getUserPrinter({
    printerCode: printerData.code,
  });
 
  if (userPrinters) {
    throw new APIError(
      '',
      t(
        'BEE4252' /* Não é possível excluir esta impressora, pois ela está vinculada a outros registros no sistema */
      )
    );
  }
}
 
export default {
  validPrinterId,
  validCode,
  validPrinterName,
  validPrinterType,
  validPrinterLocation,
  validPrinterCode,
  validOutboundOrderId,
  validSerialGroup,
  validProductBranch,
  validSerialLabel,
  validPrinterAgent,
  validPrinterLanguageCode,
  validDeleteHasAssociatedUser,
};