All files / src/services/api/upload uploadEndpoints.ts

73.68% Statements 14/19
25% Branches 1/4
50% Functions 5/10
73.68% Lines 14/19

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        505x             1x                                 505x             16x                           505x             11x           505x                                   505x             505x             505x 2x               505x                     1x                                                             505x                      
import { customInstance } from "@services/api";
import { buildAccountsUrl } from "@services/api/utils.api";
import axios from "axios";
 
export const getS3UploadUrl = ({
  attachmentType,
  name,
}: {
  attachmentType: string;
  name: string;
}) =>
  customInstance({
    url: `s3/presign-url`,
    method: "POST",
    data: {
      attachmentType: attachmentType,
      fileName: name,
    },
  });
 
type Props = {
  merchantId: number;
  customDocumentList: any[];
  challengeSlugParam?: string; //TODO: to be removed fully later
  challengeIdParam?: number;
  isTaskIdParam?: boolean;
};
 
export const createAccountFiles = ({
  merchantId,
  customDocumentList,
  challengeSlugParam,
  challengeIdParam,
  isTaskIdParam,
}: Props) =>
  customInstance({
    url: `accounts/${merchantId}/files`,
    method: "POST",
    data: { list: customDocumentList },
    params: isTaskIdParam
      ? {
          task_id: challengeIdParam,
        }
      : {
          challenge_slug: challengeSlugParam,
          challenge_id: challengeIdParam,
        },
  });
 
export const confirmAccountFiles = ({
  merchantId,
  confirmedUploadedList,
}: {
  merchantId: number;
  confirmedUploadedList: any[];
}) =>
  customInstance({
    url: `accounts/${merchantId}/files/confirm`,
    method: "POST",
    data: { list: confirmedUploadedList },
  });
 
export const updateMerchant = ({
  merchantId,
  signatureURL,
  ...rest
}: {
  merchantId: number;
  signatureURL: string;
  [key: string]: any;
}) =>
  customInstance({
    url: `/merchants/${merchantId}`,
    method: "PATCH",
    data: {
      signatureURL,
      ...rest,
    },
  });
 
export const createImage = (data: any) =>
  customInstance({
    url: buildAccountsUrl("media-items"),
    method: "POST",
    data,
  });
 
export const updateUser = ({ accID, data }: { accID: number; data: any }) =>
  customInstance({
    url: `/users/${accID}`,
    method: "PATCH",
    data,
  });
 
export const getS3UploadUrl2 = ({ formType }: { formType: string }) =>
  customInstance({
    url: "s3/upload-form",
    method: "POST",
    data: {
      formType,
    },
  });
 
export const axiosUpload = ({
  s3Response,
  file,
  signal,
  setUploadProgress,
}: {
  s3Response: any;
  file: any;
  signal: any;
  setUploadProgress: any;
}) =>
  axios.post(
    s3Response.action,
    {
      key: s3Response.key,
      acl: s3Response.acl,
      "x-amz-date": s3Response["x-amz-date"],
      "x-amz-credential": s3Response["x-amz-credential"],
      "x-amz-algorithm": s3Response["x-amz-algorithm"],
      policy: s3Response.policy,
      "Content-Type": `image/${file?.name?.split(".")?.pop()}`,
      "X-Amz-Signature": s3Response["x-amz-signature"],
      file,
    },
    {
      signal,
      onUploadProgress: (progressEvent) => {
        setUploadProgress({
          key: s3Response.key,
          data: {
            progress: progressEvent.total
              ? Math.floor((progressEvent.loaded * 100) / progressEvent.total)
              : 0,
          },
        });
      },
      headers: {
        "Content-Type": "multipart/form-data",
      },
    },
  );
 
export const deleteFile = ({
  accountToRequest,
  identifier,
}: {
  accountToRequest: string;
  identifier: string;
}) =>
  customInstance({
    url: `/accounts/${accountToRequest}/media-items/${identifier}`,
    method: "DELETE",
  });