Skip to content

The open alternative to Salesforce, designed for AI.

1.7 KB
Raw Blame
import { isDefined } from 'twenty-shared/utils';

import { type AgentChatChannelView } from '@/ai/types/AgentChatChannelView';
import { type AgentChatThreadRecord } from '@/ai/types/AgentChatThreadRecord';
import { getAgentChatThreadChannelStatus } from '@/ai/utils/getAgentChatThreadChannelStatus';
import { isAgentChatThreadInChannelThreadFilter } from '@/ai/utils/isAgentChatThreadInChannelThreadFilter';
import { AgentChatChannelAssignmentFilter } from '~/generated-metadata/graphql';

export const isAgentChatThreadInChannelView = ({
  thread,
  channelView,
  currentWorkspaceMemberId,
  isMentioned,
}: {
  thread: Pick<
    AgentChatThreadRecord,
    | 'deletedAt'
    | 'channelId'
    | 'assigneeId'
    | 'lastActivityAt'
    | 'channelArchivedAt'
    | 'channelSnoozedUntil'
    | 'pendingQuestionMessageId'
  >;
  channelView: AgentChatChannelView;
  currentWorkspaceMemberId: string | undefined;
  isMentioned: boolean;
}): boolean => {
  if (
    isDefined(thread.deletedAt) ||
    thread.channelId !== channelView.channelId ||
    getAgentChatThreadChannelStatus(thread) !== channelView.channelStatus ||
    !isAgentChatThreadInChannelThreadFilter({
      thread,
      filter: channelView.filter,
      isMentioned,
    })
  ) {
    return false;
  }

  switch (channelView.assignment) {
    case AgentChatChannelAssignmentFilter.ANY:
      return true;
    case AgentChatChannelAssignmentFilter.ASSIGNED:
      return isDefined(thread.assigneeId);
    case AgentChatChannelAssignmentFilter.UNASSIGNED:
      return !isDefined(thread.assigneeId);
    case AgentChatChannelAssignmentFilter.ASSIGNED_TO_ME:
      return (
        isDefined(currentWorkspaceMemberId) &&
        thread.assigneeId === currentWorkspaceMemberId
      );
  }
};