← back to Freddy

app/api/impact/route.ts

33 lines

import { NextRequest, NextResponse } from 'next/server';
import { query } from '@/lib/db';
import { verifyAuth } from '@/lib/auth';

export async function GET(request: NextRequest) {
  const user = verifyAuth(request);
  if (!user) {
    return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
  }

  try {
    const result = await query(`
      SELECT ir.id, ir.title, ir.summary as description, ir.people_helped, ir.funds_used,
             ir.outcomes, ir.evidence_urls, ir.verified as status, ir.submitted_at as period_start,
             ir.created_at,
             o.name as org_name,
             c.title as cause_title,
             m.match_score
      FROM impact_reports ir
      JOIN organizations o ON o.id = ir.org_id
      LEFT JOIN matches m ON m.id = ir.match_id
      LEFT JOIN causes c ON c.id = m.cause_id
      ORDER BY ir.created_at DESC
      LIMIT 50
    `);

    return NextResponse.json({ impact_reports: result.rows });
  } catch (err) {
    console.error('[impact] GET error:', (err as Error).message);
    return NextResponse.json({ error: 'Failed to fetch impact reports' }, { status: 500 });
  }
}