All files / src/shared/GiveExportModal/hooks/merchantTable merchantTableConfig.ts

93.47% Statements 43/46
90.38% Branches 47/52
100% Functions 18/18
93.02% Lines 40/43

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                                                                      43x   43x                                                                                                                                 27x 27x 27x 27x 27x     27x                                   41x                     896x                                         37x           37x 259x 4101x     37x 37x 37x     37x   37x                         37x                                   37x       1087x 492x 142x 247x 422x 422x 141x                                     15x 43x   15x 14x 5x       2x 3x       1x 2x         2x         2x           2x    
import { getMerchantExportColumns } from "@shared/GiveExportModal/hooks/merchantTable/merchant";
import {
  businessOwner,
  businessProfile,
  getMerchantInfoOptions,
  ofacInfo,
  primaryAccountHolder,
  riskMonitor,
  salesInfo,
} from "@shared/GiveExportModal/const";
import {
  MerchantColumnsIncluded,
  MerchantReportColumns,
} from "@shared/GiveExportModal/giveExportTypes";
import { subMonths } from "date-fns";
import { format } from "date-fns-tz";
 
/**
 * Merchant-table "Data Selection" options.
 *
 * This mirrors the legacy ExportView "columnsIncluded" select:
 * - Some presets are only relevant for specific tabs/contexts (e.g. sponsor flow, pending tab).
 * - The selected value drives side effects (selected columns, fileName prefix, date range).
 */
export function getMerchantColumnsIncludedOptions({
  tab,
  isPendingTab,
  isExportAllowed,
  isAcquirerPortal,
}: {
  tab?: string;
  isPendingTab: boolean;
  isExportAllowed: boolean;
  isAcquirerPortal: boolean;
}) {
  const isSponsorTab = tab === "sponsor";
 
  return [
    {
      label: "All columns",
      value: "all",
      description: "Entire columns including the hidden ones will be exported",
    },
    {
      label: "Columns in the view only",
      value: "visible",
      description: "Visible columns within the table will be exported",
    },
    ...(isSponsorTab
      ? [
          {
            label: "New Merchant Approval Report",
            value: "new_merchant_approval",
            description: "Exports the preset New Merchant Approval report",
          },
        ]
      : []),
    ...(!isSponsorTab && isExportAllowed
      ? [
          {
            label: "Processing Monthly Report",
            value: "processing_monthly",
          },
          {
            label: "Reserve Monthly Report",
            value: "reserve_monthly",
          },
          ...(isAcquirerPortal
            ? [
                {
                  label: "Merchant Reconciliation Report",
                  value: "reconciliation_monthly",
                },
                {
                  label: "Bank Reconciliation Report",
                  value: "bank_reconciliation_monthly",
                },
              ]
            : []),
        ]
      : []),
    ...(!isSponsorTab && !isPendingTab && isExportAllowed
      ? [
          {
            label: "OFAC Business Profile Report",
            value: "ofac_business",
          },
          {
            label: "OFAC Persons Report",
            value: "ofac_persons",
          },
        ]
      : []),
    {
      label: "Custom",
      value: "custom",
      description: "Selection of specific columns to be exported",
    },
  ];
}
 
function formatShortRangeLastMonth() {
  const today = new Date();
  const lastMonth = subMonths(today, 1);
  const start = new Date(lastMonth.getFullYear(), lastMonth.getMonth(), 1);
  const end = new Date(lastMonth.getFullYear(), lastMonth.getMonth() + 1, 0);
  return `${format(start, "d")}–${format(end, "d MMMM")}`;
}
 
export const merchantDateRangeOptions = [
  { value: "current_month", label: "Current Month" },
  {
    value: "month",
    label: "Last Month",
    description: formatShortRangeLastMonth(),
  },
  {
    value: "custom",
    label: "Custom",
  },
];
 
/**
 * In the legacy panel, date range is only shown for report-like exports.
 * For plain exports (visible/all/custom), a date filter is not required.
 */
export function shouldHideMerchantTableDateRange(columnsIncluded?: string) {
  return ["visible", "all", "custom"].includes(String(columnsIncluded));
}
 
/**
 * Reports that use the async GET reconciliation pipeline: All Time by default,
 * an optional date range, and no column selection. Both the Merchant
 * Reconciliation Report and the Bank Reconciliation Report must share identical
 * date-range / allTime handling, so gate that behavior on this predicate rather
 * than on a single hardcoded value (prevents the two from drifting apart).
 */
