All files / web/src/lib/classroom family-manager.ts

88.75% Statements 300/338
96.96% Branches 32/33
60% Functions 6/10
88.75% Lines 300/338

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 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 3391x 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 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 2x 2x 8x 8x 8x 8x 8x 1x 1x 1x 1x 1x 1x 7x 7x 7x 8x 1x 1x 1x 1x 1x 8x 6x 6x 6x 6x 6x 6x 10x 1x 1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 10x 1x 1x 4x 4x 4x 4x 4x 10x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x   3x 3x 3x 3x   3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 1x 1x                             1x 1x 1x 1x             1x 1x 1x 1x                             1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 2x 2x 2x 2x 2x 2x 2x 3x 3x 3x 3x 3x 3x   3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 2x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 3x 3x 3x 3x 3x 3x 3x 3x 3x 4x 1x   1x 1x 3x 3x 3x 1x 1x 1x 1x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x  
/**
 * Family Manager Module
 *
 * Manages parent-child relationships:
 * - Link parent to child via family code
 * - Get linked parents for a child
 * - Unlink parent from child
 * - Generate family codes
 */
 
import { and, eq } from 'drizzle-orm'
import { db } from '@/db'
import {
  familyEvents,
  generateFamilyCode,
  parentChild,
  type Player,
  players,
  users,
  type User,
} from '@/db/schema'
import { syncParentLink, removeParentLink } from '@/lib/auth/sync-relationships'
import { autoFormHousehold } from '@/lib/household'
import { notifyParentLinked } from '@/lib/notifications/family-link-email'
 
/** Maximum number of parents that can be linked to a single child */
export const MAX_PARENTS_PER_CHILD = 4
 
/** Number of days before a family code expires */
export const FAMILY_CODE_EXPIRY_DAYS = 7
 
/**
 * Result of linking a parent to a child
 */
export interface LinkResult {
  success: boolean
  player?: Player
  error?: string
}
 
/**
 * Link a parent to a child via family code
 *
 * Parents share family codes to link another parent to their child.
 * This creates a many-to-many relationship where children can have
 * multiple parents with equal access.
 */
export async function linkParentToChild(
  parentUserId: string,
  familyCode: string
): Promise<LinkResult> {
  // Normalize code
  const normalizedCode = familyCode.toUpperCase().trim()
 
  // Find player by family code
  const player = await db.query.players.findFirst({
    where: eq(players.familyCode, normalizedCode),
  })
 
  if (!player) {
    return { success: false, error: 'Invalid family code' }
  }
 
  // Check if family code has expired (7-day window)
  if (player.familyCode) {
    const generatedAt = player.familyCodeGeneratedAt
    if (!generatedAt) {
      // Code exists but no timestamp — treat as expired (legacy code without timestamp)
      return {
        success: false,
        error: 'This family code has expired. Ask the parent to regenerate it.',
      }
    }
    const expiresAt = new Date(
      generatedAt.getTime() + FAMILY_CODE_EXPIRY_DAYS * 24 * 60 * 60 * 1000
    )
    if (new Date() > expiresAt) {
      return {
        success: false,
        error: 'This family code has expired. Ask the parent to regenerate it.',
      }
    }
  }
 
  // Only students owned by non-guest (authenticated) users can be shared.
  // Guest-created students are ephemeral and shouldn't be linked to other accounts.
  const owner = await db.query.users.findFirst({
    where: eq(users.id, player.userId),
  })
  if (!owner?.upgradedAt) {
    return { success: false, error: 'This student cannot be shared' }
  }
 
  // Check if already linked
  const existing = await db.query.parentChild.findFirst({
    where: and(
      eq(parentChild.parentUserId, parentUserId),
      eq(parentChild.childPlayerId, player.id)
    ),
  })
 
  if (existing) {
    return { success: false, error: 'Already linked to this child' }
  }
 
  // Check parent cap
  const existingParents = await db.query.parentChild.findMany({
    where: eq(parentChild.childPlayerId, player.id),
  })
  if (existingParents.length >= MAX_PARENTS_PER_CHILD) {
    return {
      success: false,
      error: `This student already has the maximum number of linked parents (${MAX_PARENTS_PER_CHILD})`,
    }
  }
 
  // Create link
  await db.insert(parentChild).values({
    parentUserId,
    childPlayerId: player.id,
  })
 
  // Record event (non-fatal)
  recordFamilyEvent(player.id, 'parent_linked', parentUserId, parentUserId).catch((err) =>
    console.error('[family-events] Failed to record parent_linked:', err)
  )
 
  // Sync to Casbin (non-fatal)
  syncParentLink(parentUserId, player.id).catch((err) =>
    console.error('[auth-sync] Failed to sync parent link:', err)
  )
 
  // Auto-form household between child owner and linking parent (non-fatal)
  autoFormHousehold(player.userId, parentUserId).catch((err) =>
    console.error('[household] Failed to auto-form household:', err)
  )
 
  // Notify existing parents (non-fatal)
  notifyParentLinked(player.id, player.name, parentUserId).catch((err) =>
    console.error('[family-notify] Failed to send parent link notification:', err)
  )
 
  return { success: true, player }
}
 
/**
 * Get all parents linked to a child
 */
