@@ -237,22 +237,41 @@ const KiRouter = () => {
237237 res . redirect ( '/login?redirect=/ki' ) ;
238238 return false ;
239239 }
240- let s = await sessionRepository . getSession ( user . owner ) ;
241- if ( ! s ) {
240+
241+ // Create new session if requested or get existing one
242+ const isNewSession = req . query . new === 'true' ;
243+ let s ;
244+
245+ if ( isNewSession ) {
242246 const w = new Workspace ( true , 'fs' ) ;
243247 await sessionRepository . createSession ( user . owner , {
244248 uploadedFiles : [ ] ,
245249 deckInfo : [ ] ,
246250 workspace : w ,
247251 } ) ;
248252 s = await sessionRepository . getSession ( user . owner ) ;
253+ } else {
254+ s = await sessionRepository . getSession ( user . owner ) ;
255+ if ( ! s ) {
256+ const w = new Workspace ( true , 'fs' ) ;
257+ await sessionRepository . createSession ( user . owner , {
258+ uploadedFiles : [ ] ,
259+ deckInfo : [ ] ,
260+ workspace : w ,
261+ } ) ;
262+ s = await sessionRepository . getSession ( user . owner ) ;
263+ }
249264 }
250265
251266 if ( ! s . data ?. workspace ?. location ) {
252267 s . data . workspace = new Workspace ( true , 'fs' ) ;
253- await sessionRepository . updateSession ( user . owner , {
254- workspace : s . data . workspace ,
255- } ) ;
268+ await sessionRepository . updateSession (
269+ user . owner ,
270+ {
271+ workspace : s . data . workspace ,
272+ } ,
273+ s . id
274+ ) ;
256275 }
257276
258277 const exists = oldFs . existsSync ( s . data . workspace . location ) ;
@@ -447,9 +466,13 @@ const KiRouter = () => {
447466 }
448467
449468 // Update session with uploaded files (ONLY update files here)
450- await sessionRepository . updateSession ( userId , {
451- uploadedFiles : userSession . data . uploadedFiles ,
452- } ) ;
469+ await sessionRepository . updateSession (
470+ userId ,
471+ {
472+ uploadedFiles : userSession . data . uploadedFiles ,
473+ } ,
474+ userSession . id
475+ ) ;
453476 } catch ( error ) {
454477 console . error ( '[UPLOAD] Error during upload process:' , error ) ;
455478 res . status ( 500 ) . json ( {
@@ -490,9 +513,13 @@ const KiRouter = () => {
490513 userSession . data . uploadedFiles = userSession . data . uploadedFiles . filter (
491514 ( f : { id : string } ) => f . id !== fileId
492515 ) ;
493- await sessionRepository . updateSession ( user . owner , {
494- uploadedFiles : userSession . data . uploadedFiles ,
495- } ) ;
516+ await sessionRepository . updateSession (
517+ user . owner ,
518+ {
519+ uploadedFiles : userSession . data . uploadedFiles ,
520+ } ,
521+ userSession . id
522+ ) ;
496523 }
497524 res . send ( '' ) ;
498525 } catch ( error ) {
@@ -980,16 +1007,24 @@ const KiRouter = () => {
9801007 userSession . data . deckInfo = transformToDecks ( userSession . data . deckInfo ) ;
9811008
9821009 // Update session with FINAL deck info
983- await sessionRepository . updateSession ( user . owner , {
984- deckInfo : userSession . data . deckInfo ,
985- } ) ;
1010+ await sessionRepository . updateSession (
1011+ user . owner ,
1012+ {
1013+ deckInfo : userSession . data . deckInfo ,
1014+ } ,
1015+ userSession . id
1016+ ) ;
9861017
9871018 // Store text input in session data
9881019 if ( content . trim ( ) . length > 0 ) {
9891020 userSession . data . text = content ;
990- await sessionRepository . updateSession ( user . owner , {
991- text : content ,
992- } ) ;
1021+ await sessionRepository . updateSession (
1022+ user . owner ,
1023+ {
1024+ text : content ,
1025+ } ,
1026+ userSession . id
1027+ ) ;
9931028 }
9941029
9951030 res . write (
@@ -1160,7 +1195,6 @@ const KiRouter = () => {
11601195
11611196 router . delete ( '/ki/session/delete' , async ( req , res ) => {
11621197 const canDeleteSession = await canContinue ( req , res ) ;
1163- console . log ( '[DELETE SESSION] Can delete session:' , canDeleteSession ) ;
11641198 if ( ! canDeleteSession ) {
11651199 return ; // Exit if the user cannot continue
11661200 }
@@ -1171,18 +1205,21 @@ const KiRouter = () => {
11711205 return false ;
11721206 }
11731207
1174- const userSession = await sessionRepository . getSession ( user . owner ) ;
1175- if ( ! userSession ) {
1176- console . error ( '[DELETE SESSION] User session not found ' ) ;
1177- return res . status ( 403 ) . json ( { error : 'Session expired ' } ) ;
1208+ const { sessionId } = req . query ;
1209+ if ( ! sessionId ) {
1210+ console . error ( '[DELETE SESSION] No session ID provided ' ) ;
1211+ return res . status ( 400 ) . json ( { error : 'Session ID is required ' } ) ;
11781212 }
11791213
11801214 try {
1181- await sessionRepository . deleteSession ( user . owner ) ;
1182- console . log ( '[DELETE SESSION] Session cleared for user:' , user . owner ) ;
1215+ await sessionRepository . deleteSessionById (
1216+ sessionId as string ,
1217+ user . owner
1218+ ) ;
1219+ console . log ( '[DELETE SESSION] Session deleted:' , sessionId ) ;
11831220 res . status ( 200 ) . json ( { message : 'Session deleted successfully' } ) ;
11841221 } catch ( error ) {
1185- console . error ( '[DELETE SESSION] Error clearing session:' , error ) ;
1222+ console . error ( '[DELETE SESSION] Error deleting session:' , error ) ;
11861223 res . status ( 500 ) . json ( { error : 'Failed to delete session' } ) ;
11871224 }
11881225 } ) ;
@@ -1244,13 +1281,15 @@ const KiRouter = () => {
12441281 const sessions = await sessionRepository . getUserSessions (
12451282 user . owner . toString ( )
12461283 ) ;
1247- res . json (
1248- sessions . map ( ( session ) => ( {
1284+ const currentSession = await sessionRepository . getSession ( user . owner ) ;
1285+ res . json ( {
1286+ sessions : sessions . map ( ( session ) => ( {
12491287 id : session . id ,
12501288 createdAt : session . createdAt ,
12511289 updatedAt : session . updatedAt ,
1252- } ) )
1253- ) ;
1290+ } ) ) ,
1291+ currentSessionId : currentSession ?. id ,
1292+ } ) ;
12541293 } catch ( error ) {
12551294 console . error ( '[HISTORY] Error fetching session history:' , error ) ;
12561295 res . status ( 500 ) . json ( { error : 'Failed to fetch session history' } ) ;
0 commit comments