-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathdocumentsService.ts
More file actions
152 lines (110 loc) · 4.98 KB
/
documentsService.ts
File metadata and controls
152 lines (110 loc) · 4.98 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
140
141
142
143
144
145
146
147
148
149
150
151
152
import { SearchRequest } from "../types/searchRequest";
import { httpClient } from "../utils/httpClient/httpClient";
import { DocumentResults } from "./apiTypes/documentResults";
import { Embedded } from "./apiTypes/embedded";
export async function searchDocuments(payload: SearchRequest): Promise<DocumentResults> {
const apiEndpoint = import.meta.env.VITE_API_ENDPOINT + '/Documents/GetDocuments'; // Ensure this is the correct endpoint
const requestBody = {
pageNumber: payload.currentPage || 1,
...(payload.startDate && { startDate: payload.startDate }),
...(payload.endDate && { endDate: payload.endDate }),
pageSize: 10,
keyword: payload.queryText, // Assuming queryText is a part of your SearchRequest
tags: {
// Here we ensure that tags is formatted correctly
...payload.filters // Spread the filters directly into tags
},
//Change to json body
};
try {
const response: DocumentResults = await httpClient.post(
apiEndpoint || '',
requestBody,
{
headers: {
'Content-Type': 'application/json' // Ensure the correct content type
}
}
);
return response;
} catch (error) {
console.error('Error searching documents:', error);
throw error; // Re-throw the error if needed for further handling
}
}
// Modify the importDocuments function to accept a FormData object instead of File[]
export const importDocuments = async (formData: FormData): Promise<any> => {
const apiEndpoint = `${import.meta.env.VITE_API_ENDPOINT}/Documents/ImportDocument`;
try {
const response = await httpClient.upload(apiEndpoint, formData);
return response;
} catch (error) {
console.error("Error uploading documents:", error);
throw error;
}
};
// function formatKeywords(keywords: { [key: string]: string }): { [key: string]: string } {
// // This function formats keywords into the desired comma-separated string for each category
// const formattedKeywords: { [key: string]: string } = {};
// Object.keys(keywords).forEach((category) => {
// const keywordList = keywords[category];
// if (Array.isArray(keywordList)) {
// // If the keywords are in an array, join them into a comma-separated string
// formattedKeywords[category] = keywordList.join(', ');
// } else {
// // If already a string (or incorrect format), preserve it
// formattedKeywords[category] = keywordList;
// }
// });
// return formattedKeywords;
// }
// Update in your documentsService file
export const downloadDocument = async (documentId: string, fileName: string): Promise<Blob> => {
const apiEndpoint = `${import.meta.env.VITE_API_ENDPOINT}/Documents/${documentId}/${encodeURIComponent(fileName)}`;
const response = await fetch(apiEndpoint, {
method: 'GET',
// headers: {
// 'Accept': 'application/pdf', // Adjust based on the document type
// },
});
// Check if the response is okay
if (!response.ok) {
const errorText = await response.text(); // Get error response if any
console.error(`Failed to download document. Status: ${response.status} - ${errorText}`);
throw new Error(`Failed to download document: ${response.statusText}`);
}
const blob = await response.blob(); // Convert response to Blob
// Check if blob is valid
if (!(blob instanceof Blob)) {
throw new Error('Response is not a Blob');
}
return blob; // Return the Blob directly
};
// export async function getCoverImage(indexKey: string) {
// const response: CoverImage = await httpClient.get(`${window.ENV.API_URL}/api/Documents/${indexKey}/Cover`);
// return response;
// }
export async function getEmbedded(indexKey: string) {
const response: Embedded = await httpClient.post(
`${window.ENV.API_URL}/api/Documents/${indexKey}/Embedded`
);
return response;
}
// export async function getDocument(documentId: string) {
// const response: SingleDocument = await httpClient.get(`${window.ENV.API_URL}/api/Documents/${documentId}`);
// return response;
// }
// export async function getMyDocuments(){
// const response: any = await httpClient.post(`${window.ENV.API_URL}/api/Documents/MyDocuments?currentPage=1&rowCount=20`);
// return response;
// }
// export async function toggleVisibility(documentId: string, isRestricted: boolean) {
// const response: Response = await httpClient.post(
// `${window.ENV.API_URL}/api/Documents/${documentId}/Visibility?isRestricted=${isRestricted}`
// );
// return response;
// }
// export async function deleteDocument(documentId: string) {
// const response: Response = await httpClient.delete(`${window.ENV.API_URL}/api/Documents/${documentId}`);
// return response;
// }