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

4.44% Statements 2/45
0% Branches 0/41
0% Functions 0/9
4.54% Lines 2/44

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                                                                                  23x                                                     23x                                                                                                                                                                                                                                                                                                                                                                                      
import { InputAdornment } from "@mui/material";
import {
  CheckCircleIcon,
  Icon,
  ProhibitIcon,
  WarningCircleIcon,
} from "@phosphor-icons/react";
import GiveSelect from "@shared/GiveInputs/GiveSelect";
import { useCustomThemeV2 } from "@theme/hooks/useCustomThemeV2";
import { useAppTheme } from "@theme/v2/Provider";
import { formatString } from "@utils/helpers";
import { capitalizeFirstLetter } from "@utils/index";
import { useEffect, useState } from "react";
import useFetchMerchantRisk from "../../hooks/useFetchMerchantRisk";
import useUpdateMerchant from "../../hooks/useUpdateMerchant";
import { useAccessControl } from "features/Permissions/AccessControl";
import RESOURCE_BASE, {
  ACTION_DENY_MESSAGE,
  OPERATIONS,
} from "@constants/permissions";
import { CustomToolTip as PermissionTooltip } from "componentsV2/Table/CustomTooltip";
import NiceModal, { useModal } from "@ebay/nice-modal-react";
import { GIVE_CONFIRMATION_POP_UP } from "modals/modal_names";
import { useEnterprisePermissions } from "@components/AcquirerEnterprises/CreateEnterprise/hooks/useEnterprisePermissions";
import { checkPortals } from "@utils/routing";
import { useMerchantSidePanelContext } from "../../Provider/MerchantSidePanelProvider";
import { useGetFeatureFlagValues } from "FeatureFlags/useGetFeatureFlagValues";
 
type RiskLevel = "normal" | "high" | "restricted";
 
type TSelectOption = {
  label: string;
  value: string;
  IconLeft: Icon;
  checkedIconType: "Check";
  isSelected: boolean;
};
 
const riskLevelConstants: Record<
  RiskLevel,
  { color: string | undefined; Icon: Icon }
> = {
  normal: {
    color: undefined,
    Icon: CheckCircleIcon,
  },
  high: {
    color: undefined,
    Icon: WarningCircleIcon,
  },
  restricted: {
    color: undefined,
    Icon: ProhibitIcon,
  },
};
 
interface Props {
  setLocalRiskValue: (val: string) => void;
  localRiskValue?: string;
  name?: string;
  category: number | null;
  categoryCodeID?: number;
  categoryCodeName: string;
  merchantRiskStatus: string;
  withAPI?: boolean;
  onSaveSuccess?: (val: any) => void;
}
 
