@@ -32,15 +32,15 @@ export async function GET(request: Request) {
3232
3333 const requestLocale = url . searchParams . get ( "locale" ) || "en" ;
3434
35- // Check for cached summary — shared across all users
35+ // Check for cached summary — shared across all users, excluding hidden ones
3636 if ( ! forceRefresh ) {
3737 const cached = db
38- . prepare ( "SELECT content, created_at FROM ai_summaries WHERE locale = ? ORDER BY created_at DESC LIMIT 1" )
39- . get ( requestLocale ) as { content : string ; created_at : string } | undefined ;
38+ . prepare ( "SELECT id, content, created_at FROM ai_summaries WHERE locale = ? AND is_hidden = 0 ORDER BY created_at DESC LIMIT 1" )
39+ . get ( requestLocale ) as { id : number ; content : string ; created_at : string } | undefined ;
4040
4141 if ( cached ) {
4242 console . debug ( "[summary] Returning cached" , requestLocale , "summary from" , cached . created_at ) ;
43- return NextResponse . json ( { summary : cached . content , cached : true , created_at : cached . created_at } ) ;
43+ return NextResponse . json ( { id : cached . id , summary : cached . content , cached : true , created_at : cached . created_at } ) ;
4444 }
4545 }
4646
@@ -350,16 +350,37 @@ ${(() => { const goals = db.prepare("SELECT name, target_amount, saved_amount, t
350350 proc . stdin . end ( ) ;
351351 } ) ;
352352
353+ let newId : number | undefined ;
353354 if ( summaryText ) {
354- db . prepare ( "INSERT INTO ai_summaries (user_id, locale, content) VALUES (?, ?, ?)" )
355+ const result = db . prepare ( "INSERT INTO ai_summaries (user_id, locale, content) VALUES (?, ?, ?)" )
355356 . run ( user . id , requestLocale , summaryText ) ;
356-
357- console . info ( "[summary] Summary generated and cached, length:" , summaryText . length ) ;
357+ newId = Number ( result . lastInsertRowid ) ;
358+ console . info ( "[summary] Summary generated and cached, length:" , summaryText . length , "id:" , newId ) ;
358359 }
359360
360- return NextResponse . json ( { summary : summaryText , cached : false , created_at : new Date ( ) . toISOString ( ) } ) ;
361+ return NextResponse . json ( { id : newId , summary : summaryText , cached : false , created_at : new Date ( ) . toISOString ( ) } ) ;
361362 } catch ( error ) {
362363 console . error ( "[summary] Error:" , error ) ;
363364 return NextResponse . json ( { summary : null , error : "Failed to generate summary" } , { status : 500 } ) ;
364365 }
365366}
367+
368+ export async function PATCH ( request : Request ) {
369+ try {
370+ const user = await getSession ( ) ;
371+ if ( ! user ) return NextResponse . json ( { error : "Not authenticated" } , { status : 401 } ) ;
372+
373+ const body = await request . json ( ) ;
374+ const { id, is_hidden } = body ;
375+ if ( ! id ) return NextResponse . json ( { error : "id required" } , { status : 400 } ) ;
376+
377+ const db = getDb ( ) ;
378+ db . prepare ( "UPDATE ai_summaries SET is_hidden = ? WHERE id = ?" ) . run ( is_hidden ? 1 : 0 , id ) ;
379+ console . info ( "[summary] Set is_hidden=" , is_hidden ? 1 : 0 , "on summary id" , id , "by user" , user . id ) ;
380+
381+ return NextResponse . json ( { success : true } ) ;
382+ } catch ( error ) {
383+ console . error ( "[summary] PATCH error:" , error ) ;
384+ return NextResponse . json ( { error : "Failed to update summary" } , { status : 500 } ) ;
385+ }
386+ }
0 commit comments