From ea88e8833dc0f1bc9847fd58d27c6162879196ad Mon Sep 17 00:00:00 2001 From: 1sonhador <1sonhador@gmail.com> Date: Wed, 1 Jul 2026 09:44:19 +0100 Subject: [PATCH] feat: retry all failed messages from emails page Add "Repetir todas as falhadas" with batched API calls and progress feedback; raise retry-batch limit to 100 and support all=true on API. --- admin-ui/src/lib/api.ts | 5 +- admin-ui/src/pages/Emails.tsx | 77 +++++++++++++++++++++++++--- src/forwarder/api/routes/messages.py | 51 ++++++++++-------- 3 files changed, 103 insertions(+), 30 deletions(-) diff --git a/admin-ui/src/lib/api.ts b/admin-ui/src/lib/api.ts index bda763e..f14d1e1 100644 --- a/admin-ui/src/lib/api.ts +++ b/admin-ui/src/lib/api.ts @@ -150,9 +150,10 @@ export const api = { return request(`/messages${q.toString() ? '?' + q : ''}`) }, retry: (id: number) => request(`/messages/${id}/retry`, { method: 'POST' }), - retryBatch: (opts?: { limit?: number; account_id?: number }) => { + retryBatch: (opts?: { limit?: number; account_id?: number; all?: boolean }) => { const q = new URLSearchParams() - if (opts?.limit != null) q.set('limit', String(opts.limit)) + if (opts?.all) q.set('all', 'true') + else if (opts?.limit != null) q.set('limit', String(opts.limit)) if (opts?.account_id != null) q.set('account_id', String(opts.account_id)) return request(`/messages/retry-batch${q.toString() ? '?' + q : ''}`, { method: 'POST', diff --git a/admin-ui/src/pages/Emails.tsx b/admin-ui/src/pages/Emails.tsx index 29c79aa..edc29be 100644 --- a/admin-ui/src/pages/Emails.tsx +++ b/admin-ui/src/pages/Emails.tsx @@ -74,6 +74,57 @@ export default function Emails() { } } + const handleRetryAll = async () => { + let failedCount = filterStatus === 'failed' ? total : 0 + if (failedCount === 0) { + try { + const stats = await api.stats.get() + failedCount = stats.messages_failed + } catch { + failedCount = 0 + } + } + if (failedCount === 0) { + setActionMsg({ ok: true, text: 'Não há mensagens falhadas para repetir.' }) + return + } + const ok = confirm( + `Repetir todas as ${failedCount} mensagens falhadas?\n\nIsto pode demorar vários minutos. Mantém a conta inactiva durante o processo.`, + ) + if (!ok) return + + setBatchRetrying(true) + setActionMsg({ ok: false, text: `A repetir ${failedCount} mensagens…` }) + let totalSucceeded = 0 + let totalFailed = 0 + let totalAttempted = 0 + try { + while (true) { + const r = await api.messages.retryBatch({ limit: 50 }) + totalSucceeded += r.succeeded + totalFailed += r.failed + totalAttempted += r.attempted + setActionMsg({ + ok: false, + text: `A repetir… ${totalSucceeded} OK, ${totalFailed} falhas (${totalAttempted} processadas)`, + }) + if (r.attempted === 0) break + } + setActionMsg({ + ok: totalFailed === 0, + text: `Concluído: ${totalSucceeded} reencaminhadas, ${totalFailed} falharam (${totalAttempted} processadas).`, + }) + reload() + } catch (e) { + setActionMsg({ + ok: false, + text: e instanceof Error ? e.message : 'Retry de todas falhou', + }) + } finally { + setBatchRetrying(false) + } + } + return (
@@ -83,14 +134,24 @@ export default function Emails() {
{(filterStatus === 'failed' || filterStatus === '') && ( - + <> + + + )}