feature/IO-3182-Phone-Number-Consent - Checkpoint

This commit is contained in:
Dave Richer
2025-05-21 14:32:35 -04:00
parent 7bd5190bf2
commit 8ee52598e8
31 changed files with 128 additions and 991 deletions

View File

@@ -8,15 +8,12 @@ import ChatPopupComponent from "../chat-popup/chat-popup.component";
import "./chat-affix.styles.scss";
import { registerMessagingHandlers, unregisterMessagingHandlers } from "./registerMessagingSocketHandlers";
import { useSocket } from "../../contexts/SocketIO/useSocket.js";
import { GET_PHONE_NUMBER_CONSENT } from "../../graphql/consent.queries";
export function ChatAffixContainer({ bodyshop, chatVisible }) {
const { t } = useTranslation();
const client = useApolloClient();
const { socket } = useSocket();
const enforceConsent = bodyshop?.enforce_sms_consent ?? false;
useEffect(() => {
if (!bodyshop || !bodyshop.messagingservicesid) return;
@@ -41,63 +38,11 @@ export function ChatAffixContainer({ bodyshop, chatVisible }) {
if (socket && socket.connected) {
registerMessagingHandlers({ socket, client });
// Handle consent-changed events only if enforce_sms_consent is true
const handleConsentChanged = ({ bodyshopId, phone_number, consent_status, reason }) => {
if (!enforceConsent || bodyshopId !== bodyshop.id) return;
try {
const cacheData = client.readQuery({
query: GET_PHONE_NUMBER_CONSENT,
variables: { bodyshopid: bodyshopId, phone_number }
});
if (!cacheData?.phone_number_consent?.[0]) {
console.warn("No cached data for GET_PHONE_NUMBER_CONSENT:", { bodyshopId, phone_number });
return;
}
const updatedConsent = {
...cacheData.phone_number_consent[0],
consent_status,
consent_updated_at: new Date().toISOString(),
phone_number_consent_history: [
{
__typename: "phone_number_consent_history",
id: `temp-${Date.now()}`,
reason,
changed_at: new Date().toISOString(),
old_value: cacheData.phone_number_consent[0].consent_status,
new_value: consent_status,
changed_by: "system"
},
...(cacheData.phone_number_consent[0].phone_number_consent_history || [])
]
};
client.writeQuery(
{
query: GET_PHONE_NUMBER_CONSENT,
variables: { bodyshopid: bodyshopId, phone_number }
},
{
phone_number_consent: [updatedConsent]
}
);
console.log("Cache update in handleConsentChanged:", { phone_number, consent_status, updatedConsent });
} catch (error) {
console.error("Error updating consent cache in handleConsentChanged:", error.message, error.stack);
}
};
socket.on("consent-changed", handleConsentChanged);
return () => {
socket.off("consent-changed", handleConsentChanged);
unregisterMessagingHandlers({ socket });
};
}
}, [bodyshop, socket, t, client, enforceConsent]);
}, [bodyshop, socket, t, client]);
if (!bodyshop || !bodyshop.messagingservicesid) return <></>;