All files / src/features/Merchants/MerchantSidePanel/Modals/MatchReport/hooks useMATCHReports.tsx

40.62% Statements 13/32
47.82% Branches 11/23
38.46% Functions 5/13
41.93% Lines 13/31

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                          28x 40x     40x           28x 202x     39x 19x                 202x     28x       1x     1x           1x     28x                                                                                                                                            
import { customInstance } from "@services/api";
import { useMutation, useQuery, useQueryClient } from "react-query";
import { showMessage } from "@common/Toast";
import { useUploadFiles } from "@hooks/upload-api/uploadHooks";
import { useCustomTheme } from "@theme/hooks/useCustomTheme";
import { MERCHANT_SIDE_PANEL_PREVIEW_API_KEYS } from "@features/Merchants/MerchantSidePanel/constants";
import {
  MATCHDataType,
  MATCHFormFields,
  ReportFile,
  ReportType,
} from "../types";
 
const getMATCHReports = (merchantID?: number, reportID?: number) => {
  const url = `/merchants/${merchantID}/match-reports${
    reportID !== undefined ? "/" + reportID : "?sort=-createdAt" //TODO: check sorting if needed
  }`;
  return customInstance({
    url: url,
    method: "GET",
  });
};
 
export const useGetMATCHReports = (merchantID?: number, enabled = true) => {
  const { data, error, refetch, isLoading } = useQuery(
    [MERCHANT_SIDE_PANEL_PREVIEW_API_KEYS.GET_MATCH, merchantID || ""],
    async () => {
      const reports = await getMATCHReports(merchantID);
      return reports?.data || [];
    },
    {
      enabled: enabled && Boolean(merchantID),
      refetchOnWindowFocus: false,
      refetchOnMount: false,
    },
  );
 
  return { error, isLoading, data: data as ReportType[], refetch };
};
 
export const useGetSingleMATCHReport = (
  merchantID: number,
  reportID?: number,
) => {
  const { data, error, isLoading } = useQuery(
    ["match-report", merchantID, reportID || ""],
    async () => {
      const report = await getMATCHReports(merchantID, reportID);
      return report || [];
    },
    { enabled: Boolean(merchantID) && Boolean(reportID), staleTime: Infinity }, //existing reports cannot be updated, so no point in refetching
  );
 
  return { error, isLoading, data: data as ReportType };
};
 
export const useCreateMATCHReport = ({
  merchantID,
  files,
}: {
  merchantID: number;
  files?: ReportFile[];
}) => {
  const { isDesktopView } = useCustomTheme();
  const { handleUpload: handleUploadEdit } = useUploadFiles();
  const queryClient = useQueryClient();
 
  const uploadFiles = async (
    merchantID: number,
    reportID: number,
    files: any,
  ) => {
    const uploadRes: string[] | "upload_failed" = await handleUploadEdit({
      list: files,
      attachmentType: "underwriting_match_report",
      merchantId: merchantID,
      resourceID: reportID,
      label: "underwriting match report",
      tag: "underwriting match report",
    });
 
    if (uploadRes === "upload_failed")
      showMessage("Error", "Evidence upload failed.", isDesktopView);
  };
 
  const onSuccessCallback = (data: ReportType) => {
    if (data && files) uploadFiles(merchantID, data.ID, files);
  };
 
  const updateMATCHCheck = useMutation(
    ({ data, merchantID }: MATCHDataType) => {
      return customInstance({
        url: `/merchants/${merchantID}/match-reports`,
        method: "POST",
        data,
      });
    },
    {
      onSuccess: (data: ReportType) => {
        onSuccessCallback(data);
      },
      onError: (error: any) => {
        showMessage(
          "Error",
          error?.message || error?.response?.status,
          true,
          "Something went wrong...",
        );
      },
      onSettled: () => {
        queryClient.invalidateQueries([
          MERCHANT_SIDE_PANEL_PREVIEW_API_KEYS.GET_MATCH,
          merchantID,
        ]);
      },
    },
  );
 
  const handleSubmit = ({ data }: { data: MATCHFormFields }) => {
    updateMATCHCheck.mutate({ data, merchantID });
  };
 
  return {
    handleSubmit,
  };
};