All files / src/features/Merchants/MerchantSidePanel/ApprovalFlowPanel/components/AccountOverview ApprovalAccountOverview.tsx

88.57% Statements 31/35
73.68% Branches 42/57
75% Functions 3/4
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                                              21x               198x 198x 198x 198x       198x 198x                             198x     198x     198x       198x   198x       198x     198x     198x   198x 99x   99x                   99x                   99x                 198x   198x                                   198x     198x   198x                                                                                                                                                           21x       198x 198x 198x   198x   198x                                                        
import { useAppTheme } from "@theme/v2/Provider";
import GiveUnderwritingSidepanelTitle from "@features/Merchants/MerchantSidePanel/components/GiveUnderwritingSidepanelTitle";
import { useMerchantSidePanelContext } from "@features/Merchants/MerchantSidePanel/Provider/MerchantSidePanelProvider";
import { TChipColors } from "@shared/Chip/GiveChip";
import { CardContainer } from "features/Underwriting/UnderwritingCard/UnderwritingCard.style";
import StatusOverviewSections from "@features/Merchants/MerchantSidePanel/components/AccountStatusOverviewSection/components/StatusOverviewSections";
import { Box, Stack } from "@mui/material";
import AccountStatusDropdown from "@features/Merchants/MerchantSidePanel/components/AccountStatusOverviewSection/components/AccountStatusDropdown";
import UnderwritingCardBody from "@features/Underwriting/UnderwritingCard/UnderwritingCardBody";
import { useComponentVisibilityOnStatus } from "@features/Merchants/MerchantSidePanel/hooks/useComponentVisibilityOnStatus";
import GiveUnderwritingProgressBarV2 from "@features/Merchants/MerchantSidePanel/components/GiveUnderwritingProgressBarV2";
import { useGetUnderwritingTasksStats } from "@features/Merchants/MerchantSidePanel/ApprovalFlowPanel/hooks/useGetUnderwritingTasksStats";
import SponsorOverviewActions from "./SponsorOverviewActions";
import { checkPortals } from "@utils/routing";
import { useMemo } from "react";
import { useChangeAssignee } from "@shared/MerchantAssignmentManager/hooks/useChangeAssignee";
import { useEnterprisePermissions } from "@components/AcquirerEnterprises/CreateEnterprise/hooks/useEnterprisePermissions";
import { useCustomThemeV2 } from "@theme/hooks/useCustomThemeV2";
import { QueryBoundary } from "@features/Merchants/MerchantSidePanel/components/QueryBoundary";
import MerchantRiskTabs from "./MerchantRiskTabs";
import { MerchantRiskTabEnum } from "../../types";
import { isMidErrorStatus } from "@features/Merchants/MerchantSidePanel/utils/statusHelpers";
 
