From 1c4f46aff0154a80d884d8ffc93f0c0161663ee5 Mon Sep 17 00:00:00 2001 From: Phani Raj Date: Thu, 17 Oct 2024 10:01:06 -0500 Subject: [PATCH] fix(surveys): do not reset survey iteration count if value is nil (#25632) 1. Change the default value of iteration_count to be None if its not part of the request. 2. Guard for None value before setting the iteration_count on the Survey instance. --- posthog/api/survey.py | 7 ++++--- posthog/api/test/test_survey.py | 15 ++++++++++++++- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/posthog/api/survey.py b/posthog/api/survey.py index b2faf7a419186..ea894a7dd30c0 100644 --- a/posthog/api/survey.py +++ b/posthog/api/survey.py @@ -386,7 +386,7 @@ def update(self, instance: Survey, validated_data): instance.targeting_flag.active = False instance.targeting_flag.save() - iteration_count = validated_data.get("iteration_count") + iteration_count = validated_data.get("iteration_count", None) if ( instance.current_iteration is not None and iteration_count is not None @@ -396,8 +396,9 @@ def update(self, instance: Survey, validated_data): f"Cannot change survey recurrence to {iteration_count}, should be at least {instance.current_iteration}" ) - instance.iteration_count = iteration_count - instance.iteration_frequency_days = validated_data.get("iteration_frequency_days") + if iteration_count is not None: + instance.iteration_count = iteration_count + instance.iteration_frequency_days = validated_data.get("iteration_frequency_days") instance = super().update(instance, validated_data) diff --git a/posthog/api/test/test_survey.py b/posthog/api/test/test_survey.py index 4f171d91b6c14..c6de95a44702e 100644 --- a/posthog/api/test/test_survey.py +++ b/posthog/api/test/test_survey.py @@ -2371,6 +2371,19 @@ def test_can_create_recurring_survey(self): assert len(response_data["iteration_start_dates"]) == 2 assert response_data["current_iteration"] == 1 + def test_can_create_and_launch_recurring_survey(self): + survey = self._create_recurring_survey() + response = self.client.patch( + f"/api/projects/{self.team.id}/surveys/{survey.id}/", + data={ + "start_date": datetime.now() - timedelta(days=1), + }, + ) + response_data = response.json() + assert response_data["iteration_start_dates"] is not None + assert len(response_data["iteration_start_dates"]) == 2 + assert response_data["current_iteration"] == 1 + def test_can_set_internal_targeting_flag(self): survey = self._create_recurring_survey() response = self.client.patch( @@ -2493,7 +2506,7 @@ def test_guards_for_nil_iteration_count(self): ) assert response.status_code == status.HTTP_200_OK survey.refresh_from_db() - self.assertIsNone(survey.current_iteration) + self.assertIsNotNone(survey.current_iteration) response = self.client.patch( f"/api/projects/{self.team.id}/surveys/{survey.id}/", data={