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 | 1x | import { useSettlementFiltersRepository } from "@components/VirtualList/hooks";
import { useGetCurrentMerchantId } from "@hooks/common";
import { useAppSelector } from "@redux/hooks";
import { selectSelectedMerchant } from "@redux/slices/merchantFilters";
import { customInstance } from "@services/api";
import { useState } from "react";
import { generateSettlementFileName } from "@features/Settlements/hooks/useExportSettlementMerchants";
import { showMessage } from "@common/Toast";
import moment from "moment";
import { QKEY_GET_SETTLEMENT_MERCHANTS } from "@constants/queryKeys";
import { useGetFeatureFlagValues } from "FeatureFlags/useGetFeatureFlagValues";
import { ProcessorValue } from "@features/Merchants/MerchantSidePanel/types";
export const useSettlementExportInfoTable = () => {
const { settlementDate, filterString, filterStringFBO } =
useSettlementFiltersRepository();
const { isMerchantProcessorEnabled } = useGetFeatureFlagValues();
const { merchantId: currentId } = useGetCurrentMerchantId();
const selectedMerchant = useAppSelector((state) =>
selectSelectedMerchant(state, QKEY_GET_SETTLEMENT_MERCHANTS),
);
const [isLoading, setIsLoading] = useState<boolean>(false);
const showExportFailed = () => {
showMessage("Error", "Please try again later", true, "Export failed");
};
const performDownload = async (
endpoint: string,
merchantId: number | undefined,
filter: string | undefined,
): Promise<void> => {
let url = `/merchants/${merchantId}/${endpoint}`;
if (filter) url += filter;
const data = await customInstance({ method: "GET", url });
const blob = new Blob([data]);
const _url = window.URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = _url;
link.setAttribute("download", generateSettlementFileName("csv", true));
document.body.appendChild(link);
link.click();
if (document.body.contains(link)) document.body.removeChild(link);
window.URL.revokeObjectURL(_url);
};
const downloadReport = async (
reportType: "default" | "fbo",
processor?: ProcessorValue,
) => {
setIsLoading(true);
const merchantId = selectedMerchant?.accID || currentId;
const endpoint =
reportType === "fbo"
? "fbo-settlements.csv"
: "transaction-settlements.csv";
const processorQueryString =
processor && isMerchantProcessorEnabled
? `processorName=${processor}`
: "";
const filter = reportType === "fbo" ? filterStringFBO : filterString;
const usedEndpoint = processorQueryString
? `${endpoint}?${processorQueryString}`
: endpoint;
const formattedFilter = usedEndpoint.includes("?")
? `${filter.replace("?", "&")}`
: filter;
try {
await performDownload(usedEndpoint, merchantId, formattedFilter);
} catch (error: any) {
const is404Error =
error?.response?.status === 404 &&
error?.response?.data?.message?.includes("FBO daily settlement");
// Special handling for FBO 404 errors
if (is404Error && reportType === "fbo") {
const now = moment().tz("Atlantic/Canary");
const currentHour = now.hour();
const currentMinute = now.minute();
const settlementMoment = moment(settlementDate);
const isLatestDateSelected = settlementMoment.isSame(
now.clone().subtract(1, "day"),
"day",
);
// Maintenance window: 11:00 – 13:01 LP time
const isMaintenance =
isLatestDateSelected &&
(currentHour === 11 ||
currentHour === 12 ||
(currentHour === 13 && currentMinute <= 1));
if (isMaintenance) {
showMessage(
"Info",
"FBO reports are being generated. Please try again after 13:01 WET.",
false,
"Maintenance Window",
);
} else {
showMessage(
"Info",
"FBO report is not available for this date. Please try a different date.",
false,
"Report Not Available",
);
}
} else {
showExportFailed();
}
} finally {
setIsLoading(false);
}
};
const downloadDefaultReport = (processor?: ProcessorValue) =>
downloadReport("default", processor);
const downloadFBOReport = () => downloadReport("fbo");
return {
downloadDefaultReport,
downloadFBOReport,
isLoading,
settlementDate,
};
};
|