diff --git a/components/header.js b/components/header.js index 4d6f02ff..2377f044 100644 --- a/components/header.js +++ b/components/header.js @@ -61,6 +61,7 @@ const Header = (props) => { "/myportal/representation", "/myportal/viewall", "/contactus", + "/myportal/contactus", "/unsubscribe/[watchlistid]", "/unsubscribeall/[watchlistid]", ]; diff --git a/components/myportal/topthree.js b/components/myportal/topthree.js index fb545461..16f75fe6 100644 --- a/components/myportal/topthree.js +++ b/components/myportal/topthree.js @@ -115,6 +115,27 @@ const TopThree = (props) => { .then((data) => data) .then(() => { getWatchedCasesProxy(cookies.pinsUser).then((data) => { + let showWatchedCases = (submittedArr) => { + const required = submittedArr.value.filter((el) => { + return ( + el.pinswg_representationsubmitted == null + ); + }); + + let newObj = {}; + return Object.assign(newObj, { + "@odata.count": required.length, + "value": required, + }); + }; + + data = showWatchedCases(data); + + data.value.sort(function compare(a, b) { + var dateA = new Date(a.createdon); + var dateB = new Date(b.createdon); + return dateB - dateA; + }); setWatchedCases(data), getDetailsProxy(data, "myWatchedCases").then( (data) => { diff --git a/components/myportal/viewall.js b/components/myportal/viewall.js index 42c9ee58..43a2bbfb 100644 --- a/components/myportal/viewall.js +++ b/components/myportal/viewall.js @@ -12,6 +12,8 @@ import { getAwaitingSubmissionFromBlobProxy, getRepsFromBlobProxy, getWatchedCasesProxy, + getWatchedCases, + createWatchedCases, } from "../../actions"; import { getDetailsProxy, getFormCollectionByID } from "../../components/utils"; import transLookup from "../../data/lookuptranslations.json"; @@ -272,12 +274,21 @@ const ViewAllResults = (props) => { /> {viewAllKey == "myRepresentations" && ( <> -
- + {resultsArr[key].representationType} )} + {viewAllKey == "mySubmittedReps" && ( + <> + + {item.pinswg_representationtype}{" "} + {formatDates( + item.pinswg_representationsubmitted + )} + + + )} )} @@ -415,10 +426,15 @@ const ViewAllResults = (props) => { {/* Helper function to extract address lines based on current view and data */} {[ "pinswg_siteaddressline1", + "pinswg_addressline1", "pinswg_siteaddressline2", + "pinswg_addressline2", "pinswg_siteaddresstown", + "pinswg_addresstown", "pinswg_siteaddresscounty", "pinswg_siteaddresspostcode", + "pinswg_postcode", + "pinswg_projectlocation", ].map((field, idx) => { const isAwaitingSubmission = viewAllKey === "awaitingSubmissionDetails"; @@ -426,7 +442,7 @@ const ViewAllResults = (props) => { viewAllKey === "myRepresentations"; const searchValue = _.get( searchDetailsObj, - `${key}.${field}` + `${key}.${field} ` ); const detailsValue = _.get(detailsObj, field); @@ -738,12 +754,15 @@ const ViewAllResults = (props) => { )} {viewAllKey == "watchedCases" && ( -
+
+ {item.pinswg_emailnotifications == true ? ( + + ) : ( + + )}
)} ); }); }; - buildResultsRowArr(); const sortDataBy = (whichField) => { setOrderbyState(whichField); @@ -861,6 +928,7 @@ const ViewAllResults = (props) => { }); }); }; + const deleteItem = (caseID, topThreeType) => { let cookies = parseCookies(); //console.log("sssss", caseID, topThreeType); @@ -884,6 +952,78 @@ const ViewAllResults = (props) => { }); }; + const isEmailSignUp = (whichIncident) => { + let isEmail = jsonpath({ + path: + "$[?(@ && @._pinswg_watchedcase_value=='" + + whichIncident + + "' && @.pinswg_emailnotifications==true)]", + + json: resultsArr, + eval: true, + }); + return isEmail; + }; + + const selectEmailNotifications = ( + emailaddress, + loggedInUser, + incidentID, + appealType, + updateEmailNotifications + ) => { + let updateBody = { + "pinswg_WatchedCase@odata.bind": "/incidents(" + incidentID + ")", + "pinswg_Contact@odata.bind": "/contacts(" + loggedInUser + ")", + "pinswg_appealcasetype": appealType, + "pinswg_emailnotifications": updateEmailNotifications, + }; + + console.log("Email signed up"); + createWatchedCases(updateBody) + .then((data) => data) + .then(() => { + getWatchedCases(props.accountDetails.loggedinUserId).then( + (data) => { + let showWatchedCases = (submittedArr) => { + const required = submittedArr.value.filter((el) => { + return ( + el.pinswg_representationsubmitted == null + ); + }); + + let newObj = {}; + return Object.assign(newObj, { + "@odata.count": required.length, + "value": required, + }); + }; + + data = showWatchedCases(data); + + data.value.sort(function compare(a, b) { + var dateA = new Date(a.createdon); + var dateB = new Date(b.createdon); + return dateB - dateA; + }); + + setWatchedCases(data); + getDetailsProxy(data, "myWatchedCases") + .then((data) => { + setWatchedCasesDetails(data); + }) + .then(() => { + setCurrentView({ + "viewName": "Watched Cases", + "viewKey": "watchedCases", + }); + }); + } + ); + }); + }; + + buildResultsRowArr(); return ( <>
diff --git a/components/search/repsonresults.js b/components/search/repsonresults.js index f99a6f97..2409e142 100644 --- a/components/search/repsonresults.js +++ b/components/search/repsonresults.js @@ -91,7 +91,6 @@ const RepsOnResults = (props) => { return ( <> -

{(_.has(detailsObj, "pinswg_startdate") || _.has(detailsObj, "pinswg_applicationacceptedasvalid") || diff --git a/pages/api/endpoint/createwatchedcases_api.js b/pages/api/endpoint/createwatchedcases_api.js index 5483e948..4368899a 100644 --- a/pages/api/endpoint/createwatchedcases_api.js +++ b/pages/api/endpoint/createwatchedcases_api.js @@ -1,14 +1,14 @@ -/** - * @swagger - * /api/endpoint/createmywatchedcases_api: - * post: - * tags: [Portal,Case] - * summary: Phase 2 - * description: Create my watched cases - * responses: - * 200: - * description: Success - */ +// /** +// * @swagger +// * /api/endpoint/createmywatchedcases_api: +// * post: +// * tags: [Portal,Case] +// * summary: Phase 2 +// * description: Create my watched cases +// * responses: +// * 200: +// * description: Success +// */ import axios from "axios"; import CryptoJS from "crypto-js"; @@ -32,31 +32,96 @@ const hashAPIPath = (queryPath) => { return (queryPath.indexOf("?") > -1 ? "&hash=" : "?hash=") + hashlink; }; -export default async function ApiProxy(req, res) { - var token = await getToken(); - var data = JSON.stringify(req.body); - var queryUrl = "pinswg_watchlists"; +// export default async function ApiProxy(req, res) { +// var token = await getToken(); +// var data = JSON.stringify(req.body); +// var queryUrl = "pinswg_watchlists"; - var config = { - method: "post", +// console.log(data); +// var config = { +// method: "put", +// url: WEBAPI_URL + queryUrl + hashAPIPath(queryUrl), +// headers: { +// "OData-MaxVersion": "4.0", +// "OData-Version": "4.0", +// "Accept": "application/json", +// "Prefer": 'odata.include-annotations="*",return=representation', +// "Authorization": "Bearer " + token.access_token, +// "Content-Type": "application/json", +// }, +// data: data, +// }; + +// return axios(config) +// .then(({ data }) => { +// res.status(200).json(data); +// }) +// .catch((error) => { +// consoleLogger(Object.assign(err, data)); +// res.status(400).json(error); +// }); +// } +const recordExists = async (incidentId, contactId, token) => { + const filter = `$filter=pinswg_WatchedCase/incidentid eq ${incidentId} and pinswg_Contact/contactid eq ${contactId}`; + const queryUrl = `pinswg_watchlists?${filter}`; + const url = WEBAPI_URL + queryUrl + hashAPIPath(queryUrl); + + try { + const res = await axios.get(url, { + headers: { + Authorization: "Bearer " + token.access_token, + Accept: "application/json", + }, + }); + + if (res.data.value && res.data.value.length > 0) { + return res.data.value[0]; // return the existing record + } + + return null; + } catch (err) { + throw err; + } +}; + +export default async function ApiProxy(req, res) { + const token = await getToken(); + const data = req.body; + + const incidentId = + data["pinswg_WatchedCase@odata.bind"].match(/\(([^)]+)\)/)[1]; + const contactId = data["pinswg_Contact@odata.bind"].match(/\(([^)]+)\)/)[1]; + + const existingRecord = await recordExists(incidentId, contactId, token); + + let method, queryUrl; + + if (existingRecord) { + method = "patch"; + queryUrl = `pinswg_watchlists(${existingRecord.pinswg_watchlistid})`; // adjust with your primary key logical name + } else { + method = "post"; + queryUrl = "pinswg_watchlists"; + } + + const config = { + method: method, url: WEBAPI_URL + queryUrl + hashAPIPath(queryUrl), headers: { "OData-MaxVersion": "4.0", "OData-Version": "4.0", - "Accept": "application/json", - "Prefer": 'odata.include-annotations="*",return=representation', - "Authorization": "Bearer " + token.access_token, + Accept: "application/json", + Prefer: 'odata.include-annotations="*",return=representation', + Authorization: "Bearer " + token.access_token, "Content-Type": "application/json", }, - data: data, + data: JSON.stringify(data), }; return axios(config) - .then(({ data }) => { - res.status(200).json(data); - }) - .catch((error) => { - consoleLogger(Object.assign(err, data)); - res.status(400).json(error); + .then(({ data }) => res.status(200).json(data)) + .catch((err) => { + consoleLogger(err); + res.status(400).json(err.response?.data || err); }); } diff --git a/pages/api/endpoint/getwatchedcasesproxy_api.js b/pages/api/endpoint/getwatchedcasesproxy_api.js index c3db49a2..370a3972 100644 --- a/pages/api/endpoint/getwatchedcasesproxy_api.js +++ b/pages/api/endpoint/getwatchedcasesproxy_api.js @@ -43,7 +43,7 @@ export default async function ApiProxy(req, res) { var queryUrl = "pinswg_watchlists?$filter= _pinswg_contact_value eq " + loggedInUserId + - "&$select=pinswg_emailnotifications,modifiedon,pinswg_appealcasetype,pinswg_watchlistid,_pinswg_watchedcase_value,statuscode&$count=true&$orderby=createdon desc&$expand=pinswg_WatchedCase($select=pinswg_AssociatedLPA)"; + "&$select=pinswg_emailnotifications,modifiedon,pinswg_appealcasetype,pinswg_watchlistid,_pinswg_watchedcase_value,statuscode,pinswg_representationsubmitted,pinswg_representationtype&$count=true&$orderby=createdon desc&$expand=pinswg_WatchedCase($select=pinswg_AssociatedLPA)"; return axios .get( @@ -51,6 +51,32 @@ export default async function ApiProxy(req, res) { azureHeaders(token.access_token) ) .then(({ data }) => { + data.value.forEach(function (element) { + element.ticketnumber = element.pinswg_WatchedCase?.ticketnumber; + element.pinswg_title = + element[ + "_pinswg_watchedcase_value@OData.Community.Display.V1.FormattedValue" + ]; + + element[ + "_pinswg_associatedlpa_value@OData.Community.Display.V1.FormattedValue" + ] = + element.pinswg_WatchedCase?.[ + "_pinswg_associatedlpa_value@OData.Community.Display.V1.FormattedValue" + ]; + element._pinswg_associatedlpa_value = + element.pinswg_WatchedCase?._pinswg_associatedlpa_value; + element[ + "_ownerid_value@OData.Community.Display.V1.FormattedValue" + ] = + element.pinswg_WatchedCase?.[ + "_ownerid_value@OData.Community.Display.V1.FormattedValue" + ]; + element._ownerid_value = + element.pinswg_WatchedCase?._ownerid_value; + + delete element.pinswg_WatchedCase; + }); res.status(200).json(data); }) .catch((error) => {