@@ -12,6 +12,12 @@ const {
1212} = require ( '../services/questionEngine' ) ;
1313const llmScorer = require ( '../services/llmScorer' ) ;
1414const { isEnabled : isLlmEnabled } = llmScorer ;
15+ const {
16+ mirrorChatMessage,
17+ mirrorCareerProbabilities,
18+ deleteMirroredConversationData,
19+ deleteMirroredUserHistory
20+ } = require ( '../services/userDataStore' ) ;
1521
1622const MEMORY_MESSAGES = [ ] ;
1723const MIN_CONF_SCORE = 1 ;
@@ -664,24 +670,38 @@ router.put('/conversation/:conversationId', requireAuth, async (req, res) => {
664670 ) ;
665671} ) ;
666672
667- router . delete ( '/history/:userId' , requireAuth , ( req , res ) => {
673+ router . delete ( '/history/:userId' , requireAuth , async ( req , res ) => {
668674 const { userId } = req . params ;
669675 if ( req . user . user_type !== 'admin' && String ( req . user . user_id ) !== String ( userId ) ) {
670676 return res . status ( 403 ) . json ( { success : false , error : 'Forbidden' } ) ;
671677 }
672678 const delMessages = 'DELETE FROM chat_messages WHERE user_id = ?' ;
673679 const delRecs = 'DELETE FROM recommendations WHERE user_id = ?' ;
680+ const delState = 'DELETE FROM conversation_state WHERE user_id = ?' ;
674681 const delConversations = 'DELETE FROM conversations WHERE user_id = ?' ;
675- global . db . serialize ( ( ) => {
676- global . db . run ( delMessages , [ userId ] ) ;
677- global . db . run ( delRecs , [ userId ] , function ( err ) {
678- if ( err ) return res . status ( 500 ) . json ( { success : false , error : err . message } ) ;
679- global . db . run ( delConversations , [ userId ] , ( err2 ) => {
680- if ( err2 ) return res . status ( 500 ) . json ( { success : false , error : err2 . message } ) ;
681- res . json ( { success : true , data : { deleted : true } } ) ;
682+ try {
683+ await new Promise ( ( resolve , reject ) => {
684+ global . db . serialize ( ( ) => {
685+ global . db . run ( delMessages , [ userId ] ) ;
686+ global . db . run ( delRecs , [ userId ] ) ;
687+ global . db . run ( delState , [ userId ] ) ;
688+ global . db . run ( delConversations , [ userId ] , ( err ) => {
689+ if ( err ) return reject ( err ) ;
690+ resolve ( ) ;
691+ } ) ;
682692 } ) ;
683693 } ) ;
684- } ) ;
694+
695+ try {
696+ await deleteMirroredUserHistory ( { appUserId : Number ( userId ) || userId } ) ;
697+ } catch ( mirrorError ) {
698+ console . warn ( '[CHAT DEBUG] deleteMirroredUserHistory warning:' , mirrorError . message ) ;
699+ }
700+
701+ res . json ( { success : true , data : { deleted : true } } ) ;
702+ } catch ( error ) {
703+ res . status ( 500 ) . json ( { success : false , error : error . message } ) ;
704+ }
685705} ) ;
686706
687707router . get ( '/messages/:conversationId' , requireAuth , ( req , res ) => {
@@ -739,12 +759,24 @@ router.delete('/conversation/:conversationId', requireAuth, (req, res) => {
739759
740760 const deleteMessages = 'DELETE FROM chat_messages WHERE conversation_id = ?' ;
741761 const deleteRecs = 'DELETE FROM recommendations WHERE conversation_id = ?' ;
762+ const deleteState = 'DELETE FROM conversation_state WHERE conversation_id = ?' ;
742763 const deleteConv = 'DELETE FROM conversations WHERE id = ?' ;
743764 global . db . serialize ( ( ) => {
744765 global . db . run ( deleteMessages , [ conversationId ] ) ;
745766 global . db . run ( deleteRecs , [ conversationId ] ) ;
746- global . db . run ( deleteConv , [ conversationId ] , function ( err2 ) {
767+ global . db . run ( deleteState , [ conversationId ] ) ;
768+ global . db . run ( deleteConv , [ conversationId ] , async function ( err2 ) {
747769 if ( err2 ) return res . status ( 500 ) . json ( { success : false , error : err2 . message } ) ;
770+
771+ try {
772+ await deleteMirroredConversationData ( {
773+ appUserId : row ?. user_id || null ,
774+ conversationId
775+ } ) ;
776+ } catch ( mirrorError ) {
777+ console . warn ( '[CHAT DEBUG] deleteMirroredConversationData warning:' , mirrorError . message ) ;
778+ }
779+
748780 res . json ( { success : true , data : { deleted : true } } ) ;
749781 } ) ;
750782 } ) ;
@@ -776,9 +808,25 @@ async function saveMessage(convId, userId, sender, message, nodeId) {
776808 reject ( err ) ;
777809 } else {
778810 console . log ( '[CHAT DEBUG] saveMessage SUCCESS - messageId:' , this . lastID ) ;
779- resolve ( this . lastID ) ;
811+ const insertedId = this . lastID ;
812+ resolve ( insertedId ) ;
780813 }
781814 } ) ;
815+ } ) . then ( async ( messageId ) => {
816+ try {
817+ await mirrorChatMessage ( {
818+ appUserId : userId ,
819+ conversationId : convId ,
820+ messageId,
821+ sender,
822+ message,
823+ nodeId,
824+ createdAt : new Date ( ) . toISOString ( )
825+ } ) ;
826+ } catch ( mirrorError ) {
827+ console . warn ( '[CHAT DEBUG] mirrorChatMessage warning:' , mirrorError . message ) ;
828+ }
829+ return messageId ;
782830 } ) ;
783831}
784832
@@ -881,6 +929,16 @@ async function saveRecommendations(convId, userId, recommendations) {
881929 ) ;
882930 } ) ;
883931 }
932+
933+ try {
934+ await mirrorCareerProbabilities ( {
935+ appUserId : userId ,
936+ conversationId : convId ,
937+ recommendations
938+ } ) ;
939+ } catch ( mirrorError ) {
940+ console . warn ( '[CHAT DEBUG] mirrorCareerProbabilities warning:' , mirrorError . message ) ;
941+ }
884942}
885943
886944async function logEvent ( eventType , userId , metadata ) {
0 commit comments