const MerchantRiskCategoryInput = ({
  localRiskValue,
  setLocalRiskValue,
  name,
  categoryCodeID,
  category,
  categoryCodeName,
  merchantRiskStatus,
  withAPI = true,
  onSaveSuccess = () => null,
}: Props) => {
  const { palette } = useAppTheme();
  const { isMobileView } = useCustomThemeV2();
  const { remove } = useModal(GIVE_CONFIRMATION_POP_UP);
  const { updateMerchantMutation } = useUpdateMerchant();
  const { mutateAsync, isLoading: isUpdateLoading } = updateMerchantMutation;
  const { data: categoriesData, isLoading } = useFetchMerchantRisk(category);
  const { modify_merchant } = useEnterprisePermissions();
  const { isEnterprisePortal } = checkPortals();
  const [selectOptions, setSelectOptions] = useState<Array<TSelectOption>>([]);
  const { data: merchantData, isKYBTab } = useMerchantSidePanelContext();
  const { isNewApprovalFlowEnabled } = useGetFeatureFlagValues();
  const isKYB =
    isNewApprovalFlowEnabled &&
    merchantData?.status?.statusName === "ready_for_kyb";
  const isKYBSectionName = merchantData?.header?.onbSectionName === "kyb";
  const isDeclined = merchantData?.status?.statusName === "declined";
 
  const getRiskLevelColor = (riskLevel: RiskLevel): string | undefined => {
    switch (riskLevel) {
      case "normal":
        return palette?.primitive?.success?.[50];
      case "high":
        return palette?.filled?.orange;
      case "restricted":
        return palette?.primitive?.error?.[50];
      default:
        return undefined;
    }
  };
 
  const getInputValueColor = (label: string | undefined) => {
    const riskKey = (label?.toLowerCase() as RiskLevel) || "normal";
    return {
      color: getRiskLevelColor(riskKey),
      Icon: riskLevelConstants[riskKey].Icon,
    };
  };
 
  const Icon = getInputValueColor(merchantRiskStatus).Icon;
 
  const isDisabled =
    !categoryCodeID ||
    isLoading ||
    isUpdateLoading ||
    isKYB ||
    isKYBTab ||
    (isKYBSectionName && isDeclined);
 
  const hasEditMerchantPermission = useAccessControl({
    resource: RESOURCE_BASE.MERCHANT,
    operation: OPERATIONS.UPDATE,
  });
 
  const allowedEditMerchant = isEnterprisePortal
    ? hasEditMerchantPermission && modify_merchant
    : hasEditMerchantPermission;
 
  const handleSelectChange = (e: any) => {
    const value = e?.target?.value;
    if (value && value !== merchantRiskStatus) {
      NiceModal.show(GIVE_CONFIRMATION_POP_UP, {
        modalType: "change-mcc",
        title: "Change MCC",
        description: (
          <>
            The risk level for MCC{" "}
            <strong>
              {category} - {categoryCodeName}
            </strong>{" "}
            is currently set to{" "}
            <strong>{capitalizeFirstLetter(merchantRiskStatus)}</strong>. Are
            you sure you want to change it to{" "}
            <strong>{capitalizeFirstLetter(value)}</strong>? This change will
            apply only to this merchant.
          </>
        ),
        actions: {
          handleSuccess: {
            onClick: () => {
              setLocalRiskValue(value);
              if (withAPI)
                mutateAsync(
                  { merchantRiskStatus: `${value}` },
                  {
                    onSuccess: (data: any) => {
                      onSaveSuccess(data);
                    },
                  },
                );
              remove();
            },
          },
          handleCancel: {
            onClick: remove,
          },
        },
      });
    }
  };
  useEffect(() => {
    if (categoriesData?.data) {
      const newOptions = categoriesData.data.map((category: any) => ({
        label: capitalizeFirstLetter(category?.name),
        value: category?.name,
        IconLeft: getInputValueColor(category?.name).Icon,
        checkedIconType: "Check",
        isSelected:
          formatString(category?.name) ===
          formatString(merchantRiskStatus || ""),
      })) as Array<TSelectOption>;
      setSelectOptions(newOptions);
    }
  }, [categoriesData, merchantRiskStatus, categoryCodeID]);
 
  return (
    <PermissionTooltip
      showToolTip={!allowedEditMerchant}
      message={ACTION_DENY_MESSAGE}
      placement="top"
      alignment="end"
    >
      <GiveSelect
        name={name}
        disabled={isDisabled || !allowedEditMerchant}
        label="Merchant Risk Category"
        options={selectOptions}
        value={localRiskValue}
        inputValueColor={getInputValueColor(localRiskValue).color}
        useContextualMenu
        withIconOffset={false}
        onChange={handleSelectChange}
        {...(!isMobileView
          ? {
              contextualMenuProps: {
                color: "transparent",
                texture: "blurred",
                anchorOrigin: {
                  vertical: -20,
                  horizontal: "center",
                },
                transformOrigin: {
                  vertical: "bottom",
                  horizontal: "center",
                },
              },
            }
          : {})}
        InputProps={{
          sx: {
            padding: "0 0 0 14px !important",
            "&.Mui-disabled > div": {
              opacity: 0.5,
            },
            "& .MuiInputBase-input": {
              WebkitTextFillColor: getInputValueColor(
                localRiskValue || merchantRiskStatus,
              ).color,
            },
          },
          startAdornment: !merchantRiskStatus ? null : (
            <InputAdornment position="start">
              <Icon
                size={20}
                fill={
                  getInputValueColor(localRiskValue || merchantRiskStatus).color
                }
              />
            </InputAdornment>
          ),
        }}
      />
    </PermissionTooltip>
  );
};
 
export default MerchantRiskCategoryInput;