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 fields to report to make it human readable #1655

Merged
merged 3 commits into from
Apr 12, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
24 changes: 20 additions & 4 deletions apps/andi/lib/andi_web/controllers/reports_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,17 @@ defmodule AndiWeb.ReportsController do
defp build_csv() do
values =
get_datasets()
|> Enum.map(fn dataset -> [dataset.id, get_users_for_dataset(dataset.is_public, dataset.access_groups, dataset.org_id)] end)
|> Enum.map(fn dataset ->
[
dataset.id,
dataset.dataTitle,
dataset.orgTitle,
dataset.systemName,
get_users_for_dataset(dataset.is_public, dataset.access_groups, dataset.org_id)
]
end)

[["Dataset ID", "Users"] | values]
[["Dataset ID", "Dataset Title", "Organization", "System Name", "Users"] | values]
end

defp get_users_for_dataset(is_public, access_groups, org_id) do
Expand All @@ -39,12 +47,20 @@ defmodule AndiWeb.ReportsController do
query =
from(dataset in Dataset,
where: dataset.submission_status != :draft,
preload: [:technical, access_groups: [:users]]
preload: [:business, :technical, access_groups: [:users]]
)

Andi.Repo.all(query)
|> Enum.map(fn dataset ->
%{id: dataset.id, is_public: not dataset.technical.private, access_groups: dataset.access_groups, org_id: dataset.technical.orgId}
%{
id: dataset.id,
dataTitle: dataset.business.dataTitle,
orgTitle: dataset.business.orgTitle,
systemName: dataset.technical.systemName,
is_public: not dataset.technical.private,
access_groups: dataset.access_groups,
org_id: dataset.technical.orgId
}
end)
end

Expand Down
2 changes: 1 addition & 1 deletion apps/andi/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Andi.MixProject do
def project do
[
app: :andi,
version: "2.6.57",
version: "2.6.58",
build_path: "../../_build",
config_path: "../../config/config.exs",
deps_path: "../../deps",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,41 @@ defmodule Andi.ReportsControllerTest do
use Placebo
use AndiWeb.Test.AuthConnCase.IntegrationCase
alias Andi.InputSchemas.Datasets.Dataset
alias Andi.InputSchemas.Datasets.Business
alias Andi.InputSchemas.Datasets.Technical
alias Andi.InputSchemas.AccessGroup
alias Andi.InputSchemas.Organization
alias Andi.Schemas.User

describe "download_report" do
test "sets dataset users to public when dataset is not private", %{curator_conn: conn} do
dataset1 = %Dataset{id: "12345", technical: %Technical{private: false}}
dataset1 = %Dataset{
id: "12345",
business: %Business{dataTitle: "Example", orgTitle: "Test"},
technical: %Technical{private: false, systemName: "Test__Example"}
}

allow Andi.Repo.all(any()), seq: [[dataset1]]
result = get(conn, "/report")
assert result.status == 200
assert result.resp_body == "Dataset ID,Users\r\n12345,All (public)\r\n"

assert result.resp_body ==
"Dataset ID,Dataset Title,Organization,System Name,Users\r\n12345,Example,Test,Test__Example,All (public)\r\n"
end

test "adds users to private dataset based on the dataset's org", %{curator_conn: conn} do
dataset1 = %Dataset{id: "12345", technical: %Technical{private: true, orgId: "1122"}, access_groups: []}
dataset2 = %Dataset{id: "6789", technical: %Technical{private: false}}
dataset1 = %Dataset{
id: "12345",
business: %Business{dataTitle: "Example", orgTitle: "Test"},
technical: %Technical{private: true, orgId: "1122", systemName: "Test__Example"},
access_groups: []
}

dataset2 = %Dataset{
id: "6789",
business: %Business{dataTitle: "Example2", orgTitle: "Test2"},
technical: %Technical{private: false, systemName: "Test2__Example2"}
}

user1 = %User{
subject_id: UUID.uuid4(),
Expand All @@ -40,7 +57,9 @@ defmodule Andi.ReportsControllerTest do
allow Andi.Repo.all(any()), seq: [[dataset1, dataset2], [org]]
result = get(conn, "/report")
assert result.status == 200
assert result.resp_body == "Dataset ID,Users\r\n12345,\"[email protected], [email protected]\"\r\n6789,All (public)\r\n"

assert result.resp_body ==
"Dataset ID,Dataset Title,Organization,System Name,Users\r\n12345,Example,Test,Test__Example,\"[email protected], [email protected]\"\r\n6789,Example2,Test2,Test2__Example2,All (public)\r\n"
end

test "adds users to private dataset based on the dataset's org and access groups", %{curator_conn: conn} do
Expand All @@ -64,16 +83,28 @@ defmodule Andi.ReportsControllerTest do

access_group1 = %AccessGroup{users: [user1]}
access_group2 = %AccessGroup{users: [user2]}
dataset1 = %Dataset{id: "12345", technical: %Technical{private: true, orgId: "1122"}, access_groups: [access_group1, access_group2]}
dataset2 = %Dataset{id: "6789", technical: %Technical{private: false}}

dataset1 = %Dataset{
id: "12345",
business: %Business{dataTitle: "Example", orgTitle: "Test"},
technical: %Technical{private: true, orgId: "1122", systemName: "Test__Example"},
access_groups: [access_group1, access_group2]
}

dataset2 = %Dataset{
id: "6789",
business: %Business{dataTitle: "Example2", orgTitle: "Test2"},
technical: %Technical{private: false, systemName: "Test2__Example2"}
}

org = %Organization{id: "1122", users: [user3]}

allow Andi.Repo.all(any()), seq: [[dataset1, dataset2], [org]]
result = get(conn, "/report")
assert result.status == 200

assert result.resp_body ==
"Dataset ID,Users\r\n12345,\"[email protected], [email protected], [email protected]\"\r\n6789,All (public)\r\n"
"Dataset ID,Dataset Title,Organization,System Name,Users\r\n12345,Example,Test,Test__Example,\"[email protected], [email protected], [email protected]\"\r\n6789,Example2,Test2,Test2__Example2,All (public)\r\n"
end

test "filters duplicates", %{curator_conn: conn} do
Expand All @@ -85,14 +116,28 @@ defmodule Andi.ReportsControllerTest do

access_group1 = %AccessGroup{users: [user1]}
access_group2 = %AccessGroup{users: [user1]}
dataset1 = %Dataset{id: "12345", technical: %Technical{private: true, orgId: "1122"}, access_groups: [access_group1, access_group2]}
dataset2 = %Dataset{id: "6789", technical: %Technical{private: false}}

dataset1 = %Dataset{
id: "12345",
business: %Business{dataTitle: "Example", orgTitle: "Test"},
technical: %Technical{private: true, orgId: "1122", systemName: "Test__Example"},
access_groups: [access_group1, access_group2]
}

dataset2 = %Dataset{
id: "6789",
business: %Business{dataTitle: "Example2", orgTitle: "Test2"},
technical: %Technical{private: false, systemName: "Test2__Example2"}
}

org = %Organization{id: "1122", users: [user1]}

allow Andi.Repo.all(any()), seq: [[dataset1, dataset2], [org]]
result = get(conn, "/report")
assert result.status == 200
assert result.resp_body == "Dataset ID,Users\r\n12345,[email protected]\r\n6789,All (public)\r\n"

assert result.resp_body ==
"Dataset ID,Dataset Title,Organization,System Name,Users\r\n12345,Example,Test,Test__Example,[email protected]\r\n6789,Example2,Test2,Test2__Example2,All (public)\r\n"
end
end
end