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
This commit is contained in:
Jack Levy
2026-03-15 14:22:18 -04:00
parent 0d49e17cea
commit b952db1806

View File

@@ -243,21 +243,27 @@ def fetch_bill_votes(self, bill_id: str) -> dict:
def fetch_votes_for_stanced_bills(self) -> dict: def fetch_votes_for_stanced_bills(self) -> dict:
""" """
Nightly task: queue vote fetches for every bill any user has a stance on 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 (pocket_veto or pocket_boost). Queues bills with no votes yet, plus bills
a vote stored, so re-runs are cheap after the first pass. 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() db = get_sync_db()
try: try:
from sqlalchemy import text as sa_text from sqlalchemy import text as sa_text
rows = db.execute(sa_text(""" rows = db.execute(sa_text("""
SELECT DISTINCT f.follow_value AS bill_id SELECT DISTINCT f.follow_value AS bill_id
FROM follows f 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' WHERE f.follow_type = 'bill'
AND f.follow_mode IN ('pocket_veto', 'pocket_boost') 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() """)).fetchall()
queued = 0 queued = 0