From 8e8797418ae535f66d75cb8728006fca16200b50 Mon Sep 17 00:00:00 2001 From: "robert.bond@test.gov.wales" Date: Tue, 7 Sep 2021 12:46:59 +0100 Subject: [PATCH 01/10] added crm driven form via oauth --- actions/index.js | 136 +++++++++++++++++++++----- components/elements/index.js | 35 ++----- components/newappeal/buildcheckrow.js | 76 +++++++------- components/newappeal/buildfield.js | 1 + components/newappeal/buildrow.js | 32 +++--- components/newappeal/createCase.js | 45 +++------ pages/newappeal/[appealtypes].js | 26 +++-- pages/searchresults.js | 9 +- store/formData/action.js | 15 +-- store/formData/reducer.js | 4 + store/searchOutput/action.js | 8 ++ store/searchOutput/reducer.js | 7 ++ 12 files changed, 245 insertions(+), 149 deletions(-) diff --git a/actions/index.js b/actions/index.js index 015e296e..1ab22b86 100644 --- a/actions/index.js +++ b/actions/index.js @@ -13,6 +13,10 @@ if (process.browser) { BASE_URL = process.env.API_ROOT || `http://localhost:${port}`; } +const WEBAPI_URL = "https://devcrm2016.llcdt.gov.wales/DVPINS"; + +const API_PATH = "/api/data/v8.2/"; + const agent = new https.Agent({ rejectUnauthorized: false, }); @@ -24,11 +28,8 @@ const crmHeaders = { "OData-Version": "4.0", "Accept": "application/json", "Prefer": 'odata.include-annotations="*"', - agent, - }, - auth: { - username: "xm\bondr", - password: "!Sky9fish1972", + "Authorization": + "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6IlVXelVaa0U3OEx0NVBGRkJ0LV9seVdoMjdjTSIsImtpZCI6IlVXelVaa0U3OEx0NVBGRkJ0LV9seVdoMjdjTSJ9.eyJhdWQiOiJodHRwczovL2RldmNybTIwMTYubGxjZHQuZ292LndhbGVzLyIsImlzcyI6Imh0dHA6Ly9mcy50ZXN0Lmdvdi53YWxlcy9hZGZzL3NlcnZpY2VzL3RydXN0IiwiaWF0IjoxNjMxMDAwMjUxLCJuYmYiOjE2MzEwMDAyNTEsImV4cCI6MTYzMTAyOTA1MSwidXBuIjpbIlJvYmVydC5Cb25kQHRlc3QuZ292LndhbGVzIiwicm9iZXJ0LmJvbmRAdGVzdC5nb3Yud2FsZXMiXSwicHJpbWFyeXNpZCI6IlMtMS01LTIxLTE4MDA4NDIwNzYtMjQ1NTYwNjU5LTQyMTU3NzU0NjktMTY2MjgiLCJ1bmlxdWVfbmFtZSI6IlhNXFxCb25kUiIsImFwcHR5cGUiOiJQdWJsaWMiLCJhcHBpZCI6IjQyN2ZhNzljLWI1ZmMtNDJhZC04M2Q0LTQxMGIwMzk0OGFiNiIsImF1dGhtZXRob2QiOiJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvYXV0aGVudGljYXRpb25tZXRob2Qvd2luZG93cyIsImF1dGhfdGltZSI6IjIwMjEtMDktMDdUMDc6Mzc6MjIuMzM3WiIsInZlciI6IjEuMCJ9.L8husDSMXMtZxpxKDS0cpPplG6xoHQjSdd17qMfgKZaEUPUOKPy4rznxNwAMp7WxussNeK__nOKE9vHNgc5Nk4lzxbvcYy3jeE7bLWHsB5UYFa1FIrPZpzuEdPIgoWAzhTa4k-anzsA0YNMHpV6QHT14F9wkBTsT8MUtpT2BiNfbflQQ_xtQavp_pCww-1-guysM6USv5NJkuvrHpXjA5_HpaGbprLI8Or3Htjsdgq9c4uCxfjoAGZsjUmtvaIL-jQnkR6-ndYezo12tes7ql2iV4iZT4-fvcib8sUdzHkgFvNmyzMblhogUo8QciTBcvF7-syCfNzLG2vvWSZJ9_g", }, }; @@ -39,9 +40,17 @@ export const getBasicSearch = (searchString) => { axios //.get("https://devcrm2016.llcdt.gov.wales/DVPINS/api/data/v8.2/incidents?$select=_accountid_value,_customerid_value,pinswg_appealcasetype,statuscode,ticketnumber,title&$filter=contains(title, '" + searchString + "')&$count=true", crmHeaders) //.get("https://jsonplaceholder.typicode.com/todos/1",{proxy:{protocol: 'http',host:'lon3.sme.zscloud.net',port: 80}}) - .get(BASE_URL + "/api/searchresults2", { - httpsAgent: new https.Agent({ rejectUnauthorized: false }), - }) + // .get(BASE_URL + "/api/searchresults2", { + // httpsAgent: new https.Agent({ rejectUnauthorized: false }), + // }) + .get( + WEBAPI_URL + + API_PATH + + "incidents?$select=_accountid_value,_customerid_value,pinswg_appealcasetype,statuscode,ticketnumber,title&$filter=contains(title, '" + + searchString + + "')&$count=true", + crmHeaders + ) .then((res) => res.data) .catch((error) => { console.log("thiserror", error); @@ -49,6 +58,24 @@ export const getBasicSearch = (searchString) => { ); }; +export const getBasicSearchDetails = (appealType, caseReference) => { + return axios + .get( + WEBAPI_URL + + API_PATH + + "/pinswg_ " + + appealType + + "?$filter=pinswg_name eq '" + + caseReference + + "'&$count=true", + crmHeaders + ) + .then((res) => res.data) + .catch((error) => { + console.log("thiserror", error); + }); +}; + export const getFormData = (whichForm) => { console.log("got to axios", whichForm); console.log("api_root: ", BASE_URL); @@ -62,9 +89,18 @@ export const getFormData = (whichForm) => { //.get("api/appealforms/pinswg_harbourrevisionorder", { //.get("api/appealforms/pinswg_planningappeals78", { //.get(api/appealforms/pinswg_householderappealhas", { - .get(BASE_URL + "/api/appealforms/pinswg_" + whichForm, { - httpsAgent: new https.Agent({ rejectUnauthorized: false }), - }) + // .get(BASE_URL + "/api/appealforms/pinswg_" + whichForm, { + // httpsAgent: new https.Agent({ rejectUnauthorized: false }), + // }) + + .get( + WEBAPI_URL + + API_PATH + + "systemforms?$select=formid,name,formxml,type,objecttypecode&$filter=(objecttypecode%20eq%20%27pinswg_" + + whichForm + + "%27and%20type%20eq%202)&$count=true", + crmHeaders + ) .then((res) => res.data) .catch((error) => { console.log("thiserror", error); @@ -72,19 +108,59 @@ export const getFormData = (whichForm) => { ); }; -export const getAppealsTypes = () => { - console.log("got to axios: appeal types"); - console.log("api_root: ", BASE_URL); +export const getMandatoryFields = (whichForm) => { return axios - .get(BASE_URL + "/api/lookups/pinswg_appealcasetype", { - httpsAgent: new https.Agent({ rejectUnauthorized: false }), - }) + .get( + WEBAPI_URL + + API_PATH + + "EntityDefinitions(LogicalName='pinswg_" + + whichForm + + "')/Attributes?$count=true&$select=LogicalName,RequiredLevel", + crmHeaders + ) .then((res) => res.data) .catch((error) => { console.log("thiserror", error); }); }; +export const getPickLists = (whichForm) => { + return axios + .get( + WEBAPI_URL + + API_PATH + + "EntityDefinitions(LogicalName='pinswg_" + + whichForm + + "')/Attributes/Microsoft.Dynamics.CRM.PicklistAttributeMetadata?$select=LogicalName&$expand=OptionSet,GlobalOptionSet&$count=true", + crmHeaders + ) + .then((res) => res.data) + .catch((error) => { + console.log("thiserror", error); + }); +}; + +export const getAppealsTypes = () => { + console.log("got to axios: appeal types"); + console.log("api_root: ", BASE_URL); + return ( + axios + // .get(BASE_URL + "/api/lookups/pinswg_appealcasetype", { + // httpsAgent: new https.Agent({ rejectUnauthorized: false }), + // }) + .get( + WEBAPI_URL + + API_PATH + + "stringmaps?$filter=attributename%20eq%20%27pinswg_appealcasetype%27&$count=true&$select=value,stringmapid,organizationid,attributevalue", + crmHeaders + ) + .then((res) => res.data) + .catch((error) => { + console.log("thiserror", error); + }) + ); +}; + export const getBasicSearch2 = (searchString) => { var req = new XMLHttpRequest(); req.open( @@ -189,17 +265,23 @@ export const getAppealTypeDetail = (caseReference, appealcasetype) => { export const getLPA = () => { console.log("api_root: ", BASE_URL); - return axios + return ( + axios - .get( - BASE_URL + "/api/lookups/pinswg_lpa", - //.get("https://devcrm2016.llcdt.gov.wales/DVPINS/api/data/v8.2/accounts?$select=name", - { httpsAgent: new https.Agent({ rejectUnauthorized: false }) } - ) - .then((res) => res.data) - .catch((error) => { - console.log("thi serror", error); - }); + // .get( + // BASE_URL + "/api/lookups/pinswg_lpa", + // //.get("https://devcrm2016.llcdt.gov.wales/DVPINS/api/data/v8.2/accounts?$select=name", + // { httpsAgent: new https.Agent({ rejectUnauthorized: false }) } + // ) + .get( + WEBAPI_URL + API_PATH + "accounts?$count=true&$select=name", + crmHeaders + ) + .then((res) => res.data) + .catch((error) => { + console.log("thi serror", error); + }) + ); }; export const getPersonalAccount = () => { diff --git a/components/elements/index.js b/components/elements/index.js index 60e6afe2..207c2036 100644 --- a/components/elements/index.js +++ b/components/elements/index.js @@ -5,6 +5,7 @@ import DatePicker from "react-datepicker"; import React, { useState } from "react"; import "react-datepicker/dist/react-datepicker.css"; import moment from "moment"; +import jsonpath from "jsonpath"; const RenderTextfield = ({ id, @@ -356,35 +357,17 @@ export function YesNofield(props) { const RenderPickList = ({ datafieldname, + picklistData, name, label, input, meta: { touched, errorStr }, }) => { - const fetcher = (url) => fetch(url).then((res) => res.json()); - - const { data, error } = useSWR("/api/lookups/" + datafieldname, fetcher); - if (error) - return ( -
- - -
- ); - if (!data) - return ( -
-
{label} loading...
-
- ); - //console.log(JSON.stringify(data.GlobalOptionSet.Options)); - - const dropdownObj = data.GlobalOptionSet.Options; - + var dropdownObj = jsonpath.query( + picklistData, + "$..value[?(@.LogicalName=='" + datafieldname + "')]..Options" + ); + dropdownObj = dropdownObj[0]; return (
+
+ ); + } else { + return ( + + ); + } } const RenderMultiline = ({ @@ -349,7 +367,7 @@ export function YesNofield(props) { id={props.name} className="govuk-radios__input" //validate={[required]} - errorMsg="Select and option" + errorMsg="Select an option" component={RenderYesNo} /> ); @@ -414,7 +432,7 @@ export function NumericField(props) { ); break; @@ -57,9 +62,17 @@ export default function BuildField(props) { /> ); break; - case "{ 4273EDBD-AC1D-40d3-9FB2-095C621B552D}": + case "{4273EDBD-AC1D-40d3-9FB2-095C621B552D}": return ( - + ); break; case "{5B773807-9FB2-42db-97C3-7A91EFF8ADFF}": diff --git a/components/newappeal/complete.js b/components/newappeal/complete.js index 5feaf6fe..77ace2f4 100644 --- a/components/newappeal/complete.js +++ b/components/newappeal/complete.js @@ -11,12 +11,22 @@ import { updateCase } from "../../actions"; let CompleteAppeal = (props) => { // un comment to update incident with an appealtype id - // useEffect(() => { - // updateCase( - // props.appealType.caseReference.incidentid, - // props.appealType.appealTypeID - // ); - // }, []); + useEffect(() => { + // updateCase( + // props.appealType.caseReference.incidentid, + // props.appealType.appealTypeID, + // props.form.appealform.values, + // pinswg_appealcasetype@OData.Community.Display.V1.Formatted + // ); + console.log(props.appealType.caseReference.incidentid); + console.log(props.appealType.appealTypeID); + console.log(props.form); + console.log( + props.appealType.caseReference[ + "pinswg_appealcasetype@OData.Community.Display.V1.Formatted" + ] + ); + }, []); let { t } = useTranslation(); @@ -57,6 +67,7 @@ let CompleteAppeal = (props) => { setCurrentSection(currentSection + 1); }; + console.log(JSON.stringify(props.props.form.appealForm.values, null, 2)); alert(JSON.stringify(props.props.form.appealForm.values, null, 2)); return ( From b7815c112e8a4fe96f2eca30210403680e251df7 Mon Sep 17 00:00:00 2001 From: "robert.bond@test.gov.wales" Date: Tue, 14 Sep 2021 12:23:43 +0100 Subject: [PATCH 08/10] appeal update case --- actions/index.js | 27 +++----------- components/elements/index.js | 10 +++--- components/newappeal/complete.js | 61 ++++++++++++++++++++++++-------- 3 files changed, 57 insertions(+), 41 deletions(-) diff --git a/actions/index.js b/actions/index.js index 2970eb72..c43c6f44 100644 --- a/actions/index.js +++ b/actions/index.js @@ -335,14 +335,7 @@ export const createCase = (appealTypeId) => { var config = { method: "post", url: WEBAPI_URL + "incidents", - headers: { - "OData-MaxVersion": "4.0", - "OData-Version": "4.0", - "Accept": "application/json", - "Prefer": 'odata.include-annotations="*",return=representation', - "Content-Type": "application/json", - "ServiceBusAuthorization": SASToken, - }, + azureHeaders, data: data, }; console.log(config); @@ -356,23 +349,13 @@ export const createCase = (appealTypeId) => { }); }; -export const updateCase = (incidentID, appealTypeId) => { - appealTypeId = parseInt(appealTypeId); - var data = JSON.stringify({ - "pinswg_appealcasetype": appealTypeId, - }); +export const updateCase = (incidentId, updateBody, updateFormCollection) => { + var data = JSON.stringify(updateBody); var config = { method: "patch", - url: WEBAPI_URL + `incidents(${incidentID})`, - headers: { - "OData-MaxVersion": "4.0", - "OData-Version": "4.0", - "Accept": "application/json", - "Prefer": "return=representation", - "Authorization": "Bearer " + accessToken, - "Content-Type": "application/json", - }, + url: WEBAPI_URL + updateFormCollection + "(" + incidentId + ")", + azureHeaders, data: data, }; // console.log(config); diff --git a/components/elements/index.js b/components/elements/index.js index fc7f20b9..e3824ff1 100644 --- a/components/elements/index.js +++ b/components/elements/index.js @@ -432,14 +432,15 @@ export function NumericField(props) { ); @@ -455,14 +456,15 @@ export function DecimalField(props) { ); diff --git a/components/newappeal/complete.js b/components/newappeal/complete.js index 77ace2f4..2fef3643 100644 --- a/components/newappeal/complete.js +++ b/components/newappeal/complete.js @@ -8,26 +8,57 @@ import { reduxForm, formValueSelector } from "redux-form"; import { useDispatch, useSelector, shallowEqual, connect } from "react-redux"; import { setCurrentSection } from "../../store/appealType/action"; import { updateCase } from "../../actions"; +import jsonpath from "jsonpath"; + +import data from "../../data/collections.json"; let CompleteAppeal = (props) => { // un comment to update incident with an appealtype id useEffect(() => { - // updateCase( - // props.appealType.caseReference.incidentid, - // props.appealType.appealTypeID, - // props.form.appealform.values, - // pinswg_appealcasetype@OData.Community.Display.V1.Formatted - // ); - console.log(props.appealType.caseReference.incidentid); - console.log(props.appealType.appealTypeID); - console.log(props.form); - console.log( - props.appealType.caseReference[ - "pinswg_appealcasetype@OData.Community.Display.V1.Formatted" - ] + // console.log(props.appealType.caseReference.incidentid); + // console.log(props.appealType.appealTypeID); + // console.log(props.props.form.appealForm.values); + + // console.log(updateBody); + // console.log( + // getFormCollection( + // "pinswg_" + + // props.appealType.caseReference[ + // "pinswg_appealcasetype@OData.Community.Display.V1.FormattedValue" + // ] + // .replace(/[\s\-\+\(\)\,\&\.]/g, "") + // .toLowerCase() + // ) + // ); + + let incidentId = props.appealType.caseReference.incidentid; + let updateBody = props.props.form.appealForm.values; + updateBody = JSON.stringify(updateBody); + + updateBody = updateBody.replace(/:"Yes"/gm, `:true`); + updateBody = updateBody.replace(/:"No"/gm, `:false`); + updateBody = JSON.parse(updateBody); + let updateFormCollection = getFormCollection( + "pinswg_" + + props.appealType.caseReference[ + "pinswg_appealcasetype@OData.Community.Display.V1.FormattedValue" + ] + .replace(/[\s\-\+\(\)\,\&\.]/g, "") + .toLowerCase() ); + + updateCase(incidentId, updateBody, updateFormCollection); }, []); + const getFormCollection = (formtype) => { + const collectionName = jsonpath.query( + data, + "$..[?(@.LogicalName=='" + formtype + "')].LogicalCollectionName" + ); + //console.log(collectionName[0]); + return collectionName[0]; + }; + let { t } = useTranslation(); const router = useRouter(); @@ -67,8 +98,8 @@ let CompleteAppeal = (props) => { setCurrentSection(currentSection + 1); }; - console.log(JSON.stringify(props.props.form.appealForm.values, null, 2)); - alert(JSON.stringify(props.props.form.appealForm.values, null, 2)); + // console.log(JSON.stringify(props.props.form.appealForm.values, null, 2)); + //alert(JSON.stringify(props.props.form.appealForm.values, null, 2)); return (
From 855b8b1bd93874b47acdede274537b3e9931ad7b Mon Sep 17 00:00:00 2001 From: "robert.bond@test.gov.wales" Date: Tue, 14 Sep 2021 12:32:21 +0100 Subject: [PATCH 09/10] updated header on create case --- actions/index.js | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/actions/index.js b/actions/index.js index c43c6f44..627215b9 100644 --- a/actions/index.js +++ b/actions/index.js @@ -335,7 +335,14 @@ export const createCase = (appealTypeId) => { var config = { method: "post", url: WEBAPI_URL + "incidents", - 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, + }, data: data, }; console.log(config); @@ -355,7 +362,14 @@ export const updateCase = (incidentId, updateBody, updateFormCollection) => { var config = { method: "patch", url: WEBAPI_URL + updateFormCollection + "(" + incidentId + ")", - 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, + }, data: data, }; // console.log(config); From 1a1ad02fcf85f65d929d397a3439271438b1edfc Mon Sep 17 00:00:00 2001 From: "robert.bond@test.gov.wales" Date: Tue, 14 Sep 2021 14:36:26 +0100 Subject: [PATCH 10/10] added proxy updting case to avoid cors and be SSR --- actions/index.js | 5 +- api/proxy/[...route].js | 26 -------- components/newappeal/complete.js | 4 ++ pages/api/proxy/[...route].js | 110 +++++++++++++++++++++++++++++++ 4 files changed, 117 insertions(+), 28 deletions(-) delete mode 100644 api/proxy/[...route].js create mode 100644 pages/api/proxy/[...route].js 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(); + }); + }); +}