reports_controller.rb 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. # frozen_string_literal: true
  2. class Api::V1::Admin::ReportsController < Api::BaseController
  3. include Authorization
  4. include AccountableConcern
  5. LIMIT = 100
  6. before_action -> { authorize_if_got_token! :'admin:read', :'admin:read:reports' }, only: [:index, :show]
  7. before_action -> { authorize_if_got_token! :'admin:write', :'admin:write:reports' }, except: [:index, :show]
  8. before_action :set_reports, only: :index
  9. before_action :set_report, except: :index
  10. after_action :verify_authorized
  11. after_action :insert_pagination_headers, only: :index
  12. FILTER_PARAMS = %i(
  13. resolved
  14. account_id
  15. target_account_id
  16. ).freeze
  17. PAGINATION_PARAMS = (%i(limit) + FILTER_PARAMS).freeze
  18. def index
  19. authorize :report, :index?
  20. render json: @reports, each_serializer: REST::Admin::ReportSerializer
  21. end
  22. def show
  23. authorize @report, :show?
  24. render json: @report, serializer: REST::Admin::ReportSerializer
  25. end
  26. def update
  27. authorize @report, :update?
  28. @report.update!(report_params)
  29. render json: @report, serializer: REST::Admin::ReportSerializer
  30. end
  31. def assign_to_self
  32. authorize @report, :update?
  33. @report.update!(assigned_account_id: current_account.id)
  34. log_action :assigned_to_self, @report
  35. render json: @report, serializer: REST::Admin::ReportSerializer
  36. end
  37. def unassign
  38. authorize @report, :update?
  39. @report.update!(assigned_account_id: nil)
  40. log_action :unassigned, @report
  41. render json: @report, serializer: REST::Admin::ReportSerializer
  42. end
  43. def reopen
  44. authorize @report, :update?
  45. @report.unresolve!
  46. log_action :reopen, @report
  47. render json: @report, serializer: REST::Admin::ReportSerializer
  48. end
  49. def resolve
  50. authorize @report, :update?
  51. @report.resolve!(current_account)
  52. log_action :resolve, @report
  53. render json: @report, serializer: REST::Admin::ReportSerializer
  54. end
  55. private
  56. def set_reports
  57. @reports = filtered_reports.order(id: :desc).with_accounts.to_a_paginated_by_id(limit_param(LIMIT), params_slice(:max_id, :since_id, :min_id))
  58. end
  59. def set_report
  60. @report = Report.find(params[:id])
  61. end
  62. def filtered_reports
  63. ReportFilter.new(filter_params).results
  64. end
  65. def report_params
  66. params.permit(:category, rule_ids: [])
  67. end
  68. def filter_params
  69. params.permit(*FILTER_PARAMS)
  70. end
  71. def insert_pagination_headers
  72. set_pagination_headers(next_path, prev_path)
  73. end
  74. def next_path
  75. api_v1_admin_reports_url(pagination_params(max_id: pagination_max_id)) if records_continue?
  76. end
  77. def prev_path
  78. api_v1_admin_reports_url(pagination_params(min_id: pagination_since_id)) unless @reports.empty?
  79. end
  80. def pagination_max_id
  81. @reports.last.id
  82. end
  83. def pagination_since_id
  84. @reports.first.id
  85. end
  86. def records_continue?
  87. @reports.size == limit_param(LIMIT)
  88. end
  89. def pagination_params(core_params)
  90. params.slice(*PAGINATION_PARAMS).permit(*PAGINATION_PARAMS).merge(core_params)
  91. end
  92. end