Skip to content

Commit 4fbc359

Browse files
committed
Add reporting and tests
1 parent 96eae2a commit 4fbc359

6 files changed

Lines changed: 82 additions & 3 deletions

File tree

app/controllers/complaints_controller.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ class ComplaintsController < ApplicationController
22
before_action :set_complaint, only: [:show, :comment, :self_assign, :update_status]
33
before_action :access_check, only: [:show, :comment]
44
before_action :write_access_check, only: [:self_assign, :update_status]
5-
before_action :verify_staff, only: [:reports]
5+
before_action :verify_staff, only: [:reports, :reporting]
66

77
def index
88
render layout: 'without_sidebar'
@@ -150,6 +150,17 @@ def update_status
150150
redirect_to complaint_path(@complaint.access_token)
151151
end
152152

153+
def reporting
154+
@total = Complaint.recent(12.months.ago).count
155+
@by_type = Complaint.recent(12.months.ago).group(:report_type).group_by_month(:created_at).count
156+
@by_content_type = Complaint.recent(12.months.ago).where(report_type: 'illegal')
157+
.group(:content_type).group_by_month(:created_at).count
158+
@by_outcome = Complaint.recent(12.months.ago).where.not(outcome: nil)
159+
.group(:outcome).group_by_month(:created_at).count
160+
@type_totals = Complaint.recent(12.months.ago).group(:report_type).count
161+
render layout: 'without_sidebar'
162+
end
163+
153164
private
154165

155166
def access_check

app/models/complaint.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
class Complaint < ApplicationRecord
2+
include Timestamped
3+
24
belongs_to :user, required: false
35
belongs_to :assignee, required: false, class_name: 'User'
46
has_many :comments, class_name: 'ComplaintComment', dependent: :destroy

app/views/complaints/index.html.erb

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
</p>
66

77
<div class="grid">
8-
<div class="grid--cell is-12 is-6-md is-6-lg">
8+
<div class="grid--cell is-12 is-6-lg">
99
<div class="widget">
1010
<div class="widget--body">
1111
<h2 class="has-font-size-larger h-m-t-0">
@@ -23,7 +23,7 @@
2323

2424
<% if user_signed_in? && current_user.staff? %>
2525
<div class="grid">
26-
<div class="grid--cell is-12 is-6-md is-6-lg">
26+
<div class="grid--cell is-12 is-6-lg">
2727
<div class="widget">
2828
<div class="widget--body">
2929
<h2 class="has-font-size-larger h-m-t-0">
@@ -37,5 +37,19 @@
3737
</div>
3838
</div>
3939
</div>
40+
<div class="grid--cell is-12 is-6-lg">
41+
<div class="widget">
42+
<div class="widget--body">
43+
<h2 class="has-font-size-larger h-m-t-0">
44+
<%= link_to complaints_reporting_path do %>
45+
Reporting &raquo;
46+
<% end %>
47+
</h2>
48+
<p class="has-font-size-caption">
49+
Charts and statistics reporting about user complaints.
50+
</p>
51+
</div>
52+
</div>
53+
</div>
4054
</div>
4155
<% end %>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<%= link_to safety_center_path do %>
2+
&laquo; Back to Safety Center
3+
<% end %>
4+
5+
<h1>Reporting</h1>
6+
7+
<div class="flex-row">
8+
<%= stat_panel 'total reports', @total %>
9+
</div>
10+
11+
<div class="grid">
12+
<div class="grid--cell is-12 is-6-lg">
13+
<h2 class="has-font-size-base has-text-align-center">Report types by month, last 12 months</h2>
14+
<%= line_chart @by_type %>
15+
</div>
16+
<div class="grid--cell is-12 is-6-lg">
17+
<h2 class="has-font-size-base has-text-align-center">Content types by month, last 12 months</h2>
18+
<%= line_chart @by_content_type %>
19+
</div>
20+
<div class="grid--cell is-12 is-6-lg">
21+
<h2 class="has-font-size-base has-text-align-center">Outcomes by month, last 12 months</h2>
22+
<%= line_chart @by_outcome %>
23+
</div>
24+
<div class="grid--cell is-12 is-6-lg">
25+
<h2 class="has-font-size-base has-text-align-center">Totals by report type, last 12 months</h2>
26+
<%= column_chart @type_totals %>
27+
</div>
28+
</div>

config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,7 @@
383383
post 'report/:token/assign', to: 'complaints#self_assign', as: :complaint_self_assign
384384
post 'report/:token/status', to: 'complaints#update_status', as: :update_complaint_status
385385
get 'reports', to: 'complaints#reports', as: :complaints
386+
get 'reporting', to: 'complaints#reporting', as: :complaints_reporting
386387
end
387388

388389
get '403', to: 'errors#forbidden'

test/controllers/complaints_controller_test.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,25 @@ class ComplaintsControllerTest < ActionDispatch::IntegrationTest
288288
assert_response(:not_found)
289289
end
290290

291+
test 'reporting should work for staff' do
292+
sign_in users(:staff)
293+
try_reporting
294+
assert_response(:success)
295+
assert_not_nil assigns(:by_type)
296+
assert_not_nil assigns(:by_content_type)
297+
assert_not_nil assigns(:by_outcome)
298+
end
299+
300+
test 'reporting should not be accessible to anonymous' do
301+
try_reporting
302+
assert_response(:not_found)
303+
end
304+
305+
test 'reporting should not be accessible to basic user' do
306+
sign_in users(:basic_user)
307+
try_reporting
308+
end
309+
291310
private
292311

293312
def try_create_report(**params)
@@ -316,4 +335,8 @@ def try_update_status(complaint_sym, status, outcome: nil)
316335
def try_reports(status: nil, report_type: nil, outcome: nil)
317336
get complaints_path, params: { status: status, report_type: report_type, outcome: outcome }
318337
end
338+
339+
def try_reporting
340+
get complaints_reporting_path
341+
end
319342
end

0 commit comments

Comments
 (0)