import { auth } from "@/lib/auth"; import { prisma } from "@/lib/prisma"; import { pushToPostiz, getPostizIntegrations } from "@/lib/postiz"; export async function GET() { const session = await auth(); if (!session) return new Response("Unauthorized", { status: 401 }); try { const integrations = await getPostizIntegrations(); return Response.json(integrations); } catch (error) { return Response.json( { error: error instanceof Error ? error.message : "Failed to fetch integrations" }, { status: 500 } ); } } export async function POST(request: Request) { const session = await auth(); if (!session) return new Response("Unauthorized", { status: 401 }); const { assetIds, scheduledAt } = await request.json(); if (!assetIds?.length || !scheduledAt) { return Response.json( { error: "assetIds and scheduledAt required" }, { status: 400 } ); } const results = []; for (const id of assetIds) { const asset = await prisma.asset.findUnique({ where: { id } }); if (!asset) continue; try { const post = await pushToPostiz(asset, scheduledAt); await prisma.asset.update({ where: { id }, data: { status: "published", postizPostId: post.id, }, }); results.push({ id, status: "scheduled", postId: post.id }); } catch (error) { results.push({ id, status: "failed", error: error instanceof Error ? error.message : "Unknown error", }); } } return Response.json({ results }); }