2026-07-29 05:53:13 -04:00
|
|
|
// AGENT INSTRUCTION: After every write (INSERT/UPDATE/DELETE), you MUST:
|
|
|
|
|
// 1. Insert activity feed entry
|
|
|
|
|
// 2. Call pg.notify('project_e_events', JSON.stringify({ type, action, id, workspace_id }))
|
|
|
|
|
// See AGENTS.md for full rules.
|
|
|
|
|
|
2026-07-16 06:19:58 -04:00
|
|
|
import { NextRequest, NextResponse } from 'next/server';
|
|
|
|
|
import { withAuth } from '@/lib/auth';
|
2026-07-29 08:03:28 -04:00
|
|
|
import { db, webhookDeliveries } from '@project-e/db';
|
|
|
|
|
import { and, desc, eq, sql } from 'drizzle-orm';
|
2026-07-16 06:19:58 -04:00
|
|
|
|
|
|
|
|
// GET /api/webhook-deliveries — List webhook deliveries with filtering
|
|
|
|
|
export const GET = withAuth(async (request: NextRequest, _user) => {
|
|
|
|
|
const { searchParams } = new URL(request.url);
|
2026-07-29 08:03:28 -04:00
|
|
|
const limit = Math.min(parseInt(searchParams.get('limit') || '50'), 200);
|
|
|
|
|
const offset = parseInt(searchParams.get('offset') || '0');
|
2026-07-16 06:19:58 -04:00
|
|
|
const webhookId = searchParams.get('webhook_id') || '';
|
|
|
|
|
|
2026-07-29 08:03:28 -04:00
|
|
|
const conditions = [];
|
2026-07-16 06:19:58 -04:00
|
|
|
if (webhookId) {
|
2026-07-29 08:03:28 -04:00
|
|
|
conditions.push(eq(webhookDeliveries.webhookId, webhookId));
|
2026-07-16 06:19:58 -04:00
|
|
|
}
|
|
|
|
|
|
2026-07-29 08:03:28 -04:00
|
|
|
const [items, countResult] = await Promise.all([
|
|
|
|
|
db.select()
|
|
|
|
|
.from(webhookDeliveries)
|
|
|
|
|
.where(conditions.length > 0 ? and(...conditions) : undefined)
|
|
|
|
|
.orderBy(desc(webhookDeliveries.createdAt))
|
|
|
|
|
.limit(limit)
|
|
|
|
|
.offset(offset),
|
|
|
|
|
db.select({ count: sql<number>`count(*)` })
|
|
|
|
|
.from(webhookDeliveries)
|
|
|
|
|
.where(conditions.length > 0 ? and(...conditions) : undefined),
|
|
|
|
|
]);
|
2026-07-16 06:19:58 -04:00
|
|
|
|
|
|
|
|
return NextResponse.json({
|
2026-07-29 08:03:28 -04:00
|
|
|
items,
|
|
|
|
|
totalItems: Number(countResult[0]?.count || 0),
|
|
|
|
|
limit,
|
|
|
|
|
offset,
|
2026-07-16 06:19:58 -04:00
|
|
|
});
|
|
|
|
|
});
|