Browse Source

CLDC-1386: Organisation specific csv download (#793)

* Organisation specific log CSV download

* Organisation specific user CSV download
pull/795/head
baarkerlounger 2 years ago committed by GitHub
parent
commit
5c2bfd46ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 42
      app/controllers/organisations_controller.rb
  2. 2
      app/views/case_logs/_log_list.html.erb
  3. 2
      app/views/users/_user_list.html.erb
  4. 61
      spec/requests/organisations_controller_spec.rb

42
app/controllers/organisations_controller.rb

@ -29,13 +29,24 @@ class OrganisationsController < ApplicationController
end end
def users def users
@pagy, @users = pagy(filtered_users(@organisation.users.sorted_by_organisation_and_role, search_term)) organisation_users = @organisation.users.sorted_by_organisation_and_role
@searched = search_term.presence unpaginated_filtered_users = filtered_collection(organisation_users, search_term)
@total_count = @organisation.users.size
if current_user.support? respond_to do |format|
render "users", layout: "application" format.html do
else @pagy, @users = pagy(unpaginated_filtered_users)
render "users/index" @searched = search_term.presence
@total_count = @organisation.users.size
if current_user.support?
render "users", layout: "application"
else
render "users/index"
end
end
format.csv do
send_data unpaginated_filtered_users.to_csv, filename: "users-#{@organisation.name}-#{Time.zone.now}.csv"
end
end end
end end
@ -83,11 +94,18 @@ class OrganisationsController < ApplicationController
organisation_logs = CaseLog.all.where(owning_organisation_id: @organisation.id) organisation_logs = CaseLog.all.where(owning_organisation_id: @organisation.id)
unpaginated_filtered_logs = filtered_case_logs(filtered_collection(organisation_logs, search_term)) unpaginated_filtered_logs = filtered_case_logs(filtered_collection(organisation_logs, search_term))
@pagy, @case_logs = pagy(unpaginated_filtered_logs) respond_to do |format|
@searched = search_term.presence format.html do
@total_count = organisation_logs.size @pagy, @case_logs = pagy(unpaginated_filtered_logs)
@searched = search_term.presence
render "logs", layout: "application" @total_count = organisation_logs.size
render "logs", layout: "application"
end
format.csv do
send_data unpaginated_filtered_logs.to_csv, filename: "logs-#{@organisation.name}-#{Time.zone.now}.csv"
end
end
else else
redirect_to(case_logs_path) redirect_to(case_logs_path)
end end

2
app/views/case_logs/_log_list.html.erb

@ -2,7 +2,7 @@
<%= govuk_table do |table| %> <%= govuk_table do |table| %>
<%= table.caption(classes: %w[govuk-!-font-size-19 govuk-!-font-weight-regular], id: title.dasherize) do |caption| %> <%= table.caption(classes: %w[govuk-!-font-size-19 govuk-!-font-weight-regular], id: title.dasherize) do |caption| %>
<%= render(SearchResultCaptionComponent.new(searched:, count: pagy.count, item_label:, total_count:, item: "logs", path: request.path)) %> <%= render(SearchResultCaptionComponent.new(searched:, count: pagy.count, item_label:, total_count:, item: "logs", path: request.path)) %>
<%= govuk_link_to "Download (CSV)", "/logs.csv", type: "text/csv" %> <%= govuk_link_to "Download (CSV)", "#{request.path}.csv", type: "text/csv" %>
<% end %> <% end %>
<%= table.head do |head| %> <%= table.head do |head| %>
<%= head.row do |row| %> <%= head.row do |row| %>

2
app/views/users/_user_list.html.erb

@ -4,7 +4,7 @@
<%= render(SearchResultCaptionComponent.new(searched:, count: pagy.count, item_label:, total_count:, item: "users", path: request.path)) %> <%= render(SearchResultCaptionComponent.new(searched:, count: pagy.count, item_label:, total_count:, item: "users", path: request.path)) %>
<% if current_user.support? %> <% if current_user.support? %>
<% query = searched.present? ? "?search=#{searched}" : nil %> <% query = searched.present? ? "?search=#{searched}" : nil %>
<%= govuk_link_to "Download (CSV)", "/users.csv#{query}", type: "text/csv" %> <%= govuk_link_to "Download (CSV)", "#{request.path}.csv#{query}", type: "text/csv" %>
<% end %> <% end %>
<% end %> <% end %>
<%= table.head do |head| %> <%= table.head do |head| %>

61
spec/requests/organisations_controller_spec.rb

@ -1020,4 +1020,65 @@ RSpec.describe OrganisationsController, type: :request do
end end
end end
end end
context "when the user is a support user" do
let(:user) { FactoryBot.create(:user, :support) }
before do
allow(user).to receive(:need_two_factor_authentication?).and_return(false)
sign_in user
end
context "when they view the logs tab" do
before do
get "/organisations/#{organisation.id}/logs"
end
it "has a CSV download button with the correct path" do
expect(page).to have_link("Download (CSV)", href: "/organisations/#{organisation.id}/logs.csv")
end
context "when you download the CSV" do
let(:headers) { { "Accept" => "text/csv" } }
let(:other_organisation) { FactoryBot.create(:organisation) }
before do
FactoryBot.create_list(:case_log, 3, owning_organisation: organisation)
FactoryBot.create_list(:case_log, 2, owning_organisation: other_organisation)
end
it "only includes logs from that organisation" do
get "/organisations/#{organisation.id}/logs", headers:, params: {}
csv = CSV.parse(response.body)
expect(csv.count).to eq(4)
end
end
end
context "when they view the users tab" do
before do
get "/organisations/#{organisation.id}/users"
end
it "has a CSV download button with the correct path" do
expect(page).to have_link("Download (CSV)", href: "/organisations/#{organisation.id}/users.csv")
end
context "when you download the CSV" do
let(:headers) { { "Accept" => "text/csv" } }
let(:other_organisation) { FactoryBot.create(:organisation) }
before do
FactoryBot.create_list(:user, 3, organisation:)
FactoryBot.create_list(:user, 2, organisation: other_organisation)
end
it "only includes users from that organisation" do
get "/organisations/#{other_organisation.id}/users", headers:, params: {}
csv = CSV.parse(response.body)
expect(csv.count).to eq(3)
end
end
end
end
end end

Loading…
Cancel
Save