All files / web/src/lib/practice-picker contract.ts

99.54% Statements 219/220
56.52% Branches 13/23
100% Functions 9/9
99.54% Lines 219/220

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 2212x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 309x 309x 309x 2x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 2x 103x 103x 103x 103x 103x 103x 103x 103x 103x 2x 103x 103x 103x 103x 103x 103x 103x 103x 2x 103x 103x 103x 103x 103x 103x 103x 103x 2x 2x 4x 4x 4x 4x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 2x 1x 1x 1x 1x 1x 1x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x  
import type { Classroom } from '@/db/schema/classrooms'
import type { Player } from '@/db/schema/players'
import type {
  StudentActiveSessionInfo,
  StudentIntervention,
  StudentPresenceInfo,
  StudentWithSkillData,
} from '@/utils/studentGrouping'
import type { SkillCategoryKey } from '@/constants/skillCategories'
 
export const PRACTICE_PICKER_API_VERSION = 1 as const
 
/**
 * Explicit public player fields used by the practice picker.
 *
 * Keep this independent from the database row type. New database columns must
 * never cross the server/client boundary without an intentional contract edit.
 */
export type PracticePickerPlayerFields = Pick<
  Player,
  'id' | 'name' | 'emoji' | 'color' | 'createdAt' | 'isArchived'
>
 
export interface PracticePickerClassroomV1 {
  id: string
  teacherId: string
  name: string
  code: string
  createdAt: string
  entryPromptExpiryMinutes: number | null
}
 
export interface PracticePickerPresenceV1 {
  playerId: string
  classroomId: string
  enteredAt: string
  enteredBy: string
  classroom?: PracticePickerClassroomV1
}
 
export interface PracticePickerActiveSessionV1 {
  sessionId: string
  status: string
  completedProblems: number
  totalProblems: number
}
 
export interface PracticePickerInterventionV1 {
  type: StudentIntervention['type']
  severity: StudentIntervention['severity']
  message: string
  icon: string
}
 
export interface PracticePickerStudentV1 {
  id: string
  name: string
  emoji: string
  color: string
  createdAt: string
  isArchived: boolean
  practicingSkills: string[]
  lastPracticedAt: string | null
  skillCategory: SkillCategoryKey | null
  intervention: PracticePickerInterventionV1 | null
  enrolledClassrooms: PracticePickerClassroomV1[]
  currentPresence: PracticePickerPresenceV1 | null
  activeSession: PracticePickerActiveSessionV1 | null
}
 
export interface PracticePickerCountsV1 {
  active: number
  archived: number
  total: number
}
 
export interface PracticePickerV1Response {
  version: typeof PRACTICE_PICKER_API_VERSION
  students: PracticePickerStudentV1[]
  counts: PracticePickerCountsV1
}
 
export interface PracticePickerNotesV1Response {
  version: typeof PRACTICE_PICKER_API_VERSION
  studentId: string
  notes: string | null
}
 
export interface PracticePickerV1Data {
  version: typeof PRACTICE_PICKER_API_VERSION
  students: StudentWithSkillData[]
  counts: PracticePickerCountsV1
}
 
function toIsoString(value: Date | string): string {
  return value instanceof Date ? value.toISOString() : new Date(value).toISOString()
}
 
function toClassroomV1(classroom: Classroom): PracticePickerClassroomV1 {
  return {
    id: classroom.id,
    teacherId: classroom.teacherId,
    name: classroom.name,
    code: classroom.code,
    createdAt: toIsoString(classroom.createdAt),
    entryPromptExpiryMinutes: classroom.entryPromptExpiryMinutes,
  }
}
 
function toPresenceV1(presence: StudentPresenceInfo): PracticePickerPresenceV1 {
  return {
    playerId: presence.playerId,
    classroomId: presence.classroomId,
    enteredAt: presence.enteredAt,
    enteredBy: presence.enteredBy,
    ...(presence.classroom ? { classroom: toClassroomV1(presence.classroom) } : {}),
  }
}
 
function toActiveSessionV1(activeSession: StudentActiveSessionInfo): PracticePickerActiveSessionV1 {
  return {
    sessionId: activeSession.sessionId,
    status: activeSession.status,
    completedProblems: activeSession.completedProblems,
    totalProblems: activeSession.totalProblems,
  }
}
 
function toInterventionV1(intervention: StudentIntervention): PracticePickerInterventionV1 {
  return {
    type: intervention.type,
    severity: intervention.severity,
    message: intervention.message,
    icon: intervention.icon,
  }
}
 
export function createPracticePickerV1Response(
  students: readonly StudentWithSkillData[]
): PracticePickerV1Response {
  let active = 0
  const pickerStudents = students.map((student): PracticePickerStudentV1 => {
    if (!student.isArchived) active += 1
 
    return {
      id: student.id,
      name: student.name,
      emoji: student.emoji,
      color: student.color,
      createdAt: toIsoString(student.createdAt),
      isArchived: student.isArchived,
      practicingSkills: [...student.practicingSkills],
      lastPracticedAt: student.lastPracticedAt ? toIsoString(student.lastPracticedAt) : null,
      skillCategory: student.skillCategory,
      intervention: student.intervention ? toInterventionV1(student.intervention) : null,
      enrolledClassrooms: (student.enrolledClassrooms ?? []).map(toClassroomV1),
      currentPresence: student.currentPresence ? toPresenceV1(student.currentPresence) : null,
      activeSession: student.activeSession ? toActiveSessionV1(student.activeSession) : null,
    }
  })
 
  return {
    version: PRACTICE_PICKER_API_VERSION,
    students: pickerStudents,
    counts: {
      active,
      archived: pickerStudents.length - active,
      total: pickerStudents.length,
    },
  }
}
 
function normalizeClassroom(classroom: PracticePickerClassroomV1): Classroom {
  return {
    ...classroom,
    createdAt: new Date(classroom.createdAt),
  }
}
 
function normalizePresence(presence: PracticePickerPresenceV1): StudentPresenceInfo {
  return {
    playerId: presence.playerId,
    classroomId: presence.classroomId,
    enteredAt: presence.enteredAt,
    enteredBy: presence.enteredBy,
    ...(presence.classroom ? { classroom: normalizeClassroom(presence.classroom) } : {}),
  }
}
 
export function normalizePracticePickerV1Response(
  response: PracticePickerV1Response
): PracticePickerV1Data {
  if (response.version !== PRACTICE_PICKER_API_VERSION) {
    throw new Error(`Unsupported practice picker API version: ${response.version}`)
  }
 
  return {
    version: response.version,
    counts: response.counts,
    students: response.students.map(
      (student): StudentWithSkillData => ({
        id: student.id,
        name: student.name,
        emoji: student.emoji,
        color: student.color,
        createdAt: new Date(student.createdAt),
        isArchived: student.isArchived,
        practicingSkills: [...student.practicingSkills],
        lastPracticedAt: student.lastPracticedAt ? new Date(student.lastPracticedAt) : null,
        skillCategory: student.skillCategory,
        intervention: student.intervention,
        enrolledClassrooms: student.enrolledClassrooms.map(normalizeClassroom),
        currentPresence: student.currentPresence
          ? normalizePresence(student.currentPresence)
          : null,
        activeSession: student.activeSession,
      })
    ),
  }
}