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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 | 21x 21x 21x 3648x 3648x 3648x 3648x 3648x 3648x 3648x 3648x 3648x 3648x 3648x 3648x 3648x 3648x 3648x 3648x 3648x | import { ReasonType } from "@features/Merchants/MerchantSidePanel/Modals/DeclineMerchantModal";
import { showMessage } from "@common/Toast";
import { useUpdateUnderwriting } from "@components/Merchants/MerchantPreview/hooks/useUnderwritingUpdate";
import { QKEY_LIST_ACQUIRER_MERCHANTS, QKEY_LIST_ENTERPRISE_STATS } from "@constants/queryKeys";
import {
getGlobalTopic,
useConversationsModal,
} from "features/Minibuilders/Conversations/hooks/useConversationsModal";
import { useMutation, useQueryClient } from "react-query";
import { SubmitHandler } from "react-hook-form";
import { useAppSelector } from "@redux/hooks";
import { TThreadInfo } from "@features/Minibuilders/Conversations/types";
import { createThread } from "@components/Merchants/MerchantPreview/hooks/useRiskMarkAllAsOk";
import { selectUser } from "@redux/slices/auth/auth";
import { useRefetchCounters } from "@hooks/acquirer-api/merchants/stats/useGetMerchantCounters";
import { useKYBUpdate } from "./useKYBUpdate";
import { usePendingUpdate } from "./usePendingUpdate";
import { useSponsorUpdate } from "@components/Merchants/MerchantPreview/hooks/useSponsorUpdate";
import { MERCHANT_SIDE_PANEL_PREVIEW_API_KEYS } from "@features/Merchants/MerchantSidePanel/constants";
import { useGetCurrentMerchantId } from "@hooks/common";
import { checkPortals } from "@utils/routing";
import { useConversationsInformations } from "@features/Merchants/MerchantSidePanel/WithRepository/Challenges/hooks/useConversationsInformations";
import { useHandleUpdateMerchantStatusConversations } from "./useHandleUpdateMerchantStatusConversations";
const typeDisplayName = {
kyb: "KYB",
underwriting: "Underwriting",
sponsor: "Sponsor",
pending: "Pending",
};
const messages = {
declined: {
defaultMessage: "I declined this merchant",
errorMessage: "Merchant account can not be declined",
},
back_to_kyb: {
defaultMessage: "I moved this merchant back to KYB",
errorMessage: "Merchant account can not be moved back to KYB",
},
back_to_underwriting: {
defaultMessage: "I moved this merchant back to Underwriting",
errorMessage: "Merchant account can not be moved back to Underwriting",
},
back_to_pending: {
defaultMessage: "I moved this merchant back to pending",
errorMessage: "Merchant account cannot be moved back to pending",
},
};
type Props = {
merchantId: number;
merchantName?: string;
merchantImageURL?: string;
type: "underwriting" | "sponsor" | "kyb" | "pending";
status:
| "declined"
| "back_to_kyb"
| "back_to_underwriting"
| "back_to_pending";
};
const useUpdateMerchantStatusWithReason = ({
merchantId,
type,
status,
}: Props) => {
const queryClient = useQueryClient();
const { openConversationHandler } = useConversationsModal();
const { id: globalMerchantId } = useAppSelector(selectUser);
const { merchantId: enterpriseID } = useGetCurrentMerchantId();
const { isEnterprisePortal, isAcquirerEnterprises } = checkPortals();
const usedID = isEnterprisePortal ? enterpriseID : globalMerchantId;
const conversationMutation = useMutation(
async (data: any) => await createThread(usedID, data),
);
const { fetchGlobalTopic } = useConversationsInformations({
merchantID: merchantId,
});
const { updateUnderwriting } = useUpdateUnderwriting(merchantId);
const { updateKYBStatus } = useKYBUpdate(merchantId);
const { updatePendingStatus } = usePendingUpdate(merchantId);
const { updateSponsorStatus } = useSponsorUpdate(merchantId);
const { handleRefetchCounters } = useRefetchCounters();
const { handlePostMerchantStatusChangeConversation } =
useHandleUpdateMerchantStatusConversations();
const handleOpenConversation = async (
message: string,
merchantId: number,
) => {
const result = await fetchGlobalTopic(type);
if (!result) return;
const { topicID } = result;
if (!topicID) return;
await conversationMutation.mutateAsync(
{
TopicDisplayName: typeDisplayName[type],
subjectAccID: merchantId,
topicID: topicID,
message: {
body: message,
mentions: [],
},
},
{
onSuccess: async (data: TThreadInfo) => {
queryClient.invalidateQueries({
queryKey: ["get-conversation-topics", merchantId],
});
queryClient.invalidateQueries([
"get-conversation-messages",
data.subjectAccID,
]);
},
},
);
};
const onStatusChange: (
handleClose?: () => void,
isNewConversationFlow?: boolean,
) => SubmitHandler<ReasonType> =
(handleClose, isNewConversationFlow) => async (data) => {
let mutate = updateUnderwriting.mutate;
if (type === "kyb") mutate = updateKYBStatus.mutate;
if (type === "sponsor") mutate = updateSponsorStatus.mutate;
if (type === "pending") mutate = updatePendingStatus.mutate;
const customData = {
status,
reason: data.reason,
};
mutate(customData, {
onSuccess: async () => {
handleRefetchCounters();
if (isAcquirerEnterprises) {
queryClient.invalidateQueries(QKEY_LIST_ENTERPRISE_STATS);
} else {
queryClient.invalidateQueries(QKEY_LIST_ACQUIRER_MERCHANTS);
}
queryClient.invalidateQueries([
MERCHANT_SIDE_PANEL_PREVIEW_API_KEYS.GET,
merchantId,
]);
handleClose?.();
if (isNewConversationFlow) {
handlePostMerchantStatusChangeConversation({
merchantId,
statusName: status,
comment: data.reason,
});
} else {
if (type !== "pending") {
await handleOpenConversation(
`${messages[status].defaultMessage}\nReason: ${data.reason}`,
merchantId,
);
}
const { data: topic } = await getGlobalTopic({
topicName: type,
});
const internalTopic = topic?.find(
(item: any) =>
[item?.name, item?.Name]?.includes(type) &&
[item?.typeName, item?.Type]?.includes("internal"),
);
openConversationHandler({
id: internalTopic,
name: typeDisplayName[type],
paths: [],
});
}
},
onError: () => {
showMessage("Error", messages[status].errorMessage);
},
});
};
return {
isLoading:
updateKYBStatus.isLoading ||
updateSponsorStatus.isLoading ||
updateUnderwriting.isLoading,
onStatusChange,
};
};
export default useUpdateMerchantStatusWithReason;
|