Skip to content

Commit

Permalink
update_device_token
Browse files Browse the repository at this point in the history
  • Loading branch information
voynow committed Nov 10, 2024
1 parent ffa1f66 commit c3e2397
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 21 deletions.
23 changes: 22 additions & 1 deletion api/src/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging

from fastapi import Depends, FastAPI, HTTPException
from fastapi import Body, Depends, FastAPI, HTTPException
from src import supabase_client
from src.auth_manager import validate_user
from src.types.training_week import TrainingWeek
Expand All @@ -27,3 +27,24 @@ async def training_week(user: UserRow = Depends(validate_user)):
except ValueError as e:
logger.error(f"Error retrieving training week: {e}", exc_info=True)
raise HTTPException(status_code=404, detail=str(e))


@app.post("/device_token/")
async def update_device_token(
device_token: str = Body(..., embed=True), user: UserRow = Depends(validate_user)
) -> dict:
"""
Update device token for push notifications
:param device_token: The device token to register
:param user: The authenticated user
:return: Success status
"""
try:
supabase_client.update_user_device_token(
athlete_id=user.athlete_id, device_token=device_token
)
return {"success": True}
except Exception as e:
logger.error(f"Failed to update device token: {e}", exc_info=True)
raise HTTPException(status_code=400, detail=str(e))
12 changes: 12 additions & 0 deletions api/src/supabase_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,15 @@ def upsert_user_auth(user_auth_row: UserAuthRow) -> None:

table = client.table("user_auth")
table.upsert(user_auth_row, on_conflict="athlete_id").execute()


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()
11 changes: 11 additions & 0 deletions api/tests/test_get_training_week.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,14 @@ def test_get_training_week_success():
)
assert TrainingWeek(**response.json())
assert response.status_code == 200


def test_update_device_token_success():
"""Test successful update of device token"""
user_auth = get_user_auth(os.environ["JAMIES_ATHLETE_ID"])
response = client.post(
"/device_token/",
json={"device_token": user_auth.device_token},
headers={"Authorization": f"Bearer {user_auth.jwt_token}"},
)
assert response.status_code == 200
1 change: 0 additions & 1 deletion lambda/src/frontend_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ def update_device_token_handler(athlete_id: str, payload: dict) -> dict:
"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
54 changes: 35 additions & 19 deletions mobile/mobile/APIManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -268,32 +268,48 @@ class APIManager {
}

func updateDeviceToken(
token: String, deviceToken: String, completion: @escaping (Result<Void, Error>) -> Void
token: String,
deviceToken: String,
completion: @escaping (Result<Void, Error>) -> Void
) {
let startTime = CFAbsoluteTimeGetCurrent()
let body: [String: Any] = [
"jwt_token": token,
"payload": ["device_token": deviceToken],
"method": "update_device_token",
]
guard let url = URL(string: "\(apiURL)/device_token/") else {
completion(
.failure(NSError(domain: "", code: 0, userInfo: [NSLocalizedDescriptionKey: "Invalid URL"]))
)
return
}

performRequest(body: body, responseType: GenericResponse.self) { result in
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
request.setValue("application/json", forHTTPHeaderField: "Content-Type")

let payload = ["device_token": deviceToken]
request.httpBody = try? JSONSerialization.data(withJSONObject: payload)

session.dataTask(with: request) { data, response, error in
let timeElapsed = CFAbsoluteTimeGetCurrent() - startTime
print("APIManager: updateDeviceToken took \(timeElapsed) seconds")

switch result {
case .success(let response):
if response.success {
completion(.success(()))
} else {
let errorMessage = response.message ?? "Failed to update device token"
completion(
.failure(
NSError(domain: "", code: 0, userInfo: [NSLocalizedDescriptionKey: errorMessage])))
}
case .failure(let error):
if let httpResponse = response as? HTTPURLResponse,
!(200..<300).contains(httpResponse.statusCode)
{
let message = "Failed to update device token"
completion(
.failure(
NSError(
domain: "", code: httpResponse.statusCode,
userInfo: [NSLocalizedDescriptionKey: message])))
return
}

if let error = error {
completion(.failure(error))
return
}
}

completion(.success(()))
}.resume()
}
}

0 comments on commit c3e2397

Please sign in to comment.