summaryrefslogtreecommitdiff
path: root/app/graphql/resolvers/todo_resolver.rb
blob: 9a8f7a71154a10422d71fb242e4301a3c748455c (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
# frozen_string_literal: true

module Resolvers
  class TodoResolver < BaseResolver
    type Types::TodoType.connection_type, null: true

    alias_method :target, :object

    argument :action, [Types::TodoActionEnum],
             required: false,
             description: 'The action to be filtered'

    argument :author_id, [GraphQL::ID_TYPE],
             required: false,
             description: 'The ID of an author'

    argument :project_id, [GraphQL::ID_TYPE],
             required: false,
             description: 'The ID of a project'

    argument :group_id, [GraphQL::ID_TYPE],
             required: false,
             description: 'The ID of a group'

    argument :state, [Types::TodoStateEnum],
             required: false,
             description: 'The state of the todo'

    argument :type, [Types::TodoTargetEnum],
             required: false,
             description: 'The type of the todo'

    def resolve(**args)
      return Todo.none unless current_user.present? && target.present?
      return Todo.none if target.is_a?(User) && target != current_user

      TodosFinder.new(current_user, todo_finder_params(args)).execute
    end

    private

    def todo_finder_params(args)
      {
        state: args[:state],
        type: args[:type],
        group_id: args[:group_id],
        author_id: args[:author_id],
        action_id: args[:action],
        project_id: args[:project_id]
      }.merge(target_params)
    end

    def target_params
      return {} unless TodosFinder::TODO_TYPES.include?(target.class.name)

      {
        type: target.class.name,
        target_id: target.id
      }
    end
  end
end