first commit
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
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 })
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { getServerSession } from 'next-auth'
|
||||
import { authOptions } from '@/lib/auth'
|
||||
import sql from '@/lib/db'
|
||||
|
||||
// GET /api/apps/[id]/config?env=production
|
||||
export async function GET(req: NextRequest, { params }: { params: Promise<{ id: string }> }) {
|
||||
const session = await getServerSession(authOptions)
|
||||
if (!session) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
|
||||
const { id } = await params
|
||||
const env = req.nextUrl.searchParams.get('env') ?? 'production'
|
||||
|
||||
const entries = await sql`
|
||||
SELECT ce.*
|
||||
FROM config_entries ce
|
||||
JOIN environments e ON e.id = ce.environment_id
|
||||
WHERE e.app_id = ${id} AND e.name = ${env}
|
||||
ORDER BY ce.key
|
||||
`
|
||||
return NextResponse.json(entries)
|
||||
}
|
||||
|
||||
// POST /api/apps/[id]/config
|
||||
export async function POST(req: NextRequest, { params }: { params: Promise<{ id: string }> }) {
|
||||
const session = await getServerSession(authOptions)
|
||||
if (!session) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
|
||||
const { id } = await params
|
||||
const body = await req.json()
|
||||
const { environment_id, key, value, type, description, environment } = body
|
||||
|
||||
if (!environment_id || !key || value === undefined) {
|
||||
return NextResponse.json({ error: 'environment_id, key, value are required' }, { status: 400 })
|
||||
}
|
||||
|
||||
try {
|
||||
const [entry] = await sql`
|
||||
INSERT INTO config_entries (environment_id, key, value, type, description)
|
||||
VALUES (${environment_id}, ${key.toUpperCase()}, ${value}, ${type ?? 'text'}, ${description ?? null})
|
||||
RETURNING *
|
||||
`
|
||||
|
||||
await sql`
|
||||
INSERT INTO audit_logs (app_id, environment, action, key, actor)
|
||||
VALUES (${id}, ${environment}, 'create', ${key}, ${session.user?.name ?? 'admin'})
|
||||
`
|
||||
|
||||
return NextResponse.json(entry, { status: 201 })
|
||||
} catch (err: any) {
|
||||
if (err.code === '23505') {
|
||||
return NextResponse.json({ error: `Key "${key}" already exists in this environment` }, { status: 409 })
|
||||
}
|
||||
return NextResponse.json({ error: err.message }, { status: 500 })
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user