Files
pedwfrontend/actions/index.js
T

794 lines
21 KiB
JavaScript

import axios from "axios";
import https from "https";
import CryptoJS from "crypto-js";
import HmacSHA256 from "crypto-js/hmac-sha256";
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 WORDKEY = process.env.HASHKEY;
const cache = {};
const API_PATH = "/api/data/v8.2/";
const accessTokenEndpoint = process.env.ACCESS_TOKEN_ENDPOINT;
const tenantId = process.env.TENANT;
//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 getToken = () => {
return axios
.post(
`${accessTokenEndpoint}${tenantId}/oauth2/v2.0/token`,
tokenBody,
tokenConfig
)
.then((res) => res.data)
.then((data) => {
cache.tokenResponse = data;
return data;
})
.catch((error) => {
console.log("error :", error.response);
// return error;
});
};
const tokenBody =
"grant_type=" +
process.env.GRANT_TYPE +
"&client_id=" +
process.env.CLIENT_ID +
"&client_secret=" +
process.env.CLIENT_SECRET +
"&scope=" +
"https://" +
process.env.RELAYURI +
"/.default";
const tokenConfig = {
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"Cache-Control": "no-cache",
},
};
export const azureHeaders = (access_token) => {
return {
headers: {
"OData-MaxVersion": "4.0",
"OData-Version": "4.0",
"Accept": "application/json;odata.metadata=none",
"Prefer": 'odata.include-annotations="*",return=representation',
"Content-Type": "application/json",
"Authorization": "Bearer " + access_token,
},
};
};
export const azureHeadersPaged = (access_token) => {
return {
headers: {
"OData-MaxVersion": "4.0",
"OData-Version": "4.0",
"Accept": "application/json;odata.metadata=none",
"Prefer":
'odata.include-annotations="*",return=representation, odata.maxpagesize=10',
"Content-Type": "application/json",
"Authorization": "Bearer " + access_token,
},
};
};
export const hashAPIPath = (queryPath) => {
var hashlink = CryptoJS.HmacSHA256(
queryPath,
CryptoJS.enc.Hex.parse(WORDKEY)
);
hashlink = hashlink.toString(CryptoJS.enc.Hex);
return (queryPath.indexOf("?") > -1 ? "&hash=" : "?hash=") + hashlink;
};
export const hashString = (stringToHash) => {
console.log("string to hash", stringToHash);
let hashedStr = CryptoJS.AES.encrypt(stringToHash, "pedw");
console.log("hashed String", hashedStr.toString());
return hashedStr.toString();
};
export const dehashString = (stringToDeHash) => {
console.log("string to dehash", stringToDeHash);
let dehashedStr = CryptoJS.AES.decrypt(decodeURI(stringToDeHash), "pedw");
console.log("dehshed string:", dehashedStr.toString(CryptoJS.enc.Utf8));
return dehashedStr.toString(CryptoJS.enc.Utf8);
};
export const getBasicSearch = (token, searchString) => {
return axios
.get(
BASE_URL +
"api/endpoint/getbasicsearch_api?searchString=" +
searchString
)
.then((res) => {
return res.data;
})
.catch((error) => {
console.log(error);
return error.response;
});
};
export const getBasicSearchPaged = (searchString, pageNumber) => {
//console.log(queryUrl);
return axios
.get(
"/api/endpoint/getbasicsearchpaged_api?searchString=" +
searchString +
"&pageNumber=" +
pageNumber
)
.then((res) => {
return res.data;
})
.catch((error) => {
return error.response;
});
};
export const getBasicDNSSearch = (token, searchString) => {
var queryUrl =
"incidents?$select=numberofchildincidents,_accountid_value,_customerid_value,_pinswg_associatedlpa_value,_ownerid_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";
return axios
.get(BASE_URL + "/api/endpoint/getbasicdnssearch_api")
.then((res) => res.data)
.catch((error) => {
return error.response;
});
};
export const getBasicDNSSearchPaged = (pageNumber) => {
return axios
.get(
"/api/endpoint/getbasicdnssearchpaged_api?pageNumber=" + pageNumber
)
.then((res) => res.data)
.catch((error) => {
return error.response;
});
};
export const getAdvancedSearch = (token, searchString) => {
return axios
.get(
BASE_URL +
"/api/endpoint/getadvancedsearch_api?searchstring=" +
JSON.stringify(searchString)
)
.then((res) => res.data)
.catch((error) => {
let ErrResponse = {
value: [],
errorCode: error.response.status,
errorMsg: error.response.statusText,
};
return ErrResponse;
});
};
export const getAdvancedSearchPaged = (searchString, pageNumber) => {
return axios
.get(
"/api/endpoint/getadvancedsearchpaged_api?searchstring=" +
JSON.stringify(searchString) +
"&pageNumber=" +
pageNumber
)
.then((res) => res.data)
.catch((error) => {
let ErrResponse = {
value: [],
errorCode: error.response.status,
errorMsg: error.response.statusText,
};
return ErrResponse;
});
};
export const getBasicSearchDetails = (
token,
appealTypeName,
caseReference,
primaryIdAttribute,
incidentID
) => {
return axios
.get(
BASE_URL +
"/api/endpoint/getbasicsearchdetails_api?appealTypeName=" +
appealTypeName +
"&primaryIdAttribute=" +
primaryIdAttribute +
"&incidentID=" +
incidentID
)
.then((res) => res.data)
.catch((error) => {
console.log("this error", error);
});
};
export const getBasicSearchDetailsPaged = (
appealTypeName,
caseReference,
primaryIdAttribute,
incidentID
) => {
//console.log("check appealtype:", appealType, caseReference);
return axios
.get(
"/api/endpoint/getbasicsearchdetailspaged_api?appealTypeName=" +
appealTypeName +
"&primaryIdAttribute=" +
primaryIdAttribute +
"&incidentID=" +
incidentID
)
.then((res) => res.data)
.catch((error) => {
console.log("this error", error);
});
};
export const getSearchDocumentDetails = (incidentID) => {
return axios
.get(
"/api/endpoint/getsearchdocumentdetails_api?incidentid=" +
incidentID
)
.then((res) => res.data)
.catch((error) => {
console.log("thiserror", error);
});
};
export const getSearchDocumentDetailsPaged = (incidentID, pageNumber) => {
return axios
.get(
"/api/endpoint/getsearchdocumentdetailspaged_api?incidentid=" +
incidentID +
"&pageNumber=" +
pageNumber
)
.then((res) => res.data)
.catch((error) => {
console.log("thiserror", error);
});
};
export const getSearchDocumentHistory = (documentID) => {
return axios
.get(
"/api/endpoint/getsearchdocumenthistory_api?documentid=" +
documentID
)
.then((res) => {
return res.data;
})
.catch((error) => {
console.log("thiserror", error);
});
};
export const getSearchDocumentHistoryPaged = (documentID, pageNumber) => {
return (
axios
.get(
"/api/endpoint/getsearchdocumenthistorypaged_api?documentid=" +
documentID +
"&pageNumber=" +
pageNumber
)
// .get(queryUrl + hashAPIPath(queryUrl))
.then((res) => {
return res.data;
})
.catch((error) => {
console.log("thiserror", error);
})
);
};
export const getLinkedCases = (parentIncidentid) => {
return axios
.get(
"/api/endpoint/getlinkedcases_api?parentincidentid=" +
parentIncidentid
)
.then((res) => {
//console.log(" linked date", res.data);
return res.data;
})
.catch((error) => {
console.log("thiserror", error);
});
};
export const getAppealsTypes = (token) => {
return axios
.get(BASE_URL + "/api/endpoint/getappealtypes_api")
.then((res) => res.data)
.catch((error) => {
console.log(error);
let ErrResponse = {
value: [],
errorCode: error.response.status,
errorMsg: error.response.statusText,
};
return ErrResponse;
});
};
export const getLPA = (token) => {
return axios
.get(BASE_URL + "/api/endpoint/getlpa_api")
.then((res) => {
//console.log(res.data);
return res.data;
})
.catch((error) => {
let ErrResponse = {
value: [],
errorCode: error.response.status,
errorMsg: error.response.statusText,
};
return ErrResponse;
});
};
// iteration 2 methods
export const getFormData = (token, whichForm) => {
return axios
.get("/api/endpoint/getformdata_api?whichForm=" + whichForm)
.then((res) => res.data)
.catch((error) => {
console.log("thiserror", error);
});
};
export const getMandatoryFields = (token, whichForm) => {
return axios
.get("/api/endpoint/getmandatoryfields_api?whichForm=" + whichForm)
.then((res) => res.data)
.catch((error) => {
console.log("thiserror", error);
});
};
export const getPickLists = (token, whichForm) => {
return axios
.get("/api/endpoint/getpicklists_api?whichForm=" + whichForm)
.then((res) => res.data)
.catch((error) => {
console.log("thiserror", error);
});
};
export const getPersonalAccount = (token, contactid) => {
return axios
.get("/api/endpoint/getpersonalaccount_api?contactid=" + contactid)
.then((res) => {
console.log(res.data);
return res.data;
})
.catch((error) => {
let ErrResponse = {
value: [],
errorCode: error.response.status,
errorMsg: error.response.statusText,
};
return ErrResponse;
});
};
export const getAppealID = (
caseReference,
updateFormCollection,
primaryAttribute
) => {
return axios
.get(
"/api/endpoint/getappealid_api?updateFormCollection=" +
updateFormCollection +
"&primaryAttribute=" +
primaryAttribute +
"&caseReference=" +
caseReference
)
.then((res) => {
var appealID = res.data.value[0][primaryAttribute];
return appealID;
})
.catch((error) => {
console.log("thi serror", error);
});
};
export const getLogin = (emailAddress, pwd) => {
return axios
.get(
"/api/endpoint/getlogin_api?emailAddress=" +
emailAddress +
"&pwd=" +
pwd
)
.then((res) => res.data)
.catch((error) => {
console.log("thiserror", error);
});
};
export const createCase = (token, appealTypeId, lpaID, contactid) => {
appealTypeId = parseInt(appealTypeId);
var queryUrl = "incidents";
var data = JSON.stringify({
"title": "insertion test case",
"caseorigincode": 3,
"customerid_contact@odata.bind": "/contacts(" + contactid + ")",
"pinswg_appealcasetype": appealTypeId,
"pinswg_AssociatedLPA@odata.bind": "/accounts(" + lpaID + ")",
});
var config = {
method: "post",
url: WEBAPI_URL + queryUrl + hashAPIPath(queryUrl),
headers: {
"OData-MaxVersion": "4.0",
"OData-Version": "4.0",
"Accept": "application/json;odata.metadata=none",
"Prefer": 'odata.include-annotations="*",return=representation',
"Content-Type": "application/json",
"Authorization": "Bearer " + token,
},
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 getMyCases = (token, loggedInUserId) => {
return axios
.get(
BASE_URL +
"/api/endpoint/getmycases_api?loggedInUserId=" +
loggedInUserId
)
.then((res) => {
return res.data;
})
.catch((error) => {
console.log("thi serror", error);
});
};
export const getMyRepresentations = (token, loggedInUserId) => {
return axios
.get(
BASE_URL +
"/api/endpoint/getmyrepresentations_api?loggedInUserId=" +
loggedInUserId
)
.then((res) => res.data)
.catch((error) => {
console.log("this serror", error);
});
};
export const getMyRepresentationsProxy = (token, loggedInUserId) => {
return axios
.get(
BASE_URL +
"/api/endpoint/getmyrepresentationsproxy_api?loggedInUserId=" +
loggedInUserId
)
.then((res) => res.data)
.catch((error) => {
console.log("this serror", error);
});
};
export const getRepresentations = (incidentID) => {
return axios
.get("/api/endpoint/getrepresentations_api?incidentID=" + incidentID)
.then((res) => res.data)
.catch((error) => {
console.log("this serror", error);
});
};
export const getRepresentationsProxy = (token, incidentID) => {
return axios
.get(queryUrl + hashAPIPath(queryUrl))
.get(
BASE_URL +
"/api/endpoint/getrepresentationsproxy_api?incidentID=" +
incidentID
)
.then((res) => res.data)
.catch((error) => {
console.log("this serror", error);
});
};
export const getWatchedCases = (token, loggedInUserId) => {
return axios
.get(
BASE_URL +
"/api/endpoint/getwatchedcases_api?loggedInUserId=" +
loggedInUserId
)
.then((res) => res.data)
.catch((error) => {
console.log("this serror", error);
});
};
export const getWatchedCasesProxy = (token, loggedInUserId) => {
return axios
.get(
BASE_URL +
"/api/endpoint/getwatchedcasesproxy_api?loggedInUserId=" +
loggedInUserId
)
.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 getPortalModuleDetails = (token, appealType, caseReference) => {
return axios
.get(
BASE_URL +
"/api/endpoint/getportalmoduledetails_api?appealType=" +
appealType +
"&caseReference=" +
caseReference
)
.then((res) => res.data)
.catch((error) => {
console.log("thiserror", error);
});
};
export const getPortalModuleDetailsProxy = (
token,
appealType,
caseReference
) => {
return axios
.get(
BASE_URL +
"/api/endpoint/getportalmoduledetailsproxy_api?appealType=" +
appealType +
"&caseReference=" +
caseReference
)
.then((res) => res.data)
.catch((error) => {
console.log("thiserror", error);
});
};
export const getEmailAccountCheck = (emailAddress) => {
return axios
.get(
BASE_URL +
"/api/endpoint/getemailaccountcheck_api?emailAddress=" +
emailAddress
)
.then((res) => res.data)
.catch((error) => {
console.log("thiserror", error);
});
};
//uses more than get methods
export const updatePassword = (contactId, newpassword) => {
var queryUrl = "/api/endpoint/updateaccount_api?contactId=" + contactId;
var data = { "pinswg_custom_password": newpassword };
var config = {
method: "post",
url: queryUrl,
data: data,
};
return axios(config)
.then((res) => {
return res.data;
})
.catch((error) => {
console.log("this serror", error);
});
};
export const updateAccount = async (contactId, updateBody) => {
var queryUrl = "/api/endpoint/updateaccount_api?contactId=" + contactId;
var data = updateBody;
var config = {
method: "post",
url: queryUrl,
data: data,
};
return axios(config)
.then((res) => {
return res.data;
})
.catch((error) => {
console.log("this error:", error);
});
};
export const createWatchedCases = (formValues) => {
var data = formValues;
var queryUrl = window.apiRoot + "api/endpoint/createwatchedcases_api";
console.log(queryUrl);
var config = {
method: "post",
url: queryUrl,
data: data,
};
return axios(config)
.then((res) => {
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
);
var queryUrl =
"/api/endpoint/updatecase_api?updateFormCollection=" +
updateFormCollection +
"&appealObj=" +
appealObj;
var config = {
method: "post",
url: queryUrl + hashAPIPath(queryUrl),
data: data,
};
return axios(config)
.then((res) => {
return res.data;
})
.catch((error) => {
console.log("thi serror", error);
});
};
export const deleteMyRepresentations = (myRepresentationsID) => {
var queryUrl =
"/api/endpoint/deletemyrepresentations_api?myRepresentationsID=" +
myRepresentationsID;
var config = {
method: "delete",
url: queryUrl + hashAPIPath(queryUrl),
headers: {
"OData-MaxVersion": "4.0",
"OData-Version": "4.0",
"Accept": "application/json;odata.metadata=none",
"Prefer": 'odata.include-annotations="*",return=representation',
"Content-Type": "application/json",
},
};
// console.log(config);
return axios(config)
.then((res) => {
//console.log("deleted ", res.data);
return res.data;
})
.catch((error) => {
console.log("this serror", error);
});
};
export const deleteWatchedCases = (watchedCaseID) => {
var queryUrl =
"/api/endpoint/deletewatchedcases_api?watchedCaseID=" + watchedCaseID;
var config = {
method: "delete",
url: queryUrl,
};
return axios(config)
.then((res) => {
//console.log("deleted ", res.data);
return res.data;
})
.catch((error) => {
console.log("this serror", error);
});
};
export const createAccount = (formValues) => {
var data = formValues;
var queryUrl = "/api/endpoint/createaccount_api";
var config = {
method: "post",
url: queryUrl,
data: data,
};
return axios(config)
.then((res) => {
// console.log("posted ", res.data);
return res.data;
})
.catch((error) => {
console.log("this serror", error);
});
};