Skip to content

Commit

Permalink
Merge pull request #2746 from bitzesty/csv-report
Browse files Browse the repository at this point in the history
Adds new CSV report for user data
  • Loading branch information
TheDancingClown authored Mar 21, 2024
2 parents 62a509e + d0eb982 commit 2218230
Show file tree
Hide file tree
Showing 15 changed files with 222 additions and 5 deletions.
13 changes: 13 additions & 0 deletions app/controllers/admin/users_reports_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Admin::UsersReportsController < Admin::BaseController
def assessors_judges_admins_data
authorize :reports, :show?

@report = Reports::Admin::AssessorJudgeAdminDataReport.new()

respond_to do |format|
format.csv do
send_data @report.as_csv, filename: @report.csv_filename
end
end
end
end
1 change: 1 addition & 0 deletions app/mailers/application_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def set_reception_deadlines
@reception_deadline = deadlines.where(kind: "buckingham_palace_reception_attendee_information_due_by").first
@reception_deadline_time = formatted_deadline_time(@reception_deadline)
@reception_date = deadlines.where(kind: "buckingham_palace_attendees_invite").first
@reception_start_time = formatted_deadline_time(@reception_date)
end

def deadlines
Expand Down
8 changes: 7 additions & 1 deletion app/models/assessor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def suspended?

def inactive_message
!suspended? ? super : :suspended
end
end

def self.roles
[["Not Assigned", nil], ["Lead Assessor", "lead"], ["Assessor", "regular"]]
Expand Down Expand Up @@ -149,6 +149,12 @@ def lead_roles
end
end

def roles
FormAnswer::POSSIBLE_AWARDS.select do |cat|
get_role(cat) == ("lead" || "regular")
end
end

def categories_as_lead
categories.select { |_, v| v == "lead" }.keys
end
Expand Down
16 changes: 16 additions & 0 deletions app/models/judge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ def full_name
"#{first_name} #{last_name}".strip
end

def self.role_meth(category)
"#{category}_role"
end

def self.roles
[["Not Assigned", nil], ["Assigned", "judge"]]
end
Expand All @@ -59,4 +63,16 @@ def timeout_in
def soft_delete!
update_column(:deleted, true)
end

def roles
FormAnswer::POSSIBLE_AWARDS.select do |cat|
get_role(cat) == "judge"
end
end

private

def get_role(category)
public_send self.class.role_meth(category)
end
end
83 changes: 83 additions & 0 deletions app/models/reports/admin/assessor_judge_admin_data_report.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
class Reports::Admin::AssessorJudgeAdminDataReport
include Reports::CsvHelper

MAPPING = [
{
label: "ID",
method: :id
}, {
label: "First name",
method: :first_name
}, {
label: "Last name",
method: :last_name
}, {
label: "Company",
method: :company
}, {
label: "Email",
method: :email
}, {
label: "Telephone",
method: :telephone_number
}, {
label: "User type",
method: :user_type
}, {
label: "User creation date",
method: :created_at
}, {
label: "Last sign in date",
method: :last_sign_in_at
}, {
label: "Awards assigned",
method: :awards_assigned
}
]

def as_csv
CSV.generate(encoding: "UTF-8", force_quotes: true) do |csv|
csv << headers

Admin.find_each do |user|
u = Reports::User.new(user)

csv << mapping.map do |m|
sanitize_string(
u.call_method(m[:method])
)
end
end

Assessor.find_each do |user|
u = Reports::User.new(user)

csv << mapping.map do |m|
sanitize_string(
u.call_method(m[:method])
)
end
end

Judge.find_each do |user|
u = Reports::User.new(user)

csv << mapping.map do |m|
sanitize_string(
u.call_method(m[:method])
)
end
end
end
end

def csv_filename
"aassessors_judges_admins_data.csv"
end

private

def mapping
MAPPING
end
end
32 changes: 32 additions & 0 deletions app/models/reports/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class Reports::User

def initialize(user)
@user = user
end

def call_method(methodname)
return "not implemented" if methodname.blank?

if respond_to?(methodname, true)
send(methodname)
elsif @user.respond_to?(methodname)
@user.send(methodname)
else
"N/A"
end
end

private

def user_type
@user.class.name
end

def awards_assigned
if @user.is_a?(Assessor) || @user.is_a?(Judge)
@user.roles.reject{|e| e=~ /promotion/}.join(", ")
else
"N/A"
end
end
end
8 changes: 8 additions & 0 deletions app/renderers/mail_renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,10 @@ def winners_notification
"%-d %B %Y"
)

assigns[:reception_start_time] = deadline_time(
"buckingham_palace_attendees_invite"
)

render(assigns, "account_mailers/business_apps_winners_mailer/preview/notify")
end

Expand Down Expand Up @@ -255,6 +259,10 @@ def winners_head_of_organisation_notification
"%-d %B %Y"
)

assigns[:reception_start_time] = deadline_time(
"buckingham_palace_attendees_invite"
)

render(assigns, "users/winners_head_of_organisation_mailer/preview/notify")
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ On your online account, you will also have access to The King's Awards Recipient

By accepting the Award, you agree to the terms set out in The King's Awards Recipients Manual.

