All files / src/pages/AcquirerPortal/Processing ProcessingMenu.tsx

18.42% Statements 7/38
0% Branches 0/33
0% Functions 0/14
19.44% Lines 7/36

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 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238                                                      27x       27x             27x       27x       27x                   27x       27x                                                                                                                                                                                                                                                                                                                                                                  
import { DropdownItem } from "@common/Select";
import { Text } from "@common/Text";
import { Menu } from "@mui/material";
import NiceModal, { useModal } from "@ebay/nice-modal-react";
import {
  MERCHANT_PREVIEW_PANEL_MODAL,
  MERCHANT_VIEW_CUSTOMER,
  MERCHANT_VIEW_TRANSACTION_MODAL,
  PROCESSING_CANCEL_TRANSACTION_MODAL,
  PROCESSING_MAKE_RECURRING_MODAL,
  PROCESSING_REFUND_TRANSACTION_MODAL,
  PROCESSING_SEND_RECEIPT_MODAL,
} from "modals/modal_names";
import { TransactionData } from "@components/ManageMoney/TransactionTable/data";
import { getSourceAccount } from "@components/ManageMoney/TransactionTable/transactions.helpers";
import { HiddenComponent } from "containers/HiddenComponent";
import {
  composePermission,
  useAccessControl,
} from "features/Permissions/AccessControl";
import RESOURCE_BASE, {
  FEATURE_DENY_MESSAGE,
  OPERATIONS,
} from "@constants/permissions";
import { WithTooltipWrapper } from "@common/Menu/NewDropdownMenu";
import { TransactionStatus } from "@components/ManageMoney/TransactionTable/TransactionInfoModal";
 
export const editMerchantHandler = (id?: number) => {
  NiceModal.show(MERCHANT_PREVIEW_PANEL_MODAL, { id });
};
 
export const refundHandler = ({ sourceAccount, ids }: any) => {
  NiceModal.show(PROCESSING_REFUND_TRANSACTION_MODAL, {
    sourceAccount,
    ids,
  });
};
 
export const makeRecurringHandler = (id?: string) => {
  NiceModal.show(PROCESSING_MAKE_RECURRING_MODAL, { id });
};
 
const cancelHandler = (data: any) => {
  NiceModal.show(PROCESSING_CANCEL_TRANSACTION_MODAL, data);
};
 
export const sendReceiptHandler = (row?: Record<string, any>) => {
  NiceModal.show(PROCESSING_SEND_RECEIPT_MODAL, {
    transactionID: row?.id,
    emails: [row?.sourceAccount.user.email],
    sourceAccount: getSourceAccount(row as any),
    customerID: row?.sourceAccount.id,
    isChargebackReversal: row?.status === TransactionStatus.CHARGEBACK_REVERSAL,
  });
};
 
export const viewMerchantTransactionsHandler = (row?: TransactionData) => {
  NiceModal.show(MERCHANT_VIEW_TRANSACTION_MODAL, { data: row });
};
 
const viewCustomerHandler = (customerId?: any, merchantId?: any) => {
  if (customerId)
    NiceModal.show(MERCHANT_VIEW_CUSTOMER, {
      id: customerId,
      merchantId,
    });
};
 
