All files / services/totvs getStockBalance.js

92.85% Statements 13/14
88.88% Branches 8/9
100% Functions 3/3
92.85% Lines 13/14

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              6x 6x 1x 1x     5x       4x     16x 12x       4x   4x               3x                   3x   3x              
import APIError from '../../helpers/error';
import isJson from '../../helpers/validators/totvs';
import RestService from './rest';
import { t } from '../../helpers/i18n';
import DepositInformation from '../../source/deposits/information';
 
async function getStockErp(branchCode) {
  try {
    if (!branchCode) {
      const message = t('BEE3173' /* A filial deve ser informada! */);
      throw new APIError('', message);
    }
 
    const depositCodeList = await DepositInformation.getAllDeposits({
      attributes: ['codeErp'],
    });
 
    const codDepos = [
      ...new Set(
        depositCodeList
          .filter((deposit) => deposit.codeErp)
          .map((deposit) => deposit.codeErp)
      ),
    ].join(',');
 
    const query = `codEstabel=${branchCode}&codDepos=${codDepos}`;
 
    const result = await RestService.requestRest(
      branchCode,
      null,
      'getStockBalance',
      'GET',
      query
    );
 
    Iif (
      result &&
      result.status === 200 &&
      result.data &&
      result.data.balances &&
      isJson(result.data.balances)
    ) {
      return result.data.balances;
    }
 
    return null;
  } catch (error) {
    return error;
  }
}
 
export default {
  getStockErp,
};