All files / src/features/Merchants/MerchantSidePanel/components/RiskSection RiskSummarySection.tsx

93.75% Statements 30/32
82.5% Branches 33/40
75% Functions 3/4
96.66% Lines 29/30

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                                                                  22x           186x 186x 186x           186x   186x 186x             186x 186x   186x             186x             186x   186x   186x   186x 186x   186x 2x 2x                                         186x 186x       186x     186x               186x               186x               186x               186x                 186x                                                                                                                                                                                                 186x              
import { Grid, Stack } from "@mui/material";
import { styled } from "@theme/v2/Provider";
import type { TMerchantDataFields } from "../GiveMerchantSectionsContainer";
import RiskActivity from "./RiskActivity";
import GiveAssignButton from "@shared/AssignButton/GiveAssignButton";
import { useEnterprisePermissions } from "@components/AcquirerEnterprises/CreateEnterprise/hooks/useEnterprisePermissions";
import { useComponentVisibilityOnStatus } from "@features/Merchants/MerchantSidePanel/hooks/useComponentVisibilityOnStatus";
import { useCustomThemeV2 } from "@theme/hooks/useCustomThemeV2";
import RESOURCE_BASE, { OPERATIONS } from "@constants/permissions";
import {
  composePermission,
  useAccessControl,
} from "features/Permissions/AccessControl";
import RiskMonitorButton from "./components/RiskMonitorButton";
import { useChangeAssignee } from "@shared/MerchantAssignmentManager/hooks/useChangeAssignee";
import { useGetFeatureFlagValues } from "FeatureFlags/useGetFeatureFlagValues";
import { checkPortals } from "@utils/routing";
import RiskSummaryLabels from "./RiskSummaryLabels";
import { useMerchantSidePanelContext } from "@features/Merchants/MerchantSidePanel/Provider/MerchantSidePanelProvider";
import GiveText from "@shared/Text/GiveText";
import NiceModal from "@ebay/nice-modal-react";
import { SET_MANUAL_RISK_LEVEL_MODAL } from "modals/modal_names";
import { PencilSimpleIcon } from "@phosphor-icons/react";
import GiveButton from "@shared/Button/GiveButton";
import { useRiskProfile } from "@components/Merchants/MerchantPreview/RiskProfile/hooks/useRiskProfile";
import {
  TRiskLevel,
  TRiskStatus,
} from "@components/Merchants/MerchantPreview/RiskProfile/types";
import useUpdateManualRiskLevel from "@features/Merchants/MerchantSidePanel/hooks/useUpdateManualRiskLevel";
import { SET_MANUAL_RISK_LEVEL_ACTION_LABEL } from "./manualRiskLevel/constants";
import { getTaxIdFormat } from "@utils/index";
 
