Skip to content

Commit ce01296

Browse files
committed
Refactor report processing status handling and improve upload modal behavior
1 parent 6e05f2a commit ce01296

File tree

5 files changed

+47
-38
lines changed

5 files changed

+47
-38
lines changed

backend/src/reports/reports.controller.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,14 @@ export class ReportsController {
9292
throw new NotFoundException('Processing failed');
9393
}
9494

95+
if (report.processingStatus === ProcessingStatus.IN_PROGRESS) {
96+
throw new NotFoundException('Processing in progress');
97+
}
98+
99+
if (report.processingStatus === ProcessingStatus.UNPROCESSED) {
100+
throw new NotFoundException('Processing pending');
101+
}
102+
95103
return report;
96104
}
97105

backend/src/reports/reports.service.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,23 +55,23 @@ export class ReportsService {
5555
this.tableName = this.configService.get<string>('dynamodbReportsTable')!;
5656
}
5757

58-
async findAll(userId: string, withFailed = false): Promise<Report[]> {
58+
async findAll(userId: string, onlyProcessed = true): Promise<Report[]> {
5959
if (!userId) {
6060
throw new ForbiddenException('User ID is required');
6161
}
6262

6363
try {
6464
const expressionAttributeValues: any = { ':userId': userId };
65-
const processingStatusFilter = 'processingStatus <> :failedStatus';
65+
const processingStatusFilter = 'processingStatus = :processedStatus';
6666

67-
if (!withFailed) {
68-
expressionAttributeValues[':failedStatus'] = ProcessingStatus.FAILED;
67+
if (onlyProcessed) {
68+
expressionAttributeValues[':processedStatus'] = ProcessingStatus.PROCESSED;
6969
}
7070

7171
const command = new QueryCommand({
7272
TableName: this.tableName,
7373
KeyConditionExpression: 'userId = :userId',
74-
FilterExpression: !withFailed ? processingStatusFilter : undefined,
74+
FilterExpression: onlyProcessed ? processingStatusFilter : undefined,
7575
ExpressionAttributeValues: marshall(expressionAttributeValues),
7676
});
7777

@@ -100,7 +100,7 @@ export class ReportsService {
100100
async findLatest(
101101
queryDto: GetReportsQueryDto,
102102
userId: string,
103-
withFailed = false,
103+
onlyProcessed = true,
104104
): Promise<Report[]> {
105105
this.logger.log(
106106
`Running findLatest with params: ${JSON.stringify(queryDto)} for user ${userId}`,
@@ -116,17 +116,17 @@ export class ReportsService {
116116
const expressionAttributeValues: any = { ':userId': userId };
117117

118118
try {
119-
const processingStatusFilter = 'processingStatus <> :failedStatus';
119+
const processingStatusFilter = 'processingStatus = :processedStatus';
120120

121-
if (!withFailed) {
122-
expressionAttributeValues[':failedStatus'] = ProcessingStatus.FAILED;
121+
if (onlyProcessed) {
122+
expressionAttributeValues[':processedStatus'] = ProcessingStatus.PROCESSED;
123123
}
124124

125125
const command = new QueryCommand({
126126
TableName: this.tableName,
127127
IndexName: 'userIdCreatedAtIndex',
128128
KeyConditionExpression: 'userId = :userId',
129-
FilterExpression: !withFailed ? processingStatusFilter : undefined,
129+
FilterExpression: onlyProcessed ? processingStatusFilter : undefined,
130130
ExpressionAttributeValues: marshall(expressionAttributeValues),
131131
ScanIndexForward: false,
132132
Limit: limit,

frontend/src/common/components/Upload/UploadModal.tsx

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -53,25 +53,27 @@ const UploadModal = ({ isOpen, onClose, onUploadComplete }: UploadModalProps): J
5353
} = useFileUpload({
5454
// Override onUploadComplete to store the result and not call the parent immediately
5555
// eslint-disable-next-line @typescript-eslint/no-explicit-any
56-
onUploadComplete: (result: any) => {
56+
onUploadComplete: async (result: any) => {
5757
setUploadResult(result);
5858

59-
// Automatically redirect to processing screen after 2 seconds
60-
setTimeout(() => {
61-
reset();
62-
onClose();
63-
if (onUploadComplete) {
64-
onUploadComplete(result);
65-
}
66-
// Navigate to the processing tab with reportId in state
67-
if (file) {
68-
history.push('/tabs/processing', {
69-
reportId: result.id,
70-
});
71-
} else {
72-
history.push('/tabs/processing');
73-
}
74-
}, 2000);
59+
// sleep for two seconds
60+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
61+
await new Promise((resolve: any) => setTimeout(resolve, 2000));
62+
63+
reset();
64+
if (onUploadComplete) {
65+
onUploadComplete(result);
66+
}
67+
68+
// Automatically redirect to processing screen
69+
// Navigate to the processing tab with reportId in state
70+
if (file) {
71+
history.push('/tabs/processing', {
72+
reportId: result.id,
73+
});
74+
} else {
75+
history.push('/tabs/processing');
76+
}
7577
},
7678
});
7779

frontend/src/common/hooks/useFileUpload.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export enum UploadStatus {
2020
}
2121

2222
interface UseFileUploadOptions {
23-
onUploadComplete?: (result: MedicalReport) => void;
23+
onUploadComplete?: (result: MedicalReport) => Promise<void>;
2424
}
2525

2626
interface UseFileUploadResult {
@@ -183,7 +183,7 @@ export const useFileUpload = ({
183183
setStatus(UploadStatus.SUCCESS);
184184

185185
if (onUploadComplete) {
186-
onUploadComplete(result);
186+
await onUploadComplete(result);
187187
}
188188
} catch (error) {
189189
handleUploadError(error as Error, signal);

frontend/src/pages/Upload/UploadPage.tsx

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { IonContent, IonHeader, IonPage, IonTitle, IonToolbar } from '@ionic/react';
22
import { useTranslation } from 'react-i18next';
33
import { useEffect, useState } from 'react';
4-
import { useHistory } from 'react-router-dom';
4+
import { useHistory, useLocation } from 'react-router-dom';
55
import UploadModal from 'common/components/Upload/UploadModal';
66

77
/**
@@ -10,26 +10,25 @@ import UploadModal from 'common/components/Upload/UploadModal';
1010
*/
1111
const UploadPage = (): JSX.Element => {
1212
const { t } = useTranslation();
13-
const [isModalOpen, setIsModalOpen] = useState(true);
13+
const [isModalOpen, setIsModalOpen] = useState(false);
1414
const history = useHistory();
15+
const location = useLocation();
1516

16-
const handleUploadComplete = () => {
17-
// Close the modal
18-
setIsModalOpen(false);
17+
const handleUploadComplete = () => setIsModalOpen(false);
1918

20-
// Navigate to home page to see the newly uploaded report
19+
const handleCloseComplete = () => {
20+
setIsModalOpen(false);
2121
history.push('/tabs/home');
2222
};
2323

2424
useEffect(() => {
25-
// Automatically open the upload modal when the component mounts
2625
setIsModalOpen(true);
2726

2827
// Cleanup function to close the modal when the component unmounts
2928
return () => {
3029
setIsModalOpen(false);
3130
};
32-
}, []);
31+
}, [location.pathname]);
3332

3433
return (
3534
<IonPage>
@@ -41,7 +40,7 @@ const UploadPage = (): JSX.Element => {
4140
<IonContent>
4241
<UploadModal
4342
isOpen={isModalOpen}
44-
onClose={() => setIsModalOpen(false)}
43+
onClose={handleCloseComplete}
4544
onUploadComplete={handleUploadComplete}
4645
/>
4746
</IonContent>

0 commit comments

Comments
 (0)