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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
# frozen_string_literal: true
module Resolvers
class TimelogResolver < BaseResolver
include LooksAhead
type ::Types::TimelogType.connection_type, null: false
argument :start_date, Types::TimeType,
required: false,
description: 'List timelogs within a date range where the logged date is equal to or after startDate.'
argument :end_date, Types::TimeType,
required: false,
description: 'List timelogs within a date range where the logged date is equal to or before endDate.'
argument :start_time, Types::TimeType,
required: false,
description: 'List timelogs within a time range where the logged time is equal to or after startTime.'
argument :end_time, Types::TimeType,
required: false,
description: 'List timelogs within a time range where the logged time is equal to or before endTime.'
argument :project_id, ::Types::GlobalIDType[::Project],
required: false,
description: 'List timelogs for a project.'
argument :group_id, ::Types::GlobalIDType[::Group],
required: false,
description: 'List timelogs for a group.'
argument :username, GraphQL::Types::String,
required: false,
description: 'List timelogs for a user.'
argument :sort, Types::TimeTracking::TimelogSortEnum,
description: 'List timelogs in a particular order.',
required: false,
default_value: :spent_at_asc
def resolve_with_lookahead(**args)
validate_args!(object, args)
timelogs = object&.timelogs || Timelog.all
args = parse_datetime_args(args)
timelogs = apply_user_filter(timelogs, args)
timelogs = apply_project_filter(timelogs, args)
timelogs = apply_time_filter(timelogs, args)
timelogs = apply_group_filter(timelogs, args)
timelogs = apply_sorting(timelogs, args)
apply_lookahead(timelogs)
end
private
def preloads
{
note: [:note]
}
end
def validate_args!(object, args)
# sort is always provided because of its default value so we
# should check the remaining args to make sure at least one filter
# argument was provided
cleaned_args = args.except(:sort)
if cleaned_args.empty? && object.nil?
raise_argument_error('Provide at least one argument')
elsif args[:start_time] && args[:start_date]
raise_argument_error('Provide either a start date or time, but not both')
elsif args[:end_time] && args[:end_date]
raise_argument_error('Provide either an end date or time, but not both')
end
end
def parse_datetime_args(args)
if times_provided?(args)
args
else
parsed_args = args.except(:start_date, :end_date)
parsed_args[:start_time] = args[:start_date].beginning_of_day if args[:start_date]
parsed_args[:end_time] = args[:end_date].end_of_day if args[:end_date]
parsed_args
end
end
def times_provided?(args)
args[:start_time] && args[:end_time]
end
def validate_time_difference!(args)
return unless end_time_before_start_time?(args)
raise_argument_error('Start argument must be before End argument')
end
def end_time_before_start_time?(args)
times_provided?(args) && args[:end_time] < args[:start_time]
end
def apply_project_filter(timelogs, args)
return timelogs unless args[:project_id]
timelogs.in_project(args[:project_id].model_id)
end
def apply_group_filter(timelogs, args)
return timelogs unless args[:group_id]
group = Group.find_by_id(args[:group_id].model_id)
timelogs.in_group(group)
end
def apply_user_filter(timelogs, args)
return timelogs unless args[:username]
user = UserFinder.new(args[:username]).find_by_username
timelogs.for_user(user)
end
def apply_time_filter(timelogs, args)
return timelogs unless args[:start_time] || args[:end_time]
validate_time_difference!(args)
if args[:start_time]
timelogs = timelogs.at_or_after(args[:start_time])
end
if args[:end_time]
timelogs = timelogs.at_or_before(args[:end_time])
end
timelogs
end
def apply_sorting(timelogs, args)
return timelogs unless args[:sort]
timelogs.sort_by_field(args[:sort])
end
def raise_argument_error(message)
raise Gitlab::Graphql::Errors::ArgumentError, message
end
end
end
|