All files / src/services/api/analytics transactions.ts

14.28% Statements 7/49
0% Branches 0/29
0% Functions 0/11
18.91% Lines 7/37

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                2x                                                     2x                                                               2x             2x   2x                                                     2x                             2x        
import { customInstance } from "..";
import { buildMerchantEndpoints } from "../utils.api";
import { productTypeIDs } from "./campaigns";
 
interface IParams {
  filter?: string;
}
 
export const getTransactionStats = async (params?: IParams) => {
  let newEndPoint = "stats";
 
  if (params?.filter) newEndPoint += "?";
  if (params?.filter) newEndPoint += params.filter;
 
  return customInstance({
    url: buildMerchantEndpoints(newEndPoint),
    method: "GET",
  });
};
 
interface ChartParams {
  filter?: string;
  productId?: number;
  productType?: string;
  interval?: string;
  chartType?: string;
}
interface ChartParams {
  filter?: string;
  productId?: number;
  productType?: string;
  interval?: string;
  chartType?: string;
}
 
export const getTransactionChart = (params?: ChartParams) => {
  const productTypeId = params?.productType
    ? productTypeIDs[params.productType as keyof typeof productTypeIDs]
    : "";
  const chartInterval = params?.interval || "day";
  const baseURL = `transactions/chart?interval=${chartInterval}`;
  let URL = baseURL;
  if (params?.filter) URL += `&${params.filter}`;
  if (params?.productId) URL += `&product_id=${params.productId}`;
  if (productTypeId) URL += `&product_type_id=${productTypeId}`;
  if (params?.chartType) URL += `&chart=${params.chartType}`;
 
  return customInstance({
    url: buildMerchantEndpoints(URL),
    method: "GET",
  });
};
 
type TData = {
  accID: number;
  date: Date;
  transactionItemCount: number;
  sumApprovedPurchases: number;
  averageApprovedPurchases: number;
  totalChargebacks: number;
  settledTransactionItemCount: number;
};
type Values = keyof Omit<
  TData,
  "date" | " accID" | "settledTransactionItemCount"
>;
 
const mainChartKeys: Record<string, string> = {
  Transactions: "totalTransactions",
  Purchases: "totalPurchases",
  Refunds: "totalRefunds",
  Chargebacks: "totalChargebacks",
};
 
const labels = ["Transactions", "Purchases", "Refunds", "Chargebacks"];
 
export const transactionsChartNormalizer = (trim: boolean, data: TData[]) => {
  if (!data) return labels.map((label) => ({ label, entries: [] }));
 
  let index = 0;
 
  if (trim) {
    index = data.findIndex((el) => {
      const keys = Object.values(mainChartKeys);
      const emptyValues = keys.filter((v) => el[v as Values] === 0);
      return keys.length !== emptyValues.length;
    });
  }
 
  const trimmedData = index > 0 ? data?.slice(index, data.length - 1) : data;
 
  return labels.map((label) => ({
    label,
    entries: trimmedData.map((chartData: TData) => {
      const value = chartData[mainChartKeys[label] as Values];
      return {
        x: chartData.date,
        y: value || 0,
      };
    }),
  }));
};
 
export const settledTransactionsNormalizer = (
  data: TData[],
  year: number,
  changeDate = false,
) => {
  const label = `Payments year ${year}`;
  if (!data) return { label, entries: [] };
 
  const entries = data.map((entry) => ({
    x: changeDate ? convertYear(entry.date) : entry.date,
    y: entry.settledTransactionItemCount,
  }));
  return { label, entries };
};
 
const convertYear = (date: any) => {
  const d = new Date(date);
  return new Date(d.setFullYear(new Date().getFullYear()));
};