All files / src/shared/GiveFilter/hooks useGetFilterSectionPerPage.tsx

100% Statements 50/50
95.83% Branches 69/72
100% Functions 3/3
100% Lines 49/49

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 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610                                                                                                                  29x                         1094x             1094x     1094x                                           1094x     1094x           1094x               1094x                     1094x   1094x   1094x   1094x   1094x   1094x   1094x 125x 13x 26x             1094x     1094x   1094x             1094x   1094x             1094x 1094x           1094x                   1094x                   1094x               1094x             1094x                   1094x             1094x               1094x                         1094x                     1094x                         1094x                             1094x                         1094x                                       1094x                                           1094x               1094x                       1094x               1094x               1094x               1094x               1094x               1094x                         1094x                                                                                                                                                                                                                                                                                                                                                                                 1094x        
import {
  FilterPagesEnum,
  FilterPageValuesTYpes,
  endpointNames,
  paymentMethod,
  outFlow,
  inFlow,
  watchlistOnly,
  riskLevelChips,
  providerStatusChips,
  processedVolume,
  approvedMerchantsChips,
  transactionsCountChips,
  getTransactionDateChips,
  amountTransfersRange,
  amountRange,
  feeRange,
  merchantStatusChips,
  riskStatusChips,
  disputeAmountsRange,
  useGetDisputeStatus,
  emailStatus,
  cardInfoType,
  minimalRiskLevelChips,
  transactionMinimalStatus,
  eventTicketStatus,
  eventTicketReason,
  eventTicketPickup,
  eventTransactionStatus,
} from "../constants";
import useGetEventTicketTypes from "./useGetEventTicketTypes";
import { CardPropsTypes as CardProps } from "@pages/ManageMoney/Modal/filter.types";
import { CustomFilterType } from "@pages/ManageMoney/types";
import useGetMerchantSelect from "./useGetMerchantSelect";
import { checkPortals } from "@utils/routing";
import useGetProviderSelect from "./useGetProviderSelect";
import {
  ArrowCircleUpRightIcon,
  CheckCircleIcon,
  WarningDiamondIcon,
} from "@phosphor-icons/react";
import useGetMCCSelect from "./useGetMCCSelect";
import { AvatarShapes } from "@shared/Avatar/GiveAvatar.types";
import CreditCardCheck from "@assets/icons/CreditCardCheck";
import { useEnterprisePermissions } from "@components/AcquirerEnterprises/CreateEnterprise/hooks/useEnterprisePermissions";
import { getFilteredTransactionStatuses } from "@features/TransactionPanel/utils";
import { useGetFilterChips } from "./useGetFilterChips";
import useGetTransferStatus from "./useGetTransferStatus";
import { PLATFORM_TIMEZONE } from "@utils/timezones";
import { useGetFeatureFlagValues } from "FeatureFlags/useGetFeatureFlagValues";
import { useGetDisputeCaseTypesSelect } from "./useGetDisputeCaseTypesSelect";
import { useGetToggleFilters } from "./useGetToggleFilters";
import { useGetCurrentMerchantId } from "@hooks/common";
import { useGetProcessors } from "@hooks/acquirer-api/merchants/useGetProcessors";
import { useMemo } from "react";
import { processorMap } from "@features/Merchants/MerchantSidePanel/constants";
 
