@@ -4,25 +4,27 @@ const pdfParse = require('pdf-parse');
44const tmp = require ( 'tmp' ) ;
55const fs = require ( 'fs' ) ;
66const OpenAI = require ( 'openai' ) ;
7+ const { FieldValue } = require ( 'firebase-admin/firestore' ) ;
78const { calculateCurrentGrade } = require ( './calculateGrade' ) ;
89const { predictFinalGrade } = require ( './predictGrade' ) ;
910const { formatDocumentsData } = require ( './formatDocumentsData' ) ;
1011const { DOCUMENT_TYPES , normalizeDocumentType } = require ( './constants/documentTypes' ) ;
1112
13+
1214// Initialize Firebase Admin with emulator support
1315if ( process . env . FUNCTIONS_EMULATOR ) {
1416 // Running in emulator
1517 admin . initializeApp ( {
1618 projectId : 'gradingai' ,
1719 storageBucket : 'gradingai.appspot.com' ,
1820 // This ensures Firestore uses the emulator
19- databaseURL : 'http://localhost:8080' ,
2021 } ) ;
2122 console . log ( 'Firebase Admin initialized for emulator environment' ) ;
2223} else {
2324 // Production environment
2425 admin . initializeApp ( ) ;
2526 console . log ( 'Firebase Admin initialized for production environment' ) ;
27+ const db = getFirestore ( ) ;
2628}
2729
2830// Export functions
@@ -167,7 +169,7 @@ exports.uploadDocument = functions.https.onCall(async (data, context) => {
167169 filePath : filePath ,
168170 documentType : normalizedDocType ,
169171 name : documentName ,
170- uploadedAt : admin . firestore . FieldValue . serverTimestamp ( ) ,
172+ uploadedAt : FieldValue . serverTimestamp ( ) ,
171173 status : 'uploaded'
172174 } ) ;
173175
@@ -366,7 +368,7 @@ exports.processPdfUpload = functions.storage.object().onFinalize(async (object)
366368 filePath : filePath ,
367369 documentType : documentType ,
368370 name : pathParts [ 3 ] ,
369- uploadedAt : admin . firestore . FieldValue . serverTimestamp ( ) ,
371+ uploadedAt : FieldValue . serverTimestamp ( ) ,
370372 status : 'uploaded'
371373 } ) ;
372374
@@ -450,7 +452,7 @@ async function extractTextFromPdf(userId, documentId, filePath) {
450452 console . log ( `Updating Firestore document with extracted text` ) ;
451453 await docRef . set ( {
452454 text : extractedText ,
453- lastExtracted : admin . firestore . FieldValue . serverTimestamp ( ) ,
455+ lastExtracted : FieldValue . serverTimestamp ( ) ,
454456 status : 'extracted' ,
455457 pageCount : pdfData . numpages
456458 } , { merge : true } ) ;
0 commit comments