tax build construction simplify &effecticent

You can also build the object with values in-line, like:

  let taxObj = {
    type: 'TAX_' + taxRateBrackets[i].taxCode,
    amount: {
      value: taxRateBrackets[i].bracketAmount
    }
  }

It also might be more beneficial to separate the conditional / object building logic into a separate (non-exported) method that does the mapping. Like this:

function mapTaxResponse (countryCode, taxRateBracket, currencyCode) {
  if (hasVatTax(countryCode, taxRateBracket)) {
    return getVatTax(taxRateBracket, currencyCode)
  } else {
    return getNonVatTax(taxRateBracket, currencyCode)
  }
}

function hasVatTax (countryCode, taxRateBracket) {
  if (!vatcountryCodes.includes(countryCode)) {
    return false
  }

  let taxRateAmount = _.get(taxRateBracket, 'taxRateAmount')
  let taxRateId = _.get(taxRateBracket, 'taxRateId')

  return (taxRateAmount !== null 
&
&
 taxRateId !== null)
}

function getVatTax (taxRateBracket, currencyCode) {
  return {
    type: `TAX_${taxRateBracket.taxCode}`,
    amount: {
      code: currencyCode
    },
    taxRate: {
      amount: {
        value: taxRateBracket.taxRateAmount
      }
    },
    taxRecord: taxRateBracket.taxRateId
  }
}

function getNonVatTax (taxRateBracket, currencyCode) {
  return {
    type: `TAX_${taxRateBracket.taxCode},
      amount: {}
  }
}

And then the main function can be simplified to just:

function taxBuilder (rq, taxRateBrackets) {
  return taxRateBrackets.map(taxRateBracket =>
 mapTaxResponse(rq.countryCode, taxRateBracket, rq.currencyCode))
}

Last updated

Was this helpful?