All files / src/server/api preferences.ts

90.9% Statements 10/11
100% Branches 0/0
80% Functions 4/5
90.9% Lines 10/11

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                        93x           93x 93x 93x 93x 93x 93x             93x             32x                       62x      
import type { CollectionPreferenceItem } from "@/types";
import type { PreferenceStateService } from "@/server/services/PreferenceStateService";
import type { Database } from "@/lib/db";
import { CollectionPreferenceItemRepository } from "@/server/repositories/implementations/CollectionPreferenceItemRepository";
import { DecisionEventRepository } from "@/server/repositories/implementations/DecisionEventRepository";
import { SessionRepository } from "@/server/repositories/implementations/SessionRepository";
import { ItemRepository } from "@/server/repositories/implementations/ItemRepository";
import { ScoringService } from "@/server/services/ScoringService";
import { PreferenceStateService as PreferenceStateServiceImpl } from "@/server/services/PreferenceStateService";
 
export class PreferenceController {
  constructor(
    private preferenceService: PreferenceStateService,
  ) {}
 
  static fromDb(db: Database): PreferenceController {
    // All repos share the SAME db/tx handle so the whole recompute stays inside
    // the caller's transaction (the decisions route wraps this in withTransaction).
    const prefRepo = new CollectionPreferenceItemRepository(db);
    const sessionRepo = new SessionRepository(db);
    const decisionRepo = new DecisionEventRepository(db);
    const itemRepo = new ItemRepository(db);
    const scoringService = new ScoringService();
    const prefService = new PreferenceStateServiceImpl(
      prefRepo,
      scoringService,
      itemRepo,
      sessionRepo,
      decisionRepo,
    );
    return new PreferenceController(prefService);
  }
 
  async listItemPreferences(
    collectionId: string,
    curatorId: string,
  ): Promise<CollectionPreferenceItem[]> {
    return this.preferenceService.getItemPreferences(collectionId, curatorId);
  }
 
  async getItemPreference(): Promise<CollectionPreferenceItem | null> {
    // TODO: Implement when preferenceRepo.findByItem available
    return null;
  }
 
  async recalculatePreferences(
    collectionId: string,
    curatorId: string,
  ): Promise<CollectionPreferenceItem[]> {
    return this.preferenceService.recalculatePreferences(collectionId, curatorId);
  }
}