Files
ProjectE/apps/web/app/api/webhooks/[id]/test/route.ts
T
mbatchelder b3ff23a5f0 feat: Phase 1 foundation - schema, auth, realtime, shell
- Rewrote Drizzle schema: 20 tables with enums, relations, indexes
- Generated migration with DROP TABLE records (v1 EAV removal)
- Added passkey auth routes (register/login)
- Added requireWorkspaceAccess helper
- Added seedDefaultData for Personal workspace + welcome note
- Updated SSE endpoint for v2 entities + workspace_id filtering
- Created recordActivity helper (insert + pg_notify)
- Updated sidebar: Graph replaces Reports, removed Analytics
- Updated command palette for v2 entities
- Created AGENTS.md with locked contract
- Created llm-wiki scaffold (5 stubs)
- Added inline AGENT INSTRUCTION comments to all 50 API route files
- Fixed globals.css border-border class conflict
- Updated database.ts stub for v1 compatibility
2026-07-29 05:53:13 -04:00

99 lines
2.8 KiB
TypeScript

// 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.
import { NextRequest, NextResponse } from 'next/server';
import { withAuth, createErrorResponse } from '@/lib/auth';
import { createPocketBaseClient } from '@/lib/pocketbase';
type RouteContext = { params: Promise<{ id: string }> };
// POST /api/webhooks/[id]/test — Send a test event to the webhook
export const POST = withAuth<RouteContext>(async (request: NextRequest, _user, context) => {
const { id } = await context!.params;
const pb = createPocketBaseClient();
// Get the webhook
const webhook = await pb.collection('webhooks').getOne(id);
if (!webhook.active) {
return createErrorResponse('WEBHOOK_DISABLED', 'Cannot test a disabled webhook', 400);
}
// Create a test payload
const testPayload = {
event: 'test.ping',
timestamp: new Date().toISOString(),
data: {
message: 'This is a test webhook delivery from Project E.',
webhook_id: webhook.id,
webhook_name: webhook.name,
},
};
// Create HMAC signature if secret is provided
const headers: Record<string, string> = {
'Content-Type': 'application/json',
'X-Event-Type': 'test.ping',
};
if (webhook.secret) {
const crypto = await import('node:crypto');
const body = JSON.stringify(testPayload);
const signature = crypto
.createHmac('sha256', webhook.secret)
.update(body)
.digest('hex');
headers['X-Webhook-Signature'] = signature;
}
try {
const response = await fetch(webhook.url, {
method: 'POST',
headers,
body: JSON.stringify(testPayload),
signal: AbortSignal.timeout(10000),
});
const responseBody = await response.text();
// Record the delivery
await pb.collection('webhook_deliveries').create({
webhook_id: webhook.id,
event_type: 'test.ping',
payload: testPayload as Record<string, unknown>,
success: response.ok,
response_status: response.status,
response_body: responseBody.substring(0, 1000),
attempts: 1,
});
return NextResponse.json({
success: response.ok,
status: response.status,
response: responseBody.substring(0, 500),
});
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
// Record the failed delivery
await pb.collection('webhook_deliveries').create({
webhook_id: webhook.id,
event_type: 'test.ping',
payload: testPayload as Record<string, unknown>,
success: false,
response_status: 0,
response_body: errorMessage,
attempts: 1,
});
return NextResponse.json({
success: false,
status: 0,
response: errorMessage,
});
}
});