Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
2.0 kB
3
Indexable
Never

const fetch = require('node-fetch')
let headers = new fetch.Headers()
const oldResponse = require('./response.json')

headers.append("Content-Type", "application/json")

// handle scaling for multi-exposure policy
const oldExposures = oldResponse.exposures
let newExposures = []
for (let key in oldExposures) {
  newExposures.push({
    "exposureName": oldExposures[key].name,
    "fieldValues": oldExposures[key].characteristics[0].fieldValues,
    "perils": oldExposures[key].perils
  })
}

const pasteBody = JSON.stringify(
  {
    "policyholderLocator": oldResponse.characteristics[0].policyholderLocator,
    "productName": oldResponse.productName,
    "finalize": false,
    "paymentScheduleName": oldResponse.paymentScheduleName,
    "fieldValues": oldResponse.characteristics[0].fieldValues,
    "exposures": newExposures
  }
)

const authRequest = {
  method: 'POST',
  headers: headers,
  redirect: 'follow',
  body: JSON.stringify({
    "hostName":"ddavis-candidate-configeditor.co.sandbox.socotra.com",
    "username":"alice.lee",
    "password":"socotra"
  })
}
let cutRequest = {
  method: 'POST',
  headers: headers
}
let pasteRequest = {
  method: 'POST',
  headers: headers,
  redirect: 'follow',
  body: pasteBody
}

!async function(){
  const token = await fetch("http://api.sandbox.socotra.com/account/authenticate", authRequest)
    .then(response => response.text())
    .then(response => JSON.parse(response))
    .then(response => {return response.authorizationToken})
    headers.append("Authorization", "Bearer " + token)
}()
  .then((() => {
    fetch('https://api.sandbox.socotra.com/policy/${oldResponse.locator}/discard', cutRequest)
    .then(response => console.log(response))
    .then((() => {
      fetch("https://api.sandbox.socotra.com/policy", pasteRequest)
      .then(response => response.text())
      .then(response => console.log(JSON.parse(response)))
      .catch(error => console.log('error', error))
    }))
  }))