export async function getLinkedParents(playerId: string): Promise<User[]> {
  const links = await db.query.parentChild.findMany({
    where: eq(parentChild.childPlayerId, playerId),
  })

  if (links.length === 0) return []

  const parentIds = links.map((l) => l.parentUserId)
  const linkedParents = await db.query.users.findMany({
    where: (users, { inArray }) => inArray(users.id, parentIds),
  })

  return linkedParents
}
 
/**
 * Get all parent user IDs for a child (simpler version)
 */
export async function getLinkedParentIds(playerId: string): Promise<string[]> {
  const links = await db.query.parentChild.findMany({
    where: eq(parentChild.childPlayerId, playerId),
  })
  return links.map((l) => l.parentUserId)
}
 
/**
 * Get all children linked to a parent
 */
export async function getLinkedChildren(parentUserId: string): Promise<Player[]> {
  const links = await db.query.parentChild.findMany({
    where: eq(parentChild.parentUserId, parentUserId),
  })

  if (links.length === 0) return []

  const childIds = links.map((l) => l.childPlayerId)
  const linkedChildren = await db.query.players.findMany({
    where: (players, { inArray }) => inArray(players.id, childIds),
  })

  return linkedChildren
}
 
/**
 * Unlink a parent from a child
 *
 * Note: The last parent cannot be unlinked (every child must have at least one parent).
 * Returns error if trying to unlink the only parent.
 */
export async function unlinkParentFromChild(
  parentUserId: string,
  playerId: string,
  actorUserId?: string
): Promise<{ success: boolean; error?: string }> {
  // Check how many parents this child has
  const parentCount = await db.query.parentChild.findMany({
    where: eq(parentChild.childPlayerId, playerId),
  })
 
  if (parentCount.length <= 1) {
    return { success: false, error: 'Cannot unlink the only parent' }
  }
 
  // Remove the link
  await db
    .delete(parentChild)
    .where(and(eq(parentChild.parentUserId, parentUserId), eq(parentChild.childPlayerId, playerId)))
 
  // Record event (non-fatal)
  recordFamilyEvent(playerId, 'parent_unlinked', actorUserId ?? parentUserId, parentUserId).catch(
    (err) => console.error('[family-events] Failed to record parent_unlinked:', err)
  )
 
  // Sync to Casbin (non-fatal)
  removeParentLink(parentUserId, playerId).catch((err) =>
    console.error('[auth-sync] Failed to remove parent link:', err)
  )
 
  return { success: true }
}
 
/**
 * Result of getting or creating a family code
 */
export interface FamilyCodeResult {
  familyCode: string
  generatedAt: Date | null
}
 
/**
 * Get the family code for a player, generating one if needed
 */
export async function getOrCreateFamilyCode(playerId: string): Promise<FamilyCodeResult | null> {
  const player = await db.query.players.findFirst({
    where: eq(players.id, playerId),
  })
 
  if (!player) return null
 
  if (player.familyCode) {
    return {
      familyCode: player.familyCode,
      generatedAt: player.familyCodeGeneratedAt ?? null,
    }
  }
 
  // Generate and save new family code
  const newCode = generateFamilyCode()
  const now = new Date()
 
  await db
    .update(players)
    .set({ familyCode: newCode, familyCodeGeneratedAt: now })
    .where(eq(players.id, playerId))
 
  return { familyCode: newCode, generatedAt: now }
}
 
/**
 * Regenerate family code for a player
 *
 * Use this if a parent wants to invalidate an old code that was shared.
 * Note: This won't affect existing parent-child links.
 */
export async function regenerateFamilyCode(
  playerId: string,
  userId?: string
): Promise<string | null> {
  const player = await db.query.players.findFirst({
    where: eq(players.id, playerId),
  })
 
  if (!player) return null
 
  const newCode = generateFamilyCode()
 
  await db
    .update(players)
    .set({ familyCode: newCode, familyCodeGeneratedAt: new Date() })
    .where(eq(players.id, playerId))
 
  // Record event (non-fatal)
  if (userId) {
    recordFamilyEvent(playerId, 'code_regenerated', userId).catch((err) =>
      console.error('[family-events] Failed to record code_regenerated:', err)
    )
  }
 
  return newCode
}
 
/**
 * Record a family event for the audit log
 */
async function recordFamilyEvent(
  childPlayerId: string,
  eventType: 'parent_linked' | 'parent_unlinked' | 'code_regenerated',
  actorUserId: string,
  targetUserId?: string
): Promise<void> {
  await db.insert(familyEvents).values({
    childPlayerId,
    eventType,
    actorUserId,
    targetUserId: targetUserId ?? null,
  })
}
 
/**
 * Get recent family events for a child (last 7 days, max 20)
 */
export async function getRecentFamilyEvents(playerId: string) {
  const sevenDaysAgo = new Date(Date.now() - 7 * 24 * 60 * 60 * 1000)
 
  const events = await db.query.familyEvents.findMany({
    where: and(
      eq(familyEvents.childPlayerId, playerId)
      // created_at is stored as unix timestamp in seconds
      // We need to compare with the timestamp value
    ),
    orderBy: (familyEvents, { desc }) => [desc(familyEvents.createdAt)],
    limit: 20,
  })
 
  // Filter by date in JS since drizzle timestamp mode stores as Date
  return events.filter((e) => e.createdAt >= sevenDaysAgo)
}
 
// Re-export the generateFamilyCode function from schema for convenience
export { generateFamilyCode } from '@/db/schema'