feat(apify): digest rate cap (1 per artist per 24h) + email_send_log audit trail

Closed
SW
15d ago
+172-18

Summary

The last slice of recoupable/chat#1855 (PR #6 of 6, stacked on api#761): even with the newness diff, repeated scrapes of an artist in one day (agent runs, verification, retries) could digest repeatedly — evidence on the issue: 7 alerts to one customer in a day. This caps digests at one per watched artist entity per 24h and gives them an audit trail: the old alert path called Resend directly and left zero rows in email_send_log, so the platform had no record of who was alerted when.

What changed

  • lib/supabase/email_send_log/selectRecentScrapeDigestLogs.ts (new): digest-log lookback by artist ids (raw_body prefix scrape-digest:).
  • getScrapeDigestRecipients now returns { emails, artistIds } — the artist entity ids are the cap key.
  • maybeSendScrapeDigest: skips when any watched artist digested within 24h; on send, records one email_send_log row per artist entity (scrape-digest:<batchId>, resend id) via the existing best-effort logEmailAttempt.

Tests (RED→GREEN)

  • Cap: recent digest row → no send.
  • Audit: successful send → logEmailAttempt with artist id + sent.
  • Digest suite 9/9; full sweep lib/apify + lib/socials + lib/artist → 284 tests passed; tsc at exact baseline parity (200); eslint clean.

Stack

database#41 → api#759 → api#760 → api#761 → this. Done-when for the chain (from the issue): a full artist scrape produces exactly one digest listing each platform's new posts; re-running back-to-back produces none the second time.

🤖 Generated with Claude Code


Summary by cubic

Cap scrape digests to one per watched artist per 24h, log every send to email_send_log, and enforce exact non-duplication per batch. This prevents multi-send bursts (chat#1855) and makes sends traceable.

  • New Features
    • Batch idempotency in maybeSendScrapeDigest: skips if a scrape-digest:<batchId> row exists via selectScrapeDigestLogs({ batchId }).
    • 24h per-artist rate cap using selectScrapeDigestLogs({ artistIds, since }), keyed by artistIds from getScrapeDigestRecipients.
    • Audit trail: on send, writes one email_send_log row per artist with raw_body scrape-digest:<batchId> via logEmailAttempt.
    • Idempotent run completion: updateApifyScraperRun updates only where completed_at IS NULL so webhook retries can’t re-trigger a digest.

Summary by CodeRabbit

  • New Features

    • Added safeguards to prevent duplicate scrape digest emails.
    • Added a 24-hour sending limit per artist.
    • Added tracking for digest delivery attempts.
  • Bug Fixes

    • Prevented completed scraper runs from being processed again after webhook retries.
    • Improved recipient handling by tracking both email addresses and associated artists.