export function isReconciliationLikeReport(columnsIncluded?: string): boolean {
  return (
    columnsIncluded === "reconciliation_monthly" ||
    columnsIncluded === "bank_reconciliation_monthly"
  );
}
 
/**
 * Pull Merchant export column presets from the shared ExportView implementation.
 * This ensures the rebranded panel stays consistent with the canonical column groups.
 */
export function getMerchantTableColumnsContext({
  tab,
  isEnterprisePortal,
  isPendingTab,
  isMerchantProcessorEnabled,
}: {
  tab?: string;
  isEnterprisePortal: boolean;
  isPendingTab: boolean;
  isMerchantProcessorEnabled: boolean;
}): { visibleColumns: string[]; reportColumns: MerchantReportColumns } {
  const merchantColumns = getMerchantExportColumns(
    isEnterprisePortal,
    isPendingTab,
    isMerchantProcessorEnabled,
  ) as any;
 
  const normalizeStringArray = (value: unknown): string[] =>
    Array.isArray(value)
      ? value.filter((v): v is string => typeof v === "string")
      : [];
 
  const visible = merchantColumns?.visible;
  const usedTab = tab || "default";
  const rawVisible = Array.isArray(visible)
    ? visible
    : visible?.[usedTab] || visible?.default || [];
  const visibleColumns = normalizeStringArray(rawVisible);
 
  const reportColumns: MerchantReportColumns = {
    monthly: normalizeStringArray(merchantColumns?.monthly),
    reserveMonthly: normalizeStringArray(merchantColumns?.reserveMonthly),
    reconciliationMonthly: normalizeStringArray(
      merchantColumns?.reconciliationMonthly,
    ),
    newMerchantApproval: normalizeStringArray(
      merchantColumns?.newMerchantApproval,
    ),
    ofacBusiness: normalizeStringArray(merchantColumns?.ofacBusiness),
    ofacPersons: normalizeStringArray(merchantColumns?.ofacPersons),
  };
 
  return { visibleColumns, reportColumns };
}
 
/**
 * "All columns" for merchant table is the union of all checkbox groups we render
 * in Advanced Settings (Merchant Info + Sales + Risk + Owners + Business + OFAC).
 *
 * NOTE: OFAC columns are excluded when pending tab hides them.
 */
export function getMerchantTableAllColumnValues({
  isEnterprisePortal,
  isPendingTab,
  isMerchantProcessorEnabled,
}: {
  isEnterprisePortal: boolean;
  isPendingTab: boolean;
  isMerchantProcessorEnabled: boolean;
}) {
  return [
    ...getMerchantInfoOptions(
      isMerchantProcessorEnabled,
      isEnterprisePortal,
    ).map((o) => o.value),
    ...salesInfo.map((o) => o.value),
    ...riskMonitor.map((o) => o.value),
    ...primaryAccountHolder.map((o) => o.value),
    ...businessOwner.map((o) => o.value),
    ...businessProfile.map((o) => o.value),
    ...(isPendingTab ? [] : ofacInfo.map((o) => o.value)),
  ];
}
 
/**
 * Given the currently selected checkbox values, detect whether it exactly matches
 * one of the known presets. If so, we can keep the "Data Selection" UI in sync.
 */
export function matchMerchantPresetFromColumns({
  columns,
  visibleColumns,
  reportColumns,
  allColumns,
}: {
  columns: string[];
  visibleColumns: string[];
  allColumns: string[];
  reportColumns: MerchantReportColumns;
}): MerchantColumnsIncluded | null {
  const matchesExact = (a: string[], b: string[]) =>
    a.length === b.length && a.every((c) => b.includes(c));
 
  if (matchesExact(columns, allColumns)) return "all";
  if (matchesExact(columns, visibleColumns)) return "visible";
  if (
    reportColumns.monthly.length &&
    matchesExact(columns, reportColumns.monthly)
  )
    return "processing_monthly";
  if (
    reportColumns.reserveMonthly.length &&
    matchesExact(columns, reportColumns.reserveMonthly)
  )
    return "reserve_monthly";
  Iif (
    reportColumns.newMerchantApproval.length &&
    matchesExact(columns, reportColumns.newMerchantApproval)
  )
    return "new_merchant_approval";
  Iif (
    reportColumns.ofacBusiness.length &&
    matchesExact(columns, reportColumns.ofacBusiness)
  )
    return "ofac_business";
  Iif (
    reportColumns.ofacPersons.length &&
    matchesExact(columns, reportColumns.ofacPersons)
  )
    return "ofac_persons";
 
  return null;
}