Files
pedwfrontend/actions/index.js
T

596 lines
20 KiB
JavaScript

import axios from "axios";
import https from "https";
import CryptoJS from "crypto-js";
//import {XMLHttpRequest} from 'xmlhttprequest';
//process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = 0;
let BASE_URL;
const port = parseInt(process.env.PORT, 10) || 3000;
if (process.browser) {
BASE_URL = window.apiRoot;
} else {
BASE_URL = process.env.API_ROOT || `http://localhost:${port}`;
}
const API_PATH = "/api/data/v8.2/";
//const WEBAPI_URL = "https://devcrm2016.llcdt.gov.wales/DVPINS" + API_PATH;
const WEBAPI_URL =
process.env.RELAY_ROOT ||
"https://dev-pedw-ns.servicebus.windows.net/dev-pedw-hc/";
const agent = new https.Agent({
rejectUnauthorized: false,
});
export const getSASToken = () => {
var m_ResourceURI =
process.env.RELAYURI || "dev-pedw-ns.servicebus.windows.net";
var m_Path = process.env.RELAYPATH || "dev-pedw-hc";
var m_SasKey =
process.env.SASKEY || "Ml0Y/S/nfRcmIrHFRkO4jNm2J3iH52TSxPiak7+E1+Y=";
var m_SasKeyName = process.env.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;
// console.log("///////////////////////");
// console.log(
// "encodedURI:" + "https://" + m_ResourceURI + "/" + m_Path + "/"
// );
// console.log("WEBAPI_URL: " + WEBAPI_URL);
// console.log("m_ResourceURI: " + m_ResourceURI);
// console.log("m_Path: " + m_Path);
// console.log("m_SasKey: " + m_SasKey);
// console.log("m_SasKeyName: " + m_SasKeyName);
// console.log("token: " + token);
// console.log("///////////////////////");
return token;
};
const accessToken =
"eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6IlVXelVaa0U3OEx0NVBGRkJ0LV9seVdoMjdjTSIsImtpZCI6IlVXelVaa0U3OEx0NVBGRkJ0LV9seVdoMjdjTSJ9.eyJhdWQiOiJodHRwczovL2RldmNybTIwMTYubGxjZHQuZ292LndhbGVzLyIsImlzcyI6Imh0dHA6Ly9mcy50ZXN0Lmdvdi53YWxlcy9hZGZzL3NlcnZpY2VzL3RydXN0IiwiaWF0IjoxNjMyMTIxMjkxLCJuYmYiOjE2MzIxMjEyOTEsImV4cCI6MTYzMjE1MDA5MSwidXBuIjpbIlJvYmVydC5Cb25kQHRlc3QuZ292LndhbGVzIiwicm9iZXJ0LmJvbmRAdGVzdC5nb3Yud2FsZXMiXSwicHJpbWFyeXNpZCI6IlMtMS01LTIxLTE4MDA4NDIwNzYtMjQ1NTYwNjU5LTQyMTU3NzU0NjktMTY2MjgiLCJ1bmlxdWVfbmFtZSI6IlhNXFxCb25kUiIsImFwcHR5cGUiOiJQdWJsaWMiLCJhcHBpZCI6IjQyN2ZhNzljLWI1ZmMtNDJhZC04M2Q0LTQxMGIwMzk0OGFiNiIsImF1dGhtZXRob2QiOiJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvYXV0aGVudGljYXRpb25tZXRob2Qvd2luZG93cyIsImF1dGhfdGltZSI6IjIwMjEtMDktMjBUMDc6MDE6MzEuNDYzWiIsInZlciI6IjEuMCJ9.W0ID2nCBYVxh8T1eQqWKk2Wh45kRbRzCICfzrZNAs-4u54yl0UJUcVihyI8EBmQeALCEc5eY99DjK0gaqzCdJqxbxol8yk5LrFvhV5UkCiZ7SO2Wq1cLReWHVsgz39qBtrZ8vlqqZsv7DsEaRJQbtj8Djnx26Wb_kcNu-tuXwUBF0uR5GLFFvJfM3PzkRCY-ZPOTbvPTb73oN_NKe6BrsVjyaR9xSXsRJjgHPmJgWgnvq-2X0Dj7LCwXSEygDDOGlF1yqqN0Lv6qplhQoH-heZlC1K6qe_92nHPswXag6uN5nxTmk1Qw6zJt_aHMnXdwa18ghh4hGx5R0Jz53X113Q";
//const SASToken = getSASToken();
const crmHeaders = {
withCredentials: true,
headers: {
"Content-Type": "application/json; charset=utf-8",
"OData-MaxVersion": "4.0",
"OData-Version": "4.0",
"Accept": "application/json",
"Prefer": 'odata.include-annotations="*",return=representation',
"Authorization": "Bearer " + accessToken,
},
};
const crmHeadersReturn = {
headers: {
"OData-MaxVersion": "4.0",
"OData-Version": "4.0",
"Accept": "application/json",
"Prefer": "return=representation",
"Content-Type": "application/json",
"Authorization": "Bearer " + accessToken,
},
};
const azureHeaders = (SASToken) => {
// console.log({
// headers: {
// "OData-MaxVersion": "4.0",
// "OData-Version": "4.0",
// "Accept": "application/json",
// "Prefer": 'odata.include-annotations="*",return=representation',
// "Content-Type": "application/json",
// "ServiceBusAuthorization": SASToken,
// },
// });
return {
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 azureHeaders = {
// headers: {
// "OData-MaxVersion": "4.0",
// "OData-Version": "4.0",
// "Accept": "application/json",
// "Prefer": 'odata.include-annotations="*",return=representation',
// "Content-Type": "application/json",
// "Authorization": "Bearer " + accessToken,
// },
// };
export const getBasicSearch = (token, searchString) => {
//console.log("\n\n", token, searchString);
return axios
.get(
WEBAPI_URL +
"incidents?$select=_accountid_value,_customerid_value,pinswg_appealcasetype,statuscode,ticketnumber,title, _primarycontactid_value&$expand=primarycontactid($select=fullname)&$filter=contains(title, '" +
searchString +
"') and pinswg_appealcasetype ne null and pinswg_publishtoweb eq true&$orderby=createdon desc&$count=true&$top=201",
azureHeaders(token)
)
.then((res) => {
// console.log(" ");
// console.log("/////////////////////");
// console.log("data:" + res.data.value);
// console.log("/////////////////////");
// console.log(" ");
return res.data;
})
.catch((error) => {
console.log("thiserror", error);
});
};
export const getBasicDNSSearch = (token, searchString) => {
return axios
.get(
WEBAPI_URL +
"incidents?$select=_accountid_value,_customerid_value,pinswg_appealcasetype,statuscode,ticketnumber,title, _primarycontactid_value&$expand=primarycontactid($select=fullname)&$filter=pinswg_appealcasetype eq 846040011 and pinswg_publishtoweb eq true&$orderby=createdon desc&$count=true",
azureHeaders(token)
)
.then((res) => res.data)
.catch((error) => {
console.log("thiserror", error);
});
};
export const getAdvancedSearch = (token, searchString) => {
//console.log(searchString.caseReference);
var queryString = "";
queryString +=
searchString.q != null
? "contains(title, '" + searchString.q + "') "
: "";
queryString +=
searchString.lpa != null
? (typeof searchString.q == "undefined" ? "" : " and ") +
"_customerid_value eq " +
searchString.lpa
: "";
queryString +=
searchString.apt != null
? (typeof searchString.q == "undefined" &&
typeof searchString.lpa == "undefined"
? ""
: " and ") +
"pinswg_appealcasetype eq " +
searchString.apt
: "";
// console.log(
// "incidents?$select=_accountid_value,_customerid_value,pinswg_appealcasetype,statuscode,ticketnumber,title&$filter=" +
// queryString +
// " &$count=true"
// );
return axios
.get(
WEBAPI_URL +
"incidents?$select=_accountid_value,_customerid_value,pinswg_appealcasetype,statuscode,ticketnumber,title&$filter=" +
queryString +
" and pinswg_appealcasetype ne null and pinswg_publishtoweb eq true&$orderby=createdon desc&$count=true&$top=201",
azureHeaders(token)
)
.then((res) => res.data)
.catch((error) => {
console.log("thiserror", error);
});
};
export const getBasicSearchDetails = (token, appealType, caseReference) => {
return axios
.get(
WEBAPI_URL +
appealType +
"?$filter=pinswg_name eq '" +
caseReference +
"'&$count=true",
azureHeaders(token)
)
.then((res) => res.data)
.catch((error) => {
console.log("thiserror", error);
});
};
export const getSearchDocumentDetails = (token, incidentID) => {
return axios
.get(
WEBAPI_URL +
"pinswg_documents?$count=true&$filter=_pinswg_documentids_value eq " +
incidentID +
" and pinswg_publishtoweb eq true&$select=pinswg_isharedocumentlocations,_pinswg_documentids_value,pinswg_isharelabelcasetype,pinswg_isharelabellpaname,pinswg_publishtoweb,pinswg_uploadstatus,pinswg_isharedocumentclassification,pinswg_isharedocumentreference",
azureHeaders(token)
)
.then((res) => res.data)
.catch((error) => {
console.log("thiserror", error);
});
};
export const getSearchDocumentHistory = (token, documentID) => {
return axios
.get(
WEBAPI_URL +
"pinswg_documenthistories?$count=true&$select=_pinswg_documentid_value,pinswg_createddate,pinswg_state,pinswg_fileexists,statecode,pinswg_publisheddate&$filter=_pinswg_documentid_value eq " +
documentID,
azureHeaders(token)
)
.then((res) => res.data)
.catch((error) => {
console.log("thiserror", error);
});
};
export const getBasicDNSSearchDetails = (token, appealType, caseReference) => {
return axios
.get(
WEBAPI_URL +
"pinswg_dnses?$filter=pinswg_name eq '" +
caseReference +
"'&$count=true",
azureHeaders(token)
)
.then((res) => res.data)
.catch((error) => {
console.log("thiserror", error);
});
};
export const getFormData = (token, whichForm) => {
//console.log("got to axios", whichForm);
//console.log("api_root: ", BASE_URL);
return axios
.get(
WEBAPI_URL +
"systemforms?$select=formid,name,formxml,type,objecttypecode&$filter=(objecttypecode%20eq%20%27pinswg_" +
whichForm +
"%27and%20type%20eq%202)&$count=true&$top=201",
azureHeaders(token)
)
.then((res) => res.data)
.catch((error) => {
console.log("thiserror", error);
});
};
export const getMandatoryFields = (token, whichForm) => {
return axios
.get(
WEBAPI_URL +
"EntityDefinitions(LogicalName='pinswg_" +
whichForm +
"')/Attributes?$count=true&$select=LogicalName,RequiredLevel",
azureHeaders(token)
)
.then((res) => res.data)
.catch((error) => {
console.log("thiserror", error);
});
};
export const getPickLists = (token, whichForm) => {
return axios
.get(
WEBAPI_URL +
"EntityDefinitions(LogicalName='pinswg_" +
whichForm +
"')/Attributes/Microsoft.Dynamics.CRM.PicklistAttributeMetadata?$select=LogicalName&$expand=OptionSet,GlobalOptionSet&$count=true",
azureHeaders(token)
)
.then((res) => res.data)
.catch((error) => {
console.log("thiserror", error);
});
};
export const getAppealsTypes = (token) => {
return axios
.get(
WEBAPI_URL +
"stringmaps?$filter=attributename%20eq%20%27pinswg_appealcasetype%27&$count=true&$select=value,stringmapid,organizationid,attributevalue",
azureHeaders(token)
)
.then((res) => res.data)
.catch((error) => {
console.log("thiserror", error);
});
};
//get list of entitydefinitions
//https://devcrm2016.llcdt.gov.wales/DVPINS/api/data/v8.2/EntityDefinitions?$select=LogicalName
// get list of appealtypes
//https://devcrm2016.llcdt.gov.wales/DVPINS/api/data/v8.2/stringmaps?$filter=attributename%20eq%20%27pinswg_appealcasetype%27&$count=true
//get crm xml form for appealtype
//https://devcrm2016.llcdt.gov.wales/DVPINS/api/data/v8.2/systemforms?$select=formid,name,formxml&$filter=(objecttypecode%20eq%20%27pinswg_dns%27and%20type%20eq%202)&$count=true
// then use regex to remove the escape
// convert xml to json and return
// promise example
// var xml2js = require('xml2js');
// var xml = '<foo></foo>';
// // With parser
// var parser = new xml2js.Parser(/* options */);
// parser.parseStringPromise(data).then(function (result) {
// console.dir(result);
// console.log('Done');
// })
// .catch(function (err) {
// // Failed
// });
// export const getAppealTypeDetail = (caseReference, appealcasetype) => {
// console.log("got to axios", searchString);
// console.log("api_root: ", BASE_URL);
// // example appealcasetype type pinswg_householderappealhases
// return axios
// .get(
// "https://devcrm2016.llcdt.gov.wales/DVPINS/api/data/v8.2/" +
// appealcasetype +
// "?$filter=pinswg_name eq '" +
// caseReference +
// "'",
// { httpsAgent: new https.Agent({ rejectUnauthorized: false }) }
// )
// .then((res) => res.data)
// .catch((error) => {
// console.log("thi serror", error);
// });
// };
export const getLPA = (token) => {
// console.log("api_root: ", BASE_URL);
return axios
.get(
WEBAPI_URL + "accounts?$count=true&$select=name",
azureHeaders(token)
)
.then((res) => res.data)
.catch((error) => {
console.log("thi serror", error);
});
};
export const getPersonalAccount = () => {
console.log("api_root: ", BASE_URL);
return axios
.get(
BASE_URL + "/api/lookups/personaldetails",
//.get("https://devcrm2016.llcdt.gov.wales/DVPINS/api/data/v8.2/contacts?$select=name",
{ httpsAgent: new https.Agent({ rejectUnauthorized: false }) }
)
.then((res) => res.data)
.catch((error) => {
console.log("thi serror", error);
});
};
export const getAppealID = (
token,
caseReference,
updateFormCollection,
primaryAttribute
) => {
const weburl =
"/api/proxy/" +
updateFormCollection +
"?$count=true&$select=_" +
primaryAttribute +
"s_value&$filter=pinswg_name eq '" +
caseReference +
"'";
console.log("proxy url", weburl);
return axios
.get(weburl, azureHeaders(token))
.then((res) => {
var appealID = res.data.value[0][primaryAttribute];
return appealID;
})
.catch((error) => {
console.log("thi serror", error);
});
};
export const createCase = (appealTypeId) => {
appealTypeId = parseInt(appealTypeId);
var data = JSON.stringify({
"title": "insertion test case",
"caseorigincode": 3,
"customerid_contact@odata.bind":
"/contacts(f8b454ed-eded-eb11-aac7-00224800be9c)",
"pinswg_appealcasetype": 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,
},
data: data,
};
// console.log(config);
return axios(config)
.then((res) => {
// console.log("posted ", res.data);
return res.data;
})
.catch((error) => {
console.log("this serror", error);
});
};
export const updateCase = async (
incidentId,
updateBody,
updateFormCollection,
primaryAttribute,
caseReference
) => {
var data = JSON.stringify(updateBody);
var appealObj = await getAppealID(
caseReference,
updateFormCollection,
primaryAttribute
);
console.log("appeal obj ", appealObj);
var config = {
method: "patch",
//url: WEBAPI_URL + updateFormCollection + "(" + incidentId + ")",
url: "/api/proxy/" + updateFormCollection + "(" + appealObj + ")",
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);
return axios(config)
.then((res) => {
console.log("patched ", res.data);
return res.data;
})
.catch((error) => {
console.log("thi serror", error);
});
};
export const getMyCases = (token, loggedInUserId) => {
return axios
.get(
WEBAPI_URL +
"incidents?$select=ticketnumber,casetypecode,createdon,_customerid_value,description,incidentid,pinswg_appealcasetype,_pinswg_assignedinspectrids_value,statecode,statuscode,title&$filter=_customerid_value eq " +
loggedInUserId +
" and pinswg_appealcasetype ne null&$orderby=createdon desc&$count=true&$top=3",
azureHeaders(token)
)
.then((res) => res.data)
.catch((error) => {
console.log("thi serror", error);
});
};
export const getMyRepresentations = (token) => {
return (
axios
// .get(BASE_URL + "/api/lookups/myRepresentations", {
// httpsAgent: new https.Agent({ rejectUnauthorized: false }),
// })
.get(
WEBAPI_URL +
"incidents?$select=_accountid_value,_customerid_value,pinswg_appealcasetype,statuscode,ticketnumber,title, _primarycontactid_value&$expand=primarycontactid($select=fullname)&$filter=pinswg_appealcasetype ne null &$count=true&$top=3",
azureHeaders(token)
)
.then((res) => res.data)
.catch((error) => {
console.log("thi serror", error);
})
);
};
export const getWatchedCases = (token) => {
return (
axios
// .get(BASE_URL + "/api/lookups/watchedCases", {
// httpsAgent: new https.Agent({ rejectUnauthorized: false }),
// })
.get(
WEBAPI_URL +
"incidents?$select=_accountid_value,_customerid_value,pinswg_appealcasetype,statuscode,ticketnumber,title, _primarycontactid_value&$expand=primarycontactid($select=fullname)&$filter=pinswg_appealcasetype ne null &$count=true&$top=3",
azureHeaders(token)
)
.then((res) => res.data)
.catch((error) => {
console.log("this serror", error);
})
);
};
export const getAwaitingSubmission = (token) => {
return axios
.get(BASE_URL + "/api/lookups/awaitingSubmission", {
httpsAgent: new https.Agent({ rejectUnauthorized: false }),
})
.then((res) => res.data)
.catch((error) => {
console.log("thi serror", error);
});
};
export const getPortalModuelDetails = (token, appealType, caseReference) => {
return axios
.get(
WEBAPI_URL +
appealType +
"?$filter=pinswg_name eq '" +
caseReference +
"'&$count=true",
azureHeaders(token)
)
.then((res) => res.data)
.catch((error) => {
console.log("thiserror", error);
});
};