All files / src/components/Merchants/MerchantPreview/RiskProfile/helpers parsers.ts

100% Statements 18/18
61.11% Branches 11/18
100% Functions 6/6
100% Lines 15/15

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              48x 655x                     48x 5x   4x 32x   32x 16x     32x                                         32x     4x       691x           691x 691x   691x    
import { startCase } from "lodash";
import { formatDistanceToNow } from "date-fns";
import {
  RiskTriggerDataFormatted,
  RiskTriggerResponseData,
} from "@components/Merchants/MerchantPreview/RiskProfile/hooks/useRiskTriggersV2";
 
export const riskActivitiesParser = (data: any[]) => {
  return data?.map((activity: any) => ({
    ...activity,
    merchantFactorName: replaceKeywords(startCase(activity.merchantFactorName)),
    createdAt: activity.createdAt
      ? formatDistanceToNow(new Date(activity.createdAt * 1000), {
          addSuffix: true,
        })
      : "",
  }));
};
 
export const riskTriggersParser = (data: RiskTriggerResponseData[]) => {
  if (!data) return {};
 
  const triggersCache = data.reduce((result, item) => {
    const category = item.category;
 
    if (!result[category]) {
      result[category] = [];
    }
 
    result[category].push({
      factorID: item.factorID,
      isEnabled: item.isEnabled,
      description: item.description,
      riskPoints: item.riskPoints,
      name: item.name,
      threadID: item.threadID,
      enabledByUserAvatarImageURL: item?.enabledByUserAvatarImageURL ?? "",
      enabledByUserFirstName: item?.enabledByUserFirstName ?? "",
      enabledByUserLastName: item?.enabledByUserLastName ?? "",
      hasUnreadMessages: item?.hasUnreadMessages,
      customReason: item?.customReason,
      updatedAt: item.updatedAt,
      disabledByUserAvatarImageURL: item?.disabledByUserAvatarImageURL ?? "",
      disabledByUserFirstName: item?.disabledByUserFirstName ?? "",
      disabledByUserLastName: item?.disabledByUserLastName ?? "",
      userAvatarsInThread: item?.userAvatarsInThread,
      unreadMessagesCount: item?.unreadMessagesCount,
      hasMerchantReplied: item.didMerchantReply,
      isMentioned: item.isMentioned,
    });
    return result;
  }, {} as RiskTriggerDataFormatted);
 
  return triggersCache;
};
 
export function replaceKeywords(input: string): string {
  const replacements: { [key: string]: string } = {
    Vol: "Volume",
    Txn: "Transaction",
    Avg: "Average",
  };
 
  const pattern = new RegExp(Object.keys(replacements).join("|"), "gi");
  const result = input.replace(pattern, (match) => replacements[match]);
 
  return result;
}