All files / source/packages import.js

100% Statements 25/25
100% Branches 16/16
100% Functions 2/2
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            5x 5x 5x   5x 1x 1x 1x   4x       4x 1x 1x 1x       5x 1x 1x 1x     5x               5x   5x 5x   5x 3x           2x                       2x       5x          
/* eslint-disable no-restricted-syntax */
import PackageCreate from './create';
import PackageInformation from './information';
import { t } from '../../helpers/i18n';
 
async function validPackage(pckg) {
  let isValid = true;
  let code = '';
  let message = '';
 
  if (!pckg.code) {
    isValid = false;
    code = 9000;
    message = t('BEE1171' /* Código deve ser informado ! */);
  } else {
    const existPackage = await PackageInformation.getPackage({
      code: pckg.code,
    });
 
    if (existPackage) {
      isValid = false;
      code = 9001;
      message = t('BEE1173', { 0: pckg.code } /* Codigo %{0} já cadastrado  */);
    }
  }
 
  if (!pckg.name) {
    isValid = false;
    code = 9002;
    message = t('BEE1344' /* Descrição deve ser informada */);
  }
 
  return {
    isValid,
    code,
    message,
  };
}
 
async function importPackages(pckgs, userId) {
  const listPackagesErrors = [];
 
  for (const pckg of pckgs) {
    const valid = await validPackage(pckg);
 
    if (!valid.isValid) {
      listPackagesErrors.push({
        code: pckg.code,
        error: valid.code,
        message: valid.message,
      });
    } else {
      const newPackage = {
        code: pckg.code,
        name: pckg.name,
        weight: pckg.weight ? pckg.weight : 0,
        height: pckg.height ? pckg.height : 0,
        width: pckg.width ? pckg.width : 0,
        length: pckg.length ? pckg.length : 0,
        note: pckg.note,
        createdUser: userId,
        updatedUser: userId,
      };
 
      await PackageCreate.createPackage(newPackage);
    }
  }
 
  return { listPackagesErrors };
};
 
export default {
  importPackages,
}