All files / src/features/Campaigns/CampaignDetails/hooks useGetCampaignById.ts

21.73% Statements 5/23
0% Branches 0/18
0% Functions 0/5
21.73% Lines 5/23

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                        2x 2x 2x 2x   2x                                                                                                                                                
import { customInstance } from "@services/api";
import {
  getTransactionChart,
  transactionsChartNormalizer,
} from "@services/api/analytics/transactions";
import { buildMerchantEndpoints } from "@services/api/utils.api";
import moment from "moment";
import { useMemo } from "react";
import { useQuery } from "react-query";
import { CampaignData } from "../types";
import { showMessage } from "@common/Toast";
 
const today = new Date();
const startDate = moment(today).subtract(1, "months").format("YYYY-MM-DD");
const endDate = moment(today).format("YYYY-MM-DD");
const filter = `start_date=${startDate}&end_date=${endDate}`;
 
export const useGetCampaignById = ({
  id,
  campaign,
  enabled,
}: {
  id?: number;
  campaign: string;
  enabled?: boolean;
}) => {
  const { data: Data, ...rest } = useQuery(
    ["get-campaign", id, campaign],
    async () => {
      const dataPromise = customInstance({
        url: buildMerchantEndpoints(`products/${id}`),
      });
 
      const statsPromise = id
        ? getTransactionChart({ productId: id, filter })
        : undefined;
 
      return await Promise.allSettled([dataPromise, statsPromise])
        .then((results) => {
          const dataResponse = results[0];
          const statsResponse = results[1];
 
          const data =
            dataResponse.status === "fulfilled" ? dataResponse.value : null;
          //error handling for product
          if (
            dataResponse.status === "rejected" &&
            !dataResponse?.reason?.not_authorized
          ) {
            showMessage(
              "Error",
              dataResponse?.reason?.response?.data?.message ||
                "Something went wrong",
            );
          }
 
          const stats =
            statsResponse?.status === "fulfilled"
              ? statsResponse.value.data
              : null;
          //error handling for the transacton data
          if (
            statsResponse.status === "rejected" &&
            !statsResponse?.reason?.not_authorized
          ) {
            showMessage(
              "Error",
              statsResponse?.reason?.response?.data?.message ||
                "Something went wrong",
            );
          }
          return { data, stats };
        })
        .catch(() => {
          showMessage("Error", "Something went wrong");
        });
    },
    {
      enabled,
      refetchOnWindowFocus: false,
    },
  );
  const data: CampaignData = Data?.data;
  const stats = useMemo(
    () => transactionsChartNormalizer(false, Data?.stats),
    [Data?.stats],
  );
  return { data, stats, ...rest };
};