46 lines
1.6 KiB
TypeScript
46 lines
1.6 KiB
TypeScript
/**
|
|
* Notification Channels API — list and create channels.
|
|
*/
|
|
|
|
import { NextRequest, NextResponse } from 'next/server';
|
|
import { postgresClient } from '@/lib/services/postgres-client';
|
|
|
|
export async function GET() {
|
|
try {
|
|
const result = await postgresClient.query(
|
|
`SELECT * FROM notification_channels ORDER BY name`
|
|
);
|
|
return NextResponse.json({ data: result.rows, total: result.rows.length });
|
|
} catch (error) {
|
|
const msg = error instanceof Error ? error.message : String(error);
|
|
return NextResponse.json({ error: msg }, { status: 500 });
|
|
}
|
|
}
|
|
|
|
export async function POST(request: NextRequest) {
|
|
try {
|
|
const body = await request.json();
|
|
const { name, channel_type, config, is_active } = body;
|
|
|
|
if (!name || !channel_type) {
|
|
return NextResponse.json({ error: 'name and channel_type are required' }, { status: 400 });
|
|
}
|
|
|
|
const validTypes = ['teams', 'telegram', 'ntfy', 'webhook'];
|
|
if (!validTypes.includes(channel_type)) {
|
|
return NextResponse.json({ error: `channel_type must be one of: ${validTypes.join(', ')}` }, { status: 400 });
|
|
}
|
|
|
|
const result = await postgresClient.query(
|
|
`INSERT INTO notification_channels (name, channel_type, config, is_active)
|
|
VALUES ($1, $2, $3, $4)
|
|
RETURNING *`,
|
|
[name, channel_type, JSON.stringify(config || {}), is_active ?? true]
|
|
);
|
|
|
|
return NextResponse.json(result.rows[0], { status: 201 });
|
|
} catch (error) {
|
|
const msg = error instanceof Error ? error.message : String(error);
|
|
return NextResponse.json({ error: msg }, { status: 500 });
|
|
}
|
|
}
|