mirror of
https://github.com/khoaliber/dockhand.git
synced 2026-03-06 13:21:53 +00:00
Initial commit
This commit is contained in:
78
routes/api/environments/[id]/notifications/+server.ts
Normal file
78
routes/api/environments/[id]/notifications/+server.ts
Normal file
@@ -0,0 +1,78 @@
|
||||
import { json } from '@sveltejs/kit';
|
||||
import {
|
||||
getEnvironmentNotifications,
|
||||
createEnvironmentNotification,
|
||||
getEnvironment,
|
||||
getNotificationSettings,
|
||||
type NotificationEventType
|
||||
} from '$lib/server/db';
|
||||
import { authorize } from '$lib/server/authorize';
|
||||
import type { RequestHandler } from './$types';
|
||||
|
||||
// GET /api/environments/[id]/notifications - List all notification configurations for an environment
|
||||
export const GET: RequestHandler = async ({ params, cookies }) => {
|
||||
const auth = await authorize(cookies);
|
||||
if (auth.authEnabled && !await auth.can('notifications', 'view')) {
|
||||
return json({ error: 'Permission denied' }, { status: 403 });
|
||||
}
|
||||
|
||||
const envId = parseInt(params.id);
|
||||
if (isNaN(envId)) {
|
||||
return json({ error: 'Invalid environment ID' }, { status: 400 });
|
||||
}
|
||||
|
||||
const env = await getEnvironment(envId);
|
||||
if (!env) {
|
||||
return json({ error: 'Environment not found' }, { status: 404 });
|
||||
}
|
||||
|
||||
try {
|
||||
const notifications = await getEnvironmentNotifications(envId);
|
||||
return json(notifications);
|
||||
} catch (error) {
|
||||
console.error('Error fetching environment notifications:', error);
|
||||
return json({ error: 'Failed to fetch environment notifications' }, { status: 500 });
|
||||
}
|
||||
};
|
||||
|
||||
// POST /api/environments/[id]/notifications - Add a notification channel to an environment
|
||||
export const POST: RequestHandler = async ({ params, request, cookies }) => {
|
||||
const auth = await authorize(cookies);
|
||||
if (auth.authEnabled && !await auth.can('notifications', 'edit')) {
|
||||
return json({ error: 'Permission denied' }, { status: 403 });
|
||||
}
|
||||
|
||||
const envId = parseInt(params.id);
|
||||
if (isNaN(envId)) {
|
||||
return json({ error: 'Invalid environment ID' }, { status: 400 });
|
||||
}
|
||||
|
||||
const env = await getEnvironment(envId);
|
||||
if (!env) {
|
||||
return json({ error: 'Environment not found' }, { status: 404 });
|
||||
}
|
||||
|
||||
try {
|
||||
const body = await request.json();
|
||||
const { notificationId, enabled, eventTypes } = body;
|
||||
|
||||
if (!notificationId) {
|
||||
return json({ error: 'notificationId is required' }, { status: 400 });
|
||||
}
|
||||
|
||||
const notification = await createEnvironmentNotification({
|
||||
environmentId: envId,
|
||||
notificationId,
|
||||
enabled: enabled !== false,
|
||||
eventTypes: eventTypes as NotificationEventType[]
|
||||
});
|
||||
|
||||
return json(notification);
|
||||
} catch (error: any) {
|
||||
console.error('Error creating environment notification:', error);
|
||||
if (error.message?.includes('UNIQUE constraint failed')) {
|
||||
return json({ error: 'This notification channel is already configured for this environment' }, { status: 409 });
|
||||
}
|
||||
return json({ error: error.message || 'Failed to create environment notification' }, { status: 500 });
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,103 @@
|
||||
import { json } from '@sveltejs/kit';
|
||||
import {
|
||||
getEnvironmentNotification,
|
||||
updateEnvironmentNotification,
|
||||
deleteEnvironmentNotification,
|
||||
getEnvironment,
|
||||
type NotificationEventType
|
||||
} from '$lib/server/db';
|
||||
import { authorize } from '$lib/server/authorize';
|
||||
import type { RequestHandler } from './$types';
|
||||
|
||||
// GET /api/environments/[id]/notifications/[notificationId] - Get a specific environment notification
|
||||
export const GET: RequestHandler = async ({ params, cookies }) => {
|
||||
const auth = await authorize(cookies);
|
||||
if (auth.authEnabled && !await auth.can('notifications', 'view')) {
|
||||
return json({ error: 'Permission denied' }, { status: 403 });
|
||||
}
|
||||
|
||||
const envId = parseInt(params.id);
|
||||
const notifId = parseInt(params.notificationId);
|
||||
if (isNaN(envId) || isNaN(notifId)) {
|
||||
return json({ error: 'Invalid ID' }, { status: 400 });
|
||||
}
|
||||
|
||||
const env = await getEnvironment(envId);
|
||||
if (!env) {
|
||||
return json({ error: 'Environment not found' }, { status: 404 });
|
||||
}
|
||||
|
||||
try {
|
||||
const notification = await getEnvironmentNotification(envId, notifId);
|
||||
if (!notification) {
|
||||
return json({ error: 'Environment notification not found' }, { status: 404 });
|
||||
}
|
||||
return json(notification);
|
||||
} catch (error) {
|
||||
console.error('Error fetching environment notification:', error);
|
||||
return json({ error: 'Failed to fetch environment notification' }, { status: 500 });
|
||||
}
|
||||
};
|
||||
|
||||
// PUT /api/environments/[id]/notifications/[notificationId] - Update an environment notification
|
||||
export const PUT: RequestHandler = async ({ params, request, cookies }) => {
|
||||
const auth = await authorize(cookies);
|
||||
if (auth.authEnabled && !await auth.can('notifications', 'edit')) {
|
||||
return json({ error: 'Permission denied' }, { status: 403 });
|
||||
}
|
||||
|
||||
const envId = parseInt(params.id);
|
||||
const notifId = parseInt(params.notificationId);
|
||||
if (isNaN(envId) || isNaN(notifId)) {
|
||||
return json({ error: 'Invalid ID' }, { status: 400 });
|
||||
}
|
||||
|
||||
const env = await getEnvironment(envId);
|
||||
if (!env) {
|
||||
return json({ error: 'Environment not found' }, { status: 404 });
|
||||
}
|
||||
|
||||
try {
|
||||
const body = await request.json();
|
||||
const { enabled, eventTypes } = body;
|
||||
|
||||
const notification = await updateEnvironmentNotification(envId, notifId, {
|
||||
enabled,
|
||||
eventTypes: eventTypes as NotificationEventType[]
|
||||
});
|
||||
|
||||
if (!notification) {
|
||||
return json({ error: 'Environment notification not found' }, { status: 404 });
|
||||
}
|
||||
|
||||
return json(notification);
|
||||
} catch (error: any) {
|
||||
console.error('Error updating environment notification:', error);
|
||||
return json({ error: error.message || 'Failed to update environment notification' }, { status: 500 });
|
||||
}
|
||||
};
|
||||
|
||||
// DELETE /api/environments/[id]/notifications/[notificationId] - Remove a notification from an environment
|
||||
export const DELETE: RequestHandler = async ({ params, cookies }) => {
|
||||
const auth = await authorize(cookies);
|
||||
if (auth.authEnabled && !await auth.can('notifications', 'delete')) {
|
||||
return json({ error: 'Permission denied' }, { status: 403 });
|
||||
}
|
||||
|
||||
const envId = parseInt(params.id);
|
||||
const notifId = parseInt(params.notificationId);
|
||||
if (isNaN(envId) || isNaN(notifId)) {
|
||||
return json({ error: 'Invalid ID' }, { status: 400 });
|
||||
}
|
||||
|
||||
try {
|
||||
const deleted = await deleteEnvironmentNotification(envId, notifId);
|
||||
if (!deleted) {
|
||||
return json({ error: 'Environment notification not found' }, { status: 404 });
|
||||
}
|
||||
return json({ success: true });
|
||||
} catch (error: any) {
|
||||
console.error('Error deleting environment notification:', error);
|
||||
return json({ error: error.message || 'Failed to delete environment notification' }, { status: 500 });
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user