All files / src/features/Notifications/NotificationsCenter/utils index.ts

88.88% Statements 32/36
75.92% Branches 41/54
87.5% Functions 7/8
91.42% Lines 32/35

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                          28x 221x         221x 221x   221x 930x   930x 772x         772x   158x         158x       221x           28x 930x   930x   769x   1x   160x       28x 930x                                                     28x 39x                 28x 2136x 2136x         2136x     28x         930x 148x         782x     28x         28x            
import {
  NotificationType,
  TConversationParams,
  TParsedNotification,
} from "../types";
import { formatDistanceToNow, fromUnixTime } from "date-fns";
import { formatFromUnixWithFormat } from "@utils/index";
import moment from "moment";
 
type TNotificationsGroups = "mandatory" | "generic";
type TNotificationsList = TParsedNotification[];
export type TGroupedList = Record<TNotificationsGroups, TNotificationsList>;
 
export const groupData = (list: any): TGroupedList => {
  Iif (!list || !Array.isArray(list))
    return {
      mandatory: [],
      generic: [],
    };
  const mandatoryList: TNotificationsList = [];
  const genericList: TNotificationsList = [];
 
  list.forEach((notification) => {
    const parsedNotification = parser(notification);
 
    if (parsedNotification.isMandatory) {
      const currentNotification = addLabel(
        parsedNotification,
        "mandatory",
        mandatoryList.length === 0,
      );
      mandatoryList.push(currentNotification);
    } else {
      const currentNotification = addLabel(
        parsedNotification,
        "generic",
        genericList.length === 0,
      );
      genericList.push(currentNotification);
    }
  });
 
  return {
    mandatory: mandatoryList,
    generic: genericList,
  };
};
 
const normalizeType = (type?: string): NotificationType => {
  Iif (!type) return "generic";
 
  switch (type) {
    case "underwriting":
      return "underwriting";
    case "conversation":
      return "conversation";
    default:
      return "generic";
  }
};
 
const parser = (notification: any): TParsedNotification => {
  return {
    accID: notification?.accID,
    id: notification?.id || 0,
    title: notification?.displayTitle || "",
    description: notification?.displayMessage || "",
    isMandatory: notification?.alertType === "mandatory" || false,
    merchantName: notification?.merchant?.name,
    merchantAccID: notification?.merchant?.accID,
    firstCheckedAt: notification?.readAt || null,
    type: normalizeType(notification?.alertCategory),
    createdAt: notification?.createdAt || 0,
    challengeSlug:
      notification?.challenge?.slug || notification?.task?.slug || "",
    alertName: notification?.alertName || "",
    transactionID: notification?.transaction?.id || "",
    targetMerchantId: notification?.merchant?.accID || 0,
    threadId:
      notification?.challenge?.threadID || notification?.task?.threadID || null,
    disputeId: notification?.disputeID || "",
    ...(notification?.conversationMessage && {
      conversationParams: getConversationParams(
        notification?.conversationMessage,
      ),
    }),
  };
};
 
const getConversationParams = (params?: any): TConversationParams => {
  return {
    targetMerchantId: params?.subjectAccID || 0,
    targetMerchantType: params?.subjectAccType || "",
    topicId: params?.topicID || 1,
    threadId: params?.threadID || 0,
    messageId: params?.id || 0,
  };
};
 
export const getNotificationDate = (unixTime: number) => {
  const last24HoursMark = moment(new Date()).subtract(24, "hours").unix();
  Iif (unixTime > last24HoursMark) {
    return formatDistanceToNow(fromUnixTime(unixTime), {
      addSuffix: true,
    });
  }
  return formatFromUnixWithFormat(unixTime, "MMMM. D YYYY - h:mm A");
};
 
const addLabel = (
  alert: TParsedNotification,
  type: TNotificationsGroups,
  addLabel: boolean,
) => {
  if (addLabel) {
    return {
      ...alert,
      label: labels[type],
    };
  }
  return alert;
};
 
const labels: Record<TNotificationsGroups, string> = {
  mandatory: "Mandatory notifications",
  generic: "Other notifications",
};
 
export const scrollElIntoView = (
  section: string,
  scrollOptions: boolean | ScrollIntoViewOptions = { block: "center" },
) => {
  return document.getElementById(section)?.scrollIntoView(scrollOptions);
};