wulf-pulse/app/api/notification-channels/[id]/route.ts

71 lines
2.3 KiB
TypeScript

/**
* Single Notification Channel API — get, update, delete.
*/
import { NextRequest, NextResponse } from 'next/server';
import { postgresClient } from '@/lib/services/postgres-client';
export async function GET(request: NextRequest, { params }: { params: Promise<{ id: string }> }) {
try {
const { id } = await params;
const result = await postgresClient.query(
`SELECT * FROM notification_channels WHERE id = $1`, [id]
);
if (result.rows.length === 0) {
return NextResponse.json({ error: 'Channel not found' }, { status: 404 });
}
return NextResponse.json(result.rows[0]);
} catch (error) {
const msg = error instanceof Error ? error.message : String(error);
return NextResponse.json({ error: msg }, { status: 500 });
}
}
export async function PUT(request: NextRequest, { params }: { params: Promise<{ id: string }> }) {
try {
const { id } = await params;
const body = await request.json();
const { name, channel_type, config, is_active } = body;
const result = await postgresClient.query(
`UPDATE notification_channels
SET name = COALESCE($1, name),
channel_type = COALESCE($2, channel_type),
config = COALESCE($3, config),
is_active = COALESCE($4, is_active),
updated_at = NOW()
WHERE id = $5
RETURNING *`,
[name, channel_type, config ? JSON.stringify(config) : null, is_active, id]
);
if (result.rows.length === 0) {
return NextResponse.json({ error: 'Channel not found' }, { status: 404 });
}
return NextResponse.json(result.rows[0]);
} catch (error) {
const msg = error instanceof Error ? error.message : String(error);
return NextResponse.json({ error: msg }, { status: 500 });
}
}
export async function DELETE(request: NextRequest, { params }: { params: Promise<{ id: string }> }) {
try {
const { id } = await params;
const result = await postgresClient.query(
`DELETE FROM notification_channels WHERE id = $1 RETURNING id`, [id]
);
if (result.rows.length === 0) {
return NextResponse.json({ error: 'Channel not found' }, { status: 404 });
}
return NextResponse.json({ deleted: true, id: Number(id) });
} catch (error) {
const msg = error instanceof Error ? error.message : String(error);
return NextResponse.json({ error: msg }, { status: 500 });
}
}