Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions supabase/migrations/20260630210000_create_email_send_log.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
-- Durable log of every POST /api/emails attempt (recoupable/chat#1829).
--
-- When an empty/footer-only "Message from Recoup" email reached customers, we
-- could not recover what the API received: malformed bodies were swallowed, the
-- route logged nothing, and the agent sandbox that built the request is
-- ephemeral. This records every attempt — sent, send_failed, and rejected
-- (empty/malformed) — with a copy of the body,
-- so a send is debuggable several days back. Written by api `logEmailAttempt`.
--
-- Append-only; loose ids, no FKs (like error_logs — a log shouldn't
-- cascade-delete with accounts, and rejected rows have no ids). Keyed by chat_id
-- only: chats.session_id reaches the session and sessions.account_id the account,
-- so session_id would be redundant (and isn't in the /api/emails contract).
-- account_id is denormalized (free at the API, the main query dimension).
-- Single responsibility = "what the API was asked to send"; Resend delivery
-- lifecycle is a future, separate concern keyed off resend_id.

CREATE TABLE IF NOT EXISTS public.email_send_log (

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1: Enable RLS on email_send_log before merge. Otherwise a public Supabase table containing captured email request bodies may be exposed or mutable through client roles depending on grants/policies.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At supabase/migrations/20260630210000_create_email_send_log.sql, line 11:

<comment>Enable RLS on email_send_log before merge. Otherwise a public Supabase table containing captured email request bodies may be exposed or mutable through client roles depending on grants/policies.</comment>

<file context>
@@ -0,0 +1,28 @@
+-- so a send is debuggable several days back. Written by api `logEmailAttempt`
+-- (recoupable/api PR #731). Append-only; no updated_at.
+
+CREATE TABLE IF NOT EXISTS public.email_send_log (
+    id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
+    created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
</file context>

id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
rg -n -i "create extension.*uuid" supabase/migrations

Repository: recoupable/database

Length of output: 157


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo '--- target migration ---'
cat -n supabase/migrations/20260630210000_create_email_send_log.sql

echo
echo '--- repository search: uuid_generate_v4 / uuid-ossp / gen_random_uuid / create extension ---'
rg -n -i "uuid_generate_v4|uuid-ossp|gen_random_uuid|create extension" .

echo
echo '--- migration files mentioning uuid ---'
rg -n -i "uuid_generate_v4|uuid-ossp|gen_random_uuid|create extension" supabase/migrations

Repository: recoupable/database

Length of output: 29884


Add the UUID extension or switch to gen_random_uuid()

uuid_generate_v4() depends on uuid-ossp, and there’s no migration here that enables it. This table creation will fail unless the extension is provisioned elsewhere; using gen_random_uuid() would match the rest of the schema.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@supabase/migrations/20260630210000_create_email_send_log.sql` at line 19, The
`email_send_log` table definition uses `uuid_generate_v4()` without ensuring the
required extension is enabled. Update the migration in
`create_email_send_log.sql` to either add the missing `uuid-ossp` extension
setup before the `CREATE TABLE` statement or switch the `id` default to
`gen_random_uuid()` to match the existing schema pattern.

created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
account_id UUID, -- sender account (denormalized)
chat_id TEXT, -- chat the send belongs to (chats.id is text); join chats.session_id for the session
status TEXT NOT NULL, -- 'sent' | 'send_failed' | 'rejected'
resend_id TEXT, -- Resend message id (forward hook for delivery events)
raw_body TEXT, -- the request body as received (full; bounded by the platform request-size limit)
error TEXT
);

CREATE INDEX IF NOT EXISTS email_send_log_created_at_idx ON public.email_send_log (created_at DESC);
CREATE INDEX IF NOT EXISTS email_send_log_account_id_idx ON public.email_send_log (account_id);