* feat: event log, pending operations, and MCP staging - Event log system: persist bus events to event_log table for external automation platforms. Batch insert for transaction.synced. Daily cleanup cron at 02:00 UTC. - Pending operations: MCP write tools (categorize, create customer, create invoice) now stage to pending_operations instead of executing directly. Users review and commit/reject from /pending in the web UI. - Granskning page: card-based review UI with expandable previews, commit/reject dialogs. Only shown in nav when pending ops exist. - Commit route re-executes using core lib functions (no extension imports). Guards against stale state (double-commit, deleted entities). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * feat: stage new MCP write tools after main merge Add staging for 4 new write tools from #133: - mark_invoice_paid, send_invoice, mark_invoice_sent, match_transaction_invoice - Expand pending_operations CHECK constraint - Add commit executors with full execution logic - Add UI labels and generic preview component - Remove confirm parameter from categorize (single-call staging) - Fix UUID in pending op title (fetch transaction description) - Hide Granskning nav when no pending ops Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: address PR review feedback - Fix TS build error: use `select('*, customer:customers(*)')` for match_transaction_invoice to avoid array type inference - Add status guard to commitSendInvoice (prevents duplicate sends) - Replace auth.admin.getUserById with user email from session auth - Restore optimistic lock check in commitMatchTransactionInvoice - Fix tool description typo: expense_software → expense_office Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
78 lines
2.4 KiB
TypeScript
78 lines
2.4 KiB
TypeScript
import { createClient } from '@/lib/supabase/server'
|
|
import { NextResponse } from 'next/server'
|
|
import { extractBearerToken, validateApiKey, createServiceClientNoCookies } from '@/lib/auth/api-keys'
|
|
import { validateQuery } from '@/lib/api/validate'
|
|
import { EventsQuerySchema } from '@/lib/api/schemas'
|
|
import type { SupabaseClient } from '@supabase/supabase-js'
|
|
|
|
/**
|
|
* GET /api/events
|
|
*
|
|
* Cursor-based polling endpoint for external automation platforms (n8n, Make, Zapier).
|
|
* Returns events from the event_log table in sequence order.
|
|
*
|
|
* Query params:
|
|
* - after (bigint, optional): return events with sequence > this value
|
|
* - types (string, optional): comma-separated event type filter
|
|
* - limit (int, optional): max results, default 50, cap 100
|
|
*
|
|
* Supports both session auth (browser) and API key auth (automation platforms).
|
|
*/
|
|
export async function GET(request: Request) {
|
|
// Dual auth: API key or session
|
|
let userId: string
|
|
let supabase: SupabaseClient
|
|
|
|
const token = extractBearerToken(request)
|
|
if (token?.startsWith('gnubok_sk_')) {
|
|
const authResult = await validateApiKey(token)
|
|
if ('error' in authResult) {
|
|
return NextResponse.json({ error: authResult.error }, { status: authResult.status })
|
|
}
|
|
userId = authResult.userId
|
|
supabase = createServiceClientNoCookies()
|
|
} else {
|
|
supabase = await createClient()
|
|
const { data: { user } } = await supabase.auth.getUser()
|
|
if (!user) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
userId = user.id
|
|
}
|
|
|
|
// Validate query params
|
|
const result = validateQuery(request, EventsQuerySchema)
|
|
if (!result.success) return result.response
|
|
const { after, types, limit } = result.data
|
|
|
|
// Build query
|
|
let query = supabase
|
|
.from('event_log')
|
|
.select('sequence, event_type, entity_id, data, created_at')
|
|
.eq('user_id', userId)
|
|
.order('sequence', { ascending: true })
|
|
.limit(limit)
|
|
|
|
if (after !== undefined) {
|
|
query = query.gt('sequence', after)
|
|
}
|
|
|
|
if (types && types.length > 0) {
|
|
query = query.in('event_type', types)
|
|
}
|
|
|
|
const { data, error } = await query
|
|
|
|
if (error) {
|
|
return NextResponse.json({ error: error.message }, { status: 500 })
|
|
}
|
|
|
|
const events = data ?? []
|
|
|
|
return NextResponse.json({
|
|
data: events,
|
|
cursor: events.length > 0 ? events[events.length - 1].sequence : (after ?? 0),
|
|
has_more: events.length === limit,
|
|
})
|
|
}
|