Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add active surveys to the warning message when deleting a channel #2374

Merged
merged 9 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion assets/js/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ export const newWave = (projectId, panelSurveyId) => {

export const fetchActiveSurveys = (provider, baseUrl) => {
return apiFetchJSON(
`surveys/active_channel/${provider}?base_url=${encodeURIComponent(baseUrl)}`,
`surveys/active_channels/${provider}?base_url=${encodeURIComponent(baseUrl)}`,
)
}

Expand Down
10 changes: 1 addition & 9 deletions assets/js/components/channels/ChannelIndex.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,6 @@ class ChannelIndex extends Component<any, State> {
modalIndex: index,
})
})
.catch(() => {
this.setState({
modalLoading: false,
modalSurveys: [],
modalProvider: provider,
modalIndex: index,
})
})
} else {
this.props.authActions.toggleAuthorization(provider, index)
}
Expand Down Expand Up @@ -138,7 +130,7 @@ class ChannelIndex extends Component<any, State> {
modalSurveys,
modalProvider,
modalIndex,
} = this.state;
} = this.state

if (!channels) {
return (
Expand Down
4 changes: 2 additions & 2 deletions assets/js/components/channels/ProviderModal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const ProviderModal = ({
{loading ? <span>{t("Searching active surveys...")}</span> :
surveys.length == 0 ? <span>{t("No active surveys")}</span> :
<div>
<span>{t("Active surveys")}</span>
<span>{t("These surveys are active, using channels from this provider. Deleting the channels will interrupt the surveys.")}</span>
<ul>
{surveys.map((survey) => (
<li key={`survey-${survey.id}`}>
Expand All @@ -54,7 +54,7 @@ ProviderModal.propTypes = {
multiple: PropTypes.bool,
onConfirm: PropTypes.func,
loading: PropTypes.bool,
surveys: PropTypes.any
surveys: PropTypes.any,
}

export default translate()(ProviderModal)
11 changes: 9 additions & 2 deletions lib/ask/survey.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ defmodule Ask.Survey do
RespondentStats,
ConfigHelper,
SystemTime,
PanelSurvey
PanelSurvey,
ProjectMembership
}

alias Ask.Ecto.Type.JSON
Expand Down Expand Up @@ -530,10 +531,16 @@ defmodule Ask.Survey do
%{survey | down_channels: down_channels}
end

def with_active_channel(provider, base_url) do
def with_active_channels(user_id, provider, base_url) do
query =
from s in Survey,
where: s.state == :running,
join: pm in ProjectMembership,
on: pm.project_id == s.project_id and pm.user_id == ^user_id,
select: s

query =
from s in subquery(query),
ismaelbej marked this conversation as resolved.
Show resolved Hide resolved
join: group in RespondentGroup,
on: s.id == group.survey_id,
join: rgc in RespondentGroupChannel,
Expand Down
4 changes: 2 additions & 2 deletions lib/ask_web/controllers/survey_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -529,8 +529,8 @@ defmodule AskWeb.SurveyController do
end
end

def active_channel(conn, %{"provider" => provider, "base_url" => base_url}) do
surveys = Survey.with_active_channel(provider, base_url)
def active_channels(conn, %{"provider" => provider, "base_url" => base_url}) do
surveys = Survey.with_active_channels(current_user(conn).id, provider, base_url)

render(conn, "index.json", surveys: surveys)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/ask_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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_channel, as: :surveys_active_channel
get "/surveys/active_channels/:provider", SurveyController, :active_channels, as: :surveys_active_channels
end
end

Expand Down
2 changes: 1 addition & 1 deletion locales/template/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
"Accepts refusals": "",
"Action": "",
"Active": "",
"Active surveys": "",
"Activity": "",
"Actual success rate": "",
"Actual success rate value throughout the survey's life": "",
Expand Down Expand Up @@ -478,6 +477,7 @@
"The schedule of your survey restricts the days and hours during which respondents will be contacted. You can also specify re-contact attempts intervals.": "",
"The selected questionnaire will be sent over the survey channels to every respondent until a cutoff rule is reached. If you wish, you can try an experiment to compare questionnaires performance.": "",
"The system only accepts CSV files": "",
"These surveys are active, using channels from this provider. Deleting the channels will interrupt the surveys.": "",
"This is a wave of a panel survey. The settings from previous waves will be used as a template for this wave. Any changes made to this wave's settings will serve as a template for future waves of this panel survey": "",
"This question is not relevant for partial flag": "",
"This question is relevant for partial flag": "",
Expand Down
33 changes: 23 additions & 10 deletions test/ask/survey_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -118,28 +118,41 @@ defmodule Ask.SurveyTest do
end

test "enumerates surveys with active channel" do
user = insert(:user)
project = create_project_for_user(user)

user2 = insert(:user)
project2 = create_project_for_user(user2)

surveys = [
insert(:survey, state: :ready),
insert(:survey, state: :running),
insert(:survey, state: :running),
insert(:survey, state: :running),
insert(:survey, state: :ready, project: project),
insert(:survey, state: :running, project: project),
insert(:survey, state: :running, project: project2),
insert(:survey, state: :running, project: project2),
]

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"),
insert(:channel, provider: "sms", base_url: "test", projects: [project]),
insert(:channel, provider: "sms", base_url: "test", projects: [project]),
insert(:channel, provider: "ivr", base_url: "prod", projects: [project2]),
insert(:channel, provider: "sms", base_url: "test", projects: [project2]),
]

setup_surveys_with_channels(surveys, channels)

active_surveys =
Survey.with_active_channel("sms", "test")
Survey.with_active_channels(user.id, "sms", "test")
|> Enum.map(fn c -> c.id end)
|> Enum.sort()

assert active_surveys == [Enum.at(surveys, 1).id]

active_surveys2 =
Survey.with_active_channels(user2.id, "sms", "test")
|> Enum.map(fn c -> c.id end)
|> Enum.sort()

assert active_surveys == [Enum.at(surveys, 1).id, Enum.at(surveys, 3).id]
assert active_surveys2 == [Enum.at(surveys, 3).id]
end

test "enumerates channels of a survey" do
Expand Down
2 changes: 1 addition & 1 deletion test/ask_web/controllers/survey_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3295,7 +3295,7 @@ defmodule AskWeb.SurveyControllerTest do
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"))
result = get(conn, surveys_active_channels_path(conn, :active_channels, "sms", base_url: "test"))

assert json_response(result, 200)["data"] == [
%{
Expand Down