import axios from "axios"; import https from "https"; //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 agent = new https.Agent({ rejectUnauthorized: false, }); const accessToken = "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6IlVXelVaa0U3OEx0NVBGRkJ0LV9seVdoMjdjTSIsImtpZCI6IlVXelVaa0U3OEx0NVBGRkJ0LV9seVdoMjdjTSJ9.eyJhdWQiOiJodHRwczovL2RldmNybTIwMTYubGxjZHQuZ292LndhbGVzLyIsImlzcyI6Imh0dHA6Ly9mcy50ZXN0Lmdvdi53YWxlcy9hZGZzL3NlcnZpY2VzL3RydXN0IiwiaWF0IjoxNjMxMTcwMjI1LCJuYmYiOjE2MzExNzAyMjUsImV4cCI6MTYzMTE5OTAyNSwidXBuIjpbIlJvYmVydC5Cb25kQHRlc3QuZ292LndhbGVzIiwicm9iZXJ0LmJvbmRAdGVzdC5nb3Yud2FsZXMiXSwicHJpbWFyeXNpZCI6IlMtMS01LTIxLTE4MDA4NDIwNzYtMjQ1NTYwNjU5LTQyMTU3NzU0NjktMTY2MjgiLCJ1bmlxdWVfbmFtZSI6IlhNXFxCb25kUiIsImFwcHR5cGUiOiJQdWJsaWMiLCJhcHBpZCI6IjQyN2ZhNzljLWI1ZmMtNDJhZC04M2Q0LTQxMGIwMzk0OGFiNiIsImF1dGhtZXRob2QiOiJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvYXV0aGVudGljYXRpb25tZXRob2Qvd2luZG93cyIsImF1dGhfdGltZSI6IjIwMjEtMDktMDlUMDY6NTA6MjIuMTgzWiIsInZlciI6IjEuMCJ9.jds1ILVmVonh1g_KDDzFHZV7_iFpR4GZ6m_uw7YDQpQLXQlFXkPLf-Fan1h61Jl78ZmsBnhPKREvqALQ13tcMPPDg4SK5i6BYI7Jw8eKnqCjNb_mFAnnSCL0NeqZY4JV-HYzgY5tpvuCWgpjL2v_5BKOtU9mRL_700D7Fjt_R6nlI89dOKy3iT88lUxPostTVEC1nBxvh1AjjACG6103ymtRBtRy2ONowk4PClhCO3ZWwnVg1kb4GmXqhKNsXBvEscuZQuAzvawwTIXMvjwUBFSu4fsGdxxjfdsXVGACPdcUn7FekJIzaDtS2I_DvzrWLn9B1BDyBEctUqoVWOWqBw"; 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="*"', "Authorization": "Bearer " + accessToken, }, }; 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", "Access-Control-Allow-Origin": "*", "Access-Control-Allow-Credentials": "true", "Content-Type": "application/json", "Authorization": "Bearer " + accessToken, }, }; export const getBasicSearch = (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 &$count=true", crmHeaders ) .then((res) => res.data) .catch((error) => { console.log("thiserror", error); }); }; export const getBasicSearchDetails = (appealType, caseReference) => { return axios .get( WEBAPI_URL + 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); return axios .get( WEBAPI_URL + "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); }); }; export const getMandatoryFields = (whichForm) => { return axios .get( WEBAPI_URL + "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 + "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 = () => { return axios .get( WEBAPI_URL + "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( "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", true ); req.setRequestHeader("OData-MaxVersion", "4.0"); req.setRequestHeader("OData-Version", "4.0"); req.setRequestHeader("Accept", "application/json"); req.setRequestHeader("Content-Type", "application/json; charset=utf-8"); req.setRequestHeader("Prefer", 'odata.include-annotations="*"'); req.onreadystatechange = function () { if (this.readyState === 4) { req.onreadystatechange = null; if (this.status === 200) { var results = JSON.parse(this.response); var recordCount = results["@odata.count"]; for (var i = 0; i < results.value.length; i++) { var _accountid_value = results.value[i]["_accountid_value"]; var _accountid_value_formatted = results.value[i][ "_accountid_value@OData.Community.Display.V1.FormattedValue" ]; var _accountid_value_lookuplogicalname = results.value[i][ "_accountid_value@Microsoft.Dynamics.CRM.lookuplogicalname" ]; var _customerid_value = results.value[i]["_customerid_value"]; var _customerid_value_formatted = results.value[i][ "_customerid_value@OData.Community.Display.V1.FormattedValue" ]; var _customerid_value_lookuplogicalname = results.value[i][ "_customerid_value@Microsoft.Dynamics.CRM.lookuplogicalname" ]; var pinswg_appealcasetype = results.value[i]["pinswg_appealcasetype"]; var pinswg_appealcasetype_formatted = results.value[i][ "pinswg_appealcasetype@OData.Community.Display.V1.FormattedValue" ]; var statuscode = results.value[i]["statuscode"]; var ticketnumber = results.value[i]["ticketnumber"]; var title = results.value[i]["title"]; } } else { console.log(this.statusText); } } }; return req.send(); }; //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 = ''; // // 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); }); }; //https://devcrm2016.llcdt.gov.wales/DVPINS/api/data/v8.2/pinswg_planningappeals78s?$filter=pinswg_name eq 'CAS-00015-L7J9H1' export const getLPA = () => { console.log("api_root: ", BASE_URL); return axios .get(WEBAPI_URL + "accounts?$count=true&$select=name", crmHeaders) .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 createCase = (lpaType, appealType) => { return axios .post( WEBAPI_URL + "incidents", { "title": "insertion test case", "customerid_contact@odata.bind": "/contacts(f8b454ed-eded-eb11-aac7-00224800be9c)", }, crmHeadersReturn ) .then((res) => res.data) .catch((error) => { console.log("thi serror", error); }); }; export const updateCase = (incidentID, appealTypeId) => { return axios .patch( WEBAPI_URL + "incidents(" + incidentID + ")", { "pinswg_appealtype": appealTypeId, }, crmHeadersReturn ) .then((res) => { console.log("patched ", res.data); return res.data; }) .catch((error) => { console.log("thi serror", error); }); }; // var req = new XMLHttpRequest(); // req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/incidents?$select=_accountid_value,_customerid_value,pinswg_appealcasetype,statuscode,ticketnumber,title&$filter=contains(title, '000')&$count=true", true); // req.setRequestHeader("OData-MaxVersion", "4.0"); // req.setRequestHeader("OData-Version", "4.0"); // req.setRequestHeader("Accept", "application/json"); // req.setRequestHeader("Content-Type", "application/json; charset=utf-8"); // req.setRequestHeader("Prefer", "odata.include-annotations=\"*\""); // req.onreadystatechange = function() { // if (this.readyState === 4) { // req.onreadystatechange = null; // if (this.status === 200) { // var results = JSON.parse(this.response); // var recordCount = results["@odata.count"]; // for (var i = 0; i < results.value.length; i++) { // var _accountid_value = results.value[i]["_accountid_value"]; // var _accountid_value_formatted = results.value[i]["_accountid_value@OData.Community.Display.V1.FormattedValue"]; // var _accountid_value_lookuplogicalname = results.value[i]["_accountid_value@Microsoft.Dynamics.CRM.lookuplogicalname"]; // var _customerid_value = results.value[i]["_customerid_value"]; // var _customerid_value_formatted = results.value[i]["_customerid_value@OData.Community.Display.V1.FormattedValue"]; // var _customerid_value_lookuplogicalname = results.value[i]["_customerid_value@Microsoft.Dynamics.CRM.lookuplogicalname"]; // var pinswg_appealcasetype = results.value[i]["pinswg_appealcasetype"]; // var pinswg_appealcasetype_formatted = results.value[i]["pinswg_appealcasetype@OData.Community.Display.V1.FormattedValue"]; // var statuscode = results.value[i]["statuscode"]; // var ticketnumber = results.value[i]["ticketnumber"]; // var title = results.value[i]["title"]; // } // } else { // Xrm.Utility.alertDialog(this.statusText); // } // } // }; // req.send(); // //jquery version to get case number from an lpa and an appeal type // var entity = {}; // entity.caseorigincode = 3; // entity["customerid_account@odata.bind"] = "/accounts(c75f6f84-49da-eb11-aabd-00224800d1bd)"; // entity.pinswg_appealcasetype = 846040004; // $.ajax({ // type: "POST", // contentType: "application/json; charset=utf-8", // datatype: "json", // url: Xrm.Page.context.getClientUrl() + "/api/data/v8.2/incidents", // data: JSON.stringify(entity), // beforeSend: function(XMLHttpRequest) { // XMLHttpRequest.setRequestHeader("OData-MaxVersion", "4.0"); // XMLHttpRequest.setRequestHeader("OData-Version", "4.0"); // XMLHttpRequest.setRequestHeader("Accept", "application/json"); // XMLHttpRequest.setRequestHeader("Prefer", "odata.include-annotations=\"*\",return=representation"); // }, // async: true, // success: function(data, textStatus, xhr) { // var uri = xhr.getResponseHeader("OData-EntityId"); // var regExp = /\(([^)]+)\)/; // var matches = regExp.exec(uri); // var newEntityId = matches[1]; // //Handle returned attributes // }, // error: function(xhr, textStatus, errorThrown) { // Xrm.Utility.alertDialog(textStatus + " " + errorThrown); // } // }); export const getMyCases = () => { return axios .get(BASE_URL + "/api/lookups/myCases", { httpsAgent: new https.Agent({ rejectUnauthorized: false }), }) .then((res) => res.data) .catch((error) => { console.log("thi serror", error); }); }; export const getMyRepresentations = () => { return axios .get(BASE_URL + "/api/lookups/myRepresentations", { httpsAgent: new https.Agent({ rejectUnauthorized: false }), }) .then((res) => res.data) .catch((error) => { console.log("thi serror", error); }); }; export const getWatchedCases = () => { return axios .get(BASE_URL + "/api/lookups/watchedCases", { httpsAgent: new https.Agent({ rejectUnauthorized: false }), }) .then((res) => res.data) .catch((error) => { w; console.log("thi serror", error); }); }; export const getAwaitingSubmission = () => { 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); }); };