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

100% Statements 55/55
94.73% Branches 18/19
100% Functions 3/3
100% Lines 54/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                                                                    20x           19x 7x 7x 7x                 19x 5x 5x 5x       19x         19x     2x 2x 2x     2x 2x 2x     5x 5x 5x     2x       2x 2x 2x 2x     2x 2x 2x 2x 2x 2x             2x 2x       2x 2x 2x 2x 2x       1x       1x 1x 1x 1x     1x 1x 1x 1x 1x     1x 1x 1x 1x 1x   1x      
import type { UseFormReturn } from "react-hook-form";
import type {
  FormValues,
  MerchantColumnsIncluded,
  MerchantReportColumns,
} from "@shared/GiveExportModal/giveExportTypes";
import {
  fileNamePrefixNewMerchantApprovalReport,
  reportTypeMap,
} from "@shared/GiveExportModal/const";
import { PLATFORM_TIMEZONE } from "@utils/timezones";
import { buildReserveMonthlyFileName } from "./merchantTableReserveDate";
 
type Props = {
  columnsIncluded?: MerchantColumnsIncluded | string;
  methods: UseFormReturn<FormValues>;
  merchantAllColumnValues: string[];
  merchantVisibleColumns: string[];
  merchantReportColumns: MerchantReportColumns;
  timestamp: string;
  currentTimezone: string;
  setIsOpenAdvancedSettings: (v: boolean) => void;
};
 
export function applyMerchantColumnsIncludedSelection({
  columnsIncluded,
  methods,
  merchantAllColumnValues,
  merchantVisibleColumns,
  merchantReportColumns,
  timestamp,
  currentTimezone,
  setIsOpenAdvancedSettings,
}: Props) {
  if (!columnsIncluded) return;
 
  /**
   * These presets always export "current month".
   * We store it in the shared `dateRange` object used by the radio UI + payload builder.
   */
  const getCurrentMonthRange = (timeZone: string = currentTimezone) => {
    const now = new Date();
    const start = new Date(now.getFullYear(), now.getMonth(), 1);
    return {
      value: "current_month",
      label: "Current Month",
      startDate: start,
      endDate: now,
      timeZone,
    };
  };
 
  const setCurrentMonthRange = (timeZone: string = currentTimezone) => {
    const range = getCurrentMonthRange(timeZone);
    methods.setValue("dateRange", range);
    return range;
  };
 
  // Keep `reportType` aligned for payload building and downstream behavior.
  methods.setValue(
    "reportType",
    reportTypeMap[String(columnsIncluded)] || "merchant_report",
  );
 
  switch (columnsIncluded) {
    case "custom":
      // Custom means: user will pick columns manually, so open Advanced Settings.
      methods.setValue("fileName", `GIVE_MERCHANTS_${timestamp}`);
      setIsOpenAdvancedSettings(true);
      return;
    case "all":
      // Select all available checkbox columns.
      methods.setValue("fileName", `GIVE_MERCHANTS_${timestamp}`);
      methods.setValue("columns", merchantAllColumnValues);
      return;
    case "visible":
      // Select the "visible columns" preset for the current merchant tab.
      methods.setValue("fileName", `GIVE_MERCHANTS_${timestamp}`);
      methods.setValue("columns", merchantVisibleColumns);
      return;
    case "processing_monthly":
      // Preset report: forces All Data + current-month range (ET) + monthly columns.
      methods.setValue(
        "fileName",
        `GIVE_MONTHLY_MERCHANT_PROCESSING_${timestamp}`,
      );
      methods.setValue("type", { value: "all", label: "All Data" });
      methods.setValue("columns", merchantReportColumns.monthly);
      setCurrentMonthRange(PLATFORM_TIMEZONE);
      return;
    case "reserve_monthly": {
      // Preset report: forces All Data + current-month range (ET) + reserve monthly columns.
      const range = getCurrentMonthRange(PLATFORM_TIMEZONE);
      methods.setValue("fileName", buildReserveMonthlyFileName(range));
      methods.setValue("type", { value: "all", label: "All Data" });
      methods.setValue("columns", merchantReportColumns.reserveMonthly);
      methods.setValue("dateRange", range);
      return;
    }
    case "reconciliation_monthly":
    case "bank_reconciliation_monthly": {
      // Preset reports: async GET endpoint, All Time by default, no column preset.
      // The two differ only in the file-name scope token.
      const reconciliationScope =
        columnsIncluded === "bank_reconciliation_monthly" ? "BANK" : "MERCHANT";
      methods.setValue(
        "fileName",
        `GIVE_ALL TIME_${reconciliationScope}_RECONCILIATION_${timestamp}`,
      );
      methods.setValue("type", { value: "all", label: "All Data" });
      methods.setValue("allTime", true);
      methods.setValue("columns", []);
      methods.setValue("dateRange", { value: "all_time", label: "All Time" });
      return;
    }
    case "new_merchant_approval":
      // Preset report: forces All Data + current-month range + approval columns.
      methods.setValue(
        "fileName",
        `${fileNamePrefixNewMerchantApprovalReport}${timestamp}`,
      );
      methods.setValue("type", { value: "all", label: "All Data" });
      methods.setValue("columns", merchantReportColumns.newMerchantApproval);
      setCurrentMonthRange();
      return;
    case "ofac_business":
      // Preset report: forces All Data + current-month range + OFAC business columns.
      methods.setValue("fileName", `GIVE_OFAC_BUSINESS_${timestamp}`);
      methods.setValue("type", { value: "all", label: "All Data" });
      methods.setValue("columns", merchantReportColumns.ofacBusiness);
      setCurrentMonthRange();
      return;
    case "ofac_persons":
      // Preset report: forces All Data + current-month range + OFAC persons columns.
      methods.setValue("fileName", `GIVE_OFAC_PERSONS_${timestamp}`);
      methods.setValue("type", { value: "all", label: "All Data" });
      methods.setValue("columns", merchantReportColumns.ofacPersons);
      setCurrentMonthRange();
      return;
    default:
      return;
  }
}