diff --git a/components/header.js b/components/header.js index cab4ce5b..970f8408 100644 --- a/components/header.js +++ b/components/header.js @@ -61,6 +61,8 @@ const Header = (props) => { "/myportal/representation", "/myportal/viewall", "/contactus", + "/unsubscribe/[watchlistid]", + "/unsubscribeall/[watchlistid]", ]; const hasContactLink = [ diff --git a/locales/cy/common.json b/locales/cy/common.json index 1c740397..66d8873f 100644 --- a/locales/cy/common.json +++ b/locales/cy/common.json @@ -85,5 +85,9 @@ "further-details-para-one": "Mae angen i PEDW gadarnhau ychydig mwy o wybodaeth amdanoch cyn y gallwch barhau.", "further-details-para-two": "Ffoniwch 0300 123 1590 neu cysylltwch â ni ar ", "login-modal-message": "Mae eich sesiwn ar fin dod i ben mewn {{remaining}} eiliad oherwydd anweithgarwch. Byddwch yn cael eich ailgyfeirio i'r dudalen fewngofnodi.", - "login-modal-button": "Arhoswch wedi mewngofnodi" + "login-modal-button": "Arhoswch wedi mewngofnodi", + "unsubscribe-case-title": "Wedi dad-danysgrifio i'r Achos", + "unsubscribe-case-all-label": "Rydych chi wedi dad-danysgrifio o dderbyn diweddariadau o'r achos canlynol", + "unsubscribe-case-none-label": "Dim byd i'w ddad-danysgrifio", + "unsubscribe-case-label": "Rydych chi wedi dad-danysgrifio o dderbyn diweddariadau e-bost ar gyfer" } \ No newline at end of file diff --git a/locales/en/common.json b/locales/en/common.json index 0168e85a..59c5da4e 100644 --- a/locales/en/common.json +++ b/locales/en/common.json @@ -85,5 +85,9 @@ "further-details-para-one": "PEDW needs to confirm a bit more information about you before you can continue.", "further-details-para-two": "Please call 0300 123 1590 or contact us on ", "login-modal-message": "Your session is about to expire in {{remaining}} seconds due to inactivity. You will be redirected to the login page.", - "login-modal-button": "Stay logged in " + "login-modal-button": "Stay logged in ", + "unsubscribe-case-title": "Unsubscribed to Case", + "unsubscribe-case-all-label": "You have unsubscribed from receiving updates from the following case", + "unsubscribe-case-none-label": "Nothing to unsubscribe", + "unsubscribe-case-label": "You have unsubscribed from receiving email updates for" } \ No newline at end of file diff --git a/pages/api/email/getall.js b/pages/api/email/getall.js index e8a3ac4e..4e325f42 100644 --- a/pages/api/email/getall.js +++ b/pages/api/email/getall.js @@ -64,7 +64,7 @@ const buildNotifyPayloads = (watchlistByEmail) => { const link = process.env.NEXTAUTH_URL + doc.pinswg_hashlink || "[No Link]"; - return ` - ${ref}: ${name} (${date})\n Link: ${link}`; + return ` - ${name} (${date})\n Link: ${link}`; }) .join("\n"); @@ -105,7 +105,7 @@ const buildNotifyPayloads = (watchlistByEmail) => { .map(([email, entries]) => { const contactName = entries[0].firstname + " " + entries[0].lastname; - const contactId = entries[0].contactId; + const contactId = entries[0].contactid; const filteredEntries = entries.filter( (entry) => @@ -140,9 +140,11 @@ const buildNotifyPayloads = (watchlistByEmail) => { )}\n` : ""; - const caseUnsubscribelink = `Unsubscribe to updates on this case https://${process.env.NEXTAUTH_URL}/unsubscribe/${entry.pinswg_watchlistid}`; + const caseUnsubscribelink = `Unsubscribe to updates on this case ${process.env.NEXTAUTH_URL}/unsubscribe/${entry.pinswg_watchlistid}`; - return `Case: ${caseRef}\n${[ + return `Case: ${caseRef}\nLink to case:${ + process.env.NEXTAUTH_URL + }/case/${caseRef}\n\n${[ docSection, sipSection, repsSection, @@ -153,7 +155,7 @@ const buildNotifyPayloads = (watchlistByEmail) => { }) .join("\n\n"), unsubscribeLink: `Unsubscribe by selecting this link if you no longer want to receive any updates ${ - process.env.NEXTAUTH_URL + "unsubscribeall/" + "contactId" + process.env.NEXTAUTH_URL + "/unsubscribeall/" + contactId }`, }; @@ -163,7 +165,7 @@ const buildNotifyPayloads = (watchlistByEmail) => { personalisation, reference, oneClickUnsubscribeURL: - process.env.NEXTAUTH_URL + "unsubscribe/" + "eee", + process.env.NEXTAUTH_URL + "/unsubscribeall/" + contactId, }; }) .filter(Boolean); // Remove any nulls from filtered-out users @@ -280,7 +282,7 @@ export default async function CombinedApiProxy(req, res) { for (const payload of payloads) { if (!payload) continue; // skip null entries - console.log(payload); + //console.log(payload); const result = { email: payload.email_address, reference: payload.reference, diff --git a/pages/unsubscribe/[watchlistid].js b/pages/unsubscribe/[watchlistid].js new file mode 100644 index 00000000..024e8b9d --- /dev/null +++ b/pages/unsubscribe/[watchlistid].js @@ -0,0 +1,156 @@ +import { wrapper } from "../../store/store"; +import { connect } from "react-redux"; +import useTranslation from "next-translate/useTranslation"; +import Head from "next/head"; +import Link from "next/link"; +import CookieBanner from "../../components/cookieBanner"; +import Footer from "../../components/footer"; +import Header from "../../components/header"; +import { + hashAPIPath, + getToken, + azureHeaders, + consoleLogger, +} from "../../actions"; +import axios from "axios"; + +const UnsubscribeCase = (props) => { + let { t, lang } = useTranslation(); + const { footerLinks, pages } = props; + + return ( +
+ + + {t("common:unsubscribe-case-title")} -{" "} + {t("common:service-name")} + + +
+ +
+
+
+
+
+

{t("common:unsubscribe-case-title")}

+

+ {props.caseRef == null + ? t( + "common:unsubscribe-case-none-label" + ) + : `You have unsubscribed from receiving email + updates for ${props.caseRef}`} +

+ + + {t("common:logged-out-link")} + +
+
+
+
+
+
+ ); +}; + +const WEBAPI_URL = + process.env.RELAY_ROOT || + "https://dev-pedw-ns.servicebus.windows.net/dev-pedw-hc/"; + +const getMailCaseById = async (watchedCaseID) => { + var token = await getToken(); + + var queryUrl = + "pinswg_watchlists?$count=true&$filter=pinswg_emailnotifications eq true and pinswg_watchlistid eq " + + watchedCaseID + + "&$select=_pinswg_contact_value,_pinswg_watchedcase_value"; + return axios + .get( + WEBAPI_URL + queryUrl + hashAPIPath(queryUrl), + azureHeaders(token.access_token) + ) + .then(({ data }) => { + return data; + }) + .catch((error) => { + consoleLogger(error); + res.status(400).json(error); + }); +}; + +const deleteWatchedCases = async (watchedCaseID) => { + var watchedCaseID = watchedCaseID; + var token = await getToken(); + var queryUrl = "pinswg_watchlists(" + watchedCaseID + ")"; + + console.log(WEBAPI_URL + queryUrl + hashAPIPath(queryUrl)); + + var config = { + method: "delete", + 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", + }, + }; + + return axios(config) + .then(({ data }) => { + //console.log("deleted watched case - " + watchedCaseID); + res.status(200).json(data); + }) + .catch((error) => { + console.log("err----:", error); + if (error.response && error.response.status === 404) { + console.warn("Record not found — nothing to delete."); + return { success: false, caseRef: "Not found" }; + } else { + consoleLogger(error); + //res.status(400).json(error); + } + }); +}; + +export const getServerSideProps = wrapper.getServerSideProps( + (store) => async (ctx) => { + const { query, req, res } = ctx; + + console.log("the query", query.watchlistid); + + const searchResultsObj = await getMailCaseById(query.watchlistid); + + let caseObj = searchResultsObj.value[0] || {}; + + let hasUnsubscribed = caseObj.hasOwnProperty("pinswg_watchlistid"); + + console.log("is there a caseObj", hasUnsubscribed); + + //const deleteCase = await deleteWatchedCases(query.watchlistid); + + hasUnsubscribed && (await deleteWatchedCases(query.watchlistid)); + + //console.log(deleteCase); + return { + props: { + caseRef: hasUnsubscribed + ? caseObj[ + "_pinswg_watchedcase_value@OData.Community.Display.V1.FormattedValue" + ] + : null, + }, + }; + } +); + +const mapStateToProps = (state) => {}; + +const mapDispatchToProps = (dispatch) => {}; + +export default connect(mapStateToProps, mapDispatchToProps)(UnsubscribeCase); diff --git a/pages/unsubscribeall/[watchlistid].js b/pages/unsubscribeall/[watchlistid].js new file mode 100644 index 00000000..47222f65 --- /dev/null +++ b/pages/unsubscribeall/[watchlistid].js @@ -0,0 +1,174 @@ +import { wrapper } from "../../store/store"; +import { connect } from "react-redux"; +import useTranslation from "next-translate/useTranslation"; +import Head from "next/head"; +import Link from "next/link"; +import CookieBanner from "../../components/cookieBanner"; +import Footer from "../../components/footer"; +import Header from "../../components/header"; +import { + hashAPIPath, + getToken, + azureHeaders, + consoleLogger, +} from "../../actions"; +import axios from "axios"; + +const UnsubscribeCase = (props) => { + let { t, lang } = useTranslation(); + const { footerLinks, pages } = props; + + const casesArr = props.caseRef; + + console.log(casesArr); + + return ( +
+ + + {t("common:unsubscribe-case-title")} -{" "} + {t("common:service-name")} + + +
+ +
+
+
+
+
+

{t("common:unsubscribe-case-title")}

+ +

+ {t("common:unsubscribe-case-all-label")} + {casesArr.length > 0 ? "s" : ""} +

+
    + {casesArr.map((key, index) => ( +
  • + + { + key[ + "_pinswg_watchedcase_value@OData.Community.Display.V1.FormattedValue" + ] + } + +
  • + ))} +
+ + + {t("common:logged-out-link")} + +
+
+
+
+
+
+ ); +}; + +const WEBAPI_URL = + process.env.RELAY_ROOT || + "https://dev-pedw-ns.servicebus.windows.net/dev-pedw-hc/"; + +const getMailCaseById = async (watchedCaseID) => { + var token = await getToken(); + + var queryUrl = + "pinswg_watchlists?$count=true&$filter=pinswg_emailnotifications eq true and _pinswg_contact_value eq " + + watchedCaseID; + + return axios + .get( + WEBAPI_URL + queryUrl + hashAPIPath(queryUrl), + azureHeaders(token.access_token) + ) + .then(({ data }) => { + return data; + }) + .catch((error) => { + consoleLogger(error); + res.status(400).json(error); + }); +}; + +const deleteWatchedCases = async (watchedCaseID) => { + var watchedCaseID = watchedCaseID; + var token = await getToken(); + var queryUrl = "pinswg_watchlists(" + watchedCaseID + ")"; + + console.log(WEBAPI_URL + queryUrl + hashAPIPath(queryUrl)); + + var config = { + method: "delete", + 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", + }, + }; + + return axios(config) + .then(({ data }) => { + //console.log("deleted watched case - " + watchedCaseID); + res.status(200).json(data); + }) + .catch((error) => { + console.log("err----:", error); + if (error.response && error.response.status === 404) { + console.warn("Record not found — nothing to delete."); + return { success: false, caseRef: "Not found" }; + } else { + consoleLogger(error); + //res.status(400).json(error); + } + }); +}; + +export const getServerSideProps = wrapper.getServerSideProps( + (store) => async (ctx) => { + const { query, req, res } = ctx; + + console.log("the query", query.watchlistid); + + const searchResultsObj = await getMailCaseById(query.watchlistid); + + let caseObj = searchResultsObj.value.length > 0 || {}; + + console.log("hwo many in caseObj", searchResultsObj.value.length); + + let hasUnsubscribed = searchResultsObj.value.length > 0; + + console.log("is there a caseObj", searchResultsObj.value.length > 0); + + console.log("hasUnsubscribed ", hasUnsubscribed); + + //const deleteCase = await deleteWatchedCases(query.watchlistid); + + //hasUnsubscribed && (await deleteWatchedCases(query.watchlistid)); + + searchResultsObj.value.forEach(function (watchListID) { + console.log(watchListID.pinswg_watchlistid); + }); + + //console.log(deleteCase); + return { + props: { + caseRef: hasUnsubscribed ? searchResultsObj.value : null, + }, + }; + } +); + +const mapStateToProps = (state) => {}; + +const mapDispatchToProps = (dispatch) => {}; + +export default connect(mapStateToProps, mapDispatchToProps)(UnsubscribeCase);