File size: 2,239 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
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
import { NextRequest, NextResponse } from "next/server";

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

/**
 * POST /api/microsoft/sync
 *
 * Fetches emails from Microsoft Graph using the access token.
 * Body: { token: string, maxEmails?: number }
 *
 * Returns: { messages: [...] }
 */
export async function POST(request: NextRequest) {
  const body = await request.json().catch(() => ({}));
  const token = body.token;
  const maxEmails = body.maxEmails || 50;

  if (!token) {
    return NextResponse.json({ error: "Access token is required" }, { status: 400 });
  }

  try {
    const controller = new AbortController();
    const timeout = setTimeout(() => controller.abort(), 9000);
    try {
      // Fetch emails from Microsoft Graph
      const res = await fetch(
        `https://graph.microsoft.com/v1.0/me/messages?$top=${maxEmails}&$select=id,subject,from,receivedDateTime,bodyPreview,hasAttachments,importance,categories&$orderby=receivedDateTime desc`,
        {
          headers: { Authorization: `Bearer ${token}` },
          signal: controller.signal,
        }
      );

      if (!res.ok) {
        const err = await res.text();
        return NextResponse.json({ error: `Graph API error: ${err}` }, { status: res.status });
      }

      const data = await res.json();
      const messages = (data.value || []).map((msg: any) => ({
        id: msg.id,
        subject: msg.subject || "(no subject)",
        from: msg.from?.emailAddress
          ? `${msg.from.emailAddress.name || ""} <${msg.from.emailAddress.address || ""}>`.trim()
          : "unknown",
        fromAddress: msg.from?.emailAddress?.address || "",
        receivedDateTime: msg.receivedDateTime || "",
        bodyPreview: msg.bodyPreview || "",
        hasAttachments: msg.hasAttachments || false,
        importance: msg.importance || "normal",
        categories: msg.categories || [],
      }));

      return NextResponse.json({
        success: true,
        count: messages.length,
        messages,
      });
    } finally {
      clearTimeout(timeout);
    }
  } catch (e: any) {
    return NextResponse.json({ error: e.name === "AbortError" ? "Graph request timed out" : e.message }, { status: 504 });
  }
}