Skip to content

Commit 9fdadeb

Browse files
committed
Refactor notification content handling and update database configuration
- Changed the DATABASE_URL in the configuration to point to the pecha-worker database. - Introduced a new asynchronous function, fetch_plan_notification_content, to retrieve notification content from the backend. - Updated the dispatch service to use the new resolve_notification_content function for building notification content. - Enhanced the push notification function to accept an optional image_url parameter for improved notification payloads.
1 parent 6eaff2d commit 9fdadeb

5 files changed

Lines changed: 66 additions & 7 deletions

File tree

worker_api/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import re
33

44
DEFAULTS = dict(
5-
DATABASE_URL="postgresql://admin:pechaAdmin@localhost:5434/pecha",
5+
DATABASE_URL="postgresql://admin:pechaAdmin@localhost:5434/pecha-worker",
66
MONGO_CONNECTION_STRING="mongodb://admin:pechaAdmin@localhost:27017/pecha?authSource=admin",
77
MONGO_DATABASE_NAME="webuddhist",
88

worker_api/notifications/services/backend_client.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import httpx
22

3+
from uuid import UUID
4+
35
from worker_api.config import get
4-
from worker_api.notifications.schemas import RoutineNotificationTargetsResponse
6+
from worker_api.notifications.schemas import NotificationContent, RoutineNotificationTargetsResponse
57

68

79
async def fetch_routine_notification_targets() -> RoutineNotificationTargetsResponse:
@@ -20,3 +22,26 @@ async def fetch_routine_notification_targets() -> RoutineNotificationTargetsResp
2022
)
2123
response.raise_for_status()
2224
return RoutineNotificationTargetsResponse.model_validate(response.json())
25+
26+
27+
async def fetch_plan_notification_content(
28+
*,
29+
user_id: UUID,
30+
plan_id: UUID,
31+
) -> NotificationContent:
32+
backend_url = get("BACKEND_API_URL").rstrip("/")
33+
if not backend_url:
34+
raise RuntimeError("BACKEND_API_URL is not configured")
35+
36+
dispatch_token = get("NOTIFICATION_DISPATCH_SECRET_TOKEN")
37+
if not dispatch_token:
38+
raise RuntimeError("NOTIFICATION_DISPATCH_SECRET_TOKEN is not configured")
39+
40+
async with httpx.AsyncClient(timeout=30.0) as client:
41+
response = await client.get(
42+
f"{backend_url}/internal/plan-notification-content",
43+
params={"user_id": str(user_id), "plan_id": str(plan_id)},
44+
headers={"X-Dispatch-Token": dispatch_token},
45+
)
46+
response.raise_for_status()
47+
return NotificationContent.model_validate(response.json())

worker_api/notifications/services/dispatch_service.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from worker_api.db.database import SessionLocal
66
from worker_api.notifications.repositories import reminder_repository
77
from worker_api.notifications.schemas import DispatchDueNotificationsResponse
8-
from worker_api.notifications.services.notification_content_service import build_notification_content
8+
from worker_api.notifications.services.notification_content_service import resolve_notification_content
99
from worker_api.notifications.services.push_service import (
1010
_already_dispatched,
1111
_mark_dispatched,
@@ -36,9 +36,14 @@ async def dispatch_due_notifications_service() -> DispatchDueNotificationsRespon
3636
skipped += 1
3737
continue
3838

39-
title, body = build_notification_content(reminder)
39+
content = await resolve_notification_content(reminder)
4040
try:
41-
await send_push_notification(reminder, title, body)
41+
await send_push_notification(
42+
reminder,
43+
content.title,
44+
content.body,
45+
content.image_url,
46+
)
4247
reminder_repository.mark_sent(db, reminder)
4348
reminder_repository.create_reminder(
4449
db,

worker_api/notifications/services/notification_content_service.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
import logging
2+
13
from worker_api.config import get
24
from worker_api.notifications.models.reminder_models import UpcomingReminder
5+
from worker_api.notifications.schemas import NotificationContent
6+
7+
logger = logging.getLogger(__name__)
38

49

510
def build_notification_content(reminder: UpcomingReminder) -> tuple[str, str]:
@@ -15,3 +20,21 @@ def build_notification_content(reminder: UpcomingReminder) -> tuple[str, str]:
1520
body = f"Day {routine['current_day_number']}: time for your practice."
1621

1722
return title, body
23+
24+
25+
async def resolve_notification_content(reminder: UpcomingReminder) -> NotificationContent:
26+
from worker_api.notifications.services.backend_client import fetch_plan_notification_content
27+
28+
try:
29+
return await fetch_plan_notification_content(
30+
user_id=reminder.user_id,
31+
plan_id=reminder.plan_id,
32+
)
33+
except Exception:
34+
logger.exception(
35+
"Failed to fetch plan notification content from backend for user %s plan %s",
36+
reminder.user_id,
37+
reminder.plan_id,
38+
)
39+
title, body = build_notification_content(reminder)
40+
return NotificationContent(title=title, body=body, image_url=None)

worker_api/notifications/services/push_service.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from worker_api.config import get, get_bool, get_int
88
from worker_api.notifications.enums import PushPlatform
99
from worker_api.notifications.models.reminder_models import UpcomingReminder
10-
from worker_api.notifications.services.notification_content_service import build_notification_content
1110
from worker_api.notifications.services.push.config_loader import is_push_configured
1211
from worker_api.notifications.services.push.fcm_client import (
1312
build_routine_notification_data,
@@ -49,7 +48,12 @@ def _mark_dispatched(reminder_id: UUID) -> None:
4948
)
5049

5150

52-
async def send_push_notification(reminder: UpcomingReminder, title: str, body: str) -> None:
51+
async def send_push_notification(
52+
reminder: UpcomingReminder,
53+
title: str,
54+
body: str,
55+
image_url: str | None = None,
56+
) -> None:
5357
if not is_push_configured(reminder.platform):
5458
logger.warning(
5559
"Push not configured for platform %s; skipping send for reminder %s",
@@ -63,11 +67,13 @@ async def send_push_notification(reminder: UpcomingReminder, title: str, body: s
6367
device_token=reminder.device_token,
6468
title=title,
6569
body=body,
70+
image_url=image_url,
6671
data=build_routine_notification_data(
6772
session_type="PLAN",
6873
source_id=reminder.plan_id,
6974
title=title,
7075
body=body,
76+
image_url=image_url,
7177
),
7278
)
7379
return

0 commit comments

Comments
 (0)