Skip to content

Commit

Permalink
send event when level changed
Browse files Browse the repository at this point in the history
  • Loading branch information
raquelmsmith committed Nov 1, 2023
1 parent e473e27 commit 71f9cc2
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 6 deletions.
21 changes: 21 additions & 0 deletions posthog/event_usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,27 @@ def report_bulk_invited(
)


def report_user_organization_membership_level_changed(
user: User,
organization: Organization,
new_level: int,
previous_level: int,
) -> None:
"""
Triggered after a user's membership level in an organization is changed.
"""
posthoganalytics.capture(
user.distinct_id,
"membership level changed",
properties={
"organization_id": str(organization.id),
"new_level": new_level,
"previous_level": previous_level,
},
groups=groups(organization),
)


def report_user_action(user: User, event: str, properties: Dict = {}):
posthoganalytics.capture(
user.distinct_id,
Expand Down
23 changes: 17 additions & 6 deletions posthog/models/organization.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,7 @@
from posthog.cloud_utils import is_cloud
from posthog.constants import MAX_SLUG_LENGTH, AvailableFeature
from posthog.email import is_email_available
from posthog.models.utils import (
LowercaseSlugField,
UUIDModel,
create_with_slug,
sane_repr,
)
from posthog.models.utils import LowercaseSlugField, UUIDModel, create_with_slug, sane_repr
from posthog.redis import get_client
from posthog.utils import absolute_uri

Expand Down Expand Up @@ -415,3 +410,19 @@ def ensure_organization_membership_consistency(sender, instance: OrganizationMem
save_user = True
if save_user:
instance.user.save()


@receiver(models.signals.pre_save, sender=OrganizationMembership)
def organization_membership_saved(sender: Any, instance: OrganizationMembership, **kwargs: Any) -> None:
from posthog.event_usage import report_user_organization_membership_level_changed

try:
old_instance = OrganizationMembership.objects.get(id=instance.id)
if old_instance.level != instance.level:
# the level has been changed
report_user_organization_membership_level_changed(
instance.user, instance.organization, instance.level, old_instance.level
)
except OrganizationMembership.DoesNotExist:
# The instance is new, or we are setting up test data
pass
28 changes: 28 additions & 0 deletions posthog/models/test/test_organization_model.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from unittest import mock
from unittest.mock import patch

from django.utils import timezone

from posthog.models import Organization, OrganizationInvite, Plugin
from posthog.models.organization import OrganizationMembership
from posthog.plugins.test.mock import mocked_plugin_requests_get
from posthog.plugins.test.plugin_archives import HELLO_WORLD_PLUGIN_GITHUB_ZIP
from posthog.test.base import BaseTest
Expand Down Expand Up @@ -77,3 +79,29 @@ def test_update_available_features_ignored_if_usage_info_exists(self):
new_org.usage = {"events": {"usage": 1000, "limit": None}}
new_org.update_available_features()
assert new_org.available_features == ["test1", "test2"]


class TestOrganizationMembership(BaseTest):
@patch("posthoganalytics.capture")
def test_event_sent_when_membership_level_changed(
self,
mock_capture,
):
user = self._create_user("user1")
organization = Organization.objects.create(name="Test Org")
membership = OrganizationMembership.objects.create(user=user, organization=organization, level=1)
mock_capture.assert_not_called()
# change the level
membership.level = 15
membership.save()
# check that the event was sent
mock_capture.assert_called_once_with(
user.distinct_id,
"membership level changed",
properties={
"organization_id": str(organization.id),
"new_level": 15,
"previous_level": 1,
},
groups=mock.ANY,
)

0 comments on commit 71f9cc2

Please sign in to comment.