Coverage for dao/notification_preferences_dao.py: 95.24%

36 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2026-11-30 17:31 +0000

1"""Notification Preferences DAO (SB-57). 

2 

3Per-user opt-in flags for push notification event types. Missing rows fall back 

4to defaults from `backend/notifications/preferences.py`. 

5 

6`get_preferences_batch` is the dispatcher hot-path query — one SELECT covers 

7every user about to receive a given event. 

8""" 

9 

10from __future__ import annotations 

11 

12import structlog 

13 

14from dao.base_dao import BaseDAO 

15from notifications.preferences import ( 

16 DEFAULT_PREFERENCES, 

17 EVENT_TYPES, 

18 merge_with_defaults, 

19) 

20 

21logger = structlog.get_logger() 

22 

23TABLE = "user_notification_preferences" 

24 

25 

26class NotificationPreferencesDAO(BaseDAO): 

27 """Data access for user notification preferences.""" 

28 

29 def get_preferences(self, user_id: str) -> dict[str, bool]: 

30 """Return all 6 event-type flags for a user, defaults applied.""" 

31 try: 

32 response = ( 

33 self.client.table(TABLE) 

34 .select("event_type, enabled") 

35 .eq("user_id", user_id) 

36 .execute() 

37 ) 

38 stored = {row["event_type"]: row["enabled"] for row in response.data or []} 

39 return merge_with_defaults(stored) 

40 except Exception: 

41 logger.exception("notification_prefs_get_failed", user_id=user_id) 

42 return dict(DEFAULT_PREFERENCES) 

43 

44 def set_preferences( 

45 self, user_id: str, prefs: dict[str, bool] 

46 ) -> dict[str, bool]: 

47 """Upsert preferences for a user. Only known event types are persisted. 

48 

49 Returns the resulting merged state (so the caller can echo it back to 

50 the client). 

51 """ 

52 rows = [ 

53 {"user_id": user_id, "event_type": event_type, "enabled": bool(enabled)} 

54 for event_type, enabled in prefs.items() 

55 if event_type in EVENT_TYPES 

56 ] 

57 if not rows: 

58 return self.get_preferences(user_id) 

59 try: 

60 self.client.table(TABLE).upsert( 

61 rows, on_conflict="user_id,event_type" 

62 ).execute() 

63 except Exception: 

64 logger.exception( 

65 "notification_prefs_set_failed", 

66 user_id=user_id, 

67 event_types=list(prefs.keys()), 

68 ) 

69 # Fall through to a fresh read so the client sees current truth. 

70 return self.get_preferences(user_id) 

71 

72 def get_preferences_batch( 

73 self, user_ids: list[str] 

74 ) -> dict[str, dict[str, bool]]: 

75 """Batch-fetch prefs for many users. Dispatcher hot-path. 

76 

77 Returns `{user_id: {event_type: enabled, ...}}` with defaults filled in 

78 for any user that has no stored rows. 

79 """ 

80 if not user_ids: 

81 return {} 

82 try: 

83 response = ( 

84 self.client.table(TABLE) 

85 .select("user_id, event_type, enabled") 

86 .in_("user_id", user_ids) 

87 .execute() 

88 ) 

89 stored: dict[str, dict[str, bool]] = {} 

90 for row in response.data or []: 

91 stored.setdefault(row["user_id"], {})[row["event_type"]] = row[ 

92 "enabled" 

93 ] 

94 return { 

95 uid: merge_with_defaults(stored.get(uid)) for uid in user_ids 

96 } 

97 except Exception: 

98 logger.exception( 

99 "notification_prefs_batch_failed", 

100 user_count=len(user_ids), 

101 ) 

102 # Fallback: everyone gets defaults so we don't silently drop events. 

103 return {uid: dict(DEFAULT_PREFERENCES) for uid in user_ids}