All files / src/features/Notifications/NotificationsCenter/hooks useUpdateSponsorTable.ts

78.94% Statements 15/19
62.5% Branches 5/8
100% Functions 4/4
76.47% Lines 13/17

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                  25x         403x 403x           403x   403x   403x 111x   52x 52x 472x   52x         52x 52x                  
import { useEffect } from "react";
import { InfiniteData, useQueryClient } from "react-query";
import { useGetCurrentMerchantId } from "@hooks/common";
import {
  QKEY_LIST_ACQUIRER_MERCHANTS,
  QKEY_LIST_MERCHANT_STATS,
} from "@constants/queryKeys";
import { useRefetchCounters } from "@hooks/acquirer-api/merchants/stats/useGetMerchantCounters";
 
export const useUpdateSponsorTable = ({
  unreadNotifications,
}: {
  unreadNotifications?: InfiniteData<any>;
}) => {
  const { handleRefetchCounters } = useRefetchCounters();
  const bulkStatusUpdateNotificationNames = [
    "bulk_sponsor_approve",
    "error_bulk_sponsor_decline",
    "error_bulk_sponsor_approve",
    "bulk_sponsor_decline",
  ];
  const queryClient = useQueryClient();
 
  const { merchantId } = useGetCurrentMerchantId();
 
  useEffect(() => {
    if (!unreadNotifications) return;
    const list =
      unreadNotifications?.pages?.map((row: any) => row.data).flat() ?? [];
    const bulkUpdateNotificationsExists = list.some((item) =>
      bulkStatusUpdateNotificationNames.includes(item.alertName),
    );
    const cachedQueryData: any = queryClient
      .getQueryCache()
      .find(["get-last-background-task", merchantId])?.state?.data;
 
    const shouldRefetchMerchants =
      cachedQueryData?.total > 0 && bulkUpdateNotificationsExists;
    Iif (shouldRefetchMerchants) {
      // merchants and stats are refetched when FE receives notifications of bulk status update
      queryClient.invalidateQueries(QKEY_LIST_ACQUIRER_MERCHANTS);
      queryClient.removeQueries("get-last-background-task");
      queryClient.invalidateQueries(QKEY_LIST_MERCHANT_STATS);
      handleRefetchCounters();
    }
  }, [unreadNotifications]);
};