@@ -397,20 +397,21 @@ export function createAttendanceClient(apiKey: string, baseId: string) {
397397 // ============================================
398398
399399 /**
400- * Get events for a specific cohort , optionally filtered by date range
400+ * Get events for cohorts , optionally filtered by date range
401401 * This is THE source of truth for which events count toward an apprentice's stats
402+ * Accepts multiple cohort IDs - returns events that match ANY of the cohorts
402403 */
403- function getEventsForCohort (
404+ function getEventsForCohorts (
404405 allEvents : EventForStats [ ] ,
405- cohortId : string | null ,
406+ cohortIds : string [ ] ,
406407 options ?: { startDate ?: Date ; endDate ?: Date } ,
407408 ) : EventForStats [ ] {
408- // Filter by cohort (if no cohort , return empty - apprentice must belong to a cohort)
409- if ( ! cohortId ) {
409+ // Filter by cohorts (if no cohorts , return empty - apprentice must belong to at least one cohort)
410+ if ( cohortIds . length <= 0 ) {
410411 return [ ] ;
411412 }
412413
413- let events = allEvents . filter ( e => e . cohortIds . includes ( cohortId ) ) ;
414+ let events = allEvents . filter ( e => e . cohortIds . some ( c => cohortIds . includes ( c ) ) ) ;
414415
415416 // Filter by date range if provided
416417 if ( options ?. startDate && options ?. endDate ) {
@@ -475,14 +476,15 @@ export function createAttendanceClient(apiKey: string, baseId: string) {
475476 const apprentice = apprenticeRecords [ 0 ] ;
476477 const apprenticeName = apprentice . get ( APPRENTICE_FIELDS . NAME ) as string ;
477478 const cohortLink = apprentice . get ( APPRENTICE_FIELDS . COHORT ) as string [ ] | undefined ;
478- const cohortId = cohortLink ?. [ 0 ] ?? null ;
479+ const cohortIds = cohortLink ?? [ ] ;
480+ const primaryCohortId = cohortIds [ 0 ] ?? null ; // For display purposes
479481
480- // Get cohort name if available
482+ // Get primary cohort name if available (for display)
481483 let cohortName : string | null = null ;
482- if ( cohortId ) {
484+ if ( primaryCohortId ) {
483485 const cohortRecords = await cohortsTable
484486 . select ( {
485- filterByFormula : `RECORD_ID() = "${ cohortId } "` ,
487+ filterByFormula : `RECORD_ID() = "${ primaryCohortId } "` ,
486488 maxRecords : 1 ,
487489 returnFieldsByFieldId : true ,
488490 } )
@@ -492,9 +494,9 @@ export function createAttendanceClient(apiKey: string, baseId: string) {
492494 }
493495 }
494496
495- // Get events for this cohort (with optional date filter)
497+ // Get events for all of the apprentice's cohorts (with optional date filter)
496498 const allEvents = await getAllEvents ( ) ;
497- const relevantEvents = getEventsForCohort ( allEvents , cohortId , options ) ;
499+ const relevantEvents = getEventsForCohorts ( allEvents , cohortIds , options ) ;
498500 const relevantEventIds = new Set ( relevantEvents . map ( e => e . id ) ) ;
499501
500502 // Get attendance for this apprentice, filtered to cohort events only
@@ -542,7 +544,7 @@ export function createAttendanceClient(apiKey: string, baseId: string) {
542544 ...stats ,
543545 apprenticeId,
544546 apprenticeName,
545- cohortId,
547+ cohortId : primaryCohortId ,
546548 cohortName,
547549 trend : calculateTrend ( recentRate , previousRate ) ,
548550 } ;
@@ -827,11 +829,11 @@ export function createAttendanceClient(apiKey: string, baseId: string) {
827829
828830 const apprentice = apprenticeRecords [ 0 ] ;
829831 const cohortLink = apprentice . get ( APPRENTICE_FIELDS . COHORT ) as string [ ] | undefined ;
830- const cohortId = cohortLink ?. [ 0 ] ?? null ;
832+ const cohortIds = cohortLink ?? [ ] ;
831833
832- // Get events for this cohort (with optional date filter)
834+ // Get events for all of the apprentice's cohorts (with optional date filter)
833835 const allEvents = await getAllEvents ( ) ;
834- const relevantEvents = getEventsForCohort ( allEvents , cohortId , options ) ;
836+ const relevantEvents = getEventsForCohorts ( allEvents , cohortIds , options ) ;
835837 const relevantEventIds = new Set ( relevantEvents . map ( e => e . id ) ) ;
836838
837839 // Get attendance for this apprentice, filtered to cohort events only
0 commit comments