1- import { supabase } from '../supabaseClient' ;
21import { isoToday } from '../dateUtils' ;
32import { StudySession } from './types' ;
43import { clientOrDefault , type StudentOSSupabase } from './getSupabase' ;
@@ -47,9 +46,10 @@ export async function getOrCreateTodayDailySession(
4746 return inserted ;
4847}
4948
50- export async function completeSession ( studentId : string , sessionId : string , reflection : string ) : Promise < StudySession > {
49+ export async function completeSession ( studentId : string , sessionId : string , reflection : string , client ?: StudentOSSupabase ) : Promise < StudySession > {
5150 if ( ! studentId ) throw new Error ( 'studentId required' ) ;
52- const { data, error } = await ( supabase . from ( 'study_sessions' ) as any )
51+ const db = clientOrDefault ( client ) ;
52+ const { data, error } = await ( db . from ( 'study_sessions' ) as any )
5353 . update ( {
5454 status : 'done' ,
5555 reflection,
@@ -64,10 +64,10 @@ export async function completeSession(studentId: string, sessionId: string, refl
6464 return data ;
6565}
6666
67- export async function listSessions ( studentId : string , { fromDateISO, toDateISO } : { fromDateISO ?: string ; toDateISO ?: string } ) : Promise < StudySession [ ] > {
67+ export async function listSessions ( studentId : string , { fromDateISO, toDateISO } : { fromDateISO ?: string ; toDateISO ?: string } = { } , client ?: StudentOSSupabase ) : Promise < StudySession [ ] > {
6868 if ( ! studentId ) throw new Error ( 'studentId required' ) ;
69-
70- let q = supabase . from ( 'study_sessions' ) . select ( '*' ) . eq ( 'student_id' , studentId ) . order ( 'planned_for' , { ascending : true } ) ;
69+ const db = clientOrDefault ( client ) ;
70+ let q = db . from ( 'study_sessions' ) . select ( '*' ) . eq ( 'student_id' , studentId ) . order ( 'planned_for' , { ascending : true } ) ;
7171
7272 if ( fromDateISO ) q = q . gte ( 'planned_for' , fromDateISO ) ;
7373 if ( toDateISO ) q = q . lte ( 'planned_for' , toDateISO ) ;
@@ -80,8 +80,9 @@ export async function listSessions(studentId: string, { fromDateISO, toDateISO }
8080/**
8181 * Create a new study session
8282 */
83- export async function createSession ( studentId : string , sessionData : Partial < StudySession > ) : Promise < StudySession > {
83+ export async function createSession ( studentId : string , sessionData : Partial < StudySession > , client ?: StudentOSSupabase ) : Promise < StudySession > {
8484 if ( ! studentId ) throw new Error ( 'studentId required' ) ;
85+ const db = clientOrDefault ( client ) ;
8586 const payload = {
8687 student_id : studentId ,
8788 subject_id : sessionData . subject_id || null ,
@@ -94,17 +95,18 @@ export async function createSession(studentId: string, sessionData: Partial<Stud
9495 topic : sessionData . topic || null ,
9596 start_time : sessionData . start_time || null ,
9697 } ;
97- const { data, error } = await supabase . from ( 'study_sessions' ) . insert ( payload as any ) . select ( '*' ) . single ( ) ;
98+ const { data, error } = await db . from ( 'study_sessions' ) . insert ( payload as any ) . select ( '*' ) . single ( ) ;
9899 if ( error ) throw error ;
99100 return data ;
100101}
101102
102103/**
103104 * Update an existing study session
104105 */
105- export async function updateSession ( studentId : string , sessionId : string , patch : Partial < StudySession > ) : Promise < StudySession > {
106+ export async function updateSession ( studentId : string , sessionId : string , patch : Partial < StudySession > , client ?: StudentOSSupabase ) : Promise < StudySession > {
106107 if ( ! studentId ) throw new Error ( 'studentId required' ) ;
107- const { data, error } = await ( supabase . from ( 'study_sessions' ) as any )
108+ const db = clientOrDefault ( client ) ;
109+ const { data, error } = await ( db . from ( 'study_sessions' ) as any )
108110 . update ( { ...patch , updated_at : new Date ( ) . toISOString ( ) } )
109111 . eq ( 'student_id' , studentId )
110112 . eq ( 'id' , sessionId )
@@ -117,9 +119,10 @@ export async function updateSession(studentId: string, sessionId: string, patch:
117119/**
118120 * Delete a study session
119121 */
120- export async function deleteSession ( studentId : string , sessionId : string ) : Promise < void > {
122+ export async function deleteSession ( studentId : string , sessionId : string , client ?: StudentOSSupabase ) : Promise < void > {
121123 if ( ! studentId ) throw new Error ( 'studentId required' ) ;
122- const { error } = await supabase
124+ const db = clientOrDefault ( client ) ;
125+ const { error } = await db
123126 . from ( 'study_sessions' )
124127 . delete ( )
125128 . eq ( 'student_id' , studentId )
@@ -131,11 +134,12 @@ export async function deleteSession(studentId: string, sessionId: string): Promi
131134 * Batch save sessions from AI-generated schedule
132135 * Clears existing planned sessions for the week and inserts new ones
133136 */
134- export async function saveSchedule ( studentId : string , sessions : Partial < StudySession > [ ] , weekStartISO : string , weekEndISO : string ) : Promise < StudySession [ ] > {
137+ export async function saveSchedule ( studentId : string , sessions : Partial < StudySession > [ ] , weekStartISO : string , weekEndISO : string , client ?: StudentOSSupabase ) : Promise < StudySession [ ] > {
135138 if ( ! studentId ) throw new Error ( 'studentId required' ) ;
139+ const db = clientOrDefault ( client ) ;
136140
137141 // Delete existing planned sessions for this week (except completed ones)
138- const { error : deleteError } = await supabase
142+ const { error : deleteError } = await db
139143 . from ( 'study_sessions' )
140144 . delete ( )
141145 . eq ( 'student_id' , studentId )
@@ -161,7 +165,7 @@ export async function saveSchedule(studentId: string, sessions: Partial<StudySes
161165
162166 if ( payloads . length === 0 ) return [ ] ;
163167
164- const { data, error } = await supabase . from ( 'study_sessions' ) . insert ( payloads as any ) . select ( '*' ) ;
168+ const { data, error } = await db . from ( 'study_sessions' ) . insert ( payloads as any ) . select ( '*' ) ;
165169 if ( error ) throw error ;
166170 return data || [ ] ;
167171}
@@ -236,15 +240,16 @@ export async function getStudyStreak(studentId: string, client?: StudentOSSupaba
236240 * Get recent study history (last 14 days) to avoid repetition in AI scheduling
237241 * Returns topics that were recently studied so AI can schedule spaced repetition
238242 */
239- export async function getRecentStudyHistory ( studentId : string , days = 14 ) {
243+ export async function getRecentStudyHistory ( studentId : string , days = 14 , client ?: StudentOSSupabase ) {
240244 if ( ! studentId ) return { recentTopics : [ ] , completedSessions : [ ] , skippedCount : 0 } ;
241245
242246 try {
247+ const db = clientOrDefault ( client ) ;
243248 const startDate = new Date ( ) ;
244249 startDate . setDate ( startDate . getDate ( ) - days ) ;
245250 const startISO = startDate . toISOString ( ) . split ( 'T' ) [ 0 ] ;
246251
247- const { data : sessions , error } = await supabase
252+ const { data : sessions , error } = await db
248253 . from ( 'study_sessions' )
249254 . select ( 'id, subject_id, topic, status, planned_for, duration_minutes, session_type' )
250255 . eq ( 'student_id' , studentId )
@@ -293,15 +298,16 @@ export async function getRecentStudyHistory(studentId: string, days = 14) {
293298 * Get session completion stats for AI feedback loop
294299 * Tracks patterns in when sessions are completed vs skipped
295300 */
296- export async function getSessionCompletionStats ( studentId : string ) {
301+ export async function getSessionCompletionStats ( studentId : string , client ?: StudentOSSupabase ) {
297302 if ( ! studentId ) return { completionRate : 0 , byDayOfWeek : { } , byTimeOfDay : { } , insights : [ ] } ;
298303
299304 try {
305+ const db = clientOrDefault ( client ) ;
300306 const thirtyDaysAgo = new Date ( ) ;
301307 thirtyDaysAgo . setDate ( thirtyDaysAgo . getDate ( ) - 30 ) ;
302308 const startISO = thirtyDaysAgo . toISOString ( ) . split ( 'T' ) [ 0 ] ;
303309
304- const { data : sessions , error } = await supabase
310+ const { data : sessions , error } = await db
305311 . from ( 'study_sessions' )
306312 . select ( 'id, status, planned_for, session_type, duration_minutes, start_time' )
307313 . eq ( 'student_id' , studentId )
0 commit comments