Coverage for dao/club_notifications_dao.py: 29.73%
31 statements
« prev ^ index » next coverage.py v7.10.6, created at 2026-11-30 15:03 +0000
« prev ^ index » next coverage.py v7.10.6, created at 2026-11-30 15:03 +0000
1"""
2Per-club, per-platform view of notification channels.
4Serves the original /api/clubs/{id}/notifications endpoints: one fan channel for
5each club and platform. Rows live in `notification_channels`; when a club has
6several for a platform, these methods act on the oldest.
7"""
9import structlog
11from dao.notification_channels_dao import TABLE, NotificationChannelsDAO
13logger = structlog.get_logger()
16class ClubNotificationsDAO(NotificationChannelsDAO):
17 """Club fan channels, addressed by (club, platform)."""
19 def _club_fans(self, query, club_id: int):
20 return query.eq("scope_type", "club").eq("club_id", club_id).eq("audience", "fans")
22 def list_by_club(self, club_id: int) -> list[dict]:
23 """Return all of a club's fan channels (any platform, any enabled state)."""
24 return self._select("list_by_club", lambda q: self._club_fans(q, club_id).order("platform").order("id"))
26 def get(self, club_id: int, platform: str) -> dict | None:
27 """Return the oldest fan channel for a (club, platform), or None."""
28 rows = self._select(
29 "get",
30 lambda q: self._club_fans(q, club_id).eq("platform", platform).order("id").limit(1),
31 )
32 return rows[0] if rows else None
34 def upsert(
35 self,
36 club_id: int,
37 platform: str,
38 destination: str,
39 enabled: bool = True,
40 created_by: str | None = None,
41 ) -> dict | None:
42 """Create or update the channel for a (club, platform). Raises on database errors."""
43 existing = self.get(club_id, platform)
44 if existing:
45 return self.update(existing["id"], {"destination": destination, "enabled": enabled})
46 return self.create(
47 {
48 "scope_type": "club",
49 "club_id": club_id,
50 "audience": "fans",
51 "platform": platform,
52 "destination": destination,
53 "enabled": enabled,
54 "created_by": created_by,
55 }
56 )
58 def set_enabled(self, club_id: int, platform: str, enabled: bool) -> dict | None:
59 """Flip `enabled` on an existing channel. None when there is none."""
60 existing = self.get(club_id, platform)
61 if not existing:
62 return None
63 return self.update(existing["id"], {"enabled": enabled})
65 def delete(self, club_id: int, platform: str) -> bool:
66 """Remove a (club, platform) channel. Returns True if a row was deleted."""
67 existing = self.get(club_id, platform)
68 if existing is None:
69 return False
70 try:
71 return self.delete_by_id(existing["id"])
72 except Exception:
73 logger.error("dao_delete_club_notification_failed", club_id=club_id, platform=platform)
74 return False
77__all__ = ["TABLE", "ClubNotificationsDAO"]