All files / src/server/api ranking.ts

100% Statements 10/10
100% Branches 0/0
100% Functions 6/6
100% Lines 10/10

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            9x 9x             28x     26x       2x       12x 12x       2x       18x 18x      
import { z } from "zod";
import type { Database } from "@/lib/db";
import { ClientKey } from "@/lib/decision-batch-schema";
import { SortResultService, type SavedRanking } from "@/server/services/SortResultService";
import type { Pin } from "@/server/services/sortResultMaterialization";
 
const PinSchema = z.strictObject({ position: z.number().int().min(1).max(10_000) });
const SaveSchema = z.strictObject({
  name: z.string().trim().min(1).max(200),
  saveKey: ClientKey,
});
 
/** The live Ranking's writes (spec ยง4). Curator is always the caller, never the body. */
export class RankingController {
  constructor(private service: SortResultService) {}
 
  static fromDb(db: Database): RankingController {
    return new RankingController(SortResultService.fromDb(db));
  }
 
  listPins(collectionId: string, curatorId: string): Promise<Pin[]> {
    return this.service.listPins(collectionId, curatorId);
  }
 
  pin(collectionId: string, curatorId: string, itemId: string, body: unknown): Promise<void> {
    const { position } = PinSchema.parse(body);
    return this.service.pinItem(collectionId, curatorId, itemId, position);
  }
 
  unpin(collectionId: string, curatorId: string, itemId: string): Promise<void> {
    return this.service.unpinItem(collectionId, curatorId, itemId);
  }
 
  save(collectionId: string, curatorId: string, body: unknown): Promise<SavedRanking> {
    const { name, saveKey } = SaveSchema.parse(body);
    return this.service.saveAndShare(collectionId, curatorId, name, saveKey);
  }
}