Untitled

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

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

myHeaders.append("Authorization", "Bearer eyJhbGciOiJIUzI1NiJ9.eyJhY2NvdW50LnR5cGUiOiJhY2NvdW50LnRlbmFudC5lbXBsb3llZSIsInRlbmFudC5uYW1lIjoiOTUwNGQ4ZTMtYzNkMC00YmE1LWFmYzQtODhhYjEzY2JjZWYyIiwib250b2xvZ3kudXVpZCI6IjExZWMtZWJmZC0xMThlMTQ2MC05NjI5LWI2OThmOGQwNmM1NSIsInN1YiI6ImE4YTlmNzQyLWE1NGItNGQwNi04ZmU5LWI0MWZiZDgxODVjYyIsImFjY291bnQubmFtZSI6IkFsaWNlIExlZSIsImFjY291bnQudXVpZCI6ImE4YTlmNzQyLWE1NGItNGQwNi04ZmU5LWI0MWZiZDgxODVjYyIsInRlbmFudC51dWlkIjoiNTQ0NjZmMjYtZTY0YS00ODVjLWEzNjgtMTQwODM1NGM4YjRhIiwidGVuYW50LnR5cGUiOiJ0ZW5hbnQudGVzdCIsImV4cCI6MTY1NTMxMzYxOCwidGVuYW50LnRpbWV6b25lIjoiQW1lcmljYVwvTG9zX0FuZ2VsZXMiLCJpYXQiOjE2NTUyMjcyMTh9.37Rb8OGNYaCRH-n66Ro_UhiSomjdsxVNyevnbCwMEWE");
myHeaders.append("Content-Type", "application/json");

// attempt to handle scaling for multi-exposure policy.
// would need example of same to troubleshoot reuest against
// 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 raw = JSON.stringify(
  {
    "policyholderLocator": oldResponse.characteristics[0].policyholderLocator,
    "productName": oldResponse.productName,
    "finalize": false,
    "paymentScheduleName": oldResponse.paymentScheduleName,
    "fieldValues": oldResponse.characteristics[0].fieldValues,
    "exposures": [{
      "exposureName": oldResponse.exposures[0].name,
      "fieldValues": oldResponse.exposures[0].characteristics[0].fieldValues,
      "perils": oldResponse.exposures[0].perils
    }]
  }
);

const outerRequest = {
  method: 'POST',
  headers: myHeaders
}
const innerRequest = {
  method: 'POST',
  headers: myHeaders,
  body: raw,
  redirect: 'follow'
};

fetch('https://api.sandbox.socotra.com/policy/' + oldResponse.locator + '/discard', outerRequest)
.then(response => console.log(response))
.then((() => {
  fetch("https://api.sandbox.socotra.com/policy", innerRequest)
  .then(response => response.text())
  .then(response => console.log(JSON.parse(response)))
  .catch(error => console.log('error', error));
}))