← back to Patty
app/api/petitions/[slug]/post-log/route.ts
49 lines
import { NextRequest, NextResponse } from 'next/server';
import { verifyAuth } from '@/lib/auth';
import { query } from '@/lib/db';
/**
* GET /api/petitions/[slug]/post-log
* Returns posting history for a petition.
*/
export async function GET(
request: NextRequest,
{ params }: { params: Promise<{ slug: string }> },
) {
const user = verifyAuth(request);
if (!user) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}
const { slug } = await params;
// Look up petition by slug
const petitionResult = await query(
`SELECT id FROM petitions WHERE slug = $1 LIMIT 1`,
[slug],
);
if (petitionResult.rows.length === 0) {
return NextResponse.json({ error: 'Petition not found' }, { status: 404 });
}
const petitionId = petitionResult.rows[0].id;
const logsResult = await query(
`SELECT id, platform, post_text, post_url, external_id, status, error_msg,
platform_config, actor, scheduled_at, posted_at, created_at
FROM petition_posts
WHERE petition_id = $1
ORDER BY created_at DESC
LIMIT 50`,
[petitionId],
);
return NextResponse.json({
petition_id: petitionId,
slug,
logs: logsResult.rows,
total: logsResult.rowCount,
});
}