diff --git a/actions/index.js b/actions/index.js index 015e296e..5ea300c9 100644 --- a/actions/index.js +++ b/actions/index.js @@ -11,8 +11,19 @@ if (process.browser) { BASE_URL = window.apiRoot; } else { BASE_URL = process.env.API_ROOT || `http://localhost:${port}`; + + // BASE_URL = + "https://org042f59af.api.crm11.dynamics.com/api/data/v9.2/" || + `http://localhost:${port}`; } +let TOKEN_URL = + "https://login.microsoftonline.com/513f475e-ef51-4de1-9e85-a9fbe486f8cf/oauth2/v2.0/token/"; + +let WEBAPI_URL = "https://org042f59af.crm11.dynamics.com/api/data/v9.2/"; + +const cache = {}; + const agent = new https.Agent({ rejectUnauthorized: false, }); @@ -32,6 +43,64 @@ const crmHeaders = { }, }; +const tokenBody = + // "grant_type=" + + // process.env.GRANT_TYPE + + // "&scope=" + + // process.env.SCOPE + + // "&client_id=" + + // process.env.CLIENT_ID + + // "&client_secret=" + + // process.env.CLIENT_SECRET; + "client_id=86345cdb-d4fd-4a59-a5e4-1c718512c913&client_secret=AE7Hz2D8Pyx2M~Y6Z_-qoOUcnq67jR9o_3&scope=https%3A%2F%2Forg042f59af.crm11.dynamics.com%2F.default&grant_type=client_credentials"; + +const tokenConfig = { + headers: { + "Content-Type": "application/x-www-form-urlencoded", + "Cache-Control": "no-cache", + }, +}; + +const accessToken = (authToken) => { + console.log("access toekn", authToken); + return { + headers: { + "Content-Type": "application/x-www-form-urlencoded", + "Cache-Control": "no-cache", + Authorization: "Bearer " + authToken, + }, + }; +}; + +const jsonAccessToken = (authToken) => { + return { + headers: { + "Content-Type": "application/json", + Authorization: "Bearer " + authToken, + }, + }; +}; + +export const getToken = () => { + if (cache.tokenResponse) { + return cache.tokenResponse; + } else { + return ( + axios + //.post(`${BASE_URL}/oauth/token`, tokenBody, tokenConfig) + .post(`${TOKEN_URL}/`, tokenBody, tokenConfig) + .then((res) => res.data) + .then((data) => { + cache.tokenResponse = data; + return data; + }) + .catch((error) => { + console.log("error :", error.response); + }) + ); + } +}; + export const getBasicSearch = (searchString) => { console.log("got to axios", searchString); console.log("api_root: ", BASE_URL); @@ -39,9 +108,15 @@ 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", { + .get( + WEBAPI_URL + + "incidents?$select=_accountid_value,_customerid_value,pinswg_appealcasetype,statuscode,ticketnumber,title&$filter=contains(title, '" + + searchString + + "')&$count=true", + accessToken(cache.tokenResponse.access_token) + ) + .then((res) => res.data) .catch((error) => { console.log("thiserror", error); @@ -304,16 +379,23 @@ export const createCase = (lpaType, appealType) => { // } // }); -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 getMyCases = (authToken) => { + return ( + axios + //.get(BASE_URL + "/api/lookups/myCases", { + .get( + WEBAPI_URL + + "incidents?$select=_accountid_value,_customerid_value,pinswg_appealcasetype,statuscode,ticketnumber,title", + accessToken(cache.tokenResponse.access_token) + // { + // httpsAgent: new https.Agent({ rejectUnauthorized: false }), + // } + ) + .then((res) => res.data) + .catch((error) => { + console.log("thi serror", error); + }) + ); }; export const getMyRepresentations = () => { @@ -336,7 +418,6 @@ export const getWatchedCases = () => { }) .then((res) => res.data) .catch((error) => { - w; console.log("thi serror", error); }); }; diff --git a/components/myportal/topthree.js b/components/myportal/topthree.js index fc4bba22..de81f1ee 100644 --- a/components/myportal/topthree.js +++ b/components/myportal/topthree.js @@ -32,12 +32,13 @@ const TopThree = (props) => { onClick={() => { setCurrentReference({ "currentReference": - showTopThree.value[i].reference, + showTopThree.value[i] + .ticketnumber, "currentType": topThreeType, }); }} > - {showTopThree.value[i].reference} + {showTopThree.value[i].ticketnumber} diff --git a/pages/myportal/index.js b/pages/myportal/index.js index 9c87b014..c45d8c1c 100644 --- a/pages/myportal/index.js +++ b/pages/myportal/index.js @@ -18,6 +18,7 @@ import { setMyRepresentations } from "../../store/myRepresentations/action"; import { setWatchedCases } from "../../store/watchedCases/action"; import { setAwaitingSubmission } from "../../store/awaitingSubmission/action"; import { + getToken, getMyCases, getMyRepresentations, getWatchedCases, @@ -75,6 +76,9 @@ export const getServerSideProps = wrapper.getServerSideProps( "public, s-maxage=604800, stale-while-revalidate" ); + const token = await getToken(); + console.log(token); + const [ myCases, myRepresentations, diff --git a/pages/searchresults.js b/pages/searchresults.js index 5d3b4082..d3d2aafd 100644 --- a/pages/searchresults.js +++ b/pages/searchresults.js @@ -19,7 +19,7 @@ import { setSearchResults, getSearchResultsObj, } from "../store/searchOutput/action"; -import { getBasicSearch } from "../actions"; +import { getToken, getBasicSearch } from "../actions"; const Home = (props) => { const { footerLinks, pages } = props; @@ -56,6 +56,8 @@ const Home = (props) => { export const getServerSideProps = wrapper.getServerSideProps( (store) => async ({ query, req, res }) => { + const token = await getToken(); + console.log(token); console.log("query-", query); const searchResultsObj = (await getBasicSearch(query.q)) || null; store.dispatch(setSearchResults(searchResultsObj));