All files / src/components/Settings/Business/Documents utils.ts

10.71% Statements 3/28
0% Branches 0/10
0% Functions 0/7
12% Lines 3/25

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                124x                               124x                       124x                                                                                                                                
import { showMessage } from "@common/Toast";
import axios from "axios";
import JSZip from "jszip";
import { saveAs } from "file-saver";
import { customInstance } from "@services/api";
import { TMerchantDocument } from "@components/Merchants/MerchantPreview/data.types";
import { TPosition } from "@common/Toast/ShowToast";
 
const downloadZipSingleImage = async (
  zipInstance: JSZip,
  item: TMerchantDocument,
) => {
  try {
    const res = await axios({
      url: item.fileURL,
      method: "GET",
      responseType: "blob",
    });
    return zipInstance.file(item.fileName, res.data);
  } catch (error) {
    showMessage("Error", "One or more files could not be downloaded");
  }
};
 
export const downloadAll = async (fileArr: TMerchantDocument[]) => {
  const zip = new JSZip();
  const fileArray = await Promise.all(
    fileArr.map(async (item) => await downloadZipSingleImage(zip, item)),
  );
  if (fileArray.every((item) => item === undefined)) return;
 
  zip.generateAsync({ type: "blob" }).then(function (blob) {
    saveAs(blob, "documents.zip");
  });
};
 
export const deleteDocumentAPI = async (
  id: number,
  fileID: number,
  onEnd?: () => void,
  isDesktop?: boolean,
  hideDeleteMessage?: boolean,
  onError?: () => void,
) => {
  const ToastPosition: TPosition | undefined = !isDesktop
    ? {
        position: "top-left",
        style: { top: "47px", left: "7px" },
      }
    : undefined;
  try {
    await customInstance({
      url: `/accounts/${id}/files/${fileID}`,
      method: "DELETE",
    });
    !hideDeleteMessage &&
      showMessage(
        "Success",
        "Your file is deleted successfully",
        false,
        "",
        undefined,
        ToastPosition,
      );
    if (onEnd) onEnd();
  } catch (error) {
    if (onError) {
      onError();
      return;
    }
    showMessage(
      "Warning",
      "Ups something went wrong",
      false,
      "",
      undefined,
      ToastPosition,
    );
  }
};
 
export async function deleteDocumentsInLoop(
  documents: any[],
  merchantID: number,
  hideDeleteMessage?: boolean,
) {
  for (const file of documents) {
    try {
      await deleteDocumentAPI(
        merchantID,
        file?.id,
        undefined,
        undefined,
        hideDeleteMessage,
      );
    } catch (error) {
      //
    }
  }
}