All files / src/features/Notifications/NotificationsCenter/hooks useNotificationsRedirect.tsx

63.44% Statements 92/145
47.05% Branches 32/68
68.57% Functions 24/35
70.54% Lines 91/129

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 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443                                                                    27x   2146x 2146x 2146x           2146x 2146x 2146x 2146x   2146x 31x       2146x         14x 14x 14x 14x   14x             2146x       4x 4x   4x 4x 4x           2146x                                                                       2146x 2146x             10x 10x               2x 2x               4x               2x                                                                 4x                               1x 1x                                                                                                   2146x 12x   12x 2x 10x 1x       9x       2146x           1x                   1x                           2146x   5x   1x   1x   1x     1x       2146x                     6x     6x   6x 6x   6x 2x   4x         6x   6x     2146x 4x 4x     2146x         2146x 2x 2x 2x     2146x 1x 1x     2146x     1x   1x     1x     1x   1x         2146x             2x 2x   2x 1x       2146x             1x 1x 1x     2146x 1x 1x         2146x                                   27x        
import {
  BANK_ACCOUNTS_MODAL_V2,
  BUSINESS_PROFILE_LINK_REQUEST_CANCELED_MODAL,
  BUSINESS_PROFILE_LINK_REQUEST_MODAL,
  GIVE_DISPUTE_PANEL,
  MERCHANT_PREVIEW_PANEL_MODAL,
  NOTIFICATION_SIDE_PANEL,
  TRANSACTION_INFO_MODAL,
} from "modals/modal_names";
import useRedirect from "@hooks/common/router/useRedirect";
import { checkPortals } from "@utils/routing";
import { DMenu as AcquirerDMenu } from "@pages/AcquirerPortal/Processing/ProcessingMenu";
import { DMenu as EnterpriseDMenu } from "@pages/EnterprisePortal/Processing/ProcessingMenu";
import {
  CurrentPortalKey,
  RedirectURLKeys,
  redirectPageUrls,
  getIdFromTString,
} from "../utils/redirects";
import { getTransaction } from "@components/ManageMoney/TransactionTable/TransactionInfoModal/utils";
import { TConversationParams } from "../types";
import { useConversationsModal } from "features/Minibuilders/Conversations/hooks/useConversationsModal";
import NiceModal from "@ebay/nice-modal-react";
import { delay } from "lodash";
import { useGetFeatureFlagValues } from "FeatureFlags/useGetFeatureFlagValues";
import { setOnboardingScreen } from "@redux/slices/app";
import { useAppDispatch } from "@redux/hooks";
import { OnboardingRedirectionEnum } from "@components/ProfilePage/utils";
import { useGetMerchantById } from "@hooks/enterprise-api/account/useGetMerchants";
import { customInstance } from "@services/api";
import { useOpenGiveConversation } from "@features/GiveConversation/hooks/useOpenGiveConversation";
import { TMerchantInitialSection } from "@features/Merchants/MerchantSidePanel/types";
import { scrollElIntoView } from "@features/Notifications/NotificationsCenter/utils";
 
