Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

Extract alert destinations config model owner

rcourtmana5660cc9452c
diff --git a/docs/release-control/v6/internal/subsystems/alerts.md b/docs/release-control/v6/internal/subsystems/alerts.md
index 00a92efb6f..30b72275a1 100644
--- a/docs/release-control/v6/internal/subsystems/alerts.md
+++ b/docs/release-control/v6/internal/subsystems/alerts.md
@@ -332,8 +332,10 @@ serialization,
normalization plus resource-backed override projection, and
`frontend-modern/src/features/alerts/useAlertOverridesState.ts` for reactive
override state, derived resource lists, and overview handoff, and
+`frontend-modern/src/features/alerts/alertDestinationsModel.ts` for email and
+Apprise config normalization plus outbound payload shaping, and
`frontend-modern/src/features/alerts/useAlertDestinationsState.ts` for
-notification destination reload and persistence.
+notification destination reload and persistence orchestration.
`frontend-modern/src/features/alerts/useAlertWebhookDestinationsState.ts` now
owns webhook load/mutate/test flow,
`frontend-modern/src/features/alerts/useAlertDestinationsTabState.ts` now owns
diff --git a/docs/release-control/v6/internal/subsystems/frontend-primitives.md b/docs/release-control/v6/internal/subsystems/frontend-primitives.md
index 86bdabaa42..0c0febc20c 100644
--- a/docs/release-control/v6/internal/subsystems/frontend-primitives.md
+++ b/docs/release-control/v6/internal/subsystems/frontend-primitives.md
@@ -502,8 +502,10 @@ serialization, `frontend-modern/src/features/alerts/alertOverridesModel.ts`
for override normalization and resource-backed projection, and
`frontend-modern/src/features/alerts/useAlertOverridesState.ts`
for reactive override state and thresholds-facing resource selectors, and
+`frontend-modern/src/features/alerts/alertDestinationsModel.ts` for
+destination config normalization and payload shaping, and
`frontend-modern/src/features/alerts/useAlertDestinationsState.ts` for
-notification destination reload and persistence.
+notification destination reload and persistence orchestration.
`frontend-modern/src/features/alerts/useAlertWebhookDestinationsState.ts` now
owns webhook runtime, and
`frontend-modern/src/components/Alerts/ResourceTable.tsx` now follows the same
diff --git a/frontend-modern/src/features/alerts/__tests__/alertDestinationsModel.test.ts b/frontend-modern/src/features/alerts/__tests__/alertDestinationsModel.test.ts
new file mode 100644
index 0000000000..bf5f4639f3
--- /dev/null
+++ b/frontend-modern/src/features/alerts/__tests__/alertDestinationsModel.test.ts
@@ -0,0 +1,72 @@
+import { describe, expect, it } from 'vitest';
+
+import {
+ buildAppriseConfigPayload,
+ buildEmailConfigPayload,
+ normalizeAppriseConfig,
+} from '../alertDestinationsModel';
+
+describe('alertDestinationsModel', () => {
+ it('normalizes apprise config into the UI model', () => {
+ expect(
+ normalizeAppriseConfig({
+ enabled: true,
+ mode: 'http',
+ targets: ['https://notify.example.test'],
+ serverUrl: 'https://apprise.example.test',
+ timeoutSeconds: 30,
+ }),
+ ).toEqual(
+ expect.objectContaining({
+ enabled: true,
+ mode: 'http',
+ targetsText: 'https://notify.example.test',
+ serverUrl: 'https://apprise.example.test',
+ timeoutSeconds: 30,
+ }),
+ );
+ });
+
+ it('builds outbound email and apprise payloads from UI state', () => {
+ expect(
+ buildEmailConfigPayload({
+ enabled: true,
+ provider: 'smtp',
+ server: 'smtp.internal',
+ port: 587,
+ username: '[email protected]',
+ password: '',
+ tls: true,
+ startTLS: true,
+ }),
+ ).toEqual(
+ expect.objectContaining({
+ server: 'smtp.internal',
+ }),
+ );
+
+ expect(
+ buildAppriseConfigPayload({
+ enabled: true,
+ mode: 'http',
+ targetsText: 'https://notify.internal',
+ cliPath: 'apprise',
+ timeoutSeconds: 15,
+ serverUrl: 'https://apprise.internal',
+ configKey: '',
+ apiKey: '',
+ apiKeyHeader: 'X-API-KEY',
+ skipTlsVerify: false,
+ }),
+ ).toEqual(
+ expect.objectContaining({
+ mode: 'http',
+ targets: ['https://notify.internal'],
+ serverUrl: 'https://apprise.internal',
+ }),
+ );
+ });
+});
diff --git a/frontend-modern/src/features/alerts/alertDestinationsModel.ts b/frontend-modern/src/features/alerts/alertDestinationsModel.ts
new file mode 100644
index 0000000000..4fed03249d
--- /dev/null
+++ b/frontend-modern/src/features/alerts/alertDestinationsModel.ts
@@ -0,0 +1,62 @@
+import type { AppriseConfig, EmailConfig } from '@/api/notifications';
+
+import {
+ formatAppriseTargets,
+ normalizeEmailConfigFromAPI,
+ parseAppriseTargets,
+} from './helpers';
+import type { UIAppriseConfig, UIEmailConfig } from './types';
+
+export function normalizeAppriseConfig(
+ config: Partial<AppriseConfig> | null | undefined,
+): UIAppriseConfig {
+ return {
+ enabled: config?.enabled ?? false,
+ mode: config?.mode === 'http' ? 'http' : 'cli',
+ targetsText: formatAppriseTargets(config?.targets),
+ cliPath: config?.cliPath || 'apprise',
+ timeoutSeconds:
+ typeof config?.timeoutSeconds === 'number' && config.timeoutSeconds > 0
+ ? config.timeoutSeconds
+ : 15,
+ serverUrl: config?.serverUrl || '',
+ configKey: config?.configKey || '',
+ apiKey: config?.apiKey || '',
+ apiKeyHeader: config?.apiKeyHeader || 'X-API-KEY',
+ skipTlsVerify: Boolean(config?.skipTlsVerify),
+ };
+}
+
+export function buildEmailConfigPayload(config: UIEmailConfig): EmailConfig {
+ return {
+ enabled: config.enabled,
+ provider: config.provider,
+ server: config.server,
+ port: config.port,
+ username: config.username,
+ password: config.password,
+ from: config.from,
+ to: config.to,
+ tls: config.tls,
+ startTLS: config.startTLS,
+ } as EmailConfig;
+}
+
+export function buildAppriseConfigPayload(config: UIAppriseConfig): AppriseConfig {
+ return {
+ enabled: config.enabled,
+ mode: config.mode,
+ targets: parseAppriseTargets(config.targetsText),
+ cliPath: config.cliPath,
+ timeoutSeconds: config.timeoutSeconds,
+ serverUrl: config.serverUrl,
+ configKey: config.configKey,
+ apiKey: config.apiKey,
+ apiKeyHeader: config.apiKeyHeader,
+ skipTlsVerify: config.skipTlsVerify,
+ } as AppriseConfig;
+}
+
+export {
+ normalizeEmailConfigFromAPI,
+};
diff --git a/frontend-modern/src/features/alerts/useAlertDestinationsState.ts b/frontend-modern/src/features/alerts/useAlertDestinationsState.ts
index d07c708144..f11dc23186 100644
--- a/frontend-modern/src/features/alerts/useAlertDestinationsState.ts
+++ b/frontend-modern/src/features/alerts/useAlertDestinationsState.ts
@@ -1,17 +1,19 @@
import { createEffect, createSignal } from 'solid-js';
import type { Accessor } from 'solid-js';
-import type { AppriseConfig, EmailConfig } from '@/api/notifications';
import { NotificationsAPI } from '@/api/notifications';
import { getAlertDestinationsConfigLoadError } from '@/utils/alertDestinationsPresentation';
import { logger } from '@/utils/logger';
+import {
+ buildAppriseConfigPayload,
+ buildEmailConfigPayload,
+ normalizeAppriseConfig,
+ normalizeEmailConfigFromAPI,
+} from './alertDestinationsModel';
import {
createDefaultAppriseConfig,
createDefaultEmailConfig,
- formatAppriseTargets,
- normalizeEmailConfigFromAPI,
- parseAppriseTargets,
} from './helpers';
import type { AlertTab, UIAppriseConfig, UIEmailConfig } from './types';
@@ -19,24 +21,6 @@ interface AlertDestinationsStateOptions {
activeTab: Accessor<AlertTab>;
}
-function normalizeAppriseConfig(config: Partial<AppriseConfig> | null | undefined): UIAppriseConfig {
- return {
- enabled: config?.enabled ?? false,
- mode: config?.mode === 'http' ? 'http' : 'cli',
- targetsText: formatAppriseTargets(config?.targets),
- cliPath: config?.cliPath || 'apprise',
- timeoutSeconds:
- typeof config?.timeoutSeconds === 'number' && config.timeoutSeconds > 0
- ? config.timeoutSeconds
- : 15,
- serverUrl: config?.serverUrl || '',
- configKey: config?.configKey || '',
- apiKey: config?.apiKey || '',
- apiKeyHeader: config?.apiKeyHeader || 'X-API-KEY',
- skipTlsVerify: Boolean(config?.skipTlsVerify),
- };
-}
-
export function useAlertDestinationsState(options: AlertDestinationsStateOptions) {
const [isLoadingDestinations, setIsLoadingDestinations] = createSignal(false);
const [destConfigLoadError, setDestConfigLoadError] = createSignal<string | null>(null);
@@ -100,33 +84,11 @@ export function useAlertDestinationsState(options: AlertDestinationsStateOptions
};
const saveDestinations = async () => {
- const currentEmailConfig = emailConfig();
- await NotificationsAPI.updateEmailConfig({
- enabled: currentEmailConfig.enabled,
- provider: currentEmailConfig.provider,
- server: currentEmailConfig.server,
- port: currentEmailConfig.port,
- username: currentEmailConfig.username,
- password: currentEmailConfig.password,
- from: currentEmailConfig.from,
- to: currentEmailConfig.to,
- tls: currentEmailConfig.tls,
- startTLS: currentEmailConfig.startTLS,
- } as EmailConfig);
-
- const currentAppriseConfig = appriseConfig();
- const updatedApprise = await NotificationsAPI.updateAppriseConfig({
- enabled: currentAppriseConfig.enabled,
- mode: currentAppriseConfig.mode,
- targets: parseAppriseTargets(currentAppriseConfig.targetsText),
- cliPath: currentAppriseConfig.cliPath,
- timeoutSeconds: currentAppriseConfig.timeoutSeconds,
- serverUrl: currentAppriseConfig.serverUrl,
- configKey: currentAppriseConfig.configKey,
- apiKey: currentAppriseConfig.apiKey,
- apiKeyHeader: currentAppriseConfig.apiKeyHeader,
- skipTlsVerify: currentAppriseConfig.skipTlsVerify,
- } as AppriseConfig);
+ await NotificationsAPI.updateEmailConfig(buildEmailConfigPayload(emailConfig()));
+
+ const updatedApprise = await NotificationsAPI.updateAppriseConfig(
+ buildAppriseConfigPayload(appriseConfig()),
+ );
setAppriseConfig(normalizeAppriseConfig(updatedApprise));
};
diff --git a/frontend-modern/src/pages/__tests__/Alerts.helpers.test.ts b/frontend-modern/src/pages/__tests__/Alerts.helpers.test.ts
index 3a338cae66..140775f610 100644
--- a/frontend-modern/src/pages/__tests__/Alerts.helpers.test.ts
+++ b/frontend-modern/src/pages/__tests__/Alerts.helpers.test.ts
@@ -6,6 +6,7 @@ import alertsConfigurationSnapshotStateSource from '@/features/alerts/useAlertsC
import alertsConfigurationModelSource from '@/features/alerts/alertsConfigurationModel.ts?raw';
import alertOverridesModelSource from '@/features/alerts/alertOverridesModel.ts?raw';
import alertOverridesStateSource from '@/features/alerts/useAlertOverridesState.ts?raw';
+import alertDestinationsModelSource from '@/features/alerts/alertDestinationsModel.ts?raw';
import alertDestinationsStateSource from '@/features/alerts/useAlertDestinationsState.ts?raw';
import alertDestinationsTabStateSource from '@/features/alerts/useAlertDestinationsTabState.ts?raw';
import alertWebhookDestinationsStateSource from '@/features/alerts/useAlertWebhookDestinationsState.ts?raw';
@@ -292,6 +293,16 @@ describe('tab path helpers', () => {
expect(alertDestinationsStateSource).toContain('export function useAlertDestinationsState');
expect(alertDestinationsStateSource).toContain('NotificationsAPI.getEmailConfig');
expect(alertDestinationsStateSource).toContain('NotificationsAPI.updateEmailConfig');
+ expect(alertDestinationsStateSource).toContain('buildEmailConfigPayload');
+ expect(alertDestinationsStateSource).toContain('buildAppriseConfigPayload');
+ expect(alertDestinationsStateSource).toContain('normalizeAppriseConfig');
+ expect(alertDestinationsStateSource).not.toContain('formatAppriseTargets');
+ expect(alertDestinationsStateSource).not.toContain('parseAppriseTargets');
+ expect(alertDestinationsModelSource).toContain('export function normalizeAppriseConfig');
+ expect(alertDestinationsModelSource).toContain('export function buildEmailConfigPayload');
+ expect(alertDestinationsModelSource).toContain('export function buildAppriseConfigPayload');
+ expect(alertDestinationsModelSource).toContain('formatAppriseTargets');
+ expect(alertDestinationsModelSource).toContain('parseAppriseTargets');
expect(alertDestinationsTabStateSource).toContain('export function useAlertDestinationsTabState');
expect(alertDestinationsTabStateSource).toContain('NotificationsAPI.testNotification');
expect(alertDestinationsTabStateSource).toContain('useAlertWebhookDestinationsState');
diff --git a/frontend-modern/src/utils/__tests__/frontendResourceTypeBoundaries.test.ts b/frontend-modern/src/utils/__tests__/frontendResourceTypeBoundaries.test.ts
index f4e71004b5..8e4cb05df1 100644
--- a/frontend-modern/src/utils/__tests__/frontendResourceTypeBoundaries.test.ts
+++ b/frontend-modern/src/utils/__tests__/frontendResourceTypeBoundaries.test.ts
@@ -330,6 +330,7 @@ import alertsConfigurationSnapshotStateSource from '@/features/alerts/useAlertsC
import alertsConfigurationModelSource from '@/features/alerts/alertsConfigurationModel.ts?raw';
import alertOverridesModelSource from '@/features/alerts/alertOverridesModel.ts?raw';
import alertOverridesStateSource from '@/features/alerts/useAlertOverridesState.ts?raw';
+import alertDestinationsModelSource from '@/features/alerts/alertDestinationsModel.ts?raw';
import alertDestinationsStateSource from '@/features/alerts/useAlertDestinationsState.ts?raw';
import alertDestinationsTabStateSource from '@/features/alerts/useAlertDestinationsTabState.ts?raw';
import alertWebhookDestinationsStateSource from '@/features/alerts/useAlertWebhookDestinationsState.ts?raw';
@@ -2876,6 +2877,16 @@ describe('frontend resource type boundaries', () => {
expect(alertDestinationsStateSource).toContain('getAlertDestinationsConfigLoadError');
expect(alertDestinationsStateSource).toContain('NotificationsAPI.getEmailConfig');
expect(alertDestinationsStateSource).toContain('NotificationsAPI.updateEmailConfig');
+ expect(alertDestinationsStateSource).toContain('buildEmailConfigPayload');
+ expect(alertDestinationsStateSource).toContain('buildAppriseConfigPayload');
+ expect(alertDestinationsStateSource).toContain('normalizeAppriseConfig');
+ expect(alertDestinationsStateSource).not.toContain('formatAppriseTargets');
+ expect(alertDestinationsStateSource).not.toContain('parseAppriseTargets');
+ expect(alertDestinationsModelSource).toContain('export function normalizeAppriseConfig');
+ expect(alertDestinationsModelSource).toContain('export function buildEmailConfigPayload');
+ expect(alertDestinationsModelSource).toContain('export function buildAppriseConfigPayload');
+ expect(alertDestinationsModelSource).toContain('formatAppriseTargets');
+ expect(alertDestinationsModelSource).toContain('parseAppriseTargets');
expect(alertDestinationsTabStateSource).toContain('export function useAlertDestinationsTabState');
expect(alertDestinationsTabStateSource).toContain('NotificationsAPI.testNotification');
expect(alertDestinationsTabStateSource).toContain('useAlertWebhookDestinationsState');