const useGetFilterSectionPerPage = ({
  filterPage,
  productId,
}: {
  filterPage: FilterPageValuesTYpes;
  /**
   * The entity a page-scoped filter belongs to — currently only the event whose
   * tickets the EVENT_TICKETS panel filters, whose Ticket Name chips are that
   * event's own tiers. Threaded from the Filter button through the modal.
   */
  productId?: string;
}) => {
  //user Data
  const { isMerchantProcessorEnabled } = useGetFeatureFlagValues();
  const {
    isMerchantPortal,
    isEnterprisePortal,
    isAcquirerPortal,
    isAcquirerEnterprises,
    isTransactions,
  } = checkPortals();
 
  const isGetProviderCallEnabled =
    [
      FilterPagesEnum.MERCHANT_SPONSOR_TAB,
      FilterPagesEnum.MERCHANT_KYB_TAB,
      FilterPagesEnum.MERCHANT_UNDERWRITING_TAB,
      FilterPagesEnum.MERCHANT_APPROVED_TAB,
      FilterPagesEnum.MERCHANT_PENDING_TAB,
      FilterPagesEnum.MERCHANT_RISK_TAB,
      FilterPagesEnum.MERCHANT_ALL_TAB,
      FilterPagesEnum.ACQUIRER_PROCESSING,
      FilterPagesEnum.ACQUIRER_MANAGE_MONEY,
    ].includes(filterPage) ||
    (isAcquirerPortal &&
      [FilterPagesEnum.MANAGE_MONEY, FilterPagesEnum.TRANSFERS].includes(
        filterPage,
      ));
 
  const {
    merchantOptions,
    merchantSearchBarProps,
    merchantClearSearch,
    merchantOnEndReached,
    merchantIsLoading,
  } = useGetMerchantSelect({ filterPage });
 
  const { assignSection, merchantTransfers, providerTransfers } =
    useGetToggleFilters({ filterPage });
 
  const {
    disputeCaseTypeClearSearch,
    disputeCaseTypeSearchBarProps,
    disputeCaseTypesOptions,
  } = useGetDisputeCaseTypesSelect();
 
  const {
    providerOptions,
    providerSearchBarProps,
    providerClearSearch,
    providerOnEndReached,
    providerIsLoading,
  } = useGetProviderSelect({
    enabled: isGetProviderCallEnabled,
    filterPage,
  });
 
  const {
    mccOptions,
    mccSearchBarProps,
    mccClearSearch,
    mccOnEndReached,
    mccIsLoading,
  } = useGetMCCSelect();
 
  const { businessClassificationTypeChips } = useGetFilterChips();
 
  const { risk_monitoring } = useEnterprisePermissions();
 
  const { isSponsor, merchantId } = useGetCurrentMerchantId();
  const showProcessor =
    isMerchantProcessorEnabled && isAcquirerPortal && !isSponsor;
 
  const { data } = useGetProcessors(merchantId, showProcessor);
 
  const processorOptionChips = useMemo(() => {
    if (!data) return [];
    return data.map((processorItem) => {
      return {
        label: processorMap[processorItem.name],
        value: processorItem.name,
      };
    });
  }, [data]);
 
  const isRiskLevelHidden = isEnterprisePortal && !risk_monitoring;
 
  const isAcquirerProcessing =
    FilterPagesEnum.ACQUIRER_PROCESSING === filterPage;
 
  const { transferStatuses } = useGetTransferStatus({
    isAcquirerProcessing,
    isTransactions,
    isMerchantPortal,
    isTransfers: filterPage === FilterPagesEnum.TRANSFERS,
  });
 
  const disputeStatus = useGetDisputeStatus();
 
  const transactionChips = getFilteredTransactionStatuses({
    isMerchantPortal,
    isEnterprisePortal,
    isAcquirerPortal,
    isAcquirerProcessing,
    isTransactions,
  });
  const isPendingTab = filterPage === FilterPagesEnum.MERCHANT_PENDING_TAB;
  const transactionStatus: CardProps = {
    title: "Transaction Status",
    chips: transactionChips,
    selected: [],
    apiKeyName: endpointNames.transactionStatus,
  };
  const transferStatus: CardProps = {
    title: "Transfer Status",
    chips: transferStatuses as CardProps["chips"],
    selected: [],
    apiKeyName: endpointNames.transferStatus,
  };
 
  // All date filter chips use PLATFORM_TIMEZONE (America/New_York) so the
  // Today / Last 7 Days / Last 30 Days presets match the ET filter window
  // applied by the API across every page.
  const transactionDate: CardProps = {
    title: "Transaction Date",
    chips: getTransactionDateChips(PLATFORM_TIMEZONE),
    selected: "",
    apiKeyName: endpointNames.date,
    type: CustomFilterType.dateRangeInTimezone,
  };
 
  // Event Tickets tab (frame 7397-90376). Same createdAt window as every other
  // date section — on event_ticket_holders_view createdAt IS the purchase date.
  const ticketPurchaseDate: CardProps = {
    title: "Purchase Date",
    chips: getTransactionDateChips(PLATFORM_TIMEZONE),
    selected: "",
    apiKeyName: endpointNames.date,
    type: CustomFilterType.dateRangeInTimezone,
  };
 
  const { ticketTypeChips, ticketTypesLoading } = useGetEventTicketTypes({
    productId,
    enabled:
      filterPage === FilterPagesEnum.EVENT_TICKETS ||
      filterPage === FilterPagesEnum.EVENT_TRANSACTIONS,
  });
 
  const ticketName: CardProps = {
    title: "Ticket Name",
    chips: ticketTypeChips,
    selected: [],
    apiKeyName: endpointNames.ticketType,
    isLoading: ticketTypesLoading,
  };
 
  // Same chips on the Transactions tab (frame 7397-80180), but that list is
  // transaction-items, where the tier is the product variant.
  const transactionTicketName: CardProps = {
    ...ticketName,
    apiKeyName: endpointNames.eventTxnTicketType,
  };
 
  // Independent second date window on transaction list pages: filters by
  // settledAt. Combines with the Transaction Date (createdAt) window via AND.
  const settlementDate: CardProps = {
    title: "Settlement Date",
    chips: getTransactionDateChips(PLATFORM_TIMEZONE),
    selected: "",
    apiKeyName: endpointNames.settledAt,
    type: CustomFilterType.dateRangeInTimezone,
  };
 
  const merchantName: CardProps = {
    title: "Merchant Name",
    selected: [],
    apiKeyName: endpointNames.merchantName,
    contextualMenuProps: {
      options: merchantOptions,
      searchBarProps: merchantSearchBarProps,
      clearSearch: merchantClearSearch,
      onEndReached: merchantOnEndReached,
      isLoading: merchantIsLoading,
    },
  };
 
  const disputeCaseTypes: CardProps = {
    title: "Case Type",
    selected: [],
    apiKeyName: endpointNames.disputeCaseType,
    contextualMenuProps: {
      options: disputeCaseTypesOptions,
      searchBarProps: disputeCaseTypeSearchBarProps,
      clearSearch: disputeCaseTypeClearSearch,
    },
  };
 
  const providerName: CardProps = {
    title: "Provider Name",
    selected: [],
    apiKeyName: endpointNames.providerName,
    contextualMenuProps: {
      options: providerOptions,
      searchBarProps: providerSearchBarProps,
      clearSearch: providerClearSearch,
      onEndReached: providerOnEndReached,
      isLoading: providerIsLoading,
    },
  };
 
  const enterprisetName: CardProps = {
    title: "Provider Name",
    selected: [],
    apiKeyName: endpointNames.providerName,
    contextualMenuProps: {
      options: providerOptions,
      searchBarProps: providerSearchBarProps,
      clearSearch: providerClearSearch,
      onEndReached: providerOnEndReached,
      isLoading: providerIsLoading,
      iconShape: "square" as AvatarShapes,
      checkedIcon: "Plus",
    },
  };
 
  const mccSection: CardProps = {
    title: "MCC",
    selected: [],
    apiKeyName: endpointNames.categoryCode,
    contextualMenuProps: {
      options: mccOptions,
      searchBarProps: mccSearchBarProps,
      clearSearch: mccClearSearch,
      onEndReached: mccOnEndReached,
      isLoading: mccIsLoading,
    },
  };
 
  const operationalSection: CardProps = {
    title: "Operational Status",
    apiKeyName: endpointNames.operationalSection,
    switches: [
      {
        title: "Able to transfer",
        name: "canTransferMoney",
        description: "See merchants that can transfer funds",
        icon: <CreditCardCheck width={20} height={20} />,
      },
 
      {
        title: "Able to process",
        name: "canProcessMoney",
        description: "See merchants that accept payments",
        icon: <ArrowCircleUpRightIcon size={20} />,
      },
    ],
  };
 
  const flagsSection: CardProps = {
    title: "Flags",
    apiKeyName: endpointNames.flagSection,
    useToggle: true,
    switches: [
      {
        title: "Suspected Transactions",
        name: "isBlocked",
        description:
          "Transactions that have been flagged and stopped due to suspicious activity or violation of policies",
        icon: <WarningDiamondIcon size={20} />,
      },
 
      {
        title: "False Positive Transactions",
        name: "isFalsePositive",
        description: "A false positive transaction is a legitimate transaction",
        icon: <CheckCircleIcon size={20} />,
      },
    ],
  };
 
  const creationDate: CardProps = {
    title: "Creation Date",
    chips: getTransactionDateChips(PLATFORM_TIMEZONE),
    selected: "",
    apiKeyName: endpointNames.date,
    type: CustomFilterType.dateRange,
  };
 
  const signUpDate: CardProps = {
    title: isAcquirerEnterprises
      ? "Date"
      : isPendingTab
      ? "Started Date"
      : "Sign-up Date",
    chips: getTransactionDateChips(PLATFORM_TIMEZONE),
    selected: "",
    apiKeyName: endpointNames.date,
    type: CustomFilterType.dateRange,
  };
 
  const approvalDate: CardProps = {
    title: "Approved Date",
    chips: getTransactionDateChips(PLATFORM_TIMEZONE),
    selected: "",
    apiKeyName: endpointNames.approvedDate,
    type: CustomFilterType.dateRange,
  };
 
  const readyDate: CardProps = {
    title: "Ready Date",
    chips: getTransactionDateChips(PLATFORM_TIMEZONE),
    selected: "",
    apiKeyName: endpointNames.approvalAt,
    type: CustomFilterType.dateRange,
  };
 
  const lastEscalationDate: CardProps = {
    title: "Last Escalation Date",
    chips: getTransactionDateChips(PLATFORM_TIMEZONE),
    selected: "",
    apiKeyName: endpointNames.lastEscalationAt,
    type: CustomFilterType.dateRange,
  };
 
  const lastRiskActivityDate: CardProps = {
    title: "Last Risk Activity",
    chips: getTransactionDateChips(PLATFORM_TIMEZONE),
    selected: "",
    apiKeyName: endpointNames.lastRiskActivityAt,
    type: CustomFilterType.dateRange,
  };
 
  const processorChips: CardProps = {
    title: "Processor",
    chips: processorOptionChips,
    selected: "",
    apiKeyName: endpointNames.processor,
  };
 
  //TODO clean this up when FF will be removed
  const commonFilters = [
    transactionDate,
    settlementDate,
    ...(isAcquirerPortal ? [providerName, merchantName] : []),
    ...(isEnterprisePortal ? [merchantName] : []),
    transactionStatus,
    transferStatus,
    inFlow,
    outFlow,
    paymentMethod,
    ...(showProcessor ? [processorChips] : []),
  ];
 
  const filterSectionPerPage = {
    [FilterPagesEnum.MANAGE_MONEY]: commonFilters,
    [FilterPagesEnum.ACQUIRER_MANAGE_MONEY]: commonFilters,
    [FilterPagesEnum.ACQUIRER_PROCESSING]: [
      transactionDate,
      settlementDate,
      providerName,
      merchantName,
      transactionStatus,
      transferStatus,
      amountRange,
      feeRange,
      paymentMethod,
      ...(showProcessor ? [processorChips] : []),
    ],
    [FilterPagesEnum.PROVIDER_PROCESSING]: [
      transactionDate,
      settlementDate,
      merchantName,
      transactionStatus,
      transferStatus,
      amountRange,
      feeRange,
      paymentMethod,
    ],
    [FilterPagesEnum.MERCHANT_ALL_TAB]: [
      watchlistOnly,
      ...(!isEnterprisePortal ? [enterprisetName] : []),
      processedVolume,
      merchantStatusChips({ hideClosed: isEnterprisePortal }),
      ...(showProcessor ? [processorChips] : []),
      mccSection,
      creationDate,
    ],
    [FilterPagesEnum.MERCHANT_KYB_TAB]: [
      watchlistOnly,
      ...(!isEnterprisePortal ? [enterprisetName] : []),
      ...(!isEnterprisePortal ? [assignSection] : []),
      ...(showProcessor ? [processorChips] : []),
      mccSection,
      signUpDate,
    ],
    [FilterPagesEnum.MERCHANT_UNDERWRITING_TAB]: [
      watchlistOnly,
      ...(!isEnterprisePortal ? [enterprisetName] : []),
      ...(!isEnterprisePortal ? [assignSection] : []),
      ...(showProcessor ? [processorChips] : []),
      mccSection,
      signUpDate,
    ],
    [FilterPagesEnum.MERCHANT_APPROVED_TAB]: [
      watchlistOnly,
      ...(!isEnterprisePortal ? [enterprisetName] : []),
      ...(!isRiskLevelHidden
        ? [
            riskLevelChips({
              extraChips: [{ label: "Low", value: "low" }],
            }),
          ]
        : []),
      processedVolume,
      ...(showProcessor ? [processorChips] : []),
      mccSection,
      operationalSection,
      approvalDate,
    ],
    [FilterPagesEnum.MERCHANT_PENDING_TAB]: [
      watchlistOnly,
      ...(!isEnterprisePortal ? [enterprisetName] : []),
      ...(showProcessor ? [processorChips] : []),
      mccSection,
      signUpDate,
    ],
    [FilterPagesEnum.MERCHANT_SPONSOR_TAB]: [
      watchlistOnly,
      ...(!isEnterprisePortal ? [enterprisetName] : []),
      assignSection,
      ...(showProcessor ? [processorChips] : []),
      mccSection,
      readyDate,
    ],
    [FilterPagesEnum.MERCHANT_RISK_TAB]: [
      watchlistOnly,
      ...(!isEnterprisePortal ? [enterprisetName, assignSection] : []),
      ...(!isRiskLevelHidden
        ? [
            riskLevelChips(),
            riskStatusChips,
            lastEscalationDate,
            lastRiskActivityDate,
          ]
        : []),
      operationalSection,
      ...(showProcessor ? [processorChips] : []),
      mccSection,
      approvalDate,
    ],
    [FilterPagesEnum.PROVIDER_ALL_TAB]: [
      watchlistOnly,
      providerStatusChips,
      businessClassificationTypeChips,
      processedVolume,
      transactionsCountChips,
      approvedMerchantsChips,
      creationDate,
    ],
    [FilterPagesEnum.PROVIDER_UNDERWRITING_TAB]: [
      watchlistOnly,
      assignSection,
      businessClassificationTypeChips,
      signUpDate,
    ],
    [FilterPagesEnum.PROVIDER_KYB_TAB]: [
      watchlistOnly,
      assignSection,
      businessClassificationTypeChips,
      signUpDate,
    ],
    [FilterPagesEnum.ACQUIRER_DISPUTES]: [
      {
        ...transactionDate,
        title: "Date",
      },
      assignSection,
      merchantName,
      ...(showProcessor ? [processorChips] : []),
      ...disputeStatus,
      disputeCaseTypes,
      disputeAmountsRange,
    ],
    [FilterPagesEnum.PROVIDER_APPROVED_TAB]: [
      watchlistOnly,
      businessClassificationTypeChips,
      processedVolume,
      transactionsCountChips,
      approvedMerchantsChips,
      approvalDate,
    ],
    [FilterPagesEnum.TRANSFERS]: [
      {
        ...transactionDate,
        title: "Filter by Date",
      },
      ...(isAcquirerPortal
        ? [
            {
              ...providerName,
              additionalToggles: [providerTransfers, merchantTransfers],
            },
            merchantName,
          ]
        : [merchantName]),
      transferStatus,
      amountTransfersRange,
    ],
    [FilterPagesEnum.MERCHANT_RISK_PANEL]: [
      transactionDate,
      minimalRiskLevelChips,
      transactionMinimalStatus,
      flagsSection,
      emailStatus,
      inFlow,
      outFlow,
      cardInfoType,
    ],
    // Frames 7397-90376 and 7594-66313, in the mockups' order. Ticket Name is
    // dropped when the event has sold nothing, rather than showing an empty card.
    [FilterPagesEnum.EVENT_TICKETS]: [
      ticketPurchaseDate,
      eventTicketStatus,
      eventTicketReason,
      ...(ticketTypeChips.length || ticketTypesLoading ? [ticketName] : []),
      eventTicketPickup,
    ],
    // Frame 7397-80180. Transaction Date is the shared createdAt card, which
    // already carries this title.
    [FilterPagesEnum.EVENT_TRANSACTIONS]: [
      transactionDate,
      eventTransactionStatus,
      ...(ticketTypeChips.length || ticketTypesLoading
        ? [transactionTicketName]
        : []),
    ],
  };
 
  return filterSectionPerPage[filterPage];
};
 
export default useGetFilterSectionPerPage;