const useNotificationsRedirect = () => {
  const { isOnboardingLinkBPEnabled, isNewConversationsEnabled } =
    useGetFeatureFlagValues();
  const { redirectToModal, redirectToPage } = useRedirect();
  const dispatch = useAppDispatch();
  const {
    currentPortal,
    isAcquirerPortal,
    isEnterprisePortal,
    isMerchantPortal,
  } = checkPortals();
  const { openConversationHandler } = useConversationsModal();
  const { handleOpenConversation } = useOpenGiveConversation();
  const { data: merchant } = useGetMerchantById();
 
  const buildUrl = (name: RedirectURLKeys) =>
    `${currentPortal}/${
      redirectPageUrls[name][currentPortal as CurrentPortalKey]
    }`;
 
  const openMerchantModal = (
    merchantId: number,
    isEnterprise: boolean,
    initialSection?: TMerchantInitialSection | VoidFunction,
  ) => {
    const url = buildUrl(isEnterprise ? "providers" : "merchants");
    redirectToPage(url);
    Iif (!merchantId) return;
    const modal = MERCHANT_PREVIEW_PANEL_MODAL;
 
    redirectToModal(modal, {
      id: merchantId,
      ["onModalOpen"]: initialSection,
      isProvider: isEnterprise,
    });
  };
 
  const openTransactionPanel = async (
    transactionId: string,
    page: RedirectURLKeys,
  ) => {
    const url = buildUrl(page);
    redirectToPage(url);
 
    Iif (!transactionId) return;
    const data = await getTransaction(transactionId);
    redirectToModal(TRANSACTION_INFO_MODAL, {
      data,
      DMenu: transactionDMenus[currentPortal],
    });
  };
 
  const handleReviewLinkRequest = async (
    accID?: number,
    merchantAccID?: number,
    merchantName?: string,
  ) => {
    if (!isOnboardingLinkBPEnabled || !accID || !merchantAccID) return;
    let linkStatus = "";
 
    try {
      const result = await customInstance({
        url: `/merchants/${merchantAccID}/legal-entities/${merchant?.legalEntityID}/link`,
        method: "GET",
      });
 
      linkStatus = result?.status;
    } catch (err) {
      //
    }
 
    if (linkStatus === "revoked") {
      redirectToModal(BUSINESS_PROFILE_LINK_REQUEST_CANCELED_MODAL, {
        merchantName,
      });
      return;
    }
 
    if (linkStatus && ["accepted", "rejected"].includes(linkStatus)) {
      return;
    }
 
    redirectToModal(BUSINESS_PROFILE_LINK_REQUEST_MODAL, {
      accID,
      merchantAccID,
      merchantName,
    });
  };
  const route = !isMerchantPortal ? "transactions" : "manage-money";
  const notificationsRedirects = {
    /**
     * Redirect to the settings page of the current portal
     * @memberof notificationsRedirects
     * @method settings
     */
    settings: (sectionElId?: string, routeParams?: any) => {
      const url = buildUrl("settings");
      redirectToPage(url, { sectionElId, routeParams });
    },
    /**
     * Redirects to onboarding - verify identity modal/page based on the current portal
     * @memberof notificationsRedirects
     * @method verify_identity
     */
    verify_identity: () => {
      const url = buildUrl("verify_identity");
      redirectToPage(url, { subpath: "verify_identity" });
    },
    /**
     * Redirects to the merchant page and opens the merchant preview panel
     * @memberof notificationsRedirects
     * @method merchant_panel
     */
    merchant_panel: (merchantId: number) => {
      openMerchantModal(merchantId, false);
    },
    /**
     * Redirects to the provider page and opens the provider preview panel
     * @memberof notificationsRedirects
     * @method enterprise_panel
     */
    enterprise_panel: (merchantId: number) => {
      openMerchantModal(merchantId, true);
    },
    /**
     * Redirects to the processing page and opens the bank accounts modal
     * @memberof notificationsRedirects
     * @method bank_accounts
     */
    bank_accounts: () => {
      const url = buildUrl(route);
      redirectToPage(url);
      if (isAcquirerPortal) return;
      redirectToModal(BANK_ACCOUNTS_MODAL_V2);
    },
 
    /**
     * Redirects to the processing page
     * @memberof notificationsRedirects
     * @method processing
     */
    transactions: () => {
      const url = buildUrl("transactions");
      redirectToPage(url);
    },
    "manage-money": () => {
      const url = buildUrl("manage-money");
      redirectToPage(url);
    },
    /**
     * Redirects to the processing page, fetches the transaction and opens the side panel
     * @memberof notificationsRedirects
     * @method transaction_panel
     */
    transaction_panel: (transactionId: string) => {
      openTransactionPanel(transactionId, route);
    },
    /**
     * Redirects to the transfers page, fetches the transfer and opens the side panel
     * @memberof notificationsRedirects
     * @method transaction_panel
     */
    transfer: (transactionId: string) => {
      openTransactionPanel(transactionId, "transfers");
    },
    /**
     * Redirects to the team member page
     * @memberof notificationsRedirects
     * @method team_members
     */
    team_members: () => {
      const url = buildUrl("manage_team");
      redirectToPage(url);
    },
    /**
     * Redirects to the disputes page
     * @memberof notificationsRedirects
     * @method disputes
     */
    disputes: async (id: string) => {
      const url = buildUrl("disputes");
      redirectToPage(url);
 
      if (!id) return;
 
      delay(() => {
        redirectToModal(GIVE_DISPUTE_PANEL, {
          id,
          isFirst: true,
          isLast: true,
        });
      }, 100);
    },
    /**
     * Opens Business Profile Link Request modal
     * @memberof notificationsRedirects
     * @method BusinessProfileLink
     */
    legal_entity_link_request: (
      accID?: number,
      merchantAccID?: number,
      merchantName?: string,
    ) => {
      handleReviewLinkRequest(accID, merchantAccID, merchantName);
    },
    /**
     * Opens Onboarding Business Details Step
     * @memberof notificationsRedirects
     * @method BusinessProfileLink
     */
    legal_entity_link_request_response: () => {
      if (!isOnboardingLinkBPEnabled) return;
 
      redirectToPage("/welcome#onboarding-details");
      dispatch(
        setOnboardingScreen({
          screen: OnboardingRedirectionEnum.BUSINESS_DETAILS,
        }),
      );
    },
  };
 
  const challengesRedirect = (challengeSlug: string) => {
    Iif (challengeSlug.includes("bank_account")) {
      notificationsRedirects["bank_accounts"]();
    } else if (challengeSlug.includes("primary")) {
      notificationsRedirects["verify_identity"]();
    } else if (challengeSlug.includes("business_profile")) {
      notificationsRedirects["settings"](undefined, {
        tab: "Business Profile",
      });
    } else {
      notificationsRedirects["settings"]();
    }
  };
 
  const handleOpenConversationCallback = ({
    targetMerchantId,
    topicId,
    threadId,
    messageId,
  }: TConversationParams): void => {
    Iif (isNewConversationsEnabled) {
      return handleOpenConversation({
        merchantData: {
          id: targetMerchantId,
        },
        taskID: messageId,
        initialThreadID: threadId,
      });
    }
 
    openConversationHandler({
      id: topicId,
      name: "",
      forceTopicOpen: true,
      openThread: {
        isRepliesOpen: false,
        threadId,
        commentId: messageId,
        index: 0,
      },
      paths: [],
    });
  };
 
  const conversationRedirect = (conversationParams?: TConversationParams) => {
    const isEmpty =
      !conversationParams || Object.values(conversationParams).some((p) => !p);
 
    Iif (isEmpty) return;
 
    const { targetMerchantId, targetMerchantType } = conversationParams;
 
    openMerchantModal(
      targetMerchantId,
      targetMerchantType === "enterprise",
      () => handleOpenConversationCallback(conversationParams),
    );
  };
 
  const merchantPanelRedirect = ({
    title,
    description,
    targetMerchantId,
    alertName,
  }: {
    title: string;
    alertName: string;
    description: string;
    targetMerchantId?: number;
  }) => {
    const merchantId = targetMerchantId
      ? targetMerchantId
      : getIdFromTString(description);
    Iif (!merchantId) return;
 
    let isEnterprise = false;
    Iif (alertName === "underwriter_assignement") {
      isEnterprise = title.toLowerCase().includes("provider");
    } else if (["new_merchant", "new_enterprise"].includes(alertName)) {
      isEnterprise = alertName === "new_enterprise";
    } else {
      isEnterprise = alertName === "enterprises_ready_for_approval";
    }
    const panel: Extract<
      keyof typeof notificationsRedirects,
      "merchant_panel" | "enterprise_panel"
    > = isEnterprise ? "enterprise_panel" : "merchant_panel";
 
    notificationsRedirects[panel](merchantId);
  };
 
  const transactionPanelRedirect = (transactionID: string) => {
    Iif (!transactionID) return;
    notificationsRedirects["transaction_panel"](transactionID);
  };
 
  const transferRedirect = (transactionID: string) => {
    if (!transactionID) return;
    notificationsRedirects["transfer"](transactionID);
  };
 
  const riskMonitorRedirect = ({ description }: Record<string, string>) => {
    const merchantId = getIdFromTString(description);
    Iif (!merchantId) return;
    openMerchantModal(merchantId, false, "riskProfile");
  };
 
  const ofacRedirect = (merchantId: number) => {
    Iif (!merchantId) return;
    openMerchantModal(merchantId, false, "ofac");
  };
 
  const merchantBankAccountsRedirect = ({
    description,
  }: Record<string, string>) => {
    const merchantId = getIdFromTString(description);
 
    Iif (!merchantId && (isEnterprisePortal || isMerchantPortal)) {
      notificationsRedirects["bank_accounts"]();
      return;
    } else Iif (!merchantId) {
      return;
    }
    const isEnterprise = description.includes("provider");
 
    openMerchantModal(merchantId, isEnterprise, () =>
      scrollElIntoView("bank_account"),
    );
  };
 
  const ownerApprovalRedirect = ({
    description,
    targetMerchantId,
  }: {
    description: string;
    targetMerchantId: number;
  }) => {
    Iif (!targetMerchantId) return;
    const isEnterprise = description.includes("provider");
 
    openMerchantModal(targetMerchantId, isEnterprise, () =>
      scrollElIntoView("primary_account_holder"),
    );
  };
 
  const midIssueRedirect = ({
    alertName,
    targetMerchantId,
  }: {
    alertName: string;
    targetMerchantId: number;
  }) => {
    Iif (!targetMerchantId) return;
    const isEnterprise = alertName.includes("enterprise");
    openMerchantModal(targetMerchantId, isEnterprise);
  };
 
  const openNotificationSidePanel = ({ id }: { id?: number }) => {
    Iif (!id) return;
    NiceModal.show(NOTIFICATION_SIDE_PANEL, {
      notificationMessageID: id,
    });
  };
 
  return {
    notificationsRedirects,
    challengesRedirect,
    merchantPanelRedirect,
    transactionPanelRedirect,
    riskMonitorRedirect,
    ofacRedirect,
    merchantBankAccountsRedirect,
    ownerApprovalRedirect,
    midIssueRedirect,
    conversationRedirect,
    openNotificationSidePanel,
    transferRedirect,
  };
};
 
export default useNotificationsRedirect;
 
const transactionDMenus: Record<string, any> = {
  acquirer: AcquirerDMenu,
  enterprise: EnterpriseDMenu,
};