All files / src/features/Merchants/MerchantSidePanel/WithRepository/Challenges/hooks useNotifications.tsx

51.61% Statements 16/31
20.68% Branches 6/29
50% Functions 4/8
57.14% Lines 16/28

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173                                                              850x 850x 850x 850x     850x 850x   850x         850x                                                                                                                                       850x     116x           4x           2x                     4x                   116x         4x                               4x    
import { useEnterprisePermissions } from "@components/AcquirerEnterprises/CreateEnterprise/hooks/useEnterprisePermissions";
import { QKEY_CHALLENGE_THREAD } from "@constants/queryKeys";
import { GENERAL_STALE_TIME_15 } from "@features/Merchants/MerchantSidePanel/constants";
import { useAppDispatch } from "@redux/hooks";
import {
  resetConversationTopic,
  setConversationTopic,
  setModalOpenConversation,
} from "@redux/slices/conversations";
import { customInstance } from "@services/api";
import { checkPortals } from "@utils/routing";
import { getGlobalTopic } from "features/Minibuilders/Conversations/hooks/useConversationsModal";
import { TGlobalTopic } from "features/Minibuilders/Conversations/types";
import { useQuery } from "react-query";
import { useConversationsInformations } from "./useConversationsInformations";
 
export type NotifyModalProps = {
  challengeID?: number;
  challengeTypeName?: "enhanced_due_diligence" | "customer_due_diligence";
  hideInputs?: string[];
  defaultMessage?: string;
  topicName?: string;
};
 
export function useNotifications({
  statusName,
  merchantID,
}: {
  statusName: string;
  merchantID: number;
}) {
  const openRiskModule = !["approved", "suspended"].includes(statusName);
  const { merchant_underwriting } = useEnterprisePermissions();
  const { isEnterprisePortal } = checkPortals();
  const { fetchExistingThreads } = useConversationsInformations({
    merchantID,
  });
  const isConversationDisabled = isEnterprisePortal && !merchant_underwriting;
  const dispatch = useAppDispatch();
 
  const closeNotifyMerchantModal = () => {
    dispatch(setModalOpenConversation(false));
    dispatch(resetConversationTopic());
  };
 
  const openNotifyMerchantModal = async ({
    challengeID,
    challengeTypeName,
    hideInputs = [],
    defaultMessage,
    topicName = "",
  }: NotifyModalProps) => {
    if (isConversationDisabled) return;
    const result = await fetchExistingThreads(
      challengeID,
      openRiskModule ? "underwriting" : "risk_monitor",
      "activity",
    );
 
    if (!result) return;
 
    const { existingThread, fetchedTopicID } = result;
 
    if (!fetchedTopicID) return;
 
    dispatch(setModalOpenConversation(true));
    dispatch(
      setConversationTopic({
        isOpen: true,
        isOpenedFromSidePanel: false,
        numberOfUnreadMessages: 0,
        threadId: existingThread?.id,
        queryObject: {
          id: fetchedTopicID,
          name: `Notify Merchant`,
          tabs: "Activity",
          challengeTypeName: challengeTypeName,
          challengeId: challengeID,
          defaultMessage: existingThread ? "" : defaultMessage,
          merchantId: merchantID,
          paths: !existingThread
            ? [
                {
                  isConversation: true,
                  pathName: `Notify Merchant`,
                  pathID: "new",
                  avatars: [],
                  hideInputs: hideInputs,
                  topicName: topicName,
                },
              ]
            : [
                { avatars: [], pathName: "Underwriting" },
                {
                  avatars: [],
                  pathName: topicName,
                  pathID: existingThread.id,
                  isConversation: true,
                },
              ],
          ...(existingThread && {
            openThread: {
              commentId: existingThread.messages[0]?.id,
              index: 0,
              isRepliesOpen: true,
              threadId: existingThread.id,
            },
          }),
        },
      }),
    );
  };
 
  return { openNotifyMerchantModal, closeNotifyMerchantModal };
}
 
export const useGetGlobalTopicByActivity = (
  merchantId?: number,
  topicName = "underwriting",
  type = "activity",
  enabled = true,
) => {
  const { isLoading, data } = useQuery<{
    total: number;
    data: TGlobalTopic[];
  }>(
    ["topic", merchantId, topicName],
    async () =>
      await getGlobalTopic({
        topicName: topicName,
        merchantId,
      }),
    {
      refetchOnMount: false,
      refetchOnWindowFocus: false,
      staleTime: Infinity, //global topic should not change
      enabled,
    },
  );
  return {
    isLoading,
    topicID:
      data?.total &&
      data?.data?.find(
        (item: any) => item.Name === topicName && item.Type === type,
      )?.ID,
  };
};
 
export const useGetChallengeThread = (
  merchantID: number,
  topicID?: number,
  enabled = true,
) => {
  const { isLoading, data } = useQuery(
    [QKEY_CHALLENGE_THREAD, merchantID, topicID],
    async () => {
      const data = await customInstance({
        url: `/merchants/${merchantID}/topics/${topicID}/threads?sort=createdAt`,
        method: "GET",
      });
      return { data: data?.data };
    },
    {
      enabled: !!topicID && !!merchantID && enabled,
      staleTime: GENERAL_STALE_TIME_15,
      refetchOnWindowFocus: false,
      refetchOnMount: false,
    },
  );
  return { data, isLoading };
};