Skip to content

Commit

Permalink
fix: discussions new messages verify
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuseduardomedeiros committed Dec 23, 2024
1 parent ca3fa10 commit 8a401e4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';

import { useDashboard } from '@/store/modules/dashboard';
import { useDiscussions } from '@/store/modules/chats/discussions';
import { useDiscussionMessages } from '@/store/modules/chats/discussionMessages';

Expand Down Expand Up @@ -28,13 +29,19 @@ vi.mock('@/store/modules/chats/discussionMessages', () => ({
useDiscussionMessages: vi.fn(),
}));

vi.mock('@/store/modules/dashboard', () => ({
useDashboard: vi.fn(),
}));

describe('Create discussion message service', () => {
let mockDiscussionStore,
mockDiscussionMessagesStore,
mockDashboardStore,
mockApp,
soundNotificationMock;

beforeEach(() => {
mockDashboardStore = { viewedAgent: { email: '' } };
mockDiscussionStore = {
discussions: [{ uuid: '123', name: 'Test Discussion' }],
activeDiscussion: { uuid: '123' },
Expand All @@ -58,6 +65,7 @@ describe('Create discussion message service', () => {
soundNotificationMock = new SoundNotification('ping-bing');
SoundNotification.mockReturnValue(soundNotificationMock);

useDashboard.mockReturnValue(mockDashboardStore);
useDiscussions.mockReturnValue(mockDiscussionStore);
useDiscussionMessages.mockReturnValue(mockDiscussionMessagesStore);
});
Expand Down
15 changes: 10 additions & 5 deletions src/services/api/websocket/listeners/discussion/message/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ import { isValidJson } from '@/utils/messages';

import { useDiscussions } from '@/store/modules/chats/discussions';
import { useDiscussionMessages } from '@/store/modules/chats/discussionMessages';
import { useDashboard } from '@/store/modules/dashboard';

export default (message, { app }) => {
const discussionStore = useDiscussions();
const discussionMessagesStore = useDiscussionMessages();
const dashboardStore = useDashboard();

const { viewedAgent } = dashboardStore;
const { discussions, activeDiscussion } = discussionStore;
const findDiscussion = discussions.find(
(discussion) => discussion.uuid === message.discussion,
Expand All @@ -31,12 +35,11 @@ export default (message, { app }) => {
});
}

const isCurrentDiscussion =
app.$route.name === 'discussion' &&
app.$route.params.discussionId === message.discussion;
const isCurrentDiscussion = activeDiscussion?.uuid === message.discussion;

const isViewModeCurrentDiscussion =
app.$route.params.viewedAgent &&
activeDiscussion?.uuid === message.discussion;
viewedAgent.email && activeDiscussion?.uuid === message.discussion;

const shouldAddDiscussionMessage =
isCurrentDiscussion || isViewModeCurrentDiscussion;

Expand All @@ -45,9 +48,11 @@ export default (message, { app }) => {
}

const isJsonMessage = isValidJson(message.text);

if (shouldAddDiscussionMessage || isJsonMessage) {
return;
}

discussionStore.addNewMessagesByDiscussion({
discussion: message.discussion,
message: {
Expand Down

0 comments on commit 8a401e4

Please sign in to comment.