58 lines
1.8 KiB
TypeScript
58 lines
1.8 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { getServerSession } from 'next-auth'
|
|
import { authOptions } from '@/lib/auth'
|
|
import sql from '@/lib/db'
|
|
|
|
// PATCH /api/apps/[id]/config/[entryId]
|
|
export async function PATCH(
|
|
req: NextRequest,
|
|
{ params }: { params: Promise<{ id: string; entryId: string }> }
|
|
) {
|
|
const session = await getServerSession(authOptions)
|
|
if (!session) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
|
|
const { id, entryId } = await params
|
|
const { value, environment } = await req.json()
|
|
|
|
const [old] = await sql`SELECT key, value FROM config_entries WHERE id = ${entryId}`
|
|
|
|
const [entry] = await sql`
|
|
UPDATE config_entries SET value = ${value}
|
|
WHERE id = ${entryId}
|
|
RETURNING *
|
|
`
|
|
|
|
await sql`
|
|
INSERT INTO audit_logs (app_id, environment, action, key, old_value, actor)
|
|
VALUES (
|
|
${id}, ${environment}, 'update',
|
|
${old?.key}, ${old?.value?.slice(0, 100)},
|
|
${session.user?.name ?? 'admin'}
|
|
)
|
|
`
|
|
|
|
return NextResponse.json(entry)
|
|
}
|
|
|
|
// DELETE /api/apps/[id]/config/[entryId]
|
|
export async function DELETE(
|
|
req: NextRequest,
|
|
{ params }: { params: Promise<{ id: string; entryId: string }> }
|
|
) {
|
|
const session = await getServerSession(authOptions)
|
|
if (!session) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
|
|
const { id, entryId } = await params
|
|
const env = req.nextUrl.searchParams.get('environment') ?? ''
|
|
const [old] = await sql`SELECT key FROM config_entries WHERE id = ${entryId}`
|
|
|
|
await sql`DELETE FROM config_entries WHERE id = ${entryId}`
|
|
|
|
await sql`
|
|
INSERT INTO audit_logs (app_id, environment, action, key, actor)
|
|
VALUES (${id}, ${env}, 'delete', ${old?.key}, ${session.user?.name ?? 'admin'})
|
|
`
|
|
|
|
return NextResponse.json({ success: true })
|
|
}
|