feature/IO-3096-GlobalNotifications - Checkpoint - Fix user getting all bodyshop notifications (now by associationId), fix regression in 'Assigned To' scenario.

This commit is contained in:
Dave Richer
2025-02-26 13:11:49 -05:00
parent b86309e74b
commit 0767e290f4
8 changed files with 99 additions and 65 deletions

View File

@@ -1,4 +1,3 @@
// contexts/SocketIO/socketContext.jsx
import React, { createContext, useContext, useEffect, useRef, useState } from "react";
import SocketIO from "socket.io-client";
import { auth } from "../../firebase/firebase.utils";
@@ -15,10 +14,11 @@ export const SocketProvider = ({ children, bodyshop }) => {
const [clientId, setClientId] = useState(null);
const [isConnected, setIsConnected] = useState(false);
const notification = useNotification();
const userAssociationId = bodyshop?.associations?.[0]?.id;
useEffect(() => {
const initializeSocket = async (token) => {
if (!bodyshop || !bodyshop.id || socketRef.current) return; // Prevent multiple instances
if (!bodyshop || !bodyshop.id || socketRef.current) return;
const endpoint = import.meta.env.PROD ? import.meta.env.VITE_APP_AXIOS_BASE_API_URL : "";
const socketInstance = SocketIO(endpoint, {
@@ -41,7 +41,6 @@ export const SocketProvider = ({ children, bodyshop }) => {
default:
break;
}
if (!import.meta.env.DEV) return;
};
const handleConnect = () => {
@@ -87,6 +86,10 @@ export const SocketProvider = ({ children, bodyshop }) => {
const handleNotification = (data) => {
const { jobId, jobRoNumber, notificationId, associationId, notifications } = data;
// Filter out notifications not matching the user's associationId
// Technically not required.
if (associationId !== userAssociationId) return;
const newNotification = {
__typename: "notifications",
id: notificationId,
@@ -102,7 +105,11 @@ export const SocketProvider = ({ children, bodyshop }) => {
}
};
const baseVariables = { limit: 20, offset: 0, where: {} };
const baseVariables = {
limit: 20,
offset: 0,
where: { associationid: { _eq: userAssociationId } }
};
try {
const existingNotifications =
@@ -126,8 +133,10 @@ export const SocketProvider = ({ children, bodyshop }) => {
broadcast: true
});
// Handle showUnreadOnly case
const unreadVariables = { ...baseVariables, where: { read: { _is_null: true } } };
const unreadVariables = {
...baseVariables,
where: { ...baseVariables.where, read: { _is_null: true } }
};
const unreadNotifications =
client.cache.readQuery({
query: GET_NOTIFICATIONS,
@@ -228,7 +237,7 @@ export const SocketProvider = ({ children, bodyshop }) => {
setIsConnected(false);
}
};
}, [bodyshop, notification]);
}, [bodyshop, notification, userAssociationId]);
return (
<SocketContext.Provider value={{ socket: socketRef.current, clientId, isConnected }}>