feature/IO-3000-messaging-sockets-migrations2 -

- Conversation Labels Synced
- Job Tagging Synced

Signed-off-by: Dave Richer <dave@imexsystems.ca>
This commit is contained in:
Dave Richer
2024-11-20 18:22:27 -08:00
parent 250faa672f
commit 06afd6da5b
9 changed files with 142 additions and 75 deletions

View File

@@ -97,70 +97,45 @@ export const registerMessagingHandlers = ({ socket, client }) => {
};
const handleConversationChanged = (data) => {
const { type, conversationId, jobId, label } = data;
const { conversationId, type, job_conversations, ...fields } = data;
switch (type) {
case "conversation-marked-read":
client.cache.modify({
id: client.cache.identify({
__typename: "conversations",
id: conversationId
}),
fields: {
messages_aggregate: () => ({ aggregate: { count: 0 } })
}
});
// Identify the conversation in the Apollo cache
const cacheId = client.cache.identify({
__typename: "conversations",
id: conversationId
});
// Optionally, refetch queries if needed
// client.refetchQueries({
// include: [CONVERSATION_LIST_QUERY, GET_CONVERSATION_DETAILS]
// });
break;
case "tag-added":
client.cache.modify({
id: client.cache.identify({
__typename: "conversations",
id: conversationId
}),
fields: {
job_conversations(existingJobConversations = []) {
return [...existingJobConversations, { __ref: `jobs:${jobId}` }];
}
}
});
break;
case "tag-removed":
client.cache.modify({
id: client.cache.identify({
__typename: "conversations",
id: conversationId
}),
fields: {
job_conversations(existingJobConversations = []) {
return existingJobConversations.filter((jobRef) => jobRef.__ref !== `jobs:${jobId}`);
}
}
});
break;
case "label-changed":
client.cache.modify({
id: client.cache.identify({
__typename: "conversations",
id: conversationId
}),
fields: {
label() {
return label;
}
}
});
break;
default:
console.warn(`Unhandled conversation change type: ${type}`);
if (!cacheId) {
console.error(`Could not find conversation with id: ${conversationId}`);
return;
}
client.cache.modify({
id: cacheId,
fields: {
...Object.fromEntries(
Object.entries(fields).map(([key, value]) => [
key,
(cached) => (value !== undefined ? value : cached) // Update with new value or keep existing
])
),
...(type === "conversation-marked-read" && {
messages_aggregate: () => ({
aggregate: { count: 0 } // Reset unread count
})
}),
...(type === "tag-added" && {
job_conversations: (existing = []) => {
// Merge existing job_conversations with new ones
return [...existing, ...job_conversations];
}
}),
...(type === "tag-removed" && {
job_conversations: (existing = [], { readField }) =>
existing.filter((jobConversationRef) => readField("jobid", jobConversationRef) !== data.jobId)
})
}
});
};
socket.on("new-message-summary", handleNewMessageSummary);