summaryrefslogtreecommitdiff
path: root/lib/api/todos.rb
blob: 5eae92a251e95014ca272f505e886f9b9d0e405d (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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# frozen_string_literal: true

module API
  class Todos < Grape::API::Instance
    include PaginationParams

    before { authenticate! }

    ISSUABLE_TYPES = {
      'merge_requests' => ->(iid) { find_merge_request_with_access(iid) },
      'issues' => ->(iid) { find_project_issue(iid) }
    }.freeze

    params do
      requires :id, type: String, desc: 'The ID of a project'
    end
    resource :projects, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
      ISSUABLE_TYPES.each do |type, finder|
        type_id_str = "#{type.singularize}_iid".to_sym

        desc 'Create a todo on an issuable' do
          success Entities::Todo
        end
        params do
          requires type_id_str, type: Integer, desc: 'The IID of an issuable'
        end
        post ":id/#{type}/:#{type_id_str}/todo" do
          issuable = instance_exec(params[type_id_str], &finder)
          todo = TodoService.new.mark_todo(issuable, current_user).first

          if todo
            present todo, with: Entities::Todo, current_user: current_user
          else
            not_modified!
          end
        end
      end
    end

    resource :todos do
      helpers do
        params :todo_filters do
          optional :action, String, values: Todo::ACTION_NAMES.values.map(&:to_s)
          optional :author_id, Integer
          optional :state, String, values: Todo.state_machine.states.map(&:name).map(&:to_s)
          optional :type, String, values: TodosFinder.todo_types
          optional :project_id, Integer
          optional :group_id, Integer
        end

        def find_todos
          TodosFinder.new(current_user, declared_params(include_missing: false)).execute
        end

        def issuable_and_awardable?(type)
          obj_type = Object.const_get(type, false)

          (obj_type < Issuable) && (obj_type < Awardable)
        rescue NameError
          false
        end

        def batch_load_issuable_metadata(todos, options)
          # This should be paginated and will cause Rails to SELECT for all the Todos
          todos_by_type = todos.group_by(&:target_type)

          todos_by_type.keys.each do |type|
            next unless issuable_and_awardable?(type)

            collection = todos_by_type[type]

            next unless collection

            targets = collection.map(&:target)
            options[type] = { issuable_metadata: Gitlab::IssuableMetadata.new(current_user, targets).data }
          end
        end
      end

      desc 'Get a todo list' do
        success Entities::Todo
      end
      params do
        use :pagination, :todo_filters
      end
      get do
        todos = paginate(find_todos.with_entity_associations)
        options = { with: Entities::Todo, current_user: current_user }
        batch_load_issuable_metadata(todos, options)

        present todos, options
      end

      desc 'Mark a todo as done' do
        success Entities::Todo
      end
      params do
        requires :id, type: Integer, desc: 'The ID of the todo being marked as done'
      end
      post ':id/mark_as_done' do
        todo = current_user.todos.find(params[:id])

        TodoService.new.resolve_todo(todo, current_user, resolved_by_action: :api_done)

        present todo, with: Entities::Todo, current_user: current_user
      end

      desc 'Mark all todos as done'
      post '/mark_as_done' do
        todos = find_todos

        TodoService.new.resolve_todos(todos, current_user, resolved_by_action: :api_all_done)

        no_content!
      end
    end
  end
end

API::Todos.prepend_if_ee('EE::API::Todos')