{"size": 1273, "ext": "js", "max_stars_repo_path": "src/v1/delivery/clickEntregas/clickEntregasEstimateService.js", "max_stars_repo_name": "amosbatista/integracao-loggi-", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/v1/delivery/clickEntregas/clickEntregasEstimateService.js", "max_issues_repo_name": "amosbatista/integracao-loggi-", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/v1/delivery/clickEntregas/clickEntregasEstimateService.js", "max_forks_repo_name": "amosbatista/integracao-loggi-", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 24.0188679245, "max_line_length": 93, "alphanum_fraction": 0.5813040063}
import httpReq from 'superagent'
import log from '../../request/log/logGenerator'
import notaryAddress from '../../notary/entity'

const service = (addressData) => {

  return new Promise ( (resolve, reject) => {

    const query = `
      {
        "matter":"Documents",
        "points":[{
          "address":"${addressData.completeAddress}"
        },{
          "address":"${notaryAddress.completeAddress}"
      }]}
    `

    httpReq.post(`${process.env.CLICK_ENTREGAS_API}/calculate-order`)
    .send(query)
    .set('X-DV-Auth-Token', process.env.CLICK_ENTREGAS_TOKEN)
    .set('Content-Type', 'application/json')
    .end((err, apiRes) => {
      
      if(err){
        reject({
          message: 'Erro ao fazer requisição para calcular entrega na API da Click Entregas',
          object: JSON.stringify(apiRes.body)
        })

        return
      }

      log("Estimative delivery result:", apiRes.body)

      if(apiRes.body.errors){
        reject({
          message: 'Erro interno da API Click Entregas ao realizar requisição',
          object: JSON.stringify(apiRes.body.errors.text)
        })

        return
      }
      
      resolve({
        estimatedCost:apiRes.body.order.payment_amount,
      })
      
    })
  })
}

export default service