export function DMenu({ anchorEl, open, setAnchorEl, selectedRow }: any) {
  const isVoided = selectedRow?.displayStatus === "Voided";
  const isChargeback = selectedRow?.status === "Chargeback";
  const isRefund = selectedRow?.status === "Refunded";
  const isTransfer = selectedRow?.displayType === "Money Transfer";
  const isPendingTransfer =
    isTransfer && selectedRow?.displayStatus === "Requested";
 
  const isHideRefund =
    selectedRow?.hideRefund ||
    isVoided ||
    selectedRow.isHideRefund ||
    selectedRow?.displayStatus === "Sent" ||
    selectedRow?.displayStatus === "Refunded" ||
    isTransfer ||
    isChargeback;
  const isHideSendReceipt =
    isTransfer || isChargeback || isVoided || selectedRow?.hideSendReceipt;
 
  const isMerchantDeleted = Boolean(
    selectedRow?.destinationAccount?.merchant?.deletedAt,
  );
  const isRefundAllowed = useAccessControl({
    resource: composePermission(RESOURCE_BASE.MERCHANT, RESOURCE_BASE.REFUND),
    operation: OPERATIONS.CREATE,
  });
  const isCancelTransferAllowed = useAccessControl({
    resource: composePermission(
      RESOURCE_BASE.MERCHANT,
      RESOURCE_BASE.BANK_TRANSFER,
    ),
    operation: OPERATIONS.UPDATE,
  });
 
  return (
    <Menu
      open={open}
      anchorEl={anchorEl}
      onClose={() => setAnchorEl(null)}
      anchorOrigin={{
        vertical: "bottom",
        horizontal: "right",
      }}
      transformOrigin={{
        vertical: "top",
        horizontal: "right",
      }}
      sx={{
        "& .MuiPaper-root": {
          width: "inherit",
        },
      }}
    >
      <WithHideAnchorItem
        selectedRow={selectedRow}
        onClick={refundHandler}
        title="Refund"
        hidden={isHideRefund}
        anchorEl={anchorEl}
        disabled={!isRefundAllowed}
        tooltipProps={{
          show: !isRefundAllowed,
          message: FEATURE_DENY_MESSAGE,
        }}
        setAnchorEl={setAnchorEl}
        onClickPayload={{
          sourceAccount: selectedRow ? getSourceAccount(selectedRow) : {},
          ids: {
            transactionIDs: [selectedRow?.id],
          },
        }}
      />
 
      <WithHideAnchorItem
        hidden={isHideSendReceipt}
        selectedRow={selectedRow}
        onClick={() => sendReceiptHandler(selectedRow)}
        title="Send Receipt"
        anchorEl={anchorEl}
        setAnchorEl={setAnchorEl}
      />
 
      <WithHideAnchorItem
        selectedRow={selectedRow}
        onClick={editMerchantHandler}
        hidden={!isTransfer || isMerchantDeleted}
        title="View Merchant"
        anchorEl={anchorEl}
        setAnchorEl={setAnchorEl}
        closeCurrentModal
      />
      <WithHideAnchorItem
        selectedRow={selectedRow}
        onClick={() =>
          viewCustomerHandler(
            selectedRow?.customer?.id,
            !isChargeback && !isRefund
              ? selectedRow?.destinationAccount?.id
              : selectedRow?.sourceAccount?.id,
          )
        }
        hidden={isTransfer}
        title="View customer"
        anchorEl={anchorEl}
        setAnchorEl={setAnchorEl}
        closeCurrentModal
      />
      <WithHideAnchorItem
        selectedRow={selectedRow}
        onClick={() =>
          cancelHandler({
            transactionId: selectedRow?.id,
            cancel: true,
            sourceAccount: getSourceAccount(selectedRow),
            customerID: selectedRow?.destinationAccount.id,
          })
        }
        title="Cancel Transfer"
        anchorEl={anchorEl}
        disabled={!isCancelTransferAllowed}
        setAnchorEl={setAnchorEl}
        hidden={!isPendingTransfer}
        tooltipProps={{
          show: !isCancelTransferAllowed,
          message: FEATURE_DENY_MESSAGE,
        }}
      />
    </Menu>
  );
}
 
export function WithHideAnchorItem({
  anchorEl,
  onClick,
  title,
  selectedRow,
  setAnchorEl,
  onClickPayload,
  hidden,
  disabled = false,
  closeCurrentModal = false,
  tooltipProps,
}: any) {
  const modal = useModal();
  const id =
    selectedRow?.sourceAccount?.type === "user"
      ? selectedRow?.destinationAccount?.id
      : selectedRow?.sourceAccount?.id;
 
  return (
    <HiddenComponent hidden={Boolean(hidden)}>
      <WithTooltipWrapper
        hasTooltip={!!tooltipProps}
        tooltipProps={tooltipProps}
      >
        <DropdownItem
          disabled={disabled}
          onClick={() => {
            if (anchorEl) setAnchorEl(null);
            if (closeCurrentModal) modal.hide();
            onClick(onClickPayload ?? id);
          }}
        >
          <Text>{title}</Text>
        </DropdownItem>
      </WithTooltipWrapper>
    </HiddenComponent>
  );
}