const ApprovalAccountOverview = () => {
  const {
    data,
    openUnderwritingView,
    queries,
    isRiskView,
    selectedRiskTab,
    isProvider,
  } = useMerchantSidePanelContext();
  const { merchant_underwriting } = useEnterprisePermissions();
  const { palette } = useAppTheme();
  const { handleChangeAssignee } = useChangeAssignee({
    merchantID: data?.status?.accID,
  });
  const isProviderApproved =
    isMidErrorStatus(data?.status?.statusName, data?.processor) && isProvider;
  const { status: mappedStatus } = useComponentVisibilityOnStatus({
    data: {
      ...data?.status,
      statusName: isProviderApproved ? "approved" : data?.status?.statusName,
      sponsorStatusName: isProviderApproved
        ? undefined
        : data?.status?.sponsorStatusName,
    },
    isRebranding: true,
    sponsorStatusName: isProviderApproved
      ? data?.status?.sponsorStatusName
      : undefined,
  });
 
  const isNotOverview =
    selectedRiskTab !== MerchantRiskTabEnum.OVERVIEW && isRiskView;
 
  const isUnderwriting =
    data?.status?.instant === "Pre Approved" ||
    data?.status?.instant === "Underwriting";
 
  const isReadyForReview = ["Ready for Review", "Pending"].includes(
    data?.status?.instant,
  );
 
  const status = isUnderwriting ? "Underwriting" : data?.status?.statusName;
 
  const { counts } = useGetUnderwritingTasksStats({
    merchantId: data?.merchantInfo?.merchantID,
  });
 
  const { isEnterprisePortal } = checkPortals();
 
  const isApprovedOrDeclinedOrSuspended =
    ["approved", "suspended", "declined", "closed"].includes(status) ||
    isProviderApproved;
 
  const isKYB = status === "ready_for_kyb";
 
  const assignee = useMemo(() => {
    const assignees = data?.status?.merchantAssignees;
 
    Iif (isKYB) {
      return {
        underwriterID: assignees?.kybAssigneeID,
        underwriterName: assignees?.kybAssigneeFullName,
        underwriterEmail: assignees?.kybAssigneeEmail,
        underwriterImageURL: assignees?.kybAssigneeImageURL,
        underwriterSetByAcquirer: assignees?.kybAssignedByAcquirer,
      };
    }
 
    Iif (isReadyForReview) {
      return {
        underwriterID: assignees?.sponsorAssigneeID,
        underwriterName: assignees?.sponsorAssigneeFullName,
        underwriterEmail: assignees?.sponsorAssigneeEmail,
        underwriterImageURL: assignees?.sponsorAssigneeImageURL,
        underwriterSetByAcquirer: assignees?.sponsorAssignedByAcquirer,
      };
    }
 
    return {
      underwriterID: assignees?.underwritingAssigneeID,
      underwriterName: assignees?.underwritingAssigneeFullName,
      underwriterEmail: assignees?.underwritingAssigneeEmail,
      underwriterImageURL: assignees?.underwritingAssigneeImageURL,
      underwriterSetByAcquirer: assignees?.underwritingAssignedByAcquirer,
    };
  }, [data?.status, isKYB, isReadyForReview]);
 
  const { accID } = data?.status || {};
 
  const handleChange = (
    oldAssignee: number | undefined,
    newAssignee: string,
    assigneeType?: string,
    onSuccess?: () => void,
  ) => {
    const type =
      assigneeType === "underwriting_admin" ? "underwriter" : assigneeType;
 
    handleChangeAssignee(
      oldAssignee,
      newAssignee,
      type as "underwriter" | "sponsor" | "kyb",
      onSuccess,
    );
  };
 
  const isUnderwritingNotAllowed =
    !merchant_underwriting && isEnterprisePortal && !isKYB;
 
  const needsAssignees =
    !isApprovedOrDeclinedOrSuspended && !isUnderwritingNotAllowed;
 
  return (
    <Stack spacing="20px">
      {isRiskView && (
        <CardContainer
          backgroundColor={palette.surface?.primary}
          sx={{
            "& > div": {
              paddingBottom: 0,
              borderBottom: "none",
            },
          }}
        >
          <TopOverview
            mappedStatus={mappedStatus}
            isApprovedOrDeclinedOrSuspended={isApprovedOrDeclinedOrSuspended}
          />
        </CardContainer>
      )}
      {isRiskView && <MerchantRiskTabs />}
      {!isNotOverview && !isRiskView && (
        <CardContainer backgroundColor={palette.surface?.primary}>
          {!isRiskView && (
            <TopOverview
              mappedStatus={mappedStatus}
              isApprovedOrDeclinedOrSuspended={isApprovedOrDeclinedOrSuspended}
            />
          )}
          {!isApprovedOrDeclinedOrSuspended && (
            <>
              <GiveUnderwritingProgressBarV2
                label={mappedStatus?.label}
                {...counts}
              />
              {needsAssignees && (
                <Box pt={2} pr={2} pb={2}>
                  <QueryBoundary
                    query={queries?.assigneesQuery}
                    errorTitle="Assignee information could not be loaded"
                  >
                    {isReadyForReview && !isEnterprisePortal && (
                      <SponsorOverviewActions
                        assignee={assignee}
                        handleChangeAssignee={handleChange}
                      />
                    )}
                    {!isReadyForReview && (
                      <UnderwritingCardBody
                        {...assignee}
                        id={accID}
                        handleManageUnderwriting={openUnderwritingView}
                        isOnboarding={false}
                        isKYB={isKYB}
                        handleChangeAssignee={handleChange}
                      />
                    )}
                  </QueryBoundary>
                </Box>
              )}
            </>
          )}
          {isApprovedOrDeclinedOrSuspended && (
            <StatusOverviewSections flexEndHistory />
          )}
        </CardContainer>
      )}
    </Stack>
  );
};
 
type TopOverviewProps = {
  mappedStatus: {
    label: string;
    color: string;
    bg_color: string;
  };
  isApprovedOrDeclinedOrSuspended: boolean;
};
 
const TopOverview = ({
  mappedStatus,
  isApprovedOrDeclinedOrSuspended,
}: TopOverviewProps) => {
  const { data, queries } = useMerchantSidePanelContext();
  const { isMobileView } = useCustomThemeV2();
  const { palette } = useAppTheme();
 
  const imageUrl = data?.header?.imageURL;
 
  return (
    <Box
      sx={{
        borderBottom: isApprovedOrDeclinedOrSuspended
          ? `1px solid ${palette.border?.primary}`
          : "none",
        pb: isMobileView ? 0 : 2,
      }}
    >
      <GiveUnderwritingSidepanelTitle
        title={data?.merchantInfo?.merchantName}
        imageUrl={imageUrl ? imageUrl + "/thumb" : undefined}
        id={data?.merchantInfo?.merchantID}
        businessData={data?.businessProfile}
        label={mappedStatus?.label}
        color={mappedStatus?.color as TChipColors}
        renderStatus={
          isApprovedOrDeclinedOrSuspended ? (
            <AccountStatusDropdown />
          ) : undefined
        }
        isLoading={queries?.statsOrEnterpriseQuery?.isLoading}
      />
    </Box>
  );
};
 
export default ApprovalAccountOverview;