Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
raquelmsmith committed Dec 6, 2024
1 parent bc6f627 commit 1607cbd
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
41 changes: 23 additions & 18 deletions posthog/api/team.py
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,7 @@ def add_product_intent(self, request: request.Request, *args, **kwargs):
product_type = request.data.get("product_type")
current_url = request.headers.get("Referer")
session_id = request.headers.get("X-Posthog-Session-Id")
should_report_product_intent = False

if not product_type:
return response.Response({"error": "product_type is required"}, status=400)
Expand All @@ -619,31 +620,35 @@ def add_product_intent(self, request: request.Request, *args, **kwargs):
if created:
# For new intents, check activation immediately but skip reporting
was_already_activated = product_intent.check_and_update_activation(skip_reporting=True)

# Only report the action if they haven't already activated
if isinstance(user, User) and not was_already_activated:
report_user_action(
user,
"user showed product intent",
{
"product_key": product_type,
"$set_once": {"first_onboarding_product_selected": product_type},
"$current_url": current_url,
"$session_id": session_id,
"intent_context": request.data.get("intent_context"),
"is_first_intent_for_product": created,
"intent_created_at": product_intent.created_at,
"intent_updated_at": product_intent.updated_at,
"realm": get_instance_realm(),
},
team=team,
)
should_report_product_intent = True
else:
if not product_intent.activated_at:
product_intent.check_and_update_activation()
is_activated = product_intent.check_and_update_activation()
if not is_activated:
should_report_product_intent = True
product_intent.updated_at = datetime.now(tz=UTC)
product_intent.save()

if should_report_product_intent:
report_user_action(
user,

Check failure on line 636 in posthog/api/team.py

View workflow job for this annotation

GitHub Actions / Python code quality checks

Argument 1 to "report_user_action" has incompatible type "User | AnonymousUser"; expected "User"
"user showed product intent",
{
"product_key": product_type,
"$set_once": {"first_onboarding_product_selected": product_type},
"$current_url": current_url,
"$session_id": session_id,
"intent_context": request.data.get("intent_context"),
"is_first_intent_for_product": created,
"intent_created_at": product_intent.created_at,
"intent_updated_at": product_intent.updated_at,
"realm": get_instance_realm(),
},
team=team,
)

return response.Response(TeamSerializer(team, context=self.get_serializer_context()).data, status=201)

@action(methods=["PATCH"], detail=True)
Expand Down
6 changes: 5 additions & 1 deletion posthog/api/test/test_team.py
Original file line number Diff line number Diff line change
Expand Up @@ -1052,7 +1052,7 @@ def test_can_add_product_intent(
)

@patch("posthog.api.team.calculate_product_activation.delay", MagicMock())
@patch("posthog.models.product_intent.ProductIntent.check_and_update_activation")
@patch("posthog.models.product_intent.ProductIntent.check_and_update_activation", return_value=False)
@patch("posthog.api.project.report_user_action")
@patch("posthog.api.team.report_user_action")
@freeze_time("2024-01-01T00:00:00Z")
Expand All @@ -1062,6 +1062,10 @@ def test_can_update_product_intent_if_already_exists(
mock_report_user_action_legacy_endpoint: MagicMock,
mock_check_and_update_activation: MagicMock,
) -> None:
"""
Intent already exists, but hasn't been activated yet. It should update the intent
and send a new event for the user showing the intent.
"""
intent = ProductIntent.objects.create(team=self.team, product_type="product_analytics")
original_created_at = intent.created_at
assert original_created_at == datetime(2024, 1, 1, 0, 0, 0, tzinfo=UTC)
Expand Down

0 comments on commit 1607cbd

Please sign in to comment.