d25d482dc2
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
84 lines
2.9 KiB
TypeScript
84 lines
2.9 KiB
TypeScript
import { db } from '@sim/db'
|
|
import { outboxEvent } from '@sim/db/schema'
|
|
import { createLogger } from '@sim/logger'
|
|
import { toError } from '@sim/utils/errors'
|
|
import { and, desc, eq, sql } from 'drizzle-orm'
|
|
import { type NextRequest, NextResponse } from 'next/server'
|
|
import { adminV1ListOutboxContract } from '@/lib/api/contracts/v1/admin'
|
|
import { getValidationErrorMessage, parseRequest } from '@/lib/api/server'
|
|
import { withRouteHandler } from '@/lib/core/utils/with-route-handler'
|
|
import { withAdminAuth } from '@/app/api/v1/admin/middleware'
|
|
|
|
const logger = createLogger('AdminOutboxAPI')
|
|
|
|
export const dynamic = 'force-dynamic'
|
|
|
|
const invalidOutboxQueryResponse = (message: string) =>
|
|
NextResponse.json({ success: false, error: message }, { status: 400 })
|
|
|
|
/**
|
|
* GET /api/v1/admin/outbox?status=dead_letter&eventType=...&limit=100
|
|
*
|
|
* Inspect outbox events for operator triage. Primary use: list
|
|
* dead-lettered rows to reconcile Stripe state manually after a
|
|
* permanent handler failure (e.g. Stripe account frozen, subscription
|
|
* already canceled by another path, etc.).
|
|
*
|
|
* Filters:
|
|
* - `status`: 'pending' | 'processing' | 'completed' | 'dead_letter' (default 'dead_letter')
|
|
* - `eventType`: exact match on event_type
|
|
* - `limit`: cap rows returned (default 100, max 500)
|
|
*
|
|
* Response includes aggregate counts by status for quick health read.
|
|
*/
|
|
export const GET = withRouteHandler(
|
|
withAdminAuth(async (request: NextRequest) => {
|
|
try {
|
|
const parsed = await parseRequest(
|
|
adminV1ListOutboxContract,
|
|
request,
|
|
{},
|
|
{
|
|
validationErrorResponse: (error) =>
|
|
invalidOutboxQueryResponse(getValidationErrorMessage(error, 'Invalid outbox query')),
|
|
}
|
|
)
|
|
if (!parsed.success) return parsed.response
|
|
|
|
const { status, eventType, limit } = parsed.data.query
|
|
|
|
const whereConditions = [eq(outboxEvent.status, status)]
|
|
if (eventType) {
|
|
whereConditions.push(eq(outboxEvent.eventType, eventType))
|
|
}
|
|
|
|
const rows = await db
|
|
.select()
|
|
.from(outboxEvent)
|
|
.where(and(...whereConditions))
|
|
.orderBy(desc(outboxEvent.createdAt))
|
|
.limit(limit)
|
|
|
|
// Aggregate counts per (status, eventType) for at-a-glance health.
|
|
const counts = await db
|
|
.select({
|
|
status: outboxEvent.status,
|
|
eventType: outboxEvent.eventType,
|
|
count: sql<number>`count(*)::int`,
|
|
})
|
|
.from(outboxEvent)
|
|
.groupBy(outboxEvent.status, outboxEvent.eventType)
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
filter: { status, eventType, limit },
|
|
rows,
|
|
counts,
|
|
})
|
|
} catch (error) {
|
|
logger.error('Failed to list outbox events', { error: toError(error).message })
|
|
return NextResponse.json({ success: false, error: toError(error).message }, { status: 500 })
|
|
}
|
|
})
|
|
)
|