All files / middlewares depositValidations.js

100% Statements 40/40
100% Branches 44/44
100% Functions 8/8
100% Lines 39/39

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                3x   3x 1x   2x       2x 1x               3x   3x   1x                   2x       2x 1x                 2x   2x 1x         3x   3x 2x             2x         2x   2x 1x                         3x   3x 1x                         4x   4x 1x     3x   3x 1x           2x                             2x 1x                         4x   4x   4x                             4x 3x                                      
import APIError from '../helpers/error';
import { t } from '../helpers/i18n';
import DepositInformation from '../source/deposits/information';
import BranchInformation from '../source/branches/information';
import StockInformation from '../source/stock/information';
import { models } from '../../config/database';
 
async function validDepositId(req) {
  const depositId = req.body.depositId || req.query.depositId;
 
  if (!depositId) {
    throw new APIError('INVALID_ID', t('BEE3421' /* ID inválido */));
  } else {
    const existDeposit = await DepositInformation.getDeposit({
      id: depositId,
    });
 
    if (!existDeposit) {
      throw new APIError(
        'NOT_EXIST_DEPOSIT_ID',
        t('BEE1193', { 0: depositId } /* Deposito %{0} não localizada */)
      );
    }
  }
}
async function validCode(req) {
  const code = req.body.code || req.query.code;
 
  if (!code) {
    // throw new APIError('INVALID_CODE', t('BEE3430' /* Código inválido */));
    throw new APIError(
      'NOT_EXIST_DEPOSIT_ID',
      t(
        'BEE2324',
        {
          0: t('BEE1995' /* Código do Depósito */),
        } /* %{0} deve ser informado */
      )
    );
  } else {
    const existDeposit = await DepositInformation.getDeposit({
      code,
    });
 
    if (existDeposit) {
      throw new APIError(
        'EXIST_DEPOSIT_CODE',
        t('BEE1173', { 0: code } /* Codigo %{0} já cadastrado  */)
      );
    }
  }
}
 
function validDepositName(req) {
  const name = req.body.name || req.query.name;
 
  if (!name) {
    throw new APIError('INVALID_NAME', t('BEE3562' /* Nome inválido */));
  }
}
 
async function validBranch(req) {
  const branch = req.body.branchCode || req.query.branchCode;
 
  if (branch) {
    const existBranch = await BranchInformation.getBranch(
      {
        code: branch,
      },
      { userBranches: req.userBranches }
    );
 
    if (!existBranch) throw new APIError('', `Filial ${branch} inválida!`);
  }
}
 
async function validCodeErp(req) {
  const code = req.body.codeErp || req.query.codeErp;
 
  if (!code) {
    throw new APIError(
      '',
      t(
        'BEE3740',
        {
          0: t('BEE4057', { 0: t('BEE4061' /* ERP */) } /* Código (%{0}) */),
        } /* %{0} deve ser informado */
      )
    );
  }
}
 
async function validNameErp(req) {
  const name = req.body.nameErp || req.query.nameErp;
 
  if (!name) {
    throw new APIError(
      '',
      t(
        'BEE3740',
        {
          0: t('BEE4059', { 0: t('BEE4061' /* ERP */) } /* Nome (%{0}) */),
        } /* %{0} deve ser informado */
      )
    );
  }
}
 
async function validDeleteDepositStockMovement(req) {
  const depositId = req.body.depositId || req.query.depositId;
 
  if (!depositId) {
    throw new APIError('INVALID_ID', t('BEE3421' /* ID inválido */));
  }
 
  const { code } = await DepositInformation.getDeposit({ id: depositId });
 
  if (!code) {
    throw new APIError(
      'NOT_EXIST_DEPOSIT_ID',
      t('BEE1193', { 0: code } /* Deposito %{0} não localizada */)
    );
  }
 
  const stockMovements = await StockInformation.getStockMovements(
    { depositCode: code },
    {
      attributes: ['id'],
      include: [
        {
          required: true,
          model: models.Product,
          as: 'product',
          attributes: ['id'],
        },
      ],
    }
  );
 
  if (stockMovements && stockMovements.length > 0) {
    throw new APIError(
      '',
      t(
        'BEE4159',
        {
          0: code,
        } /* O depósito %{0} não pode ser deletado pois possui movimentação em estoque */
      )
    );
  }
}
 
async function validDeleteDepositStockBalance(req) {
  const depositId = req.body.depositId || req.query.depositId;
 
  const { code } = await DepositInformation.getDeposit({ id: depositId });
 
  const stockBalance = await StockInformation.getStockBalance(
    { depositCode: code },
    {
      attributes: ['id'],
      include: [
        {
          required: true,
          model: models.Product,
          as: 'product',
          attributes: ['id'],
        },
      ],
    }
  );
 
  if (stockBalance) {
    throw new APIError(
      '',
      t(
        'BEE4226' /* Não é possível excluir este depósito, pois há produtos com saldo de estoque em seus endereços */
      )
    );
  }
}
 
export default {
  validDepositId,
  validCode,
  validDepositName,
  validBranch,
  validCodeErp,
  validNameErp,
  validDeleteDepositStockMovement,
  validDeleteDepositStockBalance,
};