All files / src/components/ProfilePage/BusinessProfileSetupNew/hooks useGenerateWarning.tsx

53.48% Statements 23/43
42.42% Branches 14/33
50% Functions 5/10
54.76% Lines 23/42

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                          18x                                                                                   18x 102x 170x 170x                             18x 34x 34x   136x 136x   34x   34x 34x                                                                                 102x 102x           136x 136x 136x 34x 102x   136x                     34x                   34x        
import { useMemo } from "react";
import { DynamicReturnType } from "../helpers/refineData";
import {
  MERCHANT_FILE_TEXT,
  MISSING_BUSINESS_OWNER_INFO_TEXT,
} from "@constants/stringConstants";
 
type TProps = {
  parsedData: DynamicReturnType;
};
 
type FieldRequirement = string | { atLeastOne: string[] };
 
const requiredFieldsMap = {
  businessDetails: {
    tabName: "Business details",
    requiredFields: [
      "legalName",
      "businessOpenedAt",
      "businessType",
      "ownershipType",
      "phoneNumber",
      { atLeastOne: ["taxIDNumber", "ssn"] },
    ],
  },
  businessAddress: {
    tabName: "Business address",
    requiredFields: ["address", "city", "country", "state", "zip"],
  },
  businessOwner: {
    tabName: "Business owner",
    requiredFields: [
      "firstName",
      "lastName",
      "email",
      "dob",
      "ownership",
      "state",
      "street",
      "zip",
      "city",
      { atLeastOne: ["ssn", "documentNumber"] },
    ],
  },
  merchantInfo: {
    tabName: "Merchant Info",
    requiredFields: [
      "name",
      "websiteURL",
      "servicePhoneNumber",
      "billingDescriptor",
    ],
  },
};
 
const checkIncompleteFields = (data: any, fields: FieldRequirement[]) => {
  return fields.some((field) => {
    Eif (typeof field === "string") {
      return (
        !data[field] ||
        data[field]?.trim() === "" ||
        (field === "servicePhoneNumber" && data[field]?.trim() === "+1")
      );
    }
 
    if ("atLeastOne" in field) {
      return !field.atLeastOne.some((f) => data[f] && data[f].trim() !== "");
    }
 
    return false;
  });
};
 
const useGenerateWarning = ({ parsedData }: TProps) => {
  const results = useMemo(() => {
    const results = Object.entries(requiredFieldsMap).reduce(
      (acc, [key, { tabName, requiredFields }]) => {
        let isIncomplete = false;
        if (key === "businessOwner") {
          // Handle businessOwners separately
          const owners = parsedData.businessOwners;
 
          if (!owners) {
            isIncomplete = true;
          } else E{
            const isBOMissingInformation = owners.some((owner: any) => {
              const hasMissingRequiredField = requiredFields.some(
                (field: any) => {
                  if (field.atLeastOne) {
                    const hasAnyOfThem = field.atLeastOne.some(
                      (key: keyof typeof owner) => {
                        return Boolean(owner[key]);
                      },
                    );
                    return !hasAnyOfThem;
                  }
 
                  const value = owner[field as keyof typeof owner];
 
                  if (typeof value === "string") {
                    return value.trim() === "";
                  }
 
                  if (typeof value === "number") {
                    return Number.isNaN(value);
                  }
 
                  return value == null; // covers undefined or null
                },
              );
 
              return hasMissingRequiredField;
            });
 
            if (
              isBOMissingInformation ||
              // This check is added to check if BO is missing ID
              owners.some((item: any) => item?.files?.allFiles?.length === 0)
            ) {
              acc.incompleteTabs.push(MISSING_BUSINESS_OWNER_INFO_TEXT);
            }
          }
        } else {
          // Handle other tabs
          const data = parsedData[key as keyof typeof parsedData];
          isIncomplete =
            !data ||
            (typeof data === "object" &&
              checkIncompleteFields(data, requiredFields));
        }
 
        acc.completionStatus[key] = { isComplete: !isIncomplete, tabName };
        Eif (isIncomplete) {
          if (tabName === "Merchant Info")
            acc.incompleteTabs.push(MERCHANT_FILE_TEXT);
          else acc.incompleteTabs.push(tabName);
        }
        return acc;
      },
      { completionStatus: {}, incompleteTabs: [] } as {
        completionStatus: Record<
          string,
          { isComplete: boolean; tabName: string }
        >;
        incompleteTabs: string[];
      },
    );
 
    return {
      isIncomplete: results.incompleteTabs.length > 0,
      completionStatus: results.completionStatus,
      warningMessage:
        results.incompleteTabs.length > 0
          ? `${results.incompleteTabs.join(", ")}.`
          : null,
    };
  }, [parsedData]);
 
  return results;
};
 
export default useGenerateWarning;