File size: 1,215 Bytes
9646e24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { NextRequest, NextResponse } from "next/server";
import { detectCommitments } from "@/lib/commitment/detector";
import { loadProcessedEmails } from "@/lib/config";
import { upsertCommitmentByEmailId, listCommitments, loadMetrics } from "@/lib/commitment/store";

export const dynamic = "force-dynamic";
export const maxDuration = 60;

export async function POST(request: NextRequest) {
  try {
    const body = await request.json().catch(() => ({}));
    const records = Array.isArray(body.records) ? body.records : loadProcessedEmails();

    const detected = detectCommitments(records);

    for (const c of detected) {
      try {
        upsertCommitmentByEmailId(c);
      } catch (e: any) {
        console.error("[commitments/detect] upsert error:", e);
      }
    }

    const commitments = listCommitments();
    const metrics = loadMetrics();

    return NextResponse.json({
      success: true,
      detected: detected.length,
      count: commitments.length,
      commitments,
      metrics,
      timestamp: new Date().toISOString(),
    });
  } catch (e: any) {
    console.error("[commitments/detect] error:", e);
    return NextResponse.json({ error: e.message }, { status: 500 });
  }
}