All files / src/features/Merchants/MerchantsTable utils.ts

87.5% Statements 63/72
80.45% Branches 70/87
100% Functions 6/6
87.32% Lines 62/71

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            135x 1576x               135x             1290x     1290x 1290x 1290x 1290x 1290x 1290x 1290x                 1290x             1290x         3x     1287x 91x     1196x 22x     22x     22x   22x           22x 11x       1174x 96x     1078x   943x     135x   94x     41x         41x     135x 3868x 3868x 48x 4x   44x     44x     44x         40x     4x               3868x     135x         3868x   79x             92x         29x         144x         121x               3354x   49x       135x 9090x   208x   309x   178x   324x   206x   88x   103x   51x   7623x      
import { TAG_STATUSES_ENUM } from "@common/TextTag/types";
import { MerchantData } from "@hooks/enterprise-api/account/types";
import { StatusMap } from "@components/Merchants/MerchantPreview/helpers/parsers";
import { checkPortals } from "@utils/routing";
import { TabNames } from "./hooks/useTabs";
 
export const isMerchantInvited = (status?: string): boolean => {
  return (
    status === "sent" ||
    status === "read" ||
    status === "delivered" ||
    status === "invited"
  );
};
 
export const getMerchantStatus = (
  rowData: MerchantData,
  showInviteStatus?: boolean,
  isMerchantStatusRefactorEnabled?: boolean,
  isNewApprovalFlowEnabled?: boolean,
  isMerchantOnboardingModalEnabled?: boolean,
) => {
  const sponsorStatus = isMerchantStatusRefactorEnabled
    ? rowData?.approvalStateName
    : "";
  const ownerMembershipStatus = rowData?.ownerMembershipStatus;
  const statusName = isMerchantStatusRefactorEnabled ? rowData?.stateName : "";
  const statusDisplay = rowData?.statusDisplayName || ""; //TODO: clean this up when we fully use stateName everywhere
  const sponsorDisplay = rowData?.sponsorStatusDisplayName; //TODO: clean this up when we fully use stateName everywhere
  const ownerInviteStatusDisplayName = rowData?.ownerInviteStatusDisplayName;
  const isProvider = !!rowData?.totalSubmerchants; //only provier has submerchants
  const stateNameValues = [
    "sponsor_mid_issue",
    "ready_for_review",
    "declined",
    "suspended",
    "approved",
    "pending",
  ];
 
  Iif (isMerchantOnboardingModalEnabled) {
    if (statusName === "onboarding") {
      return StatusMap.pending;
    }
  }
 
  // for the "Invited" tab show the specific invitation status
  if (
    showInviteStatus &&
    rowData?.ownerMembershipStatus === "invited" &&
    ownerInviteStatusDisplayName
  ) {
    return ownerInviteStatusDisplayName;
  }
 
  if (isMerchantInvited(ownerMembershipStatus)) {
    return StatusMap.invite_sent;
  }
 
  if (isMerchantStatusRefactorEnabled) {
    Iif (statusName === "sponsor_mid_issue" && isProvider) {
      return StatusMap.approved;
    }
    Iif (sponsorStatus && !stateNameValues.includes(statusName || ""))
      return StatusMap[sponsorStatus];
 
    const statusDisplayName = statusName ? StatusMap[statusName] : "";
 
    Iif (
      isNewApprovalFlowEnabled &&
      ["Ready for Review", "Pending"].includes(statusDisplayName)
    )
      return "Sponsor";
 
    if (statusDisplayName) return statusDisplayName;
    else return "";
  }
 
  //if the merchant is in onboarding tab, TODO: clean-up after using only stateName
  if (rowData?.isInOnboardingSection) {
    return StatusMap.onboarding;
  }
 
  if (statusDisplay === "Pre Approved") {
    //TODO: cleanup after using only stateName
    return StatusMap.preapproved_no_tx;
  }
 
  if (statusDisplay === "MSP Issue" || sponsorDisplay === "Ready for Review") {
    //TODO: cleanup after using only stateName
    return StatusMap.ready_for_review;
  }
 
  Iif (["Pre-approval", "Auto-approval"].includes(statusDisplay)) {
    //TODO: cleanup after using only stateName
    return sponsorDisplay;
  }
 
  return statusDisplay;
};
 
export const useGetQuery = () => {
  const { isAcquirerPortal, isEnterprisePortal } = checkPortals();
  const getQuery = (status: TAG_STATUSES_ENUM) => {
    if (status === TAG_STATUSES_ENUM.INVITE) {
      return ``;
    }
    Iif (isEnterprisePortal) {
      return `members?filter=(roleName:"owner",roleName:"admin")%3B(memberStatus:"joined")`;
    }
    Iif (!isAcquirerPortal) {
      return ``;
    }
    if (
      status === TAG_STATUSES_ENUM.UNDERWRITING ||
      status === TAG_STATUSES_ENUM.READY_FOR_REVIEW ||
      status === TAG_STATUSES_ENUM.DECLINED
    ) {
      return `members?filter=(roleName:"owner",roleName:"uw_associate",roleName:"uw_executive",roleName:"admin")%3B(memberStatus:"joined")`;
    }
 
    Iif (
      status === TAG_STATUSES_ENUM.APPROVED ||
      status === TAG_STATUSES_ENUM.SUSPENDED
    ) {
      return `members?filter=(roleName:"owner",roleName:"risk_manager",roleName:"admin")%3B(memberStatus:"joined")`;
    }
  };
 
  return getQuery;
};
 
export const getDefaultSort = (
  tab: string,
  isMerchantStatusRefactorEnabled: boolean,
  isNewApprovalFlowEnabled: boolean,
) => {
  switch (tab) {
    case "sponsor":
      return {
        defaultSort: isMerchantStatusRefactorEnabled
          ? "approvalAt,-accID"
          : "lastInternalApprovalAt",
        defaultDescending: false,
      };
    case "invited":
      return {
        defaultSort: "ownerInviteLastSentAt,-accID",
        defaultDescending: true,
      };
    case "approved":
      return {
        defaultSort: "approvedAt,-accID",
        defaultDescending: true,
      };
    case "risk":
      return {
        defaultSort: "lastEscalationAt,-accID",
        defaultDescending: true,
      };
    case "underwriting":
      return {
        defaultSort: isNewApprovalFlowEnabled
          ? "kybApprovalAt,-accID"
          : "createdAt,-accID",
        defaultDescending: true,
      };
    case "all":
    case "onboarding":
      return { defaultSort: "createdAt,-accID", defaultDescending: true };
    default:
      return { defaultSort: "createdAt,-accID", defaultDescending: true };
  }
};
 
export const getSelectedTab = (tab: string) => {
  switch (tab) {
    case TabNames.invited:
      return "invited";
    case TabNames.underwriting:
      return "underwriting";
    case TabNames.sponsor:
      return "sponsor";
    case TabNames.risk:
      return "risk";
    case TabNames.onboarding:
      return "onboarding";
    case TabNames.kyb:
      return "kyb";
    case TabNames.approved:
      return "approved";
    case TabNames.pending:
      return "pending";
    default:
      return "all";
  }
};