@@ -3,6 +3,31 @@ const functions = require('firebase-functions');
33const admin = require ( 'firebase-admin' ) ;
44const { DOCUMENT_TYPES , normalizeDocumentType } = require ( './constants/documentTypes' ) ;
55
6+ /**
7+ * Store debugging data in Firestore
8+ * @param {string } userId - User ID
9+ * @param {string } prompt - OpenAI prompt
10+ * @param {string } response - OpenAI response
11+ */
12+ async function storeDebugData ( userId , prompt , response ) {
13+ try {
14+ const db = admin . firestore ( ) ;
15+ const debugRef = db . collection ( 'users' ) . doc ( userId ) . collection ( 'debug' ) . doc ( ) ;
16+
17+ await debugRef . set ( {
18+ prompt,
19+ response,
20+ timestamp : admin . firestore . FieldValue . serverTimestamp ( )
21+ } ) ;
22+
23+ console . log ( `Debug data stored with ID: ${ debugRef . id } ` ) ;
24+ return debugRef . id ;
25+ } catch ( error ) {
26+ console . error ( "Error storing debug data:" , error ) ;
27+ // Don't throw - this is just for debugging
28+ }
29+ }
30+
631/**
732 * Formats all document data using a single OpenAI API call to ensure consistent structure
833 * @param {string } userId - The user ID
@@ -90,10 +115,14 @@ exports.formatDocumentsData = async (userId) => {
90115 } ) ;
91116
92117 // Extract and parse the JSON response
118+ const responseContent = response . choices [ 0 ] . message . content ;
93119 console . log ( "===== OPENAI RESPONSE =====" ) ;
94- console . log ( response . choices [ 0 ] . message . content ) ;
120+ console . log ( responseContent ) ;
121+
122+ // Store debug data
123+ await storeDebugData ( userId , prompt , responseContent ) ;
95124
96- const formattedData = JSON . parse ( response . choices [ 0 ] . message . content ) ;
125+ const formattedData = JSON . parse ( responseContent ) ;
97126 console . log ( "===== PARSED FORMATTED DATA =====" ) ;
98127 console . log ( JSON . stringify ( formattedData , null , 2 ) ) ;
99128
0 commit comments