#4. Reception at Windsor Castle on <%= @reception_date.try :strftime, "%-d %B %Y" %>
#4. Reception at Windsor Castle on <%= @reception_date.try :strftime, "%-d %B %Y" %> at <%= "#{@reception_start_time}" %>

Please note the date and venue. One representative from each successful organisation will be invited to attend a Reception at Windsor Castle hosted by His Majesty The King. Please note that, unfortunately, this number cannot be increased. We will contact you in due course, asking you to nominate one representative to attend the event.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ p style="font-size: 19px; line-height: 1.315789474; margin: 0 0 30px 0;"
p style="font-size: 19px; line-height: 1.315789474; margin: 30px 0 30px 0;"
strong
| 4. Reception at Windsor Castle on
=< @reception_date
=<> @reception_date
| at
=< @reception_start_time

p style="font-size: 19px; line-height: 1.315789474; margin: 30px 0 30px 0;"
| Please note the date and venue. One representative from each successful organisation will be invited to attend a Reception at Windsor Castle hosted by His Majesty The King. Please note that, unfortunately, this number cannot be increased. We will contact you in due course, asking you to nominate one representative to attend the event.
Expand Down
5 changes: 5 additions & 0 deletions app/views/admin/users/index.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
.dashboard
h1.dashboard__heading
| Users

p
= link_to "Download CSV with assessors, judges and admin's user data", assessors_judges_admins_data_admin_users_reports_path(format: :csv),
class: "download-link govuk-link", aria: { label: "Download CSV with assessors, judges and admin's user data" }

= simple_form_for @search, url: "#", method: :get, as: :search, html: { class: "search-form", aria: { label: "Search users" } } do |f|
= render 'admin/users/navigation'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ A link to The King's Awards Emblems and Recipients Manual is located on your acc
* the terms of using your Award and displaying the Royal Emblem, which you agree to by accepting the Award;
* the presentation by His Majesty's Lord-Lieutenant, the Grant of Appointment and Trophy.

#4. Reception at Windsor Castle on <%= @reception_date.try :strftime, "%-d %B %Y" %>
#4. Reception at Windsor Castle on <%= @reception_date.try :strftime, "%-d %B %Y" %> at <%= "#{@reception_start_time}" %>

This year, one employed representative from each successful organisation will be invited to attend a Reception at Windsor Castle hosted by His Majesty The King. Unfortunately, this number cannot be increased.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ div style="font-size: 19px; line-height: 1.315789474; margin: 30px 0 30px 0;"
p style="font-size: 19px; line-height: 1.315789474; margin: 30px 0 30px 0;"
strong
| 4. Reception at Windsor Castle on
=< @reception_date
=<> @reception_date
| at
=< @reception_start_time

p style="font-size: 19px; line-height: 1.315789474; margin: 30px 0 30px 0;"
| This year, one employed representative from each successful organisation will be invited to attend a Reception at Windsor Castle hosted by His Majesty The King. Unfortunately, this number cannot be increased.
Expand Down
6 changes: 6 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,12 @@
end
end

resources :users_reports, only: [] do
collection do
get :assessors_judges_admins_data
end
end

resources :users, except: [:destroy] do
member do
patch :resend_confirmation_email
Expand Down
1 change: 1 addition & 0 deletions spec/factories/assessors_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
last_name { "Doe" }
password { "my98ssdkjv9823kds=2" }
email
telephone_number { generate(:phone) }
confirmed_at { Time.zone.now }

trait :lead_for_all do
Expand Down
42 changes: 42 additions & 0 deletions spec/models/reports/admin/assessor_judge_admin_data_report_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require "rails_helper"

describe Reports::Admin::AssessorJudgeAdminDataReport do
let! (:admin) { create(:admin) }
let! (:assessor) { create(:assessor, :lead_for_trade) }
let! (:judge) { create(:judge, :innovation) }

let(:output) { described_class.new().as_csv }
it "creates the output with relevant data for Admins" do
expect(output).to include(admin.id.to_s)
expect(output).to include(admin.first_name)
expect(output).to include(admin.last_name)
expect(output).to include(admin.email)
expect(output).to include("Admin")
expect(output).to include(admin.created_at.to_s)
expect(output).to include(admin.last_sign_in_at.to_s)
end

it "creates the output with relevant data for Assessors" do
expect(output).to include(assessor.id.to_s)
expect(output).to include(assessor.first_name)
expect(output).to include(assessor.last_name)
expect(output).to include(assessor.email)
expect(output).to include(assessor.telephone_number.to_s)
expect(output).to include(assessor.company.to_s)
expect(output).to include("Assessor")
expect(output).to include(assessor.created_at.to_s)
expect(output).to include(assessor.last_sign_in_at.to_s)
expect(output).to include("trade")
end

it "creates the output with relevant data for Judges" do
expect(output).to include(judge.id.to_s)
expect(output).to include(judge.first_name)
expect(output).to include(judge.last_name)
expect(output).to include(judge.email)
expect(output).to include("Judge")
expect(output).to include(judge.created_at.to_s)
expect(output).to include(judge.last_sign_in_at.to_s)
expect(output).to include("innovation")
end
end

0 comments on commit 2218230

Please sign in to comment.