All files / src/components/Merchants/MerchantPreview/hooks useRiskMarkAllAsOk.ts

68.42% Statements 26/38
50% Branches 4/8
37.5% Functions 3/8
70.27% Lines 26/37

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                                    24x                                       24x           2x 2x 2x 2x 2x 2x         2x   2x   2x       2x                     2x 2x   2x                                                                             2x 1x       1x         1x         1x         1x       1x 1x   1x     1x           2x      
import { useMutation, useQueryClient } from "react-query";
import { customInstance } from "@services/api";
import { useAppSelector } from "@redux/hooks";
import { selectUser } from "@redux/slices/auth/auth";
import { format } from "date-fns";
import { TThreadInfo } from "features/Minibuilders/Conversations/types";
import { useConversationsModal } from "features/Minibuilders/Conversations/hooks/useConversationsModal";
import useGetGlobalTopic from "features/Minibuilders/Conversations/hooks/useGetGlobalTopic";
import { checkPortals } from "@utils/routing";
import { useRefetchCounters } from "@hooks/acquirer-api/merchants/stats/useGetMerchantCounters";
import { MERCHANT_SIDE_PANEL_PREVIEW_API_KEYS } from "@features/Merchants/MerchantSidePanel/constants";
import {
  QKEY_GET_ACTIVE_RISK_ACTIVITIES,
  QKEY_LIST_ACQUIRER_MERCHANTS,
} from "@constants/queryKeys";
import { selectMerchantTab } from "@redux/slices/enterprise/merchants";
import { useGetFeatureFlagValues } from "FeatureFlags/useGetFeatureFlagValues";
 
export const createThread = async (globalMerchantId: number, data: any) => {
  return await customInstance({
    url: `/merchants/${globalMerchantId}/threads`,
    method: "POST",
    data,
  });
};
 
interface Props {
  onCancel: () => void;
  escalationIds: number[];
  merchantId: number;
  profileId: number;
}
 
type SaveValuesType = {
  reason: string;
  description: string;
};
 
const useRiskMarkAllAsOk = ({
  merchantId,
  profileId,
  onCancel,
  escalationIds,
}: Props) => {
  const { isAcquirerPortal } = checkPortals();
  const { id: globalMerchantId } = useAppSelector(selectUser);
  const tab = useAppSelector(selectMerchantTab);
  const queryClient = useQueryClient();
  const { isNewConversationsEnabled } = useGetFeatureFlagValues();
  const { topic } = useGetGlobalTopic(
    "risk_activity",
    undefined,
    !isNewConversationsEnabled,
  );
  const { handleRefetchCounters } = useRefetchCounters();
 
  const globalTopicId = topic ? topic.ID : null;
 
  const createNewConversationThread = useMutation(
    async (data: any) => await createThread(globalMerchantId, data),
  );
 
  const markAllAsOkMutation = useMutation(
    (data: { reason: string; notes: string }) => {
      return customInstance({
        url: `/merchants/${merchantId}/risk/merchant-profiles/${profileId}/escalations/set-all-as-ok`,
        method: "PATCH",
        data,
      });
    },
  );
 
  const isLoading =
    createNewConversationThread.isLoading || markAllAsOkMutation.isLoading;
  const { openConversationHandler } = useConversationsModal();
 
  const handleThreadCreation = async ({
    reason,
    description,
  }: SaveValuesType) => {
    if (!globalTopicId) return;
    const now = new Date();
    const formattedDate = format(now, "MM/dd/yyyy 'at' HH:mm");
 
    await createNewConversationThread.mutateAsync(
      {
        subjectAccID: merchantId,
        TopicDisplayName: "Risk Activity",
        message: {
          body: `I marked all triggers as OK on ${formattedDate}\nReason: ${reason}\nComment: ${description}`,
          mentions: [],
        },
        topicID: globalTopicId,
        escalationIDs: escalationIds,
      },
      {
        onSuccess: async (data: TThreadInfo) => {
          openConversationHandler({
            id: data.topicID,
            name: "Risk Activity",
            paths: [],
            forceTopicOpen: true,
          });
          queryClient.invalidateQueries({
            queryKey: ["get-conversation-topics", merchantId],
          });
          queryClient.invalidateQueries([
            "get-conversation-messages",
            data.topicID,
          ]);
        },
      },
    );
  };
 
  const handleSave = (data: SaveValuesType) => {
    markAllAsOkMutation.mutate(
      { reason: data.reason, notes: data.description },
      {
        onSuccess: async () => {
          queryClient.invalidateQueries([
            MERCHANT_SIDE_PANEL_PREVIEW_API_KEYS.RISK_ACTIVITIES,
            profileId,
            merchantId,
          ]);
          queryClient.invalidateQueries([
            QKEY_GET_ACTIVE_RISK_ACTIVITIES,
            profileId,
            merchantId,
          ]);
          queryClient.invalidateQueries([
            MERCHANT_SIDE_PANEL_PREVIEW_API_KEYS.RISK_PROFILE,
            profileId,
            merchantId,
          ]);
          queryClient.invalidateQueries([
            MERCHANT_SIDE_PANEL_PREVIEW_API_KEYS.GET,
            merchantId,
          ]);
          queryClient.invalidateQueries([QKEY_LIST_ACQUIRER_MERCHANTS, tab]);
          handleRefetchCounters();
          // conversation is allowed only in acquirer portal
          Iif (isAcquirerPortal) {
            await handleThreadCreation(data);
          }
          onCancel();
        },
      },
    );
  };
 
  return { handleSave, isLoading };
};
 
export default useRiskMarkAllAsOk;