diff --git a/lib/ask/survey.ex b/lib/ask/survey.ex index 9141c1f5b..7e00d44f1 100644 --- a/lib/ask/survey.ex +++ b/lib/ask/survey.ex @@ -530,7 +530,7 @@ defmodule Ask.Survey do %{survey | down_channels: down_channels} end - def with_active_channels(provider, base_url) do + def with_active_channel(provider, base_url) do query = from s in Survey, where: s.state == :running, diff --git a/lib/ask_web/controllers/survey_controller.ex b/lib/ask_web/controllers/survey_controller.ex index 96da2a7b6..71764189e 100644 --- a/lib/ask_web/controllers/survey_controller.ex +++ b/lib/ask_web/controllers/survey_controller.ex @@ -529,8 +529,8 @@ defmodule AskWeb.SurveyController do end end - def active(conn, %{"provider" => provider, "base_url" => base_url}) do - surveys = Survey.with_active_channels(provider, base_url) + def active_channel(conn, %{"provider" => provider, "base_url" => base_url}) do + surveys = Survey.with_active_channel(provider, base_url) render(conn, "index.json", surveys: surveys) end diff --git a/lib/ask_web/router.ex b/lib/ask_web/router.ex index 9fd28171e..377adb6e6 100644 --- a/lib/ask_web/router.ex +++ b/lib/ask_web/router.ex @@ -191,7 +191,7 @@ defmodule AskWeb.Router do get "/settings", UserController, :settings, as: :settings post "/update_settings", UserController, :update_settings, as: :update_settings - get "/surveys/active_channel/:provider", SurveyController, :active, as: :active + get "/surveys/active_channel/:provider", SurveyController, :active_channel, as: :surveys_active_channel end end diff --git a/test/ask/survey_test.exs b/test/ask/survey_test.exs index 88199bcec..03799f323 100644 --- a/test/ask/survey_test.exs +++ b/test/ask/survey_test.exs @@ -117,6 +117,31 @@ defmodule Ask.SurveyTest do assert running_channels == [Enum.at(channels, 1).id] end + test "enumerates surveys with active channel" do + surveys = [ + insert(:survey, state: :ready), + insert(:survey, state: :running), + insert(:survey, state: :running), + insert(:survey, state: :running), + ] + + channels = [ + insert(:channel, provider: "sms", base_url: "test"), + insert(:channel, provider: "sms", base_url: "test"), + insert(:channel, provider: "ivr", base_url: "prod"), + insert(:channel, provider: "sms", base_url: "test"), + ] + + setup_surveys_with_channels(surveys, channels) + + active_surveys = + Survey.with_active_channel("sms", "test") + |> Enum.map(fn c -> c.id end) + |> Enum.sort() + + assert active_surveys == [Enum.at(surveys, 1).id, Enum.at(surveys, 3).id] + end + test "enumerates channels of a survey" do survey = insert(:survey) channel_1 = insert(:channel) diff --git a/test/ask_web/controllers/survey_controller_test.exs b/test/ask_web/controllers/survey_controller_test.exs index 7ab5fd4af..a91f3eb39 100644 --- a/test/ask_web/controllers/survey_controller_test.exs +++ b/test/ask_web/controllers/survey_controller_test.exs @@ -3282,6 +3282,66 @@ defmodule AskWeb.SurveyControllerTest do end end + test "surveys with active channel", %{conn: conn, user: user} do + project = create_project_for_user(user) + surveys = [ + insert(:survey, project: project, state: :not_ready), + insert(:survey, project: project, state: :running), + ] + channels = [ + insert(:channel, provider: "sms", base_url: "test"), + insert(:channel, provider: "sms", base_url: "test"), + ] + setup_surveys_with_channels(surveys, channels) + survey = Survey |> Repo.get(Enum.at(surveys, 1).id) + + result = get(conn, surveys_active_channel_path(conn, :active_channel, "sms", base_url: "test")) + + assert json_response(result, 200)["data"] == [ + %{ + "cutoff" => survey.cutoff, + "id" => survey.id, + "mode" => survey.mode, + "name" => survey.name, + "description" => nil, + "project_id" => project.id, + "state" => "running", + "locked" => false, + "exit_code" => nil, + "exit_message" => nil, + "schedule" => %{ + "blocked_days" => [], + "day_of_week" => %{ + "fri" => true, + "mon" => true, + "sat" => true, + "sun" => true, + "thu" => true, + "tue" => true, + "wed" => true + }, + "end_time" => "23:59:59", + "start_time" => "00:00:00", + "start_date" => nil, + "end_date" => nil, + "timezone" => "Etc/UTC" + }, + "next_schedule_time" => nil, + "started_at" => nil, + "ended_at" => nil, + "updated_at" => to_iso8601(survey.updated_at), + "down_channels" => [], + "folder_id" => nil, + "first_window_started_at" => nil, + "panel_survey_id" => nil, + "last_window_ends_at" => nil, + "is_deletable" => false, + "is_movable" => true, + "generates_panel_survey" => false + }, + ] + end + def prepare_for_state_update(user) do project = create_project_for_user(user) questionnaire = insert(:questionnaire, name: "test", project: project)