feat: webhook payload history — store and display recent incoming payloads

Backend:
- WebhookPayloadLog model (provider_id, method, headers, body, status, extracted_fields, error_message)
- Auto-log payloads in generic_webhook() with matched/unmatched/error status
- Auto-prune beyond max_stored_payloads per provider
- Header filtering (only Content-Type, User-Agent, X-* stored; no Authorization)
- GET/DELETE /api/providers/{id}/webhook-logs endpoints
- store_payloads + max_stored_payloads in WebhookProviderConfig

Frontend:
- WebhookPayloadHistory.svelte — expandable log viewer with status badges, JSON body, headers, extracted fields
- payloadHistory flag on webhook provider descriptor
- max_stored_payloads config field (0 = disabled)
- Password confirmation field on change password modal
- i18n keys for webhook logs (en + ru)
This commit is contained in:
2026-03-28 13:54:54 +03:00
parent c41182ffd0
commit 6113a0039c
13 changed files with 459 additions and 5 deletions
@@ -0,0 +1,165 @@
<script lang="ts">
import { t } from '$lib/i18n';
import { api } from '$lib/api';
import MdiIcon from '$lib/components/MdiIcon.svelte';
import Card from '$lib/components/Card.svelte';
import ConfirmModal from '$lib/components/ConfirmModal.svelte';
import type { WebhookPayloadLog } from '$lib/types';
interface Props {
providerId: number;
}
let { providerId }: Props = $props();
let logs = $state<WebhookPayloadLog[]>([]);
let loading = $state(true);
let expandedId = $state<number | null>(null);
let showClearConfirm = $state(false);
async function loadLogs() {
loading = true;
try {
logs = await api(`/providers/${providerId}/webhook-logs`);
} catch {
logs = [];
}
loading = false;
}
async function clearLogs() {
try {
await api(`/providers/${providerId}/webhook-logs`, { method: 'DELETE' });
logs = [];
} catch { /* ignore */ }
showClearConfirm = false;
}
function toggleExpand(id: number) {
expandedId = expandedId === id ? null : id;
}
function statusColor(status: string): string {
if (status === 'matched') return '#059669';
if (status === 'unmatched') return '#f59e0b';
return '#ef4444';
}
function statusIcon(status: string): string {
if (status === 'matched') return 'mdiCheckCircle';
if (status === 'unmatched') return 'mdiMinusCircle';
return 'mdiAlertCircle';
}
function statusLabel(status: string): string {
if (status === 'matched') return t('webhookLogs.statusMatched');
if (status === 'unmatched') return t('webhookLogs.statusUnmatched');
return t('webhookLogs.statusError');
}
function formatTime(iso: string): string {
return new Date(iso).toLocaleString();
}
$effect(() => {
void providerId;
loadLogs();
});
</script>
<Card>
<div class="flex items-center justify-between mb-3">
<div class="flex items-center gap-2">
<h3 class="text-sm font-semibold">{t('webhookLogs.title')}</h3>
{#if logs.length > 0}
<span class="text-xs px-1.5 py-0.5 rounded-full bg-[var(--color-secondary)] text-[var(--color-secondary-foreground)]">{logs.length}</span>
{/if}
</div>
{#if logs.length > 0}
<button
onclick={() => showClearConfirm = true}
class="text-xs px-2 py-1 rounded-md transition-colors hover:bg-[var(--color-error-bg)] text-[var(--color-error-fg)]"
>
{t('webhookLogs.clear')}
</button>
{/if}
</div>
{#if loading}
<div class="text-sm text-[var(--color-muted-foreground)] py-4 text-center">{t('common.loading')}</div>
{:else if logs.length === 0}
<div class="text-sm text-[var(--color-muted-foreground)] py-4 text-center">{t('webhookLogs.empty')}</div>
{:else}
<div class="space-y-1">
{#each logs as log}
<button
onclick={() => toggleExpand(log.id)}
class="w-full text-left px-3 py-2 rounded-md text-sm transition-colors hover:bg-[var(--color-sidebar-active)]"
style="background: {expandedId === log.id ? 'var(--color-sidebar-active)' : 'transparent'};"
>
<div class="flex items-center gap-2">
<MdiIcon name={expandedId === log.id ? 'mdiChevronDown' : 'mdiChevronRight'} size={14} />
<span class="text-xs text-[var(--color-muted-foreground)] tabular-nums">{formatTime(log.created_at)}</span>
<span class="text-xs font-mono px-1 py-0.5 rounded bg-[var(--color-secondary)]">{log.method}</span>
<span class="flex items-center gap-1" style="color: {statusColor(log.status)};">
<MdiIcon name={statusIcon(log.status)} size={14} />
<span class="text-xs font-medium">{statusLabel(log.status)}</span>
</span>
{#if log.error_message && log.status === 'error'}
<span class="text-xs text-[var(--color-muted-foreground)] truncate max-w-[200px]">{log.error_message}</span>
{/if}
</div>
</button>
{#if expandedId === log.id}
<div class="ml-6 mr-2 mb-2 space-y-2">
<!-- Headers -->
{#if Object.keys(log.headers).length > 0}
<div>
<div class="text-xs font-medium text-[var(--color-muted-foreground)] mb-1">{t('webhookLogs.headers')}</div>
<div class="bg-[var(--color-background)] border border-[var(--color-border)] rounded-md p-2 text-xs font-mono">
{#each Object.entries(log.headers) as [key, value]}
<div><span class="text-[var(--color-primary)]">{key}</span>: {value}</div>
{/each}
</div>
</div>
{/if}
<!-- Body -->
<div>
<div class="text-xs font-medium text-[var(--color-muted-foreground)] mb-1">{t('webhookLogs.body')}</div>
<pre class="bg-[var(--color-background)] border border-[var(--color-border)] rounded-md p-2 text-xs font-mono overflow-x-auto whitespace-pre-wrap break-all" style="max-height: 300px; overflow-y: auto;">{JSON.stringify(log.body, null, 2)}</pre>
</div>
<!-- Extracted fields -->
{#if log.status === 'matched' && Object.keys(log.extracted_fields).length > 0}
<div>
<div class="text-xs font-medium text-[var(--color-muted-foreground)] mb-1">{t('webhookLogs.extractedFields')}</div>
<div class="bg-[var(--color-background)] border border-[var(--color-border)] rounded-md p-2 text-xs font-mono">
{#each Object.entries(log.extracted_fields) as [key, value]}
<div><span class="text-[var(--color-primary)]">{key}</span>: {typeof value === 'object' ? JSON.stringify(value) : String(value)}</div>
{/each}
</div>
</div>
{/if}
<!-- Error message -->
{#if log.status === 'error' && log.error_message}
<div>
<div class="text-xs font-medium text-[var(--color-error-fg)] mb-1">{t('webhookLogs.errorMessage')}</div>
<div class="bg-[var(--color-error-bg)] text-[var(--color-error-fg)] rounded-md p-2 text-xs">{log.error_message}</div>
</div>
{/if}
</div>
{/if}
{/each}
</div>
{/if}
</Card>
<ConfirmModal
open={showClearConfirm}
title={t('webhookLogs.clear')}
message={t('webhookLogs.confirmClear')}
onconfirm={clearLogs}
oncancel={() => showClearConfirm = false}
/>