All files / src/features/Merchants/MerchantSidePanel/ApprovalFlowPanel/hooks useHandleUpdateMerchantStatusConversations.tsx

33.33% Statements 7/21
0% Branches 0/14
25% Functions 1/4
33.33% Lines 7/21

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              21x                                                                         21x 3648x     3648x 3648x 3648x                                                                                                             3648x    
import { showMessage } from "@common/Toast";
import { GENERAL_CONVERSATION_ERROR } from "@features/GiveConversation/constants";
import { fetchThread } from "@features/GiveConversation/hooks/useManageApi";
import { useOpenGiveConversation } from "@features/GiveConversation/hooks/useOpenGiveConversation";
import { useSendConversationMessage } from "@features/GiveConversation/hooks/useSendConversationMessage";
import { checkPortals } from "@utils/routing";
 
const generateConversationMessage = (
  statusName: string,
  comment: string,
  isProvider?: boolean,
) => {
  const merchantType = isProvider ? "Provider" : "Merchant";
  switch (statusName) {
    case "back_to_underwriting":
      return {
        threadName: "Move to Underwriting",
        comment: `I moved this ${merchantType?.toLocaleLowerCase()} back to Underwriting\nComment: ${comment}`,
      };
 
    case "back_to_kyb":
      return {
        threadName: "Move to KYB",
        comment: `I moved this ${merchantType?.toLocaleLowerCase()} back to KYB\nComment: ${comment}`,
      };
 
    case "declined":
      return {
        threadName: `Decline ${merchantType}`,
        comment: `I declined this ${merchantType?.toLocaleLowerCase()}\nComment: ${comment}`,
      };
    case "back_to_pending":
      return {
        threadName: "Move to Pending",
        comment: `I moved this merchant back to pending\nComment: ${comment}`,
      };
    default:
      return {
        threadName: "",
        comment: "",
      };
  }
};
 
export const useHandleUpdateMerchantStatusConversations = () => {
  const { handleSendMessage } = useSendConversationMessage({
    useOptimisticUpdate: false,
  });
  const { handleOpenConversation } = useOpenGiveConversation();
  const { isAcquirerEnterprises } = checkPortals();
  const handlePostMerchantStatusChangeConversation = async ({
    merchantId,
    statusName,
    comment,
  }: {
    merchantId: number;
    statusName: string;
    comment: string;
  }) => {
    try {
      const { threadName, comment: message } = generateConversationMessage(
        statusName,
        comment,
        isAcquirerEnterprises,
      );
      const thread = await fetchThread(merchantId, threadName);
      const threadId = thread?.data?.[0]?.id;
 
      handleSendMessage(
        {
          body: message,
        },
        {
          merchantId,
          threadId,
          threadData: threadId
            ? undefined
            : {
                subjectAccID: merchantId,
                title: threadName,
                isInternal: true,
              },
          onSuccess: (data: any) => {
            handleOpenConversation({
              merchantData: {
                id: merchantId,
              },
              // if it's existing thread, we use threadID, otherwise we use id
              initialThreadID: data?.threadID || data?.id,
              threadName,
              initialTab: "team",
            });
          },
        },
      );
    } catch (error: any) {
      showMessage(
        "Error",
        error?.response?.data?.message ||
          error?.message ||
          GENERAL_CONVERSATION_ERROR,
      );
    }
  };
 
  return { handlePostMerchantStatusChangeConversation };
};