All files / middlewares customerBranchValidation.js

100% Statements 26/26
100% Branches 22/22
100% Functions 5/5
100% Lines 26/26

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                4x 4x 3x             3x 2x           1x               4x   4x 1x 3x 1x 2x 1x               3x   3x     3x 2x         3x   3x           3x 2x         3x 3x   3x       3x 2x                            
import APIError from '../helpers/error';
import I18n from '../helpers/i18n';
import CustomerBranchInformation from '../source/customerBranch/information';
import CustomerInformation from '../source/customers/information';
import BranchInformation from '../source/branches/information';
 
async function validCustomerBranchId(req) {
  const customerBranchId =
    req.body.customerBranchId || req.query.customerBranchId;
  if (customerBranchId) {
    const customerBranch = await CustomerBranchInformation.getCustomerBranch(
      {
        id: customerBranchId,
      },
      { userBranches: req.userBranches }
    );
 
    if (!customerBranch) {
      throw new APIError(
        '',
        I18n.t('BEE2027' /* Cliente x Filial não localizado */)
      );
    }
  } else {
    throw new APIError(
      '',
      I18n.t('BEE2027' /* Cliente x Filial não localizado */)
    );
  }
}
 
async function validParameters(req) {
  const { customerCode, branchCode, restriction } = req.body;
 
  if (!customerCode) {
    throw new APIError('', I18n.t('BEE1920' /* Cliente não localizado */));
  } else if (!branchCode) {
    throw new APIError('', I18n.t('BEE2028' /* Filial não localizado */));
  } else if (restriction === '') {
    throw new APIError(
      '',
      I18n.t('BEE2029' /* O campo Restrição esta vazio */)
    );
  }
}
 
async function validCustomerCode(req) {
  const { customerCode } = req.body;
 
  const customer = await CustomerInformation.getCustomer({
    code: customerCode,
  });
  if (!customer) {
    throw new APIError('', I18n.t('BEE1920' /* Cliente não localizado */));
  }
}
 
async function validBranchCode(req) {
  const { branchCode } = req.body;
 
  const branch = await BranchInformation.getBranch(
    {
      code: branchCode,
    },
    { userBranches: req.userBranches }
  );
  if (!branch) {
    throw new APIError('', I18n.t('BEE2028' /* Filial não localizado */));
  }
}
 
async function RegisteredCustomerBranch(req) {
  const customerCode = req.body.customerCode || req.query.customerCode;
  const branchCode = req.body.branchCode || req.query.branchCode;
 
  const customerBranch = await CustomerBranchInformation.getCustomerBranch({
    customerCode,
    branchCode,
  });
  if (customerBranch) {
    throw new APIError(
      '',
      I18n.t('BEE2034' /* Cliente já cadastrado com a Filial */)
    );
  }
}
 
export default {
  validCustomerBranchId,
  validParameters,
  validBranchCode,
  validCustomerCode,
  RegisteredCustomerBranch,
};