mirror of
https://github.com/khoaliber/dockhand.git
synced 2026-03-07 05:40:11 +00:00
Initial commit
This commit is contained in:
81
routes/api/notifications/+server.ts
Normal file
81
routes/api/notifications/+server.ts
Normal file
@@ -0,0 +1,81 @@
|
||||
import { json } from '@sveltejs/kit';
|
||||
import {
|
||||
getNotificationSettings,
|
||||
createNotificationSetting,
|
||||
type SmtpConfig,
|
||||
type AppriseConfig,
|
||||
type NotificationEventType
|
||||
} from '$lib/server/db';
|
||||
import { authorize } from '$lib/server/authorize';
|
||||
import type { RequestHandler } from './$types';
|
||||
|
||||
export const GET: RequestHandler = async ({ cookies }) => {
|
||||
const auth = await authorize(cookies);
|
||||
if (auth.authEnabled && !await auth.can('notifications', 'view')) {
|
||||
return json({ error: 'Permission denied' }, { status: 403 });
|
||||
}
|
||||
|
||||
try {
|
||||
const settings = await getNotificationSettings();
|
||||
// Don't expose passwords
|
||||
const safeSettings = settings.map(s => ({
|
||||
...s,
|
||||
config: s.type === 'smtp' ? {
|
||||
...s.config,
|
||||
password: s.config.password ? '********' : undefined
|
||||
} : s.config
|
||||
}));
|
||||
return json(safeSettings);
|
||||
} catch (error) {
|
||||
console.error('Error fetching notification settings:', error);
|
||||
return json({ error: 'Failed to fetch notification settings' }, { status: 500 });
|
||||
}
|
||||
};
|
||||
|
||||
export const POST: RequestHandler = async ({ request, cookies }) => {
|
||||
const auth = await authorize(cookies);
|
||||
if (auth.authEnabled && !await auth.can('notifications', 'create')) {
|
||||
return json({ error: 'Permission denied' }, { status: 403 });
|
||||
}
|
||||
|
||||
try {
|
||||
const body = await request.json();
|
||||
const { type, name, enabled, config, event_types, eventTypes } = body;
|
||||
// Support both snake_case (legacy) and camelCase (new) for event types
|
||||
const resolvedEventTypes = eventTypes || event_types;
|
||||
|
||||
if (!type || !name || !config) {
|
||||
return json({ error: 'Type, name, and config are required' }, { status: 400 });
|
||||
}
|
||||
|
||||
if (type !== 'smtp' && type !== 'apprise') {
|
||||
return json({ error: 'Type must be smtp or apprise' }, { status: 400 });
|
||||
}
|
||||
|
||||
// Validate config based on type
|
||||
if (type === 'smtp') {
|
||||
const smtpConfig = config as SmtpConfig;
|
||||
if (!smtpConfig.host || !smtpConfig.port || !smtpConfig.from_email || !smtpConfig.to_emails?.length) {
|
||||
return json({ error: 'SMTP config requires host, port, from_email, and to_emails' }, { status: 400 });
|
||||
}
|
||||
} else if (type === 'apprise') {
|
||||
const appriseConfig = config as AppriseConfig;
|
||||
if (!appriseConfig.urls?.length) {
|
||||
return json({ error: 'Apprise config requires at least one URL' }, { status: 400 });
|
||||
}
|
||||
}
|
||||
|
||||
const setting = await createNotificationSetting({
|
||||
type,
|
||||
name,
|
||||
enabled: enabled !== false,
|
||||
config,
|
||||
eventTypes: resolvedEventTypes as NotificationEventType[]
|
||||
});
|
||||
|
||||
return json(setting);
|
||||
} catch (error: any) {
|
||||
console.error('Error creating notification setting:', error);
|
||||
return json({ error: error.message || 'Failed to create notification setting' }, { status: 500 });
|
||||
}
|
||||
};
|
||||
135
routes/api/notifications/[id]/+server.ts
Normal file
135
routes/api/notifications/[id]/+server.ts
Normal file
@@ -0,0 +1,135 @@
|
||||
import { json } from '@sveltejs/kit';
|
||||
import {
|
||||
getNotificationSetting,
|
||||
updateNotificationSetting,
|
||||
deleteNotificationSetting,
|
||||
type SmtpConfig,
|
||||
type AppriseConfig,
|
||||
type NotificationEventType
|
||||
} from '$lib/server/db';
|
||||
import { authorize } from '$lib/server/authorize';
|
||||
import type { RequestHandler } from './$types';
|
||||
|
||||
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 });
|
||||
}
|
||||
|
||||
try {
|
||||
const id = parseInt(params.id);
|
||||
if (isNaN(id)) {
|
||||
return json({ error: 'Invalid ID' }, { status: 400 });
|
||||
}
|
||||
|
||||
const setting = await getNotificationSetting(id);
|
||||
if (!setting) {
|
||||
return json({ error: 'Notification setting not found' }, { status: 404 });
|
||||
}
|
||||
|
||||
// Don't expose passwords
|
||||
const safeSetting = {
|
||||
...setting,
|
||||
config: setting.type === 'smtp' ? {
|
||||
...setting.config,
|
||||
password: setting.config.password ? '********' : undefined
|
||||
} : setting.config
|
||||
};
|
||||
|
||||
return json(safeSetting);
|
||||
} catch (error) {
|
||||
console.error('Error fetching notification setting:', error);
|
||||
return json({ error: 'Failed to fetch notification setting' }, { status: 500 });
|
||||
}
|
||||
};
|
||||
|
||||
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 });
|
||||
}
|
||||
|
||||
try {
|
||||
const id = parseInt(params.id);
|
||||
if (isNaN(id)) {
|
||||
return json({ error: 'Invalid ID' }, { status: 400 });
|
||||
}
|
||||
|
||||
const existing = await getNotificationSetting(id);
|
||||
if (!existing) {
|
||||
return json({ error: 'Notification setting not found' }, { status: 404 });
|
||||
}
|
||||
|
||||
const body = await request.json();
|
||||
const { name, enabled, config, event_types, eventTypes } = body;
|
||||
// Support both snake_case (legacy) and camelCase (new) for event types
|
||||
const resolvedEventTypes = eventTypes || event_types;
|
||||
|
||||
// If updating config, validate based on type
|
||||
if (config) {
|
||||
if (existing.type === 'smtp') {
|
||||
const smtpConfig = config as SmtpConfig;
|
||||
if (!smtpConfig.host || !smtpConfig.port || !smtpConfig.from_email || !smtpConfig.to_emails?.length) {
|
||||
return json({ error: 'SMTP config requires host, port, from_email, and to_emails' }, { status: 400 });
|
||||
}
|
||||
// If password is masked, keep the existing one
|
||||
if (smtpConfig.password === '********') {
|
||||
smtpConfig.password = (existing.config as SmtpConfig).password;
|
||||
}
|
||||
} else if (existing.type === 'apprise') {
|
||||
const appriseConfig = config as AppriseConfig;
|
||||
if (!appriseConfig.urls?.length) {
|
||||
return json({ error: 'Apprise config requires at least one URL' }, { status: 400 });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const updated = await updateNotificationSetting(id, {
|
||||
name,
|
||||
enabled,
|
||||
config,
|
||||
eventTypes: resolvedEventTypes as NotificationEventType[]
|
||||
});
|
||||
if (!updated) {
|
||||
return json({ error: 'Failed to update notification setting' }, { status: 500 });
|
||||
}
|
||||
|
||||
// Don't expose passwords in response
|
||||
const safeSetting = {
|
||||
...updated,
|
||||
config: updated.type === 'smtp' ? {
|
||||
...updated.config,
|
||||
password: updated.config.password ? '********' : undefined
|
||||
} : updated.config
|
||||
};
|
||||
|
||||
return json(safeSetting);
|
||||
} catch (error: any) {
|
||||
console.error('Error updating notification setting:', error);
|
||||
return json({ error: error.message || 'Failed to update notification setting' }, { status: 500 });
|
||||
}
|
||||
};
|
||||
|
||||
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 });
|
||||
}
|
||||
|
||||
try {
|
||||
const id = parseInt(params.id);
|
||||
if (isNaN(id)) {
|
||||
return json({ error: 'Invalid ID' }, { status: 400 });
|
||||
}
|
||||
|
||||
const deleted = await deleteNotificationSetting(id);
|
||||
if (!deleted) {
|
||||
return json({ error: 'Notification setting not found' }, { status: 404 });
|
||||
}
|
||||
|
||||
return json({ success: true });
|
||||
} catch (error) {
|
||||
console.error('Error deleting notification setting:', error);
|
||||
return json({ error: 'Failed to delete notification setting' }, { status: 500 });
|
||||
}
|
||||
};
|
||||
31
routes/api/notifications/[id]/test/+server.ts
Normal file
31
routes/api/notifications/[id]/test/+server.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import { json } from '@sveltejs/kit';
|
||||
import { getNotificationSetting } from '$lib/server/db';
|
||||
import { testNotification } from '$lib/server/notifications';
|
||||
import type { RequestHandler } from './$types';
|
||||
|
||||
export const POST: RequestHandler = async ({ params }) => {
|
||||
try {
|
||||
const id = parseInt(params.id);
|
||||
if (isNaN(id)) {
|
||||
return json({ error: 'Invalid ID' }, { status: 400 });
|
||||
}
|
||||
|
||||
const setting = await getNotificationSetting(id);
|
||||
if (!setting) {
|
||||
return json({ error: 'Notification setting not found' }, { status: 404 });
|
||||
}
|
||||
|
||||
const success = await testNotification(setting);
|
||||
|
||||
return json({
|
||||
success,
|
||||
message: success ? 'Test notification sent successfully' : 'Failed to send test notification'
|
||||
});
|
||||
} catch (error: any) {
|
||||
console.error('Error testing notification:', error);
|
||||
return json({
|
||||
success: false,
|
||||
error: error.message || 'Failed to test notification'
|
||||
}, { status: 500 });
|
||||
}
|
||||
};
|
||||
61
routes/api/notifications/test/+server.ts
Normal file
61
routes/api/notifications/test/+server.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
import { json } from '@sveltejs/kit';
|
||||
import { testNotification } from '$lib/server/notifications';
|
||||
import { authorize } from '$lib/server/authorize';
|
||||
import type { RequestHandler } from './$types';
|
||||
|
||||
// Test notification with provided config (without saving)
|
||||
export const POST: RequestHandler = async ({ request, cookies }) => {
|
||||
const auth = await authorize(cookies);
|
||||
if (auth.authEnabled && !await auth.can('settings', 'edit')) {
|
||||
return json({ error: 'Permission denied' }, { status: 403 });
|
||||
}
|
||||
|
||||
try {
|
||||
const data = await request.json();
|
||||
|
||||
if (!data.type || !data.config) {
|
||||
return json({ error: 'Type and config are required' }, { status: 400 });
|
||||
}
|
||||
|
||||
// Validate SMTP config
|
||||
if (data.type === 'smtp') {
|
||||
const config = data.config;
|
||||
if (!config.host || !config.from_email || !config.to_emails?.length) {
|
||||
return json({ error: 'Host, from email, and at least one recipient are required' }, { status: 400 });
|
||||
}
|
||||
}
|
||||
|
||||
// Validate Apprise config
|
||||
if (data.type === 'apprise') {
|
||||
const config = data.config;
|
||||
if (!config.urls?.length) {
|
||||
return json({ error: 'At least one Apprise URL is required' }, { status: 400 });
|
||||
}
|
||||
}
|
||||
|
||||
// Create a fake notification setting object for testing
|
||||
const setting = {
|
||||
id: 0,
|
||||
name: data.name || 'Test',
|
||||
type: data.type as 'smtp' | 'apprise',
|
||||
enabled: true,
|
||||
config: data.config,
|
||||
event_types: [],
|
||||
created_at: new Date().toISOString(),
|
||||
updated_at: new Date().toISOString()
|
||||
};
|
||||
|
||||
const success = await testNotification(setting);
|
||||
|
||||
return json({
|
||||
success,
|
||||
message: success ? 'Test notification sent successfully' : 'Failed to send test notification'
|
||||
});
|
||||
} catch (error: any) {
|
||||
console.error('Error testing notification:', error);
|
||||
return json({
|
||||
success: false,
|
||||
error: error.message || 'Failed to test notification'
|
||||
}, { status: 500 });
|
||||
}
|
||||
};
|
||||
130
routes/api/notifications/trigger-test/+server.ts
Normal file
130
routes/api/notifications/trigger-test/+server.ts
Normal file
@@ -0,0 +1,130 @@
|
||||
import { json, type RequestHandler } from '@sveltejs/kit';
|
||||
import { sendEventNotification, sendEnvironmentNotification } from '$lib/server/notifications';
|
||||
import { NOTIFICATION_EVENT_TYPES, type NotificationEventType } from '$lib/server/db';
|
||||
|
||||
/**
|
||||
* Test endpoint to trigger notifications for any event type.
|
||||
* This is intended for development/testing purposes only.
|
||||
*
|
||||
* POST /api/notifications/trigger-test
|
||||
* Body: {
|
||||
* eventType: string,
|
||||
* environmentId?: number,
|
||||
* payload: { title: string, message: string, type?: string }
|
||||
* }
|
||||
*/
|
||||
export const POST: RequestHandler = async ({ request }) => {
|
||||
try {
|
||||
const body = await request.json();
|
||||
const { eventType, environmentId, payload } = body;
|
||||
|
||||
if (!eventType) {
|
||||
return json({ error: 'eventType is required' }, { status: 400 });
|
||||
}
|
||||
|
||||
if (!payload || !payload.title || !payload.message) {
|
||||
return json({ error: 'payload with title and message is required' }, { status: 400 });
|
||||
}
|
||||
|
||||
// Validate event type - NOTIFICATION_EVENT_TYPES is array of {id, label, ...}
|
||||
const validEventIds = NOTIFICATION_EVENT_TYPES.map(e => e.id);
|
||||
if (!validEventIds.includes(eventType)) {
|
||||
return json({
|
||||
error: `Invalid event type: ${eventType}`,
|
||||
validTypes: validEventIds
|
||||
}, { status: 400 });
|
||||
}
|
||||
|
||||
// Determine if this is a system event or environment event
|
||||
const isSystemEvent = eventType === 'license_expiring';
|
||||
|
||||
let result;
|
||||
|
||||
if (isSystemEvent) {
|
||||
// System events don't have an environment
|
||||
result = await sendEventNotification(
|
||||
eventType as NotificationEventType,
|
||||
{
|
||||
title: payload.title,
|
||||
message: payload.message,
|
||||
type: payload.type || 'info'
|
||||
}
|
||||
);
|
||||
} else if (environmentId) {
|
||||
// Environment-scoped events
|
||||
result = await sendEventNotification(
|
||||
eventType as NotificationEventType,
|
||||
{
|
||||
title: payload.title,
|
||||
message: payload.message,
|
||||
type: payload.type || 'info'
|
||||
},
|
||||
environmentId
|
||||
);
|
||||
} else {
|
||||
return json({
|
||||
error: 'environmentId is required for non-system events'
|
||||
}, { status: 400 });
|
||||
}
|
||||
|
||||
return json({
|
||||
success: result.success,
|
||||
sent: result.sent,
|
||||
eventType,
|
||||
environmentId: isSystemEvent ? null : environmentId
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('[Notification Test] Error:', error);
|
||||
return json({
|
||||
error: error instanceof Error ? error.message : 'Unknown error'
|
||||
}, { status: 500 });
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* GET endpoint to list all available event types
|
||||
*/
|
||||
export const GET: RequestHandler = async () => {
|
||||
return json({
|
||||
eventTypes: NOTIFICATION_EVENT_TYPES,
|
||||
categories: {
|
||||
container: [
|
||||
'container_started',
|
||||
'container_stopped',
|
||||
'container_restarted',
|
||||
'container_exited',
|
||||
'container_unhealthy',
|
||||
'container_oom',
|
||||
'container_updated',
|
||||
'image_pulled',
|
||||
],
|
||||
autoUpdate: [
|
||||
'auto_update_success',
|
||||
'auto_update_failed',
|
||||
'auto_update_blocked',
|
||||
],
|
||||
gitStack: [
|
||||
'git_sync_success',
|
||||
'git_sync_failed',
|
||||
'git_sync_skipped',
|
||||
],
|
||||
stack: [
|
||||
'stack_started',
|
||||
'stack_stopped',
|
||||
'stack_deployed',
|
||||
'stack_deploy_failed',
|
||||
],
|
||||
security: [
|
||||
'vulnerability_critical',
|
||||
'vulnerability_high',
|
||||
'vulnerability_any',
|
||||
],
|
||||
system: [
|
||||
'environment_offline',
|
||||
'environment_online',
|
||||
'disk_space_warning',
|
||||
'license_expiring',
|
||||
],
|
||||
}
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user