All files / src/features/GiveOnboarding/pages/Business/Owners util.ts

80.76% Statements 42/52
58.82% Branches 30/51
100% Functions 3/3
80.76% Lines 42/52

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                10x     1x                                                       10x       3x   3x   3x 3x 1x       1x     1x 1x 2x                                     3x               4x   4x               4x 4x   4x 56x 12x   44x             44x         6x           4x   3x 1x 1x 1x 1x   2x       1x 1x 1x 1x 1x       4x   1x 1x     4x   4x 4x       4x    
import { NewOnboardingTBusinessOwner } from "@components/common/BusinessOwners/types";
import { IBusinessOwnerPayload } from "@features/GiveOnboarding/types/form";
import { IBusinessOwnerFormValues } from "@features/GiveOnboarding/types/form";
import { formatInUTCMoment, toISODateString } from "@utils/date.helpers";
import { formatPhone } from "@utils/helpers";
import { UPLOAD_STATUSES_ENUM } from "@hooks/upload-api/uploadHooks";
import moment from "moment";
 
export const parseApiDataToFormData = (
  ownerData: NewOnboardingTBusinessOwner,
) => {
  return {
    firstName: ownerData.firstName,
    id: ownerData.id,
    lastName: ownerData.lastName,
    email: ownerData.email,
    ownership: ownerData.ownershipPercentage,
    documentType: ownerData.docIDNumberType || "passport_id",
    documentNumber: ownerData.docIDNumberLast4,
    idImageUrl: ownerData.idImageURL,
    countryOfResidence: ownerData.countryOfResidence || "US",
    citizenship: ownerData.citizenship || "US",
    phone: formatPhone(ownerData.phoneNumber) ?? "",
    dob: formatInUTCMoment(ownerData.dateOfBirth) || "",
    address: {
      line1: ownerData?.address?.line1,
      city: ownerData?.address?.city,
      state: ownerData?.address?.state,
      zip: ownerData?.address?.zip,
      country: ownerData?.address?.country,
    },
    ssn: ownerData.taxIDNumberLast4,
    tinType: ownerData.tinType || "ssn",
    files: {
      allFiles: [],
    },
  };
};
 
export const uploadDocument = async (
  files: any,
  handleUploadPresignedDocument: any,
) => {
  const firstFile = files?.allFiles?.[0] as any;
  // if file doesnt have meta field, that means the file is from BE, so no upload required
  const isLocalFile = !!files?.allFiles[0]?.meta;
 
  let newUploadedFileUrl = "";
  if (isLocalFile) {
    const presignedUploadRes = await handleUploadPresignedDocument({
      list: files?.allFiles,
      attachmentType: "legal_principal",
    });
    Iif (presignedUploadRes === UPLOAD_STATUSES_ENUM.failed) {
      throw new Error(UPLOAD_STATUSES_ENUM.failed);
    }
    Eif (Array.isArray(presignedUploadRes))
      newUploadedFileUrl = presignedUploadRes[0];
  } else Iif (!isLocalFile && firstFile?.meta?.previewUrl) {
    if (firstFile.file) {
      const presignedImage = await handleUploadPresignedDocument({
        list: [
          {
            file: firstFile.file,
            id: firstFile.id,
          },
        ],
        attachmentType: "legal_principal",
      });
      if (presignedImage === UPLOAD_STATUSES_ENUM.failed) {
        throw new Error(UPLOAD_STATUSES_ENUM.failed);
      }
      if (Array.isArray(presignedImage))
        newUploadedFileUrl = presignedImage[0];
    }
  }
 
  return newUploadedFileUrl;
};
 
export function getPayload(
  data: IBusinessOwnerFormValues,
  dirtyFields: Partial<Readonly<any>>,
  withPrefilled: boolean,
) {
  const payload: Partial<IBusinessOwnerPayload> = {};
 
  const prefillFields: (keyof IBusinessOwnerFormValues)[] = [
    "firstName",
    "lastName",
    "dob",
    "email",
    "phone",
  ];
 
  const isUSCitizen = data.citizenship === "US";
  const countryOfResidence = data.countryOfResidence;
 
  for (const key in data) {
    if (["files", "ssn", "ownership"].includes(key)) {
      continue;
    }
    Iif (key === "idImageUrl") {
      if (!dirtyFields.files && !withPrefilled) {
        continue;
      } else {
        payload.idImageUrl = data.idImageUrl;
      }
    }
    if (
      dirtyFields[key as keyof IBusinessOwnerFormValues] ||
      (withPrefilled &&
        prefillFields.includes(key as keyof IBusinessOwnerFormValues))
    ) {
      (payload as any)[key as keyof IBusinessOwnerFormValues] =
        data[key as keyof IBusinessOwnerFormValues];
    }
  }
 
  // Now add document info conditionally
  if (isUSCitizen) {
    // Resident OR US Citizen (even if not resident): send SSN
    if (dirtyFields["ssn"]) {
      payload.taxIdNumber = data.ssn;
      payload.docIDNumber = null;
      payload.docIDNumberType = null;
      payload.citizenship = "US";
    } else {
      payload.citizenship = "US";
    }
  } else {
    // Not resident AND not US citizen: send Document Number
    Eif (dirtyFields["documentNumber"]) {
      payload.docIDNumber = data.documentNumber;
      payload.docIDNumberType = data.documentType;
      payload.taxIdNumber = null;
      payload.tinType = null;
    }
  }
 
  if (countryOfResidence !== data.citizenship) {
    //to unblock the navigation, is neded because BE stil throwns an error
    payload.countryOfResidence = data.countryOfResidence;
    payload.citizenship = data.citizenship;
  }
 
  payload.ownershipPercentage = Number(data.ownership);
 
  payload.id = data.id;
  payload.dateOfBirth = data.dob
    ? toISODateString(data.dob as Date | string)
    : undefined;
 
  return payload;
}