diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb index e46078c5..27378e64 100644 --- a/app/controllers/admin/users_controller.rb +++ b/app/controllers/admin/users_controller.rb @@ -1,3 +1,5 @@ +require "csv" + module Admin class UsersController < BaseController def change_user_roles @@ -14,5 +16,47 @@ def change_user_roles render status: :unprocessable_entity, json: "roles failed to update" end end + + def export_user_info + @users = User.where(organization_id: current_organization.id) + + respond_to do |format| + format.csv { send_data users_csv(@users), filename: "#{subdomain_name}_users_#{current_date_string}.csv" } + end + end + + private + + def users_csv(users) + CSV.generate do |csv| + attributes = [] + csv << ["User Name", "User Last Name", "User Email", "User Role", "Registration Date", "Branch", "Zip Code", "Courses User has Started", "Courses User has Completed"] + + users.each do |user| + row = [] + profile = user.profile + + row << profile.try(:first_name) + row << profile.try(:last_name) + row << user.email + row << user.roles.map(&:name).map(&:capitalize).join(", ") + row << user.created_at.in_time_zone("Central Time (US & Canada)").strftime("%m-%d-%Y") + row << profile.try(:library_location).try(:name) + row << profile.try(:zip_code) + row << user.course_progresses.where(completed_at: nil).map{ |cp| cp.course.title }.join(", ") + row << user.course_progresses.where.not(completed_at: nil).map{ |cp| cp.course.title }.join(", ") + + csv << row + end + end + end + + def subdomain_name + current_organization.try(:subdomain) + end + + def current_date_string + Date.today.strftime("%m-%d-%Y") + end end end diff --git a/app/views/admin/courses/index.html.erb b/app/views/admin/courses/index.html.erb index 007ee7c0..d49c48aa 100644 --- a/app/views/admin/courses/index.html.erb +++ b/app/views/admin/courses/index.html.erb @@ -13,7 +13,6 @@ - <% (@category_ids || []).each do |category_id| %> <%= render partial: "admin/courses/sortable_list", locals: { courses: @courses.with_category(category_id), category: Category.find_by_id(category_id) } %> <% end %> diff --git a/app/views/admin/users/index.html.erb b/app/views/admin/users/index.html.erb index f02b26c9..ea48a076 100644 --- a/app/views/admin/users/index.html.erb +++ b/app/views/admin/users/index.html.erb @@ -2,12 +2,19 @@

Users

- - <% @users.each do |user| %>
diff --git a/config/deploy/staging.rb b/config/deploy/staging.rb index f20412bb..b077e2dc 100644 --- a/config/deploy/staging.rb +++ b/config/deploy/staging.rb @@ -3,7 +3,7 @@ set :bundle_without, %w{integration production test}.join(" ") -set :branch, "release-2.3.2.1" +set :branch, "release-2.4.0" server "dl-stageapp-01.do.lark-it.com", user: fetch(:application), diff --git a/config/routes.rb b/config/routes.rb index 105de4f5..4eeeb089 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -84,7 +84,9 @@ patch 'update_pub_status' end - patch 'users/:id/change_user_roles', to: 'users#change_user_roles', as: :change_user_roles + patch 'users/:id/change_user_roles', to: 'users#change_user_roles', as: :change_user_roles + + get 'users/export_user_info', to: 'users#export_user_info', as: :export_user_info resources :courses do put :sort, on: :collection diff --git a/spec/controllers/admin/users_controller_spec.rb b/spec/controllers/admin/users_controller_spec.rb index a0c20269..cfbcf403 100644 --- a/spec/controllers/admin/users_controller_spec.rb +++ b/spec/controllers/admin/users_controller_spec.rb @@ -20,4 +20,18 @@ expect(@user.current_roles).to eq("admin") end end + + describe "#export_user_info" do + before do + 4.times do + create(:user, organization: @user.organization) + end + + get :export_user_info, format: :csv + end + + it "assigns correct number of users" do + expect(assigns(:users).count).to eq(6) + end + end end