summaryrefslogtreecommitdiff
path: root/app/finders/incident_management/timeline_events_finder.rb
blob: 09de46bb79f1a24ed1d9c8e43ca2e309737103f9 (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
32
33
34
35
36
37
# frozen_string_literal: true

module IncidentManagement
  class TimelineEventsFinder
    def initialize(user, incident, params = {})
      @user = user
      @incident = incident
      @params = params
    end

    def execute
      return ::IncidentManagement::TimelineEvent.none unless allowed?

      collection = incident.incident_management_timeline_events
      collection = by_id(collection)
      sort(collection)
    end

    private

    attr_reader :user, :incident, :params

    def allowed?
      Ability.allowed?(user, :read_incident_management_timeline_event, incident)
    end

    def by_id(collection)
      return collection unless params[:id]

      collection.id_in(params[:id])
    end

    def sort(collection)
      collection.order_occurred_at_asc
    end
  end
end