All files / src/hooks/merchant-api/manage-money useUploadBankAccountDocument.tsx

8.92% Statements 5/56
12% Branches 3/25
7.69% Functions 1/13
11.11% Lines 5/45

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                          101x 1514x                 101x                                                                                                   101x                                                                                                   101x                                                                      
import React from "react";
import axios from "axios";
import { customInstance } from "@services/api";
import { useAppSelector } from "@redux/hooks";
import { selectUser } from "@redux/slices/auth/auth";
import { isEmpty } from "lodash";
import {
  composePermission,
  useAccessControl,
} from "features/Permissions/AccessControl";
import RESOURCE_BASE, { OPERATIONS } from "@constants/permissions";
import { FileAttachmentType } from "@hooks/upload-api/uploadHooks";
 
export const useGetUploadFilePermission = (isEnterprise = false) => {
  return useAccessControl({
    resource: composePermission(
      isEnterprise ? RESOURCE_BASE.ENTERPRISE : RESOURCE_BASE.MERCHANT,
      RESOURCE_BASE.FILES,
    ),
    operation: OPERATIONS.CREATE,
  });
};
 
export const useUploadBankAccountDocument = (merchantId?: number) => {
  const { name } = useAppSelector(selectUser);
  const [isLoading, setIsLoading] = React.useState(false);
  const isUploadAllowed = useGetUploadFilePermission();
 
  const handleUpload = async (
    bankAccountId: number,
    list: File[],
    merchantName?: string,
  ) => {
    if (list.length === 0 || !merchantId) return;
    setIsLoading(true);
 
    const customDocumentList = list.map((item) => ({
      attachmentType: "bank_account",
      fileName: item.name,
      label: `${merchantName || name} bank account document`,
      tag: FileAttachmentType.BankAccountConfirm,
      resourceID: bankAccountId,
    }));
 
    const res = await customInstance({
      url: `accounts/${merchantId}/files`,
      method: "POST",
      data: { list: customDocumentList },
    });
 
    if (!res?.total || res?.total < 1) return;
 
    for (const [index, document] of res.data.entries()) {
      await axios.put(document.fileURL, list[index]);
    }
 
    const confirmedUploadedList = res.data.map((item: any) => ({
      id: item.id,
      isUploaded: true,
    }));
 
    await customInstance({
      url: `accounts/${merchantId}/files/confirm`,
      method: "POST",
      data: { list: confirmedUploadedList },
    });
 
    setIsLoading(false);
  };
 
  return { handleUpload, isLoading, isUploadAllowed };
};
 
export const useUploadDocument = (
  merchantId: number,
  payload: any,
  onEndUpload?: () => void,
) => {
  const [isLoading, setIsLoading] = React.useState(false);
  const isUploadAllowed = useGetUploadFilePermission();
 
  const handleUpload = async (list: File[], resourceID?: number) => {
    if (isEmpty(list)) return;
    setIsLoading(true);
 
    const customDocumentList = list.map((item) => ({
      attachmentType: payload.attachmentType,
      fileName: item.name,
      label: payload.label,
      tag: payload.tag,
      resourceID: resourceID ? resourceID : payload.resourceID,
    }));
 
    const res = await customInstance({
      url: `accounts/${merchantId}/files`,
      method: "POST",
      data: { list: customDocumentList },
    });
 
    if (!res?.total || res?.total < 1) return;
 
    for (const [index, document] of res.data.entries()) {
      await axios.put(document.fileURL, list[index]);
    }
 
    const confirmedUploadedList = res.data.map((item: any) => ({
      id: item.id,
      isUploaded: true,
    }));
 
    await customInstance({
      url: `accounts/${merchantId}/files/confirm`,
      method: "POST",
      data: { list: confirmedUploadedList },
    });
 
    setIsLoading(false);
    if (onEndUpload) onEndUpload();
  };
 
  return { handleUpload, isLoading, isUploadAllowed };
};
 
export const usePostUploadDocument = (payload: any) => {
  const [isLoading, setIsLoading] = React.useState(false);
 
  const handleUpload = async (list: File[]) => {
    if (list.length === 0) return;
    setIsLoading(true);
 
    const customDocumentList = list.map((item) => ({
      attachmentType: payload.attachmentType,
      fileName: item.name,
    }));
 
    const res = await Promise.all(
      customDocumentList.map((x) =>
        customInstance({
          url: `s3/presign-url`,
          method: "POST",
          data: x,
        }),
      ),
    );
 
    // await customInstance({
    //   url: `accounts/${merchantId}/files/confirm`,
    //   method: "POST",
    //   data: { list: confirmedUploadedList },
    // });
 
    setIsLoading(false);
 
    return res;
  };
 
  return { handleUpload, isLoading };
};