Skip to content

Commit 1fe1a54

Browse files
committed
Refactor getAuthConfig and uploadReport in frontend/src/common/api/reportService.ts
- Updated getAuthConfig to include 'Accept' and 'Content-Type' headers for JSON requests. - Simplified uploadReport by removing unused documentUrl and reportData structure, sending only necessary filePath to the API. - Added error logging for better debugging of API errors during report uploads.
1 parent ac67e23 commit 1fe1a54

File tree

1 file changed

+10
-17
lines changed

1 file changed

+10
-17
lines changed

frontend/src/common/api/reportService.ts

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,14 @@ export interface UploadProgressCallback {
1414
/**
1515
* Creates an authenticated request config with bearer token
1616
*/
17-
export const getAuthConfig = async (): Promise<{ headers: { Authorization: string }, onUploadProgress?: (progressEvent: AxiosProgressEvent) => void }> => {
17+
export const getAuthConfig = async (): Promise<{ headers: { Accept: string, 'Content-Type': string, Authorization: string }, onUploadProgress?: (progressEvent: AxiosProgressEvent) => void }> => {
1818
const session = await fetchAuthSession();
19+
const idToken = session.tokens?.idToken?.toString() || '';
1920
return {
20-
headers: {
21-
Authorization: session.tokens?.idToken ? `Bearer ${session.tokens.idToken.toString()}` : ''
21+
headers: {
22+
Accept: 'application/json',
23+
'Content-Type': 'application/json',
24+
Authorization: idToken ? `Bearer ${idToken}` : ''
2225
}
2326
};
2427
};
@@ -53,33 +56,23 @@ export const uploadReport = async (
5356
'reports',
5457
onProgress as (progress: number) => void
5558
);
56-
57-
// Get a signed URL for the uploaded file
58-
const documentUrl = await s3StorageService.getSignedUrl(s3Key);
5959

6060
// Then create the report record with the S3 key
6161
const config = await getAuthConfig();
62-
63-
// Create report data
64-
const reportData = {
65-
title: file.name.split('.')[0], // Use filename without extension as title
66-
filePath: s3Key,
67-
fileName: file.name,
68-
fileType: file.type,
69-
fileSize: file.size,
70-
documentUrl
71-
};
7262

7363
// Send the report metadata to the API
7464
const response = await axios.post(
7565
`${API_URL}/api/reports`,
76-
reportData,
66+
{
67+
filePath: s3Key,
68+
},
7769
config
7870
);
7971

8072
return response.data;
8173
} catch (error) {
8274
if (axios.isAxiosError(error)) {
75+
console.error('API Error Details:', error.response?.data, error.response?.headers);
8376
throw new ReportError(`Failed to upload report: ${error.message}`);
8477
}
8578
throw new ReportError('Failed to upload report');

0 commit comments

Comments
 (0)