16986 unsubscribe link

This commit is contained in:
2025-06-02 17:41:39 +01:00
parent 8b244ffb94
commit 6c7eca581b
6 changed files with 351 additions and 9 deletions
+156
View File
@@ -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 (
<div>
<Head>
<title>
{t("common:unsubscribe-case-title")} -{" "}
{t("common:service-name")}
</title>
</Head>
<div id="page_wrapper">
<CookieBanner />
<Header />
<div className="govuk-width-container govuk-!-padding-bottom-9">
<main className="govuk-main-wrapper govuk-!-padding-top-9 govuk-!-padding-bottom-9">
<div className="govuk-grid-row">
<div className="govuk-grid-column-two-thirds ">
<h1>{t("common:unsubscribe-case-title")}</h1>
<p>
{props.caseRef == null
? t(
"common:unsubscribe-case-none-label"
)
: `You have unsubscribed from receiving email
updates for ${props.caseRef}`}
</p>
<Link href="/">
{t("common:logged-out-link")}
</Link>
</div>
</div>
</main>
</div>
<Footer footerLinks={footerLinks} ticketnumber={props} />
</div>
</div>
);
};
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);