From 63a3d361ff43f2a30d80186a06be6ec675770b72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=ADas=20Garc=C3=ADa=20Isa=C3=ADa?= Date: Wed, 4 Dec 2024 17:27:24 -0300 Subject: [PATCH] Honour results filter when generating file See #2350 --- lib/ask/survey_results.ex | 8 +++++++- test/ask/survey_results_test.exs | 26 ++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/lib/ask/survey_results.ex b/lib/ask/survey_results.ex index f12a605d8..2587f6747 100644 --- a/lib/ask/survey_results.ex +++ b/lib/ask/survey_results.ex @@ -427,6 +427,12 @@ defmodule Ask.SurveyResults do } end + defp filter_hash(filter) do + filter + |> :erlang.phash2 + |> to_string + end + defp file_prefix(:interactions), do: "respondents_interactions" defp file_prefix(:incentives), do: "respondents_incentives" defp file_prefix(:disposition_history), do: "disposition_history" @@ -435,7 +441,7 @@ defmodule Ask.SurveyResults do if RespondentsFilter.empty?(filter) do "respondents" else - "respondents_filtered" + "respondents_filtered_#{filter_hash(filter)}" end end diff --git a/test/ask/survey_results_test.exs b/test/ask/survey_results_test.exs index ac0c33cad..b807f1ba7 100644 --- a/test/ask/survey_results_test.exs +++ b/test/ask/survey_results_test.exs @@ -274,6 +274,32 @@ defmodule Ask.SurveyResultsTest do assert line_3_user_stopped == "true" end + test "generates different filtered results csv per filter" do + survey = insert(:survey) + + filter = RespondentsFilter.parse("") + + assert %{files: %{respondents_results: %{created_at: nil}}} = SurveyResults.files_status(survey, [{:respondents_results, filter}]) + + SurveyResults.handle_cast({:respondents_results, survey.id, filter}, nil) + assert %{files: %{respondents_results: %{created_at: date}}} = SurveyResults.files_status(survey, [{:respondents_results, filter}]) + assert date != nil + + filter = RespondentsFilter.parse("disposition:queued") + assert %{files: %{respondents_filtered: %{created_at: nil}}} = SurveyResults.files_status(survey, [{:respondents_results, filter}]) + SurveyResults.handle_cast({:respondents_results, survey.id, filter}, nil) + assert %{files: %{respondents_filtered: %{created_at: queued_date}}} = SurveyResults.files_status(survey, [{:respondents_results, filter}]) + assert queued_date != nil + + filter = RespondentsFilter.parse("mode:sms") + assert %{files: %{respondents_filtered: %{created_at: nil}}} = SurveyResults.files_status(survey, [{:respondents_results, filter}]) + SurveyResults.handle_cast({:respondents_results, survey.id, filter}, nil) + assert %{files: %{respondents_filtered: %{created_at: mode_date}}} = SurveyResults.files_status(survey, [{:respondents_results, filter}]) + assert mode_date != nil + + assert %{files: %{respondents_filtered: %{created_at: ^queued_date}}} = SurveyResults.files_status(survey, [{:respondents_results, RespondentsFilter.parse("disposition:queued")}]) + end + test "download results csv with non-started last call" do project = insert(:project) questionnaire = insert(:questionnaire, name: "test", project: project, steps: @dummy_steps)