@@ -100,6 +100,12 @@ router.post('/message', optionalAuth, async (req, res) => {
100100 const userId = authUser ?. user_id || null ;
101101 const effectiveUserType = authUser ?. user_type || user_type || null ;
102102
103+ // DEBUG: Log userType flow
104+ console . log ( '[CHAT DEBUG] /message - authUser.user_type:' , authUser ?. user_type ) ;
105+ console . log ( '[CHAT DEBUG] /message - req.body.user_type:' , user_type ) ;
106+ console . log ( '[CHAT DEBUG] /message - effectiveUserType:' , effectiveUserType ) ;
107+ console . log ( '[CHAT DEBUG] /message - userId:' , userId ) ;
108+
103109 const convId = conversation_id || `conv_${ Date . now ( ) } _${ Math . floor ( Math . random ( ) * 1000 ) } ` ;
104110
105111 if ( userId ) {
@@ -251,9 +257,13 @@ router.post('/message', optionalAuth, async (req, res) => {
251257
252258router . get ( '/history/:userId' , requireAuth , async ( req , res ) => {
253259 const { userId } = req . params ;
260+ console . log ( '[CHAT DEBUG] /history/:userId - userId:' , userId , 'req.user.user_id:' , req . user . user_id ) ;
261+
254262 if ( req . user . user_type !== 'admin' && String ( req . user . user_id ) !== String ( userId ) ) {
263+ console . log ( '[CHAT DEBUG] /history - Forbidden: user_id mismatch' ) ;
255264 return res . status ( 403 ) . json ( { success : false , error : 'Forbidden' } ) ;
256265 }
266+
257267 const query = `
258268 SELECT m.conversation_id,
259269 COALESCE(c.title, m.conversation_id) as title,
@@ -266,8 +276,13 @@ router.get('/history/:userId', requireAuth, async (req, res) => {
266276 GROUP BY m.conversation_id
267277 ORDER BY updated_at DESC
268278 ` ;
279+
269280 global . db . all ( query , [ userId ] , ( err , rows ) => {
270- if ( err ) return res . status ( 500 ) . json ( { success : false , error : err . message } ) ;
281+ if ( err ) {
282+ console . log ( '[CHAT DEBUG] /history ERROR:' , err ) ;
283+ return res . status ( 500 ) . json ( { success : false , error : err . message } ) ;
284+ }
285+ console . log ( '[CHAT DEBUG] /history SUCCESS - rows:' , rows ?. length || 0 , 'data:' , rows ) ;
271286 res . json ( { success : true , data : rows } ) ;
272287 } ) ;
273288} ) ;
@@ -415,7 +430,10 @@ router.delete('/conversation/:conversationId', requireAuth, (req, res) => {
415430} ) ;
416431
417432async function saveMessage ( convId , userId , sender , message , nodeId ) {
433+ console . log ( '[CHAT DEBUG] saveMessage called - convId:' , convId , 'userId:' , userId , 'sender:' , sender , 'nodeId:' , nodeId ) ;
434+
418435 if ( ! global . db ) {
436+ console . log ( '[CHAT DEBUG] saveMessage - No database, using memory' ) ;
419437 MEMORY_MESSAGES . push ( {
420438 conversation_id : convId ,
421439 user_id : userId ,
@@ -431,8 +449,13 @@ async function saveMessage(convId, userId, sender, message, nodeId) {
431449 const query = `INSERT INTO chat_messages (conversation_id, user_id, sender, message, node_id)
432450 VALUES (?, ?, ?, ?, ?)` ;
433451 global . db . run ( query , [ convId , userId , sender , message , nodeId ] , function ( err ) {
434- if ( err ) reject ( err ) ;
435- else resolve ( this . lastID ) ;
452+ if ( err ) {
453+ console . log ( '[CHAT DEBUG] saveMessage ERROR:' , err ) ;
454+ reject ( err ) ;
455+ } else {
456+ console . log ( '[CHAT DEBUG] saveMessage SUCCESS - messageId:' , this . lastID ) ;
457+ resolve ( this . lastID ) ;
458+ }
436459 } ) ;
437460 } ) ;
438461}
0 commit comments