← back to Norma

app/api/archives/[id]/route.ts

40 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/archives/:id
 * Get full archive entry including content.
 * Also increments usage_count.
 */
export async function GET(
  request: NextRequest,
  { params }: { params: Promise<{ id: string }> }
) {
  const auth = requireRole(request, 'admin', 'staff');
  if (auth instanceof NextResponse) return auth;

  try {
    const { id } = await params;

    const result = await query(
      `UPDATE historical_archives
       SET usage_count = usage_count + 1, updated_at = NOW()
       WHERE id = $1
       RETURNING *`,
      [id]
    );

    if (result.rows.length === 0) {
      return NextResponse.json({ error: 'Archive not found' }, { status: 404 });
    }

    return NextResponse.json({ archive: result.rows[0] });
  } catch (err) {
    console.error('[api/archives/id] GET error:', (err as Error).message);
    return NextResponse.json({ error: 'Failed to fetch archive' }, { status: 500 });
  }
}