feat(apify): digest rate cap (1 per artist per 24h) + email_send_log audit trail
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_bodyprefixscrape-digest:).getScrapeDigestRecipientsnow returns{ emails, artistIds }— the artist entity ids are the cap key.maybeSendScrapeDigest: skips when any watched artist digested within 24h; on send, records oneemail_send_logrow per artist entity (scrape-digest:<batchId>, resend id) via the existing best-effortlogEmailAttempt.
Tests (RED→GREEN)
- Cap: recent digest row → no send.
- Audit: successful send →
logEmailAttemptwith 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 ascrape-digest:<batchId>row exists viaselectScrapeDigestLogs({ batchId }). - 24h per-artist rate cap using
selectScrapeDigestLogs({ artistIds, since }), keyed byartistIdsfromgetScrapeDigestRecipients. - Audit trail: on send, writes one
email_send_logrow per artist withraw_bodyscrape-digest:<batchId>vialogEmailAttempt. - Idempotent run completion:
updateApifyScraperRunupdates only wherecompleted_at IS NULLso webhook retries can’t re-trigger a digest.
- Batch idempotency in
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.