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 | import * as React from "react";
// @mui
import { useTheme } from "@mui/material/styles";
import useMediaQuery from "@mui/material/useMediaQuery";
// components
import { showMessage } from "@common/Toast/ShowToast";
// assets
import NiceModal, { muiDialogV5, useModal } from "@ebay/nice-modal-react";
import Popup from "@common/Popup";
import { useRefund } from "@services/api/products/transactions";
import { useQueryClient } from "react-query";
import {
QKEY_DONATION_TRANSACTIONS,
QKEY_EVENT_TRANSACTIONS,
QKEY_GET_TRANSACTION_STATS,
QKEY_INVOICE_TRANSACTIONS,
QKEY_MEMBERSHIP_TRANSACTIONS,
QKEY_PROVIDER_PROCESSING,
QKEY_SWEEPSTAKE_TRANSACTIONS,
} from "@constants/queryKeys";
import { checkPortals } from "@utils/routing";
import { processTransactionData } from "../TransactionInfoModal/utils";
import { TRANSACTION_INFO_MODAL } from "modals/modal_names";
import { CampaignType } from "@common/Campaigns/hooks/useReportMenuActions";
type RefundProps = {
purchase?: boolean;
ids: {
transactionIDs?: string[];
transactionItemIDs?: string[];
};
productData?: any;
sourceAccount?: any;
isFirst?: boolean;
isLast?: boolean;
setSelectedRow?: React.Dispatch<any>;
};
export const fakeSourceAccountData = {
fullName: " FULL name",
email: "Email@mail.com",
card: {
brand: "Amex",
numberLast4: 1234,
},
};
const Refund = NiceModal.create(
({
ids,
purchase,
sourceAccount = fakeSourceAccountData,
isFirst,
isLast,
setSelectedRow,
productData,
}: RefundProps) => {
const modal = useModal();
const theme = useTheme();
const isDesktop = useMediaQuery(theme.breakpoints.up("sm"));
const {
currentPortal,
isEvents,
isFundraisers,
isInvoice,
isMembership,
isSweepStakes,
} = checkPortals();
const message = purchase
? "purchase have been successfully refunded"
: "All purchases have been successfully refunded";
const { mutateAsync, isLoading } = useRefund();
const client = useQueryClient();
const refetcher = (keys: (string | any[])[]) =>
keys.forEach((k) => client.invalidateQueries(k));
const handleSend = async () => {
await mutateAsync(ids, {
onSuccess: (transactionData) => {
showMessage("Success", message, isDesktop);
const parsedData = transactionData?.data?.[0]
? processTransactionData(transactionData?.data?.[0])
: null;
if (currentPortal === "acquirer") {
refetcher(queriesToRefetch.acquirer);
} else if (currentPortal === "provider") {
refetcher(queriesToRefetch.enterprise);
} else if (isFundraisers) {
refetcher(queriesToRefetch.fundraisers);
} else if (isEvents) {
refetcher(queriesToRefetch.events);
} else if (isInvoice) {
refetcher(queriesToRefetch.invoices);
} else if (isMembership) {
refetcher(queriesToRefetch.memberships);
} else if (isSweepStakes) {
refetcher(queriesToRefetch.sweepstakes);
} else {
refetcher(queriesToRefetch.merchant);
}
// refetch transactions and stats for all payment forms
const transactionKey = `get-events-transactions-by-id ${
productData?.productId
} ${
productData?.productType === "standard"
? "product"
: productData?.productType
}`;
const statsKey = [`specific-product-stats`, productData?.productId];
refetcher([transactionKey, statsKey]);
if (parsedData?.displayStatus === "Refunded") {
NiceModal.show(TRANSACTION_INFO_MODAL, {
data: {
...parsedData,
objID: parsedData?.id,
transactionID: parsedData?.id,
},
isFirst: setSelectedRow ? isFirst : true,
isLast: setSelectedRow ? isLast : true,
setSelectedRow,
});
} else {
client.invalidateQueries([
"get-transaction-history",
parsedData?.id,
]);
}
modal.hide();
},
}).catch((err: any) => {
modal.hide();
showMessage(
"Error",
"Only purchases can be refunded or reversed",
isDesktop,
"",
false,
);
});
};
return (
<Popup
{...muiDialogV5(modal)}
title={purchase ? "Refund Purchase" : "Refund Transaction"}
onCancel={() => modal.hide()}
onSubmit={handleSend}
actionLabel="Issue Refund"
isLoading={isLoading}
isSubmitDisabled={isLoading}
data-testid="refund-modal"
>
{`Please Confirm This Refund to ${sourceAccount?.firstName?.trim()} ${sourceAccount?.lastName?.trim()}`}
{sourceAccount?.email && `, ${sourceAccount?.email}`}
{sourceAccount?.card?.brand &&
`, ${sourceAccount?.card.brand?.toUpperCase()}`}
{sourceAccount?.card?.numberLast4 &&
`, ${sourceAccount?.card?.numberLast4}`}
</Popup>
);
},
);
export default Refund;
const queriesToRefetch = {
fundraisers: ["get-fundraiser-by-id", QKEY_DONATION_TRANSACTIONS],
events: ["get-event-by-id", QKEY_EVENT_TRANSACTIONS],
invoices: ["get-invoice-by-id", QKEY_INVOICE_TRANSACTIONS],
memberships: ["get-membership-by-id", QKEY_MEMBERSHIP_TRANSACTIONS],
sweepstakes: ["get-sweepstake-by-id", QKEY_SWEEPSTAKE_TRANSACTIONS],
acquirer: [
QKEY_GET_TRANSACTION_STATS,
"get-all-transactions",
"acquirer-processing-transactions",
],
enterprise: [
QKEY_GET_TRANSACTION_STATS,
"get-all-transactions",
QKEY_PROVIDER_PROCESSING,
],
merchant: [
"get-all-transactions",
"manage-money-transactions",
QKEY_GET_TRANSACTION_STATS,
],
};
|