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 | 28x 28x 2671x 2671x 2671x 2671x 483x 2671x 2671x 2671x 2671x 2671x 2671x 2671x 2671x 2671x | import { MERCHANT_SIDE_PANEL_PREVIEW_API_KEYS } from "../../constants";
import { ModalMerchantData } from "../../UnderwritingTasks/types";
import { BP_INFO, showMissingInfoModal } from "../../UnderwritingTasks/utils";
import { TTaskStatus } from "../types";
import { useUpdateUnderwritingTask } from "./useUpdateTaskStatus";
import { useTaskMissingItems } from "./useTaskMissingItems";
import { useQueryClient, UseQueryResult } from "react-query";
import { showMessage } from "@common/Toast";
import { fetchAndCacheBAorBPData } from "./useFetchBAorBPData";
import { useEffect, useRef } from "react";
const errorMessageMap = {
suspended: `Your Business Profile has been suspended.
You won’t be able to continue until this is resolved.
`,
pending: BP_INFO.message,
};
export const useTaskActions = (
merchantID: number,
taskID: number,
taskStatus: TTaskStatus,
slug: string,
name: string,
merchantData?: ModalMerchantData,
requestAttention?: boolean,
baQuery?: UseQueryResult<any, unknown>,
bpQuery?: UseQueryResult<any, unknown>,
) => {
const { getMissingItems } = useTaskMissingItems(merchantID);
const queryClient = useQueryClient();
const taskStatusRef = useRef(taskStatus);
useEffect(() => {
taskStatusRef.current = taskStatus;
}, [taskStatus]);
const onCompletedSuccess = async () => {
const legalEntityID = merchantData?.businessProfile?.id;
const queriesKeyMap = {
"Primary Account Holder Identity": [
MERCHANT_SIDE_PANEL_PREVIEW_API_KEYS.GET,
merchantID,
],
"Bank Account": [
MERCHANT_SIDE_PANEL_PREVIEW_API_KEYS.GET_BANK_ACCOUNTS,
merchantID,
],
"Business Profile": [
MERCHANT_SIDE_PANEL_PREVIEW_API_KEYS.GET_LEGAL_ENTITY,
merchantID,
legalEntityID,
],
"Business Owner": [
MERCHANT_SIDE_PANEL_PREVIEW_API_KEYS.GET_LEGAL_ENTITY,
merchantID,
legalEntityID,
],
Agreement: [MERCHANT_SIDE_PANEL_PREVIEW_API_KEYS.GET, merchantID],
} as const;
//we need this fetch for ba or bp task, bc of rate limit refactor we dont fetch these datas if only the first panel is opened
const { bpData, baData } = await fetchAndCacheBAorBPData({
baQuery,
bpQuery,
merchantData,
});
const showMissingInfoModal =
(bpData && bpData?.principals?.length === 0) ||
(baData && baData?.data.length === 0);
const isMissingInfo =
merchantData &&
showMissingInfoModal &&
showMissingInfoModal({
challengeKey: slug,
challengeName: name,
merchantData,
});
if (isMissingInfo) return;
if (!name || !(name in queriesKeyMap)) return;
const key = name as keyof typeof queriesKeyMap;
queryClient.invalidateQueries(queriesKeyMap[key]);
};
const updateTaskStatus = useUpdateUnderwritingTask({
isCloseOnSuccess: true,
customErrorHandling: true,
onCompletedSuccess,
});
const handleUpdateTaskStatus = (
reason?: string,
status?: TTaskStatus,
requestAttention?: boolean,
) => {
if (taskStatusRef.current === status) return;
updateTaskStatus.mutate(
{
merchantID,
taskID,
data: {
// to update status and its reason
...(status && { status, reason }),
// to update request attention
...(typeof requestAttention === "boolean" && {
request_attention: requestAttention,
}),
},
},
{
onError: async (err: any) => {
const status = err?.response?.status;
const message = err?.response?.data?.message || "";
if ([400, 404].includes(status)) {
const missingFieldNames = await getMissingItems();
const MISSING_NAME_PAH = "Primary Account Holder";
const missingFieldName = name.includes(MISSING_NAME_PAH)
? MISSING_NAME_PAH
: name;
const match = message.toLowerCase().match(/suspended|pending/);
const customMessage =
(match &&
errorMessageMap[match[0] as keyof typeof errorMessageMap]) ||
null;
if (missingFieldNames.includes(missingFieldName)) {
return showMissingInfoModal({
challengeName: customMessage ? BP_INFO.title : name,
showNewMissingInfo: true,
customMessage,
});
}
}
showMessage("Error", err.message);
},
},
);
};
const handleComplete = async (reason?: string) => {
try {
handleUpdateTaskStatus(reason, "completed", false);
} catch (error) {
console.error("handleComplete error:", error);
}
};
const handleReadyForKYB = (reason?: string) => {
handleUpdateTaskStatus(reason, "open");
};
const handleReadyForUnderwriting = (reason?: string) => {
handleUpdateTaskStatus(reason, "ready_for_underwriting");
};
const handleInProgress = (reason?: string) => {
handleUpdateTaskStatus(reason, "in_progress");
};
const handleUpdateAttention = () => {
handleUpdateTaskStatus(undefined, undefined, !requestAttention);
};
return {
handleComplete,
handleReadyForKYB,
handleReadyForUnderwriting,
handleInProgress,
handleUpdateAttention,
};
};
|