diff options
Diffstat (limited to 'apps/web/app/api')
| -rw-r--r-- | apps/web/app/api/v1/keys/route.ts | 27 | ||||
| -rw-r--r-- | apps/web/app/api/webhook-config/route.ts | 21 |
2 files changed, 36 insertions, 12 deletions
diff --git a/apps/web/app/api/v1/keys/route.ts b/apps/web/app/api/v1/keys/route.ts index 1461532..de63a46 100644 --- a/apps/web/app/api/v1/keys/route.ts +++ b/apps/web/app/api/v1/keys/route.ts @@ -31,14 +31,15 @@ export async function GET() { ) } + const activeKeys = keys.filter((key) => key.revoked_at === null) + return NextResponse.json({ - keys: keys.map((key) => ({ - identifier: key.id, + keys: activeKeys.map((key) => ({ + keyIdentifier: key.id, keyPrefix: key.key_prefix, label: key.label, createdAt: key.created_at, lastUsedAt: key.last_used_at, - isRevoked: key.revoked_at !== null, })), }) } @@ -94,12 +95,16 @@ export async function POST(request: Request) { const { fullKey, keyHash, keyPrefix } = generateApiKey() - const { error: insertError } = await adminClient.from("api_keys").insert({ - user_id: user.id, - key_hash: keyHash, - key_prefix: keyPrefix, - label, - }) + const { data: insertedKey, error: insertError } = await adminClient + .from("api_keys") + .insert({ + user_id: user.id, + key_hash: keyHash, + key_prefix: keyPrefix, + label, + }) + .select("id") + .single() if (insertError) { return NextResponse.json( @@ -109,8 +114,8 @@ export async function POST(request: Request) { } return NextResponse.json({ - key: fullKey, + fullKey, keyPrefix, - label, + keyIdentifier: insertedKey.id, }) } diff --git a/apps/web/app/api/webhook-config/route.ts b/apps/web/app/api/webhook-config/route.ts index 049f4f3..bd38d0f 100644 --- a/apps/web/app/api/webhook-config/route.ts +++ b/apps/web/app/api/webhook-config/route.ts @@ -91,10 +91,29 @@ export async function PUT(request: Request) { } if (typeof body.webhookEnabled === "boolean") { - updates.webhook_enabled = body.webhookEnabled if (body.webhookEnabled) { + const { data: currentProfile } = await adminClient + .from("user_profiles") + .select("webhook_url") + .eq("id", user.id) + .single() + + const effectiveUrl = + typeof body.webhookUrl === "string" + ? body.webhookUrl.trim() + : currentProfile?.webhook_url + + if (!effectiveUrl) { + return NextResponse.json( + { error: "cannot enable webhooks without a url" }, + { status: 400 } + ) + } + updates.webhook_consecutive_failures = 0 } + + updates.webhook_enabled = body.webhookEnabled } if (Object.keys(updates).length === 0) { |