summaryrefslogtreecommitdiff
path: root/lib/api/helpers/events_helpers.rb
blob: bf3b76bb92d4786e89eabf62ea87d72bd52dedb1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# frozen_string_literal: true

module API
  module Helpers
    module EventsHelpers
      extend Grape::API::Helpers

      params :event_filter_params do
        optional :action, type: String, values: Event.actions, desc: 'Event action to filter on'
        optional :target_type, type: String, values: Event.target_types, desc: 'Event target type to filter on'
        optional :before, type: Date, desc: 'Include only events created before this date'
        optional :after, type: Date, desc: 'Include only events created after this date'
      end

      params :sort_params do
        optional :sort, type: String, values: %w[asc desc], default: 'desc',
                        desc: 'Return events sorted in ascending and descending order'
      end

      def present_events(events)
        events = paginate(events)

        present events, with: Entities::Event
      end

      def find_events(source)
        EventsFinder.new(params.merge(source: source, current_user: current_user, with_associations: true)).execute
      end
    end
  end
end