Skip to content

Commit

Permalink
Merge pull request #94 from voynow/91-email-on-new-activity
Browse files Browse the repository at this point in the history
hotfix for invocation_id pipeline param
  • Loading branch information
voynow authored Oct 10, 2024
2 parents 04876d9 + c84d6db commit 1ac1a37
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 18 deletions.
10 changes: 9 additions & 1 deletion src/lambda_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@


def strategy_router(event: dict, invocation_id: str) -> dict:
"""
Route the event to the appropriate handler based on the event type.
:param event: The event dictionary containing event data
:param invocation_id: The unique identifier for the invocation
:return: {"success": bool, other_metadata: dict} where the error key is
only present if success is False
"""

# Will fail on bad authenticate_with_code
if event.get("email") and event.get("code"):
Expand Down Expand Up @@ -38,7 +46,7 @@ def strategy_router(event: dict, invocation_id: str) -> dict:
and event.get("object_id")
and event.get("owner_id")
):
return webhook_router.handle_request(event)
return webhook_router.handle_request(event, invocation_id)

# This will only run if triggered by NIGHTLY_EMAIL_TRIGGER_ARN
elif (
Expand Down
20 changes: 16 additions & 4 deletions src/update_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,14 @@ def integration_test_executor(invocation_id: str) -> dict:
Run a full update pipeline for Jamies account
"""
training_week_update_executor(
get_user(os.environ["JAMIES_ATHLETE_ID"]), ExeType.MID_WEEK
user=get_user(os.environ["JAMIES_ATHLETE_ID"]),
exetype=ExeType.MID_WEEK,
invocation_id=invocation_id,
)
training_week_update_executor(
get_user(os.environ["JAMIES_ATHLETE_ID"]), ExeType.NEW_WEEK
user=get_user(os.environ["JAMIES_ATHLETE_ID"]),
exetype=ExeType.NEW_WEEK,
invocation_id=invocation_id,
)
return {"success": True}

Expand All @@ -162,9 +166,17 @@ def nightly_trigger_orchestrator(invocation_id: str) -> dict:
if datetime_now_est().weekday() != 6:
for user in list_users():
if user.is_active and not has_user_updated_today(user.athlete_id):
training_week_update_executor(user, ExeType.MID_WEEK)
training_week_update_executor(
user=user,
exetype=ExeType.MID_WEEK,
invocation_id=invocation_id,
)
else:
for user in list_users():
if user.is_active:
training_week_update_executor(user, ExeType.NEW_WEEK)
training_week_update_executor(
user=user,
exetype=ExeType.NEW_WEEK,
invocation_id=invocation_id,
)
return {"success": True}
13 changes: 9 additions & 4 deletions src/webhook_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,28 @@
get_user,
)
from src.types.update_pipeline import ExeType
from src.types.user_row import UserRow
from src.update_pipeline import training_week_update_executor


def handle_activity_create(user, event):
def handle_activity_create(user: UserRow, event: dict, invocation_id: str) -> dict:
strava_client = auth_manager.get_strava_client(user.athlete_id)
activity = strava_client.get_activity(event.get("object_id"))

if activity.sport_type == "Run":
return training_week_update_executor(user, ExeType.MID_WEEK)
return training_week_update_executor(
user=user,
exetype=ExeType.MID_WEEK,
invocation_id=invocation_id,
)

return {
"success": False,
"error": f"Unsupported activity type: {activity.sport_type}",
}


def handle_request(event: dict) -> dict:
def handle_request(event: dict, invocation_id: str) -> dict:
"""
Handle Strava webhook events for activities and athletes.
Expand All @@ -39,7 +44,7 @@ def handle_request(event: dict) -> dict:

if event_type == "activity":
if aspect_type == "create":
return handle_activity_create(user, event)
return handle_activity_create(user, event, invocation_id)
if aspect_type in {"update", "delete"}:
return {
"success": True,
Expand Down
29 changes: 20 additions & 9 deletions test.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -197,20 +197,31 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 6,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"{'success': False}"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# import requests\n",
"# import os\n",
"import requests\n",
"import os\n",
"\n",
"# url = \"https://lwg77yq7dd.execute-api.us-east-1.amazonaws.com/prod/signup\"\n",
"url = \"https://lwg77yq7dd.execute-api.us-east-1.amazonaws.com/prod/signup\"\n",
"\n",
"# response = requests.post(url, json={\n",
"# \"trigger_test_key\": os.environ[\"TRIGGER_TEST_KEY\"]\n",
"# })\n",
"response = requests.post(url, json={\n",
" \"trigger_test_key\": os.environ[\"TRIGGER_TEST_KEY\"]\n",
"})\n",
"\n",
"# response.json()"
"response.json()"
]
},
{
Expand Down

0 comments on commit 1ac1a37

Please sign in to comment.