Coverage for dao/push_send_log_dao.py: 29.17%

18 statements  

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

1"""Push Send Log DAO. 

2 

3Append-only log of every push send attempt. Used for delivery debugging 

4("I'm not getting notifications") and basic analytics. All writes go via 

5the backend service role; users have no direct access (RLS). 

6 

7Writes are fire-and-forget — any logging failure must NOT bubble up and 

8break the push send flow. 

9""" 

10 

11from __future__ import annotations 

12 

13import structlog 

14 

15from dao.base_dao import BaseDAO 

16 

17logger = structlog.get_logger() 

18 

19TABLE = "push_send_log" 

20 

21 

22class PushSendLogDAO(BaseDAO): 

23 """Append-only log of push send attempts.""" 

24 

25 def log( 

26 self, 

27 subscription_id: str | None, 

28 user_id: str | None, 

29 match_id: int | None, 

30 event_type: str, 

31 status: str, 

32 http_status: int | None = None, 

33 error: str | None = None, 

34 apns_device_id: str | None = None, 

35 platform: str | None = None, 

36 fcm_device_id: str | None = None, 

37 ) -> None: 

38 """Record a single send attempt. Never raises. 

39 

40 status ∈ {'sent', 'failed', 'expired'}. 'expired' means the push 

41 service returned 404/410 and the subscription has been cleaned up. 

42 

43 APNs sends (SB-1236) pass platform='apns' and apns_device_id with 

44 subscription_id=None. FCM sends (SB-1463) likewise pass platform='fcm' 

45 and fcm_device_id. Web sends omit both; the column defaults to 'web'. 

46 """ 

47 row = { 

48 "subscription_id": subscription_id, 

49 "user_id": user_id, 

50 "match_id": match_id, 

51 "event_type": event_type, 

52 "status": status, 

53 "http_status": http_status, 

54 "error": (error[:500] if error else None), 

55 } 

56 if platform: 

57 row["platform"] = platform 

58 if apns_device_id: 

59 row["apns_device_id"] = apns_device_id 

60 if fcm_device_id: 

61 row["fcm_device_id"] = fcm_device_id 

62 try: 

63 self.client.table(TABLE).insert(row).execute() 

64 except Exception as exc: 

65 # Don't bubble — logging is best-effort. 

66 logger.warning( 

67 "push_send_log_write_failed", 

68 error_type=type(exc).__name__, 

69 error=str(exc), 

70 )