70 lines
2.0 KiB
TypeScript
70 lines
2.0 KiB
TypeScript
import { NextResponse } from 'next/server';
|
|
import { getServerSession } from 'next-auth';
|
|
import { authOptions } from "@/app/api/auth/options";
|
|
import { toggleEmailFlag } from '@/lib/services/email-service';
|
|
import { invalidateEmailContentCache, invalidateFolderCache } from '@/lib/redis';
|
|
|
|
export async function POST(
|
|
request: Request,
|
|
context: { params: Promise<{ id: string }> }
|
|
) {
|
|
try {
|
|
const session = await getServerSession(authOptions);
|
|
if (!session || !session.user?.id) {
|
|
return NextResponse.json(
|
|
{ error: "Not authenticated" },
|
|
{ status: 401 }
|
|
);
|
|
}
|
|
|
|
// Await params as per Next.js requirements
|
|
const params = await context.params;
|
|
const id = params?.id;
|
|
if (!id) {
|
|
return NextResponse.json(
|
|
{ error: "Missing email ID" },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
const { flagged, folder, accountId } = await request.json();
|
|
|
|
if (typeof flagged !== 'boolean') {
|
|
return NextResponse.json(
|
|
{ error: "Invalid 'flagged' parameter. Must be a boolean." },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
const normalizedFolder = folder || "INBOX";
|
|
const effectiveAccountId = accountId || 'default';
|
|
|
|
// Use the email service to toggle the flag
|
|
// Note: You'll need to implement this function in email-service.ts
|
|
const success = await toggleEmailFlag(
|
|
session.user.id,
|
|
id,
|
|
flagged,
|
|
normalizedFolder,
|
|
effectiveAccountId
|
|
);
|
|
|
|
if (!success) {
|
|
return NextResponse.json(
|
|
{ error: `Failed to ${flagged ? 'star' : 'unstar'} email` },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
|
|
// Invalidate cache for this email
|
|
await invalidateEmailContentCache(session.user.id, effectiveAccountId, id);
|
|
|
|
return NextResponse.json({ success: true });
|
|
} catch (error: any) {
|
|
console.error("Error in flag API:", error);
|
|
return NextResponse.json(
|
|
{ error: "Internal server error", message: error.message },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|