Skip to content

Commit

Permalink
backend route accepting user device token
Browse files Browse the repository at this point in the history
  • Loading branch information
voynow committed Oct 30, 2024
1 parent 0e04827 commit 23453b0
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/frontend_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
get_user,
get_user_auth,
update_preferences,
update_user_device_token,
)
from src.types.update_pipeline import ExeType
from src.update_pipeline import training_week_update_executor
Expand Down Expand Up @@ -85,12 +86,24 @@ def start_onboarding(athlete_id: str, payload: dict) -> dict:
return {"success": True}


def update_device_token_handler(athlete_id: str, payload: dict) -> dict:
"""Handle update_device_token request."""
if not payload or "device_token" not in payload:
return {"success": False, "error": "Missing device_token in payload"}
try:
update_user_device_token(athlete_id=athlete_id, device_token=payload["device_token"])
return {"success": True}
except Exception as e:
return {"success": False, "error": f"Failed to update device token: {str(e)}"}


METHOD_HANDLERS: Dict[str, Callable[[str, Optional[dict]], dict]] = {
"get_training_week": get_training_week_handler,
"get_profile": get_profile_handler,
"update_preferences": update_preferences_handler,
"get_weekly_summaries": get_weekly_summaries_handler,
"start_onboarding": start_onboarding,
"update_device_token": update_device_token_handler,
}


Expand Down
12 changes: 12 additions & 0 deletions src/supabase_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,3 +257,15 @@ def user_exists(athlete_id: int) -> bool:
table = client.table("user")
response = table.select("*").eq("athlete_id", athlete_id).execute()
return bool(response.data)


def update_user_device_token(athlete_id: str, device_token: str) -> None:
"""
Update the device token for a user in the database.
:param athlete_id: The athlete's ID
:param device_token: The device token for push notifications
"""
client.table("user_auth").update({"device_token": device_token}).eq(
"athlete_id", athlete_id
).execute()
1 change: 1 addition & 0 deletions src/types/user_auth_row.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ class UserAuthRow(BaseModel):
refresh_token: str
expires_at: datetime.datetime
jwt_token: str
device_token: Optional[str] = None

0 comments on commit 23453b0

Please sign in to comment.