Skip to content

Commit

Permalink
Control user access to surveys
Browse files Browse the repository at this point in the history
  • Loading branch information
ismaelbej committed Nov 27, 2024
1 parent cf538cf commit f21e0b3
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 37 deletions.
16 changes: 0 additions & 16 deletions assets/js/components/channels/ChannelIndex.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ type State = {
modalSurveys: Array<Object>,
modalProvider: ?string,
modalIndex: ?number,
modalError: ?Object,
}

class ChannelIndex extends Component<any, State> {
Expand All @@ -42,7 +41,6 @@ class ChannelIndex extends Component<any, State> {
modalSurveys: [],
modalProvider: null,
modalIndex: null,
modalError: null,
}
}

Expand All @@ -64,7 +62,6 @@ class ChannelIndex extends Component<any, State> {
modalSurveys: [],
modalProvider: provider,
modalIndex: index,
modalError: null,
})
const { baseUrl } = config[provider][index]
api.fetchActiveSurveys(provider, baseUrl)
Expand All @@ -75,17 +72,6 @@ class ChannelIndex extends Component<any, State> {
modalSurveys: surveys,
modalProvider: provider,
modalIndex: index,
modalError: null,
})
})
.catch((error) => {
console.log(error)
this.setState({
modalLoading: false,
modalSurveys: [],
modalProvider: provider,
modalIndex: index,
modalError: error,
})
})
} else {
Expand Down Expand Up @@ -185,7 +171,6 @@ class ChannelIndex extends Component<any, State> {
const providerModal = (provider, index, friendlyName, multiple) => {
const loading = provider === modalProvider && index === modalIndex ? modalLoading : false
const surveys = provider === modalProvider && index === modalIndex ? modalSurveys : []
const error = provider === modalProvider && index === modalIndex ? modalError : null
return (
<ProviderModal
key={`${provider}-${index}`}
Expand All @@ -196,7 +181,6 @@ class ChannelIndex extends Component<any, State> {
onConfirm={() => this.deleteProvider(provider, index)}
loading={loading}
surveys={surveys}
error={error}
/>
)
}
Expand Down
3 changes: 0 additions & 3 deletions assets/js/components/channels/ProviderModal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export const ProviderModal = ({
onConfirm,
loading,
surveys,
error,
}) => {
let name = `${provider[0].toUpperCase()}${provider.slice(1)}`
if (multiple) name = `${name} (${friendlyName})`
Expand All @@ -30,7 +29,6 @@ export const ProviderModal = ({

<div className="provider-surveys">
{loading ? <span>{t("Searching active surveys...")}</span> :
error ? <span className="provider-error">{t("Error searching active surveys...")}</span> :
surveys.length == 0 ? <span>{t("No active surveys")}</span> :
<div>
<span>{t("These surveys are active, using channels from this provider. Deleting the channels will interrupt the surveys.")}</span>
Expand All @@ -57,7 +55,6 @@ ProviderModal.propTypes = {
onConfirm: PropTypes.func,
loading: PropTypes.bool,
surveys: PropTypes.any,
error: PropTypes.any,
}

export default translate()(ProviderModal)
4 changes: 0 additions & 4 deletions assets/vendor/css/materialize/components/_global.scss
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,3 @@ td, th{
}
}
}

.provider-error {
color: $input-error-color;
}
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_channels(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),
join: group in RespondentGroup,
on: s.id == group.survey_id,
join: rgc in RespondentGroupChannel,
Expand Down
2 changes: 1 addition & 1 deletion lib/ask_web/controllers/survey_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ defmodule AskWeb.SurveyController do
end

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

render(conn, "index.json", surveys: surveys)
end
Expand Down
1 change: 0 additions & 1 deletion locales/template/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@
"Error ID:": "",
"Error details": "",
"Error message": "",
"Error searching active surveys...": "",
"Error: CSV doesn't have a header for the primary language": "",
"Error: CSV is empty": "",
"Error: primary language name not found for code": "",
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_channels("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

0 comments on commit f21e0b3

Please sign in to comment.