All files / src/features/Merchants/MerchantSidePanel/Modals CloseAccountModal.tsx

50% Statements 8/16
0% Branches 0/4
50% Functions 2/4
57.14% Lines 8/14

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                          1x   3x 3x     3x 3x   3x                                                                           3x                                 2x                        
import NiceModal from "@ebay/nice-modal-react";
import CommentModalBase, { FormValues } from "./CommentModalBase";
import GiveText from "@shared/Text/GiveText";
import { useCloseAccount } from "../hooks/useCloseAccount";
import { useSendConversationMessage } from "@features/GiveConversation/hooks/useSendConversationMessage";
import { useOpenGiveConversation } from "@features/GiveConversation/hooks/useOpenGiveConversation";
import { useGetFeatureFlagValues } from "FeatureFlags/useGetFeatureFlagValues";
 
type CloseAccountProps = {
  merchantId: number;
  name: string;
};
 
const CloseAccountModal = NiceModal.create(
  ({ merchantId, name }: CloseAccountProps) => {
    const { handleClose } = useCloseAccount({ merchantId });
    const { handleSendMessage } = useSendConversationMessage({
      useOptimisticUpdate: false,
    });
    const { handleOpenConversation } = useOpenGiveConversation();
    const { isNewConversationsEnabled } = useGetFeatureFlagValues();
 
    const handleSave = async (data: FormValues) => {
      // Close the account first
      const isClosed = await handleClose({ reason: data.comment, note: "" });
 
      // If closing the account failed, don't open a conversation
      if (!isClosed) return;
 
      // New conversations flow: create a thread + open it
      if (!isNewConversationsEnabled) return;
 
      try {
        await handleSendMessage(
          {
            body: `I closed this merchant account\n**Reason:** ${data.comment}`,
          },
          {
            merchantId,
            threadData: {
              subjectAccID: merchantId,
              title: "Closed Account",
              isInternal: true,
            },
            onSuccess: (thread: any) => {
              handleOpenConversation({
                merchantData: {
                  id: merchantId,
                },
                threadName: "Closed Account",
                initialThreadID: thread.id,
                initialTab: "team",
              });
            },
          },
        );
      } catch {
        // Don't block account closing if conversation creation fails
      }
    };
    return (
      <CommentModalBase
        title="Close Account"
        description={<Description name={name} />}
        onConfirm={handleSave}
        confirmButtonLabel="Close Merchant"
        commentInputLabel="Reason"
        confirmButtonColor="destructive"
        maxLength={1000}
      />
    );
  },
);
 
export default CloseAccountModal;
 
function Description({ name }: { name: string }) {
  return (
    <GiveText variant="bodyS" color="secondary">
      Are you sure you want to close{" "}
      <GiveText component="span" variant="bodyS" color="primary">
        {name}
      </GiveText>{" "}
      account? Closing the account will stop the merchant from processing
      transactions or sending money transfers. All members of the merchant will
      lose access to this account.
    </GiveText>
  );
}