← back to Norma Platform

app/api/social/posts/pending/route.ts

46 lines

import { NextRequest, NextResponse } from 'next/server';
import { query } from '@/lib/db';
import { requireRole } from '@/lib/require-role';

export const dynamic = 'force-dynamic';

/**
 * GET /api/social/posts/pending
 * Returns posts awaiting approval.
 */
export async function GET(request: NextRequest) {
  const auth = requireRole(request, 'admin', 'staff');
  if (auth instanceof NextResponse) return auth;
  try {
    const result = await query(
      `SELECT
         p.id,
         p.body,
         p.platform,
         p.media_urls,
         p.hashtags,
         p.link_url,
         p.submitted_at,
         p.created_by,
         p.needs_approval,
         p.status,
         p.created_at,
         a.account_name,
         a.display_name
       FROM social_posts p
       LEFT JOIN social_accounts a ON a.id = p.account_id
       WHERE p.status = 'pending_approval'
         AND p.needs_approval = true
       ORDER BY p.submitted_at DESC NULLS LAST, p.created_at DESC`,
    );

    return NextResponse.json({ pending: result.rows });
  } catch (err) {
    console.error('[posts/pending] error:', (err as Error).message);
    return NextResponse.json(
      { error: 'Failed to load pending posts' },
      { status: 500 },
    );
  }
}