The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

109 lines
2.7 KiB

  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 -> { doorkeeper_authorize! :'admin:read', :'admin:read:reports' }, only: [:index, :show]
  7. before_action -> { doorkeeper_authorize! :'admin:write', :'admin:write:reports' }, except: [:index, :show]
  8. before_action :require_staff!
  9. before_action :set_reports, only: :index
  10. before_action :set_report, except: :index
  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 assign_to_self
  27. authorize @report, :update?
  28. @report.update!(assigned_account_id: current_account.id)
  29. log_action :assigned_to_self, @report
  30. render json: @report, serializer: REST::Admin::ReportSerializer
  31. end
  32. def unassign
  33. authorize @report, :update?
  34. @report.update!(assigned_account_id: nil)
  35. log_action :unassigned, @report
  36. render json: @report, serializer: REST::Admin::ReportSerializer
  37. end
  38. def reopen
  39. authorize @report, :update?
  40. @report.unresolve!
  41. log_action :reopen, @report
  42. render json: @report, serializer: REST::Admin::ReportSerializer
  43. end
  44. def resolve
  45. authorize @report, :update?
  46. @report.resolve!(current_account)
  47. log_action :resolve, @report
  48. render json: @report, serializer: REST::Admin::ReportSerializer
  49. end
  50. private
  51. def set_reports
  52. @reports = filtered_reports.order(id: :desc).with_accounts.paginate_by_id(limit_param(LIMIT), params_slice(:max_id, :since_id, :min_id))
  53. end
  54. def set_report
  55. @report = Report.find(params[:id])
  56. end
  57. def filtered_reports
  58. ReportFilter.new(filter_params).results
  59. end
  60. def filter_params
  61. params.permit(*FILTER_PARAMS)
  62. end
  63. def insert_pagination_headers
  64. set_pagination_headers(next_path, prev_path)
  65. end
  66. def next_path
  67. api_v1_admin_reports_url(pagination_params(max_id: pagination_max_id)) if records_continue?
  68. end
  69. def prev_path
  70. api_v1_admin_reports_url(pagination_params(min_id: pagination_since_id)) unless @reports.empty?
  71. end
  72. def pagination_max_id
  73. @reports.last.id
  74. end
  75. def pagination_since_id
  76. @reports.first.id
  77. end
  78. def records_continue?
  79. @reports.size == limit_param(LIMIT)
  80. end
  81. def pagination_params(core_params)
  82. params.slice(*PAGINATION_PARAMS).permit(*PAGINATION_PARAMS).merge(core_params)
  83. end
  84. end