📈(backend) capture an event when a document is left

When a user leave a document, we want to capture an event.
This commit is contained in:
Manuel Raynaud
2026-06-01 17:47:28 +02:00
parent c91c443b08
commit 1ebc8d52a0
3 changed files with 56 additions and 5 deletions
+2
View File
@@ -2602,6 +2602,8 @@ class DocumentViewSet(
)
raise
posthog_capture(PosthogEventName.DOC_LEFT, request.user, {}, document=document)
return drf.response.Response(status=drf.status.HTTP_204_NO_CONTENT)
@@ -1,10 +1,13 @@
"""Test for the leave document API"""
from unittest import mock
import pytest
from rest_framework import status
from rest_framework.test import APIClient
from core import factories, models
from core.utils.analytics import PosthogEventName
pytestmark = pytest.mark.django_db
@@ -83,10 +86,19 @@ def test_api_documents_leave_connected_user_with_link_trace(link_reach):
client = APIClient()
client.force_login(user)
response = client.post(f"/api/v1.0/documents/{document.id!s}/leave/")
with mock.patch("core.api.viewsets.posthog_capture") as mock_capture:
response = client.post(f"/api/v1.0/documents/{document.id!s}/leave/")
assert response.status_code == status.HTTP_204_NO_CONTENT
# Leaving the document should be tracked in PostHog
mock_capture.assert_called_once_with(
PosthogEventName.DOC_LEFT,
user,
{},
document=document,
)
assert not models.LinkTrace.objects.filter(document=document, user=user).exists()
assert models.LinkTrace.objects.count() == 3
assert models.DocumentAccess.objects.count() == 4
@@ -116,10 +128,19 @@ def test_api_documents_leave_connected_user_with_access(role, link_reach):
client = APIClient()
client.force_login(user)
response = client.post(f"/api/v1.0/documents/{document.id!s}/leave/")
with mock.patch("core.api.viewsets.posthog_capture") as mock_capture:
response = client.post(f"/api/v1.0/documents/{document.id!s}/leave/")
assert response.status_code == status.HTTP_204_NO_CONTENT
# Leaving the document should be tracked in PostHog
mock_capture.assert_called_once_with(
PosthogEventName.DOC_LEFT,
user,
{},
document=document,
)
assert not models.DocumentAccess.objects.filter(
document=document, user=user
).exists()
@@ -189,10 +210,19 @@ def test_api_documents_leave_connected_user_with_access_and_link_trace(
client = APIClient()
client.force_login(user)
response = client.post(f"/api/v1.0/documents/{document.id!s}/leave/")
with mock.patch("core.api.viewsets.posthog_capture") as mock_capture:
response = client.post(f"/api/v1.0/documents/{document.id!s}/leave/")
assert response.status_code == status.HTTP_204_NO_CONTENT
# Leaving the document should be tracked in PostHog
mock_capture.assert_called_once_with(
PosthogEventName.DOC_LEFT,
user,
{},
document=document,
)
assert not models.DocumentAccess.objects.filter(
document=document, user=user
).exists()
@@ -231,10 +261,19 @@ def test_api_documents_leave_connected_accessing_multiple_documents_leave_only_o
client = APIClient()
client.force_login(user)
response = client.post(f"/api/v1.0/documents/{document.id!s}/leave/")
with mock.patch("core.api.viewsets.posthog_capture") as mock_capture:
response = client.post(f"/api/v1.0/documents/{document.id!s}/leave/")
assert response.status_code == status.HTTP_204_NO_CONTENT
# Leaving the document should be tracked in PostHog
mock_capture.assert_called_once_with(
PosthogEventName.DOC_LEFT,
user,
{},
document=document,
)
assert not models.DocumentAccess.objects.filter(
document=document, user=user
).exists()
@@ -281,10 +320,19 @@ def test_api_documents_leave_connected_leave_also_sub_documents(role, link_reach
client = APIClient()
client.force_login(user)
response = client.post(f"/api/v1.0/documents/{document.id!s}/leave/")
with mock.patch("core.api.viewsets.posthog_capture") as mock_capture:
response = client.post(f"/api/v1.0/documents/{document.id!s}/leave/")
assert response.status_code == status.HTTP_204_NO_CONTENT
# Leaving the document should be tracked in PostHog
mock_capture.assert_called_once_with(
PosthogEventName.DOC_LEFT,
user,
{},
document=document,
)
assert not models.DocumentAccess.objects.filter(
document=document, user=user
).exists()
+1
View File
@@ -22,6 +22,7 @@ class PosthogEventName(StrEnum):
DOC_FAVORITED = "doc_favorited"
DOC_AI_ACTION = "doc_ai_action"
DOC_MOVED = "doc_moved"
DOC_LEFT = "doc_left"
# DocumentAccess
DOC_ACCESS_CREATED = "doc_access_created"