All files / app-leaderboard/src/courses visibility.ts

100% Statements 9/9
83.33% Branches 5/6
100% Functions 1/1
100% Lines 7/7

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                                    6x       6x 5x 4x 4x 4x   4x    
import { eq } from 'drizzle-orm';
import type { Database } from '../db/client.ts';
import { courseScore } from '../db/schema.ts';
import type { VisibilityChange } from '../scores/visibility.ts';
import { rebuildCourseBestScores } from './bests.ts';
 
/**
 * Hides one of a player's course runs, or shows it again, as `setScoreHidden` does for plays. A hidden run is kept, but
 * leaves the course rankings, the personal bests (the trophy best included), the figures and every list, and nobody
 * else can open it; the player's bests on the course are rebuilt from the runs still shown.
 */
export async function setCourseScoreHidden(
  db: Database,
  courseScoreId: string,
  userId: string,
  hidden: boolean,
  now: number,
): Promise<VisibilityChange> {
  const found = await db.query.courseScore.findFirst({
    where: eq(courseScore.id, courseScoreId),
    columns: { userId: true, courseFingerprint: true, hidden: true },
  });
  if (found === undefined) return { ok: false, status: 404, error: 'not-found' };
  if (found.userId !== userId) return { ok: false, status: 403, error: 'not-your-score' };
  Eif (found.hidden !== hidden) {
    await db.update(courseScore).set({ hidden }).where(eq(courseScore.id, courseScoreId));
    await rebuildCourseBestScores(db, userId, [found.courseFingerprint], now);
  }
  return { ok: true, hidden };
}