From d6ea0fa4b46e27581ce1f9fcfe6b21cd77e435e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Bombeck?= Date: Thu, 21 May 2026 19:20:51 +0200 Subject: [PATCH] refactor(integrations): replace nested ternaries with FailureKind copy table MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit W6 widened FailureKind from `transient | reauth_required` to `+ persistent`. Both formatter branches in `formatAdminAlertPayload` got the new arm wedged in as a nested ternary, which the style guide explicitly forbids ("prefer switch statements or if/else chains for multiple conditions"). Collapse both stacks to a single `FAILURE_KIND_COPY` table lookup keyed off the union. The table reads as the contract; adding a future fourth kind is a one-row edit instead of two more arms in two stacks. No behaviour change — the 28 integration-status tests still pin both the reason label and the action line verbatim. --- src/lib/integrations/status.ts | 40 +++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/src/lib/integrations/status.ts b/src/lib/integrations/status.ts index bcec36c66..5b3f848f4 100644 --- a/src/lib/integrations/status.ts +++ b/src/lib/integrations/status.ts @@ -502,6 +502,30 @@ export interface AlertInput { * 4096 characters but our envelope (title + summary + action line) * eats ~150 chars so we keep a comfortable margin. */ +/** + * Reason + action copy keyed off `FailureKind`. Adding a new failure + * kind is a one-row table edit instead of two more arms in two + * different ternary stacks (the style-guide forbids nested ternaries). + */ +const FAILURE_KIND_COPY: Record< + FailureKind, + { reason: string; action: string } +> = { + reauth_required: { + reason: "re-auth required", + action: "ask the user to reconnect the integration.", + }, + persistent: { + reason: "persistent error", + action: + "investigate the upstream contract — params/scope/action likely mismatched.", + }, + transient: { + reason: "transient error", + action: "investigate the upstream service.", + }, +}; + export function formatAdminAlertPayload(input: AlertInput): { title: string; message: string; @@ -510,12 +534,8 @@ export function formatAdminAlertPayload(input: AlertInput): { const integrationLabel = input.integration === "withings" ? "Withings" : "moodLog"; const subjectLabel = input.subjectLabel ?? input.userId; - const reasonLabel = - input.kind === "reauth_required" - ? "re-auth required" - : input.kind === "persistent" - ? "persistent error" - : "transient error"; + const { reason: reasonLabel, action: actionLabel } = + FAILURE_KIND_COPY[input.kind]; const codeLabel = input.errorCode ? ` (${input.errorCode})` : ""; const trimmed = input.message.length > 280 @@ -526,13 +546,7 @@ export function formatAdminAlertPayload(input: AlertInput): { const message = `${integrationLabel} sync has failed ${input.consecutiveFailures} times in a row for ${subjectLabel}.\n` + `Last error: ${reasonLabel}${codeLabel} — ${trimmed}\n` + - `Action: ${ - input.kind === "reauth_required" - ? "ask the user to reconnect the integration." - : input.kind === "persistent" - ? "investigate the upstream contract — params/scope/action likely mismatched." - : "investigate the upstream service." - }`; + `Action: ${actionLabel}`; return { title,