const RiskSummarySection = ({
  merchantId,
  riskProfile,
  status,
  id,
}: TMerchantDataFields) => {
  const { isMobileView } = useCustomThemeV2();
  const { isEnterprisePortal } = checkPortals();
  const { level, riskStatusName, profileID } = riskProfile || {};
  const {
    riskAssigneeID,
    riskAssigneeFullName,
    riskAssigneeImageURL,
    riskAssigneeEmail,
  } = status?.merchantAssignees || {};
  const { isNewApprovalFlowEnabled, isManualRiskLevelOverrideEnabled } =
    useGetFeatureFlagValues();
  const { isRiskView } = useMerchantSidePanelContext();
 
  const {
    underwritingAssigneeID,
    underwriterName,
    underwriterImageURL,
    underwriterEmail,
  } = status || {};
  const riskLabel = level || "low";
 
  const { data: fullRiskProfile } = useRiskProfile({
    profileId: profileID as number,
    merchantId,
    // Only fetch the full risk profile when the manual-override feature is on;
    // otherwise this would hit the endpoint on every side panel unnecessarily.
    enabled: isManualRiskLevelOverrideEnabled,
  });
  const { canOverride } = useUpdateManualRiskLevel(
    (profileID as number) || 0,
    merchantId,
  );
 
  // `riskLabel` (the legacy parsed-data level) is a plain `string`; cast the
  // fallback so these stay `TRiskLevel` for RiskSummaryLabels/the modal.
  const computedLevel = (fullRiskProfile?.riskLevel ||
    riskLabel) as TRiskLevel;
  const effectiveLevel = (fullRiskProfile?.effectiveRiskLevel ||
    riskLabel) as TRiskLevel;
  const isManualOverride = !!fullRiskProfile?.isManualOverride;
  const showManualOverride =
    isManualRiskLevelOverrideEnabled && isManualOverride;
  const currentManualLevel = fullRiskProfile?.manualRiskLevel ?? null;
 
  const openManualRiskLevelModal = () => {
    Iif (!profileID) return;
    NiceModal.show(SET_MANUAL_RISK_LEVEL_MODAL, {
      merchantId,
      profileId: profileID,
      computedLevel,
      currentManualLevel,
      riskLabel: effectiveLevel,
      currentStatusName:
        (riskStatusName as TRiskStatus) ||
        fullRiskProfile?.riskStatus ||
        "routine_monitoring",
      merchantData: {
        // `status` (TStatusReview) carries no merchant name/identifier fields;
        // the risk-profile endpoint (already fetched above for the effective
        // level) is the only source in this component's scope that has them.
        name: fullRiskProfile?.merchantName || "",
        identifier: getTaxIdFormat(fullRiskProfile?.merchantTaxID),
        imageURL: fullRiskProfile?.merchantImageURL,
      },
    });
  };
 
  const { risk_monitoring } = useEnterprisePermissions();
  const { handleChangeAssignee } = useChangeAssignee({
    merchantID: id,
  });
 
  const { isRiskSummaryVisible } = useComponentVisibilityOnStatus({
    data: status,
  });
  const canUpdateEscalation = useAccessControl({
    resource: composePermission(
      RESOURCE_BASE.MERCHANT,
      RESOURCE_BASE.RISK_PROFILE_ESCALATION,
    ),
    operation: OPERATIONS.UPDATE,
  });
 
  const canUpdateTrigger = useAccessControl({
    resource: composePermission(
      RESOURCE_BASE.MERCHANT,
      RESOURCE_BASE.RISK_PROFILE_FACTOR,
    ),
    operation: OPERATIONS.UPDATE,
  });
 
  const canReadTransactions = useAccessControl({
    resource: composePermission(
      RESOURCE_BASE.MERCHANT,
      RESOURCE_BASE.TRANSACTION,
    ),
    operation: OPERATIONS.READ,
  });
 
  const isRiskMonitorDisabled = [
    canReadTransactions,
    canUpdateTrigger,
    canUpdateEscalation,
    isRiskSummaryVisible,
    risk_monitoring,
  ].includes(false);
 
  const handleChange = (
    oldAssignee: number | undefined,
    newAssignee: string,
    assigneeType?: string,
    onSuccess?: () => void,
  ) => {
    handleChangeAssignee(oldAssignee, newAssignee, "risk", onSuccess);
  };
 
  return (
    <Stack spacing={2} data-testid="sidepanel-risk-summary-section">
      <Grid
        container
        alignItems="center"
        justifyContent="space-between"
        gap={isMobileView ? 2 : 0}
      >
        <GiveText>Risk Summary</GiveText>
        {isManualRiskLevelOverrideEnabled && canOverride && !!profileID && (
          <GiveButton
            variant="ghost"
            size="small"
            label={SET_MANUAL_RISK_LEVEL_ACTION_LABEL}
            startIcon={<PencilSimpleIcon size={16} />}
            onClick={openManualRiskLevelModal}
          />
        )}
      </Grid>
      <ContainerStack gap={3}>
        <Stack direction="row" alignItems="center" gap={1} width="100%">
          <GiveText
            variant="bodyS"
            color="secondary"
            sx={{ width: "50%", flexShrink: 0 }}
          >
            Risk Level
          </GiveText>
          {/* Column stack (align-items: stretch) so RiskSummaryLabels fills the
              full value-column width; its own flexWrap then keeps the chips on a
              single row when they fit (desktop) and wraps them (mobile). */}
          <Stack sx={{ width: "50%" }}>
            <RiskSummaryLabels
              effectiveLevel={effectiveLevel}
              computedLevel={computedLevel}
              isManualOverride={showManualOverride}
              manualSetByName={fullRiskProfile?.manualRiskLevelSetByName}
            />
          </Stack>
        </Stack>
 
        {!isRiskView && !isEnterprisePortal && (
          <Stack direction="row" alignItems="center" gap={1} width="100%">
            <GiveText
              variant="bodyS"
              color="secondary"
              sx={{ width: "50%", flexShrink: 0 }}
            >
              Assignee
            </GiveText>
            <Stack direction="row" alignItems="center" sx={{ width: "50%" }}>
              <GiveAssignButton
              title="Assign"
              type="risk"
              data={{
                accID: id,
                underwriterID: underwritingAssigneeID,
                underwriterName,
                underwriterImageURL,
                underwriterEmail,
                ...(isNewApprovalFlowEnabled && {
                  underwriterID: riskAssigneeID,
                  underwriterName: riskAssigneeFullName,
                  underwriterImageURL: riskAssigneeImageURL,
                  underwriterEmail: riskAssigneeEmail,
                }),
              }}
              onChange={isNewApprovalFlowEnabled ? handleChange : undefined}
              />
            </Stack>
          </Stack>
        )}
 
        <RiskActivity
          profileId={profileID}
          merchantId={merchantId}
          riskLabel={riskLabel}
        />
 
        {!isRiskView && (
          <Stack
            justifyContent="flex-end"
            alignItems="center"
            flexDirection="row"
            width="100%"
          >
            <RiskMonitorButton
              isDisabled={isRiskMonitorDisabled}
              tooltipSx={{ width: "fit-content", whiteSpace: "nowrap" }}
            />
          </Stack>
        )}
      </ContainerStack>
    </Stack>
  );
};
 
const ContainerStack = styled(Stack)(({ theme }) => ({
  padding: "20px",
  borderRadius: theme.spacing(2),
  backgroundColor: theme.palette?.surface?.primary,
}));
 
export default RiskSummarySection;