diff --git a/actions/index.js b/actions/index.js index 627215b9..28b80bd6 100644 --- a/actions/index.js +++ b/actions/index.js @@ -348,7 +348,7 @@ export const createCase = (appealTypeId) => { console.log(config); return axios(config) .then((res) => { - console.log("posted ", res.data); + // console.log("posted ", res.data); return res.data; }) .catch((error) => { @@ -361,7 +361,8 @@ export const updateCase = (incidentId, updateBody, updateFormCollection) => { var config = { method: "patch", - url: WEBAPI_URL + updateFormCollection + "(" + incidentId + ")", + //url: WEBAPI_URL + updateFormCollection + "(" + incidentId + ")", + url: "/api/proxy/" + updateFormCollection + "(" + incidentId + ")", headers: { "OData-MaxVersion": "4.0", "OData-Version": "4.0", diff --git a/api/proxy/[...route].js b/api/proxy/[...route].js deleted file mode 100644 index fa5688f4..00000000 --- a/api/proxy/[...route].js +++ /dev/null @@ -1,26 +0,0 @@ -import axios from "axios"; - -const API_PATH = "/api/data/v8.2/"; - -const WEBAPI_URL = "https://devcrm2016.llcdt.gov.wales/DVPINS" + API_PATH; - -const accessToken = - "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6IlVXelVaa0U3OEx0NVBGRkJ0LV9seVdoMjdjTSIsImtpZCI6IlVXelVaa0U3OEx0NVBGRkJ0LV9seVdoMjdjTSJ9.eyJhdWQiOiJodHRwczovL2RldmNybTIwMTYubGxjZHQuZ292LndhbGVzLyIsImlzcyI6Imh0dHA6Ly9mcy50ZXN0Lmdvdi53YWxlcy9hZGZzL3NlcnZpY2VzL3RydXN0IiwiaWF0IjoxNjMxMTcwMjI1LCJuYmYiOjE2MzExNzAyMjUsImV4cCI6MTYzMTE5OTAyNSwidXBuIjpbIlJvYmVydC5Cb25kQHRlc3QuZ292LndhbGVzIiwicm9iZXJ0LmJvbmRAdGVzdC5nb3Yud2FsZXMiXSwicHJpbWFyeXNpZCI6IlMtMS01LTIxLTE4MDA4NDIwNzYtMjQ1NTYwNjU5LTQyMTU3NzU0NjktMTY2MjgiLCJ1bmlxdWVfbmFtZSI6IlhNXFxCb25kUiIsImFwcHR5cGUiOiJQdWJsaWMiLCJhcHBpZCI6IjQyN2ZhNzljLWI1ZmMtNDJhZC04M2Q0LTQxMGIwMzk0OGFiNiIsImF1dGhtZXRob2QiOiJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvYXV0aGVudGljYXRpb25tZXRob2Qvd2luZG93cyIsImF1dGhfdGltZSI6IjIwMjEtMDktMDlUMDY6NTA6MjIuMTgzWiIsInZlciI6IjEuMCJ9.jds1ILVmVonh1g_KDDzFHZV7_iFpR4GZ6m_uw7YDQpQLXQlFXkPLf-Fan1h61Jl78ZmsBnhPKREvqALQ13tcMPPDg4SK5i6BYI7Jw8eKnqCjNb_mFAnnSCL0NeqZY4JV-HYzgY5tpvuCWgpjL2v_5BKOtU9mRL_700D7Fjt_R6nlI89dOKy3iT88lUxPostTVEC1nBxvh1AjjACG6103ymtRBtRy2ONowk4PClhCO3ZWwnVg1kb4GmXqhKNsXBvEscuZQuAzvawwTIXMvjwUBFSu4fsGdxxjfdsXVGACPdcUn7FekJIzaDtS2I_DvzrWLn9B1BDyBEctUqoVWOWqBw"; - -const crmHeadersReturn = { - withCredentials: true, - headers: { - "Content-Type": "application/json; charset=utf-8", - "OData-MaxVersion": "4.0", - "OData-Version": "4.0", - "Accept": "application/json", - "Prefer": "return=representation", - "Authorization": "Bearer " + accessToken, - }, -}; - -export default async function (res, req) { - return new Promise((resolve, reject) => { - axios.patch(); - }); -} diff --git a/components/newappeal/complete.js b/components/newappeal/complete.js index 2fef3643..7989b388 100644 --- a/components/newappeal/complete.js +++ b/components/newappeal/complete.js @@ -33,6 +33,10 @@ let CompleteAppeal = (props) => { let incidentId = props.appealType.caseReference.incidentid; let updateBody = props.props.form.appealForm.values; + + Object.assign(updateBody, { + "pinswg_name": props.appealType.caseReference.ticketnumber, + }); updateBody = JSON.stringify(updateBody); updateBody = updateBody.replace(/:"Yes"/gm, `:true`); diff --git a/pages/api/proxy/[...route].js b/pages/api/proxy/[...route].js new file mode 100644 index 00000000..479e5ddc --- /dev/null +++ b/pages/api/proxy/[...route].js @@ -0,0 +1,110 @@ +import axios from "axios"; +import https from "https"; +import CryptoJS from "crypto-js"; + +const WEBAPI_URL = "https://dev-pedw-ns.servicebus.windows.net/dev-pedw-hc/"; + +// const azureHeaders = { +// headers: { +// "OData-MaxVersion": "4.0", +// "OData-Version": "4.0", +// "Accept": "application/json", +// "Prefer": 'odata.include-annotations="*",return=representation', +// "Content-Type": "application/json", +// "ServiceBusAuthorization": SASToken, +// }, +// }; + +const getSASToken = () => { + var m_ResourceURI = "dev-pedw-ns.servicebus.windows.net"; + var m_Path = "dev-pedw-hc"; + var m_SasKey = "Ml0Y/S/nfRcmIrHFRkO4jNm2J3iH52TSxPiak7+E1+Y="; + var m_SasKeyName = "devpedwnspolicy"; + + var encodedResourceUri = encodeURIComponent( + "https://" + m_ResourceURI + "/" + m_Path + "/" + ); + + var t0 = new Date(1970, 1, 1, 0, 0, 0, 0); + var t1 = new Date(); + var expireInSeconds = + +(31 * 24 * 3600) + 3600 + (((t1.getTime() - t0.getTime()) / 1000) | 0); + + //the line below is a hack that converts to UTF8 + var plainSignature = JSON.parse( + JSON.stringify(encodedResourceUri + "\n" + expireInSeconds) + ); + + var hash = CryptoJS.HmacSHA256(plainSignature, m_SasKey); + var base64HashValue = CryptoJS.enc.Base64.stringify(hash); + + var token = + "SharedAccessSignature sr=" + + encodedResourceUri + + "&sig=" + + encodeURIComponent(base64HashValue) + + "&se=" + + expireInSeconds + + "&skn=" + + m_SasKeyName; + + return token; +}; + +export default async function (req, res) { + // console.log(req.method); + + var updateBody = { + "pinswg_areaofsiteinhectaresdec": 21, + "pinswg_developmentaffectsettingifalisted": false, + "pinswg_floorspaceinsquaremeters": "123123124", + "pinswg_incarelatestoca": false, + "pinswg_isfloodinganissue": true, + "pinswg_isthesitewithinanaonb": false, + "pinswg_landuse": "846040002", + "pinswg_sitewithinsssi": true, + "pinswg_sitewithinagreenbelt": false, + "pinswg_siteviewablefromroad": false, + "pinswg_typeofplanningapplication": "846040000", + }; + var data = JSON.stringify(req.body); + const SASToken = getSASToken(); + + // console.log(SASToken); + + var config = { + method: req.method, + //url: WEBAPI_URL + updateFormCollection + "(" + incidentId + ")", + url: WEBAPI_URL + req.query.route[0], + headers: { + "OData-MaxVersion": "4.0", + "OData-Version": "4.0", + "Accept": "application/json", + "Prefer": 'odata.include-annotations="*",return=representation', + "Authorization": SASToken, + "Content-Type": "application/json", + }, + data: data, + }; + + console.log(config); + + return new Promise((resolve, reject) => { + // console.log(config); + axios(config) + .then((response) => { + res.statusCode = 200; + res.setHeader("Content-Type", "application/json"); + res.setHeader("Cache-Control", "max-age=1800000"); + res.end(JSON.stringify(response.data)); + console.log("response data", response.data); + resolve(); + }) + .catch((error) => { + console.log("proxy response error", error); + res.json(error); + res.status(405).end(); + return resolve(); + }); + }); +}