All files / src/server/repositories/implementations DecisionEventRepository.ts

88.23% Statements 15/17
66.66% Branches 4/6
90% Functions 9/10
85.71% Lines 12/14

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                          388x               146x 146x                   146x                       40x       40x       129x 88x       152x           14x                           12x                       335x                  
import { eq, inArray } from "drizzle-orm";
import type { Database } from "@/lib/db";
import { decisionEventsTable, sessionsTable } from "@/lib/db/schema";
import type {
  DecisionEvent,
  DecisionType,
  TriageDecision,
  PairwiseDecision,
  UsageDecision,
} from "@/types";
import { generateId } from "@/lib/id";
 
export class DecisionEventRepository {
  constructor(private db: Database) {}
 
  async create(data: {
    id?: string;
    sessionId: string;
    type: DecisionType;
    decision: TriageDecision | PairwiseDecision | UsageDecision;
  }): Promise<DecisionEvent> {
    const id = data.id || generateId("evt");
    const [event] = await this.db
      .insert(decisionEventsTable)
      .values({
        id,
        sessionId: data.sessionId,
        type: data.type,
        decision: data.decision,
        createdAt: new Date(),
      })
      .returning();
    return this.mapToDecisionEvent(event);
  }
 
  async findById(id: string): Promise<DecisionEvent | null> {
    const [event] = await this.db
      .select()
      .from(decisionEventsTable)
      .where(eq(decisionEventsTable.id, id));
    return event ? this.mapToDecisionEvent(event) : null;
  }
 
  async findBySession(sessionId: string): Promise<DecisionEvent[]> {
    const events = await this.db
      .select()
      .from(decisionEventsTable)
      .where(eq(decisionEventsTable.sessionId, sessionId));
    return events.map((e) => this.mapToDecisionEvent(e));
  }
 
  async findBySessions(sessionIds: string[]): Promise<DecisionEvent[]> {
    if (sessionIds.length === 0) return [];
    const events = await this.db
      .select()
      .from(decisionEventsTable)
      .where(inArray(decisionEventsTable.sessionId, sessionIds));
    return events.map((e) => this.mapToDecisionEvent(e));
  }
 
  // Delete every decision of this collection's sessions (decisions have no
  // collection_id — scope via the parent sessions). Purge-only.
  async deleteByCollection(collectionId: string): Promise<void> {
    await this.db.delete(decisionEventsTable).where(
      inArray(
        decisionEventsTable.sessionId,
        this.db
          .select({ id: sessionsTable.id })
          .from(sessionsTable)
          .where(eq(sessionsTable.collectionId, collectionId)),
      ),
    );
  }
 
  // Delete every decision_event whose parent session belongs to the given curator.
  // Child-first prerequisite for deleteByCurator on sessions. Account-deletion only.
  async deleteByCurator(curatorId: string): Promise<void> {
    await this.db.delete(decisionEventsTable).where(
      inArray(
        decisionEventsTable.sessionId,
        this.db
          .select({ id: sessionsTable.id })
          .from(sessionsTable)
          .where(eq(sessionsTable.curatorId, curatorId)),
      ),
    );
  }
 
  private mapToDecisionEvent(row: typeof decisionEventsTable.$inferSelect): DecisionEvent {
    return {
      id: row.id,
      sessionId: row.sessionId,
      type: row.type as DecisionType,
      decision: row.decision as TriageDecision | PairwiseDecision | UsageDecision,
      createdAt: row.createdAt,
    };
  }
}