You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
2.2 KiB

  1. # frozen_string_literal: true
  2. module Admin
  3. class ReportsController < BaseController
  4. before_action :set_report, except: [:index]
  5. def index
  6. authorize :report, :index?
  7. @reports = filtered_reports.page(params[:page])
  8. end
  9. def show
  10. authorize @report, :show?
  11. @report_note = @report.notes.new
  12. @report_notes = (@report.notes.latest + @report.history).sort_by(&:created_at)
  13. @form = Form::StatusBatch.new
  14. end
  15. def update
  16. authorize @report, :update?
  17. process_report
  18. if @report.action_taken?
  19. redirect_to admin_reports_path, notice: I18n.t('admin.reports.resolved_msg')
  20. else
  21. redirect_to admin_report_path(@report)
  22. end
  23. end
  24. private
  25. def process_report
  26. case params[:outcome].to_s
  27. when 'assign_to_self'
  28. @report.update!(assigned_account_id: current_account.id)
  29. log_action :assigned_to_self, @report
  30. when 'unassign'
  31. @report.update!(assigned_account_id: nil)
  32. log_action :unassigned, @report
  33. when 'reopen'
  34. @report.unresolve!
  35. log_action :reopen, @report
  36. when 'resolve'
  37. @report.resolve!(current_account)
  38. log_action :resolve, @report
  39. when 'silence'
  40. @report.resolve!(current_account)
  41. @report.target_account.update!(silenced: true)
  42. log_action :resolve, @report
  43. log_action :silence, @report.target_account
  44. resolve_all_target_account_reports
  45. else
  46. raise ActiveRecord::RecordNotFound
  47. end
  48. @report.reload
  49. end
  50. def resolve_all_target_account_reports
  51. unresolved_reports_for_target_account.update_all(action_taken: true, action_taken_by_account_id: current_account.id)
  52. end
  53. def unresolved_reports_for_target_account
  54. Report.where(
  55. target_account: @report.target_account
  56. ).unresolved
  57. end
  58. def filtered_reports
  59. ReportFilter.new(filter_params).results.order(id: :desc).includes(
  60. :account,
  61. :target_account
  62. )
  63. end
  64. def filter_params
  65. params.permit(
  66. :account_id,
  67. :resolved,
  68. :target_account_id
  69. )
  70. end
  71. def set_report
  72. @report = Report.find(params[:id])
  73. end
  74. end
  75. end