All files / src/app/api/sort-results route.ts

88.23% Statements 15/17
87.5% Branches 7/8
100% Functions 1/1
92.3% Lines 12/13

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                        16x 16x     16x 16x       16x 15x   14x 14x 14x       12x 12x 12x    
import { NextRequest, NextResponse } from "next/server";
import { getSession } from "@/lib/auth";
import { getDb } from "@/lib/db";
import { requireApiCollectionAccess } from "@/server/dal/collection-access";
import { SortResultService } from "@/server/services/SortResultService";
 
interface CreateResultRequest {
  collectionId: string;
  name: string;
}
 
export async function POST(req: NextRequest) {
  const session = await getSession();
  Iif (!session?.user) return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
 
  let body: CreateResultRequest;
  try {
    body = await req.json();
  } catch {
    return NextResponse.json({ error: "Invalid JSON" }, { status: 400 });
  }
  if (!body.collectionId) return NextResponse.json({ error: "collectionId is required" }, { status: 400 });
  if (!body.name) return NextResponse.json({ error: "name is required" }, { status: 400 });
 
  const db = getDb();
  const access = await requireApiCollectionAccess(db, body.collectionId, session.user.id);
  if (access instanceof NextResponse) return access;
 
  // Curator is ALWAYS the session user, never the request body. The service reads
  // the curator's preferences and materializes one ranked SortResultItem per row.
  const service = SortResultService.fromDb(db);
  const result = await service.createDraftResult(body.collectionId, session.user.id, body.name);
  return NextResponse.json(result, { status: 201 });
}