-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbusiness-profile.ts
More file actions
139 lines (113 loc) · 4.16 KB
/
business-profile.ts
File metadata and controls
139 lines (113 loc) · 4.16 KB
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
import { DocumentResponse, PresignedUploadResponse, DocumentCategories, DocumentTypes } from "@/types/documents";
import { authHeader, clientAuthWrapper, getClient } from "./client";
import { getCompany } from "./company";
import { gzip } from "pako";
export const getAllDocuments = async (): Promise<DocumentResponse[]> => {
const req = async (token: string): Promise<DocumentResponse[]> => {
const companyId = (await getCompany()).id;
const documentType = DocumentTypes.GENERAL_BUSINESS;
const client = getClient();
const { data, error, response } = await client.GET("/s3/getAllDocuments", {
headers: authHeader(token),
params: {
query: {
companyId,
documentType,
},
},
});
if (!response.ok || !data) {
throw new Error(error?.error || "Failed to fetch documents");
}
return data;
};
return clientAuthWrapper<DocumentResponse[]>()(req);
};
export async function getBusinessDocumentUploadUrl(
fileName: string,
fileType: string
): Promise<PresignedUploadResponse> {
const req = async (token: string): Promise<PresignedUploadResponse> => {
const companyId = (await getCompany()).id;
const client = getClient();
const { data, error, response } = await client.POST("/s3/getUploadUrl", {
headers: authHeader(token),
body: {
companyId,
fileName,
fileType,
documentType: DocumentTypes.GENERAL_BUSINESS,
},
});
if (!response.ok || !data) {
throw new Error(error?.error || "Failed to get upload URL");
}
return data;
};
return clientAuthWrapper<PresignedUploadResponse>()(req);
}
export async function confirmBusinessDocumentUpload(
key: string,
documentId: string,
category?: DocumentCategories
): Promise<void> {
const req = async (token: string): Promise<void> => {
const client = getClient();
const companyId = (await getCompany()).id;
const { error, response } = await client.POST("/s3/confirmUpload", {
headers: authHeader(token),
body: {
key,
documentId,
documentType: DocumentTypes.GENERAL_BUSINESS,
companyId,
category: category || null,
},
});
if (!response.ok) {
throw new Error(error?.error || "Failed to confirm upload");
}
};
return clientAuthWrapper<void>()(req);
}
export async function updateDocumentCategory(documentId: string, category: DocumentCategories): Promise<void> {
const req = async (token: string): Promise<void> => {
const client = getClient();
const { error, response } = await client.PATCH("/s3/updateDocumentCategory", {
headers: authHeader(token),
body: { documentId, category },
});
if (!response.ok) {
throw new Error(error?.error || "Failed to update category");
}
};
return clientAuthWrapper<void>()(req);
}
export async function deleteBusinessDocument(key: string, documentId: string): Promise<void> {
const req = async (token: string): Promise<void> => {
const client = getClient();
const { error, response } = await client.DELETE("/s3/deleteDocument", {
headers: authHeader(token),
body: { key, documentId },
});
if (!response.ok) {
throw new Error(error?.error || "Failed to delete document");
}
};
return clientAuthWrapper<void>()(req);
}
export async function uploadToS3(uploadUrl: string, file: File): Promise<void> {
const arrayBuffer = await file.arrayBuffer();
const compressed = gzip(new Uint8Array(arrayBuffer));
const response = await fetch(uploadUrl, {
method: "PUT",
body: compressed,
headers: {
"Content-Type": file.type,
"Content-Encoding": "gzip",
},
});
if (!response.ok) {
throw new Error(`Upload failed: ${response.statusText}`);
}
}