All files / src/features/TransactionsRiskProfile/helpers transactions.helpers.ts

12.72% Statements 7/55
0% Branches 0/50
0% Functions 0/5
12.96% Lines 7/54

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              51x 51x 51x   51x                                                                         51x                                                                 51x                                                                                                           51x                                                        
import moment from "moment";
import {
  ParsedTransaction,
  FormattedListItem,
  APITransactionsGroup,
} from "../data.types";
 
const today = new Date();
const yesterday = new Date(today);
yesterday.setDate(today.getDate() - 1);
 
export const groupTransactions = (
  array: APITransactionsGroup[],
): FormattedListItem[] => {
  return array.map((group) => {
    const groupDate = moment(new Date(group.groupPeriod));
    let date = "";
 
    if (groupDate.isSame(today, "day")) {
      date = "Today";
    } else if (groupDate.isSame(yesterday, "day")) {
      date = "Yesterday";
    } else {
      date = groupDate.format("MMM. DD YYYY");
    }
 
    const groupsList: ParsedTransaction[][] = [];
 
    // group transaction in stacks by minute
    for (let i = 0; i < group.transactions.length; i++) {
      const transaction = group.transactions[i];
 
      const parsedTransaction = parseTransaction(transaction);
 
      if (groupsList.length === 0) {
        groupsList.push([parsedTransaction]);
        continue;
      }
      stackTransactionByMinute(groupsList, parsedTransaction);
    }
 
    return {
      label: date,
      list: groupsList,
    };
  });
};
 
const stackTransactionByMinute = (
  newList: ParsedTransaction[][],
  transaction: ParsedTransaction,
) => {
  const lastGroupByMinuteIndex = newList.length - 1;
  const lastGroupByMinute = newList[lastGroupByMinuteIndex];
 
  // check if the transaction date is within a minute from the first
  // transaction in the last stack (since the array it's ordered by creation date)
  const lastElementDate = lastGroupByMinute[0].date;
  const isWithinOneMinute =
    transaction.date <= lastElementDate &&
    transaction.date >= lastElementDate - 60;
  // check if the first transaction of the array are both with escalation or both
  // without because we don't stack different types
  const bothWithEscalation =
    !!transaction.escalation.createdAt &&
    !!lastGroupByMinute[0].escalation.createdAt;
  const bothWithoutEscalation =
    !transaction.escalation.createdAt &&
    !lastGroupByMinute[0].escalation.createdAt;
  const isSameType = bothWithEscalation || bothWithoutEscalation;
 
  if (isWithinOneMinute && isSameType) {
    // if within a minute and of same type, we push the transaction in the last stack
    const newEl = [...lastGroupByMinute, transaction];
    newList[lastGroupByMinuteIndex] = newEl;
  } else {
    // if not we add a new stack
    newList.push([transaction]);
  }
};
 
const parseTransaction = (thx: any): ParsedTransaction => {
  // const isEscalation = escalation.triggerPoints > 0;
  // const newRiskLevel = escalation.riskLevel + escalation.triggerPoints;
 
  /**
   * triggerPoints: thx.eventEscalation?.riskPoints || 0,
   * riskLevel: thx.ipProfileSnapshot?.riskLevel || 0,
   */
 
  const emailStatus = thx.customerUserEmailStatus;
  const newEmailStatus =
    emailStatus?.toLowerCase() === "rejected" ? "Undeliverable" : emailStatus;
  const isFalsePositive = thx.eventEscalation?.trigger === "manual_unblock";
 
  return {
    id: thx.id,
    merchantId: thx.merchantAccID,
    merchantName: thx.merchantName,
    parentMerchantName: thx.parentMerchantName,
    charged: thx.charged / 100,
    reversalState: thx.ReversalState,
    status: getStatus(
      thx.isBlocked,
      isFalsePositive,
      thx?.isQuarantined || false,
      thx.processingStateName,
      thx.displayStatus,
    ),
    quarantinedReason: thx.riskMetadata?.assessment,
    processingStateName: thx.processingStateName,
    displayStatus: thx.displayStatus,
    card: {
      cardBrand: thx.binInfo.cardBrand,
      last4: thx.paymethodLast4,
      cardHolder: thx.cardholderName,
    },
    email: {
      isValid: thx.customerUserEmailStatus === "valid",
      email: thx.customerUserEmail,
      status: newEmailStatus,
    },
    date: thx.createdAt,
    escalation: {
      createdAt: thx.eventEscalation?.createdAt || 0,
      triggerPoints: thx.eventEscalation?.riskPoints || 0,
      triggerReason: thx.eventEscalation?.triggerReason || "",
      riskLevel: thx.ipProfileSnapshot?.riskLevel || 0,
      trigger: thx.eventEscalation?.trigger || "",
      triggerType: thx.eventEscalation?.triggerType || "",
    },
    rawData: thx,
  };
};
 
const getStatus = (
  isBlocked: boolean,
  isFalsePositive: boolean,
  isQuarantine: boolean,
  processingStateName: string,
  displayStatus: string,
) => {
  if (isFalsePositive) {
    return "false_positive";
  } else if (isBlocked) {
    return "blocked";
  } else if (processingStateName === "declined") {
    return processingStateName;
  } else if (processingStateName === "failed") {
    return processingStateName;
  } else if (processingStateName === "voided") {
    return "voided";
  } else if (isQuarantine) {
    return "quarantined";
  } else if (
    processingStateName === "approved" &&
    displayStatus === "Pending"
  ) {
    return "pending";
  } else {
    return processingStateName;
  }
};