Skip to content

Commit 56c17c9

Browse files
committed
Enhance DocumentManager and DocumentProcessingStatus for improved document processing flow and error handling; update formatDocumentsData for consistent data structure and add detailed comments
1 parent 58d8a7e commit 56c17c9

File tree

4 files changed

+134
-162
lines changed

4 files changed

+134
-162
lines changed

frontend/src/components/DocumentManager.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,19 @@ const DocumentManager: React.FC = () => {
8787

8888
// Upload document
8989
const uploadDocument = httpsCallable(functions, 'uploadDocument');
90-
await uploadDocument({
90+
const result = await uploadDocument({
9191
documentType,
9292
documentName: file.name,
9393
documentBase64: base64String
9494
});
9595

96-
setStatus(`Uploaded ${file.name} as ${documentType}`);
96+
setStatus(`Uploaded ${file.name} as ${documentType}. Processing started.`);
97+
98+
// Store the document ID for reference
99+
const documentId = (result.data as any).documentId;
100+
if (documentId) {
101+
console.log(`Document ID: ${documentId}`);
102+
}
97103
resolve();
98104
} catch (err) {
99105
reject(err);

frontend/src/components/DocumentProcessingStatus.tsx

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -99,25 +99,26 @@ const DocumentProcessingStatus: React.FC<DocumentProcessingStatusProps> = ({ onP
9999
}
100100
};
101101

102-
// Handle manual text extraction
103-
const handleExtractText = async (documentId: string) => {
102+
// Handle document processing retry
103+
const handleRetryProcessing = async (documentId: string) => {
104104
if (!currentUser) return;
105105

106-
setStatus(`Extracting text for document ${documentId}...`);
106+
setStatus(`Retrying processing for document ${documentId}...`);
107107
setError(null);
108108

109109
try {
110-
const extractPdfText = httpsCallable(functions, 'extractPdfText');
111-
const result = await extractPdfText({ documentId });
110+
// We'll use the formatDocumentsData function to process all documents
111+
const formatDocumentsData = httpsCallable(functions, 'formatDocumentsData');
112+
const result = await formatDocumentsData({});
112113

113114
if ((result.data as any).success) {
114-
setStatus('Text extracted successfully');
115+
setStatus('Document processing initiated successfully');
115116
} else {
116-
setError((result.data as any).message || 'Failed to extract text');
117+
setError((result.data as any).message || 'Failed to process document');
117118
}
118119
} catch (err: any) {
119-
console.error('Error extracting text:', err);
120-
setError(`Text extraction failed: ${err.message || 'Unknown error'}`);
120+
console.error('Error processing document:', err);
121+
setError(`Processing failed: ${err.message || 'Unknown error'}`);
121122
}
122123
};
123124

@@ -252,18 +253,18 @@ const DocumentProcessingStatus: React.FC<DocumentProcessingStatusProps> = ({ onP
252253
<td style={styles.tableCell}>
253254
{doc.status === 'uploaded' && (
254255
<button
255-
onClick={() => handleExtractText(doc.id)}
256+
onClick={() => handleRetryProcessing(doc.id)}
256257
style={styles.actionButton}
257258
>
258-
Extract Text
259+
Process Document
259260
</button>
260261
)}
261262
{doc.status === 'error' && (
262263
<button
263-
onClick={() => handleExtractText(doc.id)}
264+
onClick={() => handleRetryProcessing(doc.id)}
264265
style={styles.actionButton}
265266
>
266-
Retry
267+
Retry Processing
267268
</button>
268269
)}
269270
{doc.error && (

functions-node/formatDocumentsData.js

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ const OpenAI = require('openai');
22
const functions = require('firebase-functions');
33
const admin = require('firebase-admin');
44

5+
/**
6+
* Formats all document data using a single OpenAI API call to ensure consistent structure
7+
* @param {string} userId - The user ID
8+
* @returns {Promise<Object>} Formatted data for calculations and predictions
9+
*/
510
/**
611
* Formats all document data using a single OpenAI API call to ensure consistent structure
712
* @param {string} userId - The user ID
@@ -50,7 +55,7 @@ exports.formatDocumentsData = async (userId) => {
5055
apiKey: apiKey
5156
});
5257

53-
// Create a prompt with all document texts
58+
// Create a unified prompt with all document texts
5459
const prompt = createFormattingPrompt(documentsByType);
5560

5661
// Call OpenAI API
@@ -87,6 +92,11 @@ exports.formatDocumentsData = async (userId) => {
8792
}
8893
};
8994

95+
/**
96+
* Creates the prompt for OpenAI formatting
97+
* @param {Object} documentsByType - Documents organized by type
98+
* @returns {string} Formatted prompt
99+
*/
90100
/**
91101
* Creates the prompt for OpenAI formatting
92102
* @param {Object} documentsByType - Documents organized by type
@@ -169,6 +179,12 @@ For the academicHistory.relevantCourses, analyze the transcript to find courses
169179
`;
170180
}
171181

182+
/**
183+
* Stores the formatted data in Firestore
184+
* @param {string} userId - The user ID
185+
* @param {Object} formattedData - The formatted data
186+
* @returns {Promise<void>}
187+
*/
172188
/**
173189
* Stores the formatted data in Firestore
174190
* @param {string} userId - The user ID
@@ -192,6 +208,12 @@ async function storeFormattedData(userId, formattedData) {
192208
}
193209
}
194210

211+
/**
212+
* Updates the status of processed documents
213+
* @param {string} userId - The user ID
214+
* @param {Array} documents - The document snapshots
215+
* @returns {Promise<void>}
216+
*/
195217
/**
196218
* Updates the status of processed documents
197219
* @param {string} userId - The user ID
@@ -215,6 +237,11 @@ async function updateDocumentStatus(userId, documents) {
215237
console.log('Successfully updated document statuses');
216238
}
217239

240+
/**
241+
* Creates a fallback formatted data structure if OpenAI fails
242+
* @param {Object} documentsByType - Documents organized by type
243+
* @returns {Object} Fallback formatted data
244+
*/
218245
/**
219246
* Creates a fallback formatted data structure if OpenAI fails
220247
* @param {Object} documentsByType - Documents organized by type
@@ -266,6 +293,11 @@ function createFallbackFormattedData(documentsByType) {
266293
};
267294
}
268295

296+
/**
297+
* Extract grade weights using regex patterns
298+
* @param {string} text - Text to extract grade weights from
299+
* @returns {Array} Array of {name, weight} objects
300+
*/
269301
/**
270302
* Extract grade weights using regex patterns
271303
* @param {string} text - Text to extract grade weights from
@@ -312,6 +344,9 @@ function extractGradeWeights(text) {
312344
}
313345
}
314346

347+
/**
348+
* Helper function to get OpenAI API key
349+
*/
315350
/**
316351
* Helper function to get OpenAI API key
317352
*/

0 commit comments

Comments
 (0)