From b952db180670c591e4599177f6aa782dc676dc0e Mon Sep 17 00:00:00 2001 From: Jack Levy Date: Sun, 15 Mar 2026 14:22:18 -0400 Subject: [PATCH] fix: re-fetch votes when bill has new actions since last vote fetch Previously skipped bills that had any stored votes. Now also re-queues bills where latest_action_date > MAX(vote_date), catching new votes on already-tracked bills. Authored by: Jack Levy --- backend/app/workers/vote_fetcher.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/backend/app/workers/vote_fetcher.py b/backend/app/workers/vote_fetcher.py index dfa25e3..235e7e7 100644 --- a/backend/app/workers/vote_fetcher.py +++ b/backend/app/workers/vote_fetcher.py @@ -243,21 +243,27 @@ def fetch_bill_votes(self, bill_id: str) -> dict: def fetch_votes_for_stanced_bills(self) -> dict: """ Nightly task: queue vote fetches for every bill any user has a stance on - (pocket_veto or pocket_boost). Only queues bills that don't already have - a vote stored, so re-runs are cheap after the first pass. + (pocket_veto or pocket_boost). Queues bills with no votes yet, plus bills + whose latest stored vote predates the bill's latest_action_date (new vote + recorded since last fetch). """ - from app.models.follow import Follow - db = get_sync_db() try: from sqlalchemy import text as sa_text rows = db.execute(sa_text(""" SELECT DISTINCT f.follow_value AS bill_id FROM follows f - LEFT JOIN bill_votes bv ON bv.bill_id = f.follow_value + JOIN bills b ON b.bill_id = f.follow_value WHERE f.follow_type = 'bill' AND f.follow_mode IN ('pocket_veto', 'pocket_boost') - AND bv.id IS NULL + AND ( + -- no votes fetched yet + NOT EXISTS (SELECT 1 FROM bill_votes bv WHERE bv.bill_id = f.follow_value) + OR + -- bill has had action more recently than our latest stored vote + (SELECT MAX(bv.vote_date) FROM bill_votes bv WHERE bv.bill_id = f.follow_value) + < b.latest_action_date + ) """)).fetchall() queued = 0