All files / src/features/Merchants/MerchantSidePanel/components/UnderwriterRiskAnalystAssignCard UnderwriterRiskAnalystAssignCard.tsx

86.48% Statements 32/37
75.75% Branches 50/66
60% Functions 6/10
88.57% Lines 31/35

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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273                                                                    247x   247x 247x 247x 247x 247x 247x     247x 247x     247x     247x           247x 55x                     247x     247x                 247x     247x       247x   247x       247x                 247x 247x                 247x 48x                                                   247x 48x                                                             247x                                                                                                                                             48x                   21x                       21x 48x            
import { Box, Stack } from "@mui/material";
import GiveText from "@shared/Text/GiveText";
import GiveAssignButton from "@shared/AssignButton/GiveAssignButton";
import SectionCardBase from "@shared/SidePanel/components/SectionCard/SectionCardBase";
import GiveDivider from "@shared/GiveDivider/GiveDivider";
 
import { styled } from "@theme/v2/Provider";
import { useMerchantSidePanelContext } from "../../Provider/MerchantSidePanelProvider";
import MCCInput from "./MCCInput";
import MerchantRiskCategoryInput from "./MerchantRiskCategoryInput";
import { useEnterprisePermissions } from "@components/AcquirerEnterprises/CreateEnterprise/hooks/useEnterprisePermissions";
import { checkPortals } from "@utils/routing";
import { useConversationsModal } from "features/Minibuilders/Conversations/hooks/useConversationsModal";
import GiveLink from "@shared/Link/GiveLink";
import { useCustomThemeV2 } from "@theme/hooks/useCustomThemeV2";
import { useEffect, useState } from "react";
import useAssigneeManagedByAcquirer from "@hooks/useAssigneeManagedByAcquirer";
import { useQueryClient } from "react-query";
import { QKEY_LIST_ACQUIRER_MERCHANTS } from "@constants/queryKeys";
import { SHOW_RISK_CATEGORY } from "../../constants";
import ProcessorInput from "./ProcessorInput";
import { useGetFeatureFlagValues } from "FeatureFlags/useGetFeatureFlagValues";
import { useChangeAssignee } from "@shared/MerchantAssignmentManager/hooks/useChangeAssignee";
 
interface Props {
  type?: "Underwriter" | "Risk Analyst" | "Assignee";
  hideAssignee?: boolean;
}
 
