File size: 1,923 Bytes
3f13033
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { NextRequest, NextResponse } from "next/server";
import { requireAuthContext } from "@/lib/auth/session";
import { evaluateCommit, listCommitRecords, verifyReceipt } from "@/lib/workteleport/commit-gate";

export const dynamic = "force-dynamic";

export async function GET(req: NextRequest) {
  try {
    const ctx = await requireAuthContext();
    const { searchParams } = new URL(req.url);
    const workflowId = searchParams.get("workflowId");
    const verifyId = searchParams.get("verify");

    if (verifyId) {
      const result = verifyReceipt(ctx.orgId, verifyId);
      return NextResponse.json(result);
    }

    const records = listCommitRecords(ctx.orgId, workflowId || undefined);
    return NextResponse.json({ records, count: records.length });
  } catch (e: any) {
    const status = e.message === "Authentication required" ? 401 : 500;
    return NextResponse.json({ error: e.message }, { status });
  }
}

export async function POST(req: NextRequest) {
  try {
    const ctx = await requireAuthContext();
    const body = await req.json();

    if (!body.workflowId || !body.stepId || !body.actionType) {
      return NextResponse.json(
        { error: "workflowId, stepId, and actionType are required" },
        { status: 400 },
      );
    }

    const result = evaluateCommit({
      orgId: ctx.orgId,
      workflowId: body.workflowId,
      stepId: body.stepId,
      actionType: body.actionType,
      actionTarget: body.actionTarget || "",
      actionPayload: body.actionPayload || {},
      userRole: ctx.user.role,
      userId: ctx.user.id,
      dataClass: body.dataClass,
      evidenceEnvelopeId: body.evidenceEnvelopeId,
    });

    return NextResponse.json({ result }, { status: result.committed ? 201 : 403 });
  } catch (e: any) {
    const status = e.message === "Authentication required" ? 401 : 500;
    return NextResponse.json({ error: e.message }, { status });
  }
}