@@ -160,8 +160,15 @@ const KiRouter = () => {
160160
161161 console . log ( '[KI] User:' , user . id ) ;
162162 if ( ! user . patreon ) {
163- res . redirect ( '/pricing?redirect=/ki' ) ;
164- return false ;
163+ const subscriber = await authService . getIsSubscriber (
164+ database ,
165+ user . email
166+ ) ;
167+ console . log ( '[KI] Subscriber:' , subscriber ) ;
168+ if ( ! subscriber ) {
169+ res . redirect ( '/pricing?redirect=/ki' ) ;
170+ return false ;
171+ }
165172 }
166173
167174 return true ;
@@ -303,14 +310,25 @@ const KiRouter = () => {
303310 return false ;
304311 }
305312
306- const userId = user . owner ; // Assuming token contains userId
307- const userSession = await sessionRepository . getSession ( userId ) ; // Ensure session is typed
313+ const userId = user . owner ;
314+ let userSession = await sessionRepository . getSession ( userId ) ;
315+
316+ // Create new session if none exists
317+ if ( ! userSession || ! userSession . data ) {
318+ const w = new Workspace ( true , 'fs' ) ;
319+ await sessionRepository . createSession ( userId , {
320+ uploadedFiles : [ ] ,
321+ deckInfo : [ ] ,
322+ workspace : w ,
323+ } ) ;
324+ userSession = await sessionRepository . getSession ( userId ) ;
325+ }
326+
308327 if ( ! userSession || ! userSession . data ) {
309- // Check if userSession.data is defined
310328 return res . status ( 403 ) . json ( { error : 'Session expired' } ) ;
311329 }
312330
313- const files = req . files as UploadedFile [ ] ; // Ensure files are typed
331+ const files = req . files as UploadedFile [ ] ;
314332 console . log ( '[UPLOAD] Request received:' , req . body ) ;
315333 console . log ( '[UPLOAD] Starting file upload process' ) ;
316334 try {
@@ -1348,6 +1366,98 @@ const KiRouter = () => {
13481366 }
13491367 } ) ;
13501368
1369+ // Add cache endpoints
1370+ router . post ( '/ki/cache' , async ( req , res ) => {
1371+ const user = await authService . getUserFrom ( req . cookies . token ) ;
1372+ if ( ! user ) {
1373+ return res . status ( 403 ) . json ( { error : 'Session expired' } ) ;
1374+ }
1375+
1376+ const { text, cards } = req . body ;
1377+ if ( ! text || ! cards ) {
1378+ return res . status ( 400 ) . json ( { error : 'Missing required fields' } ) ;
1379+ }
1380+
1381+ try {
1382+ // Store in user session - simple approach
1383+ const userSession = await sessionRepository . getSession ( user . owner ) ;
1384+ if ( ! userSession || ! userSession . data ) {
1385+ return res . status ( 403 ) . json ( { error : 'Session expired' } ) ;
1386+ }
1387+
1388+ // Add cache data to session
1389+ await sessionRepository . updateSession (
1390+ user . owner ,
1391+ {
1392+ cache : { text, cards, timestamp : Date . now ( ) } ,
1393+ } ,
1394+ userSession . id
1395+ ) ;
1396+
1397+ res . status ( 200 ) . json ( { success : true } ) ;
1398+ } catch ( error ) {
1399+ console . error ( '[CACHE] Error saving cache:' , error ) ;
1400+ res . status ( 500 ) . json ( { error : 'Failed to save cache' } ) ;
1401+ }
1402+ } ) ;
1403+
1404+ router . get ( '/ki/cache' , async ( req , res ) => {
1405+ const user = await authService . getUserFrom ( req . cookies . token ) ;
1406+ if ( ! user ) {
1407+ return res . status ( 403 ) . json ( { error : 'Session expired' } ) ;
1408+ }
1409+
1410+ const text = req . query . text as string ;
1411+ if ( ! text ) {
1412+ return res . status ( 400 ) . json ( { error : 'Text parameter is required' } ) ;
1413+ }
1414+
1415+ try {
1416+ const userSession = await sessionRepository . getSession ( user . owner ) ;
1417+ if ( ! userSession || ! userSession . data || ! userSession . data . cache ) {
1418+ return res . status ( 404 ) . json ( { error : 'No cache found' } ) ;
1419+ }
1420+
1421+ // Simple exact match for now
1422+ if ( userSession . data . cache . text === text ) {
1423+ return res . status ( 200 ) . json ( { cards : userSession . data . cache . cards } ) ;
1424+ }
1425+
1426+ res . status ( 404 ) . json ( { error : 'Cache miss' } ) ;
1427+ } catch ( error ) {
1428+ console . error ( '[CACHE] Error retrieving cache:' , error ) ;
1429+ res . status ( 500 ) . json ( { error : 'Failed to retrieve cache' } ) ;
1430+ }
1431+ } ) ;
1432+
1433+ router . delete ( '/ki/cache' , async ( req , res ) => {
1434+ const user = await authService . getUserFrom ( req . cookies . token ) ;
1435+ if ( ! user ) {
1436+ return res . status ( 403 ) . json ( { error : 'Session expired' } ) ;
1437+ }
1438+
1439+ try {
1440+ const userSession = await sessionRepository . getSession ( user . owner ) ;
1441+ if ( ! userSession || ! userSession . data ) {
1442+ return res . status ( 403 ) . json ( { error : 'Session expired' } ) ;
1443+ }
1444+
1445+ // Remove cache from session
1446+ await sessionRepository . updateSession (
1447+ user . owner ,
1448+ {
1449+ cache : null ,
1450+ } ,
1451+ userSession . id
1452+ ) ;
1453+
1454+ res . status ( 200 ) . json ( { success : true } ) ;
1455+ } catch ( error ) {
1456+ console . error ( '[CACHE] Error clearing cache:' , error ) ;
1457+ res . status ( 500 ) . json ( { error : 'Failed to clear cache' } ) ;
1458+ }
1459+ } ) ;
1460+
13511461 return router ;
13521462} ;
13531463
0 commit comments