function UnderwriterRiskAnalystAssignCard({
  type = "Underwriter",
  hideAssignee = false,
}: Props) {
  const { isMerchantProcessorEnabled, isNewApprovalFlowEnabled } =
    useGetFeatureFlagValues();
  const { data, id, isOnboardingView, isProvider } =
    useMerchantSidePanelContext();
  const { merchant_underwriting } = useEnterprisePermissions();
  const { isEnterprisePortal } = checkPortals();
  const { openConversationHandler } = useConversationsModal();
  const { isMobileView } = useCustomThemeV2();
  const [localRiskValue, setLocalRiskValue] = useState(
    data?.merchantInfo?.merchantRiskStatus,
  );
  const queryClient = useQueryClient();
  const { handleChangeAssignee } = useChangeAssignee({
    merchantID: id,
  });
  const isOnboardingMerchant = data?.status?.statusName === "onboarding";
 
  const assigneesType =
    isOnboardingView || isOnboardingMerchant
      ? "sales"
      : type === "Risk Analyst"
      ? "risk"
      : "underwriting_admin";
 
  useEffect(() => {
    Iif (localRiskValue !== data?.merchantInfo?.merchantRiskStatus) {
      setLocalRiskValue(data?.merchantInfo?.merchantRiskStatus);
    }
  }, [data?.merchantInfo?.merchantRiskStatus]);
 
  const {
    underwriterName,
    underwriterImageURL,
    underwriterEmail,
    assignee,
    underwriterSetByAcquirer,
  } = data?.status || {};
 
  const { isManagedByAcquirer: hideAssignButton } =
    useAssigneeManagedByAcquirer({
      underwriterSetByAcquirer,
    });
 
  const {
    riskAssigneeID,
    riskAssigneeFullName,
    riskAssigneeImageURL,
    riskAssigneeEmail,
  } = data?.status?.merchantAssignees || {};
 
  const isHide =
    (!merchant_underwriting && isEnterprisePortal) ||
    hideAssignButton ||
    hideAssignee;
 
  const tag = underwriterName || underwriterEmail;
 
  const refetchTableData = (data: any) => {
    queryClient.invalidateQueries(QKEY_LIST_ACQUIRER_MERCHANTS);
  };
 
  const handleMessage = () => {
    openConversationHandler({
      id,
      name: `General`,
      paths: [],
      defaultMessage: `@[__${tag}__](user:__${tag}__) `,
    });
  };
 
  const isRisk = assigneesType === "risk";
  const handleChangeRiskAnalyst = (
    oldAssignee: number | undefined,
    newAssignee: string,
    assigneeType?: any,
    onSuccess?: () => void,
  ) => {
    handleChangeAssignee(oldAssignee, newAssignee, assigneeType, onSuccess);
  };
 
  const AssignButton = () => (
    <GiveAssignButton
      title="Assign"
      type={assigneesType}
      isOnlyName
      data={{
        accID: id,
        underwriterName,
        underwriterImageURL,
        underwriterEmail,
        underwriterID: assignee || undefined,
        underwriterSetByAcquirer,
        ...(isNewApprovalFlowEnabled &&
          isRisk && {
            underwriterID: riskAssigneeID,
            underwriterName: riskAssigneeFullName,
            underwriterImageURL: riskAssigneeImageURL,
            underwriterEmail: riskAssigneeEmail,
          }),
      }}
      customContextualMenuPaperStyle={{
        mt: "-48px",
      }}
      onChange={isRisk ? handleChangeRiskAnalyst : undefined}
    />
  );
 
  const EmailAndMessageSection = () => {
    return (
      <Box display="flex">
        {!isMobileView && !hideAssignButton && <Box width="50%" />}
        <Stack
          width={isMobileView || hideAssignButton ? "auto" : "50%"}
          spacing={1}
          mt={hideAssignButton && !isMobileView ? 0 : 1}
        >
          {underwriterName && isMobileView && !hideAssignButton && (
            <AssignButtonTextContainer>
              <AssignButton />
            </AssignButtonTextContainer>
          )}
          <CustomText variant="bodyS" color="secondary">
            {underwriterEmail}
          </CustomText>
          {assignee && (
            <GiveLink
              component="button"
              color="secondary"
              onClick={handleMessage}
              sx={{ fontSize: 16 }}
            >
              Message
            </GiveLink>
          )}
        </Stack>
      </Box>
    );
  };
 
  return (
    <SectionCardBase>
      {!isHide && (
        <>
          <Box
            display="flex"
            flexDirection={isMobileView && hideAssignButton ? "column" : "row"}
          >
            <GiveText
              color="secondary"
              variant="bodyS"
              flexGrow={1}
              width={isMobileView ? "auto" : "50%"}
            >
              {type}
            </GiveText>
            {hideAssignButton ? (
              <EmailAndMessageSection />
            ) : (
              <AssignButtonAvatarContainer>
                <AssignButton />
              </AssignButtonAvatarContainer>
            )}
          </Box>
          {!hideAssignButton && <EmailAndMessageSection />}
          <GiveDivider />
        </>
      )}
      <Box
        mt={isHide ? "0" : "16px"}
        display="flex"
        flexDirection="column"
        gap="16px"
      >
        {isMerchantProcessorEnabled && !isProvider && !isEnterprisePortal && (
          <ProcessorInput
            processorValue={data?.merchantInfo?.processor}
            disabled
            isApproved={["approved", "suspended"].includes(
              data?.status?.statusName || "",
            )}
          />
        )}
        <MCCInput
          setLocalRiskValue={setLocalRiskValue}
          enterpriseID={data.merchantInfo.enterpriseID}
          categoryCodeID={data.merchantInfo.categoryCodeID}
          categoryCodeName={data.merchantInfo.categoryCodeName}
          code={data.merchantInfo.category}
          riskStatus={data?.merchantInfo.merchantRiskStatus}
          onConfirmChangeSuccess={refetchTableData}
          processor={data?.merchantInfo.processor}
        />
        {SHOW_RISK_CATEGORY && (
          <MerchantRiskCategoryInput
            localRiskValue={localRiskValue}
            setLocalRiskValue={setLocalRiskValue}
            categoryCodeID={data.merchantInfo.categoryCodeID}
            category={data.merchantInfo.category}
            categoryCodeName={data.merchantInfo.categoryCodeName}
            merchantRiskStatus={data.merchantInfo.merchantRiskStatus}
            onSaveSuccess={refetchTableData}
          />
        )}
      </Box>
    </SectionCardBase>
  );
}
 
export default UnderwriterRiskAnalystAssignCard;
 
const AssignButtonAvatarContainer = styled(Box)(({ theme }) => ({
  width: "50%",
  [theme.breakpoints.down("v2_sm")]: {
    width: "auto",
    "& p, & .caret-down-icon": {
      display: "none",
    },
  },
}));
 
const AssignButtonTextContainer = styled(Box)(() => ({
  "& .MuiAvatar-root": {
    display: "none",
  },
  "& p": {
    marginLeft: 0,
  },
  "& .caret-down-icon": {
    visibility: "visible",
  },
}));
 
const CustomText = styled(GiveText)(() => {
  return {
    wordBreak: "break-word",
    whiteSpace: "normal",
    overflowWrap: "break-word",
  };
});