2021-04-29 21:17:54 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Resolvers
|
|
|
|
class TimelogResolver < BaseResolver
|
|
|
|
include LooksAhead
|
|
|
|
|
|
|
|
type ::Types::TimelogType.connection_type, null: false
|
|
|
|
|
|
|
|
argument :start_date, Types::TimeType,
|
2021-09-04 01:27:46 +05:30
|
|
|
required: false,
|
|
|
|
description: 'List time logs within a date range where the logged date is equal to or after startDate.'
|
2021-04-29 21:17:54 +05:30
|
|
|
|
|
|
|
argument :end_date, Types::TimeType,
|
2021-09-04 01:27:46 +05:30
|
|
|
required: false,
|
|
|
|
description: 'List time logs within a date range where the logged date is equal to or before endDate.'
|
2021-04-29 21:17:54 +05:30
|
|
|
|
|
|
|
argument :start_time, Types::TimeType,
|
2021-09-04 01:27:46 +05:30
|
|
|
required: false,
|
|
|
|
description: 'List time-logs within a time range where the logged time is equal to or after startTime.'
|
2021-04-29 21:17:54 +05:30
|
|
|
|
|
|
|
argument :end_time, Types::TimeType,
|
2021-09-04 01:27:46 +05:30
|
|
|
required: false,
|
|
|
|
description: 'List time-logs within a time range where the logged time is equal to or before endTime.'
|
2021-04-29 21:17:54 +05:30
|
|
|
|
|
|
|
def resolve_with_lookahead(**args)
|
2021-09-04 01:27:46 +05:30
|
|
|
build_timelogs
|
2021-04-29 21:17:54 +05:30
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
if args.any?
|
|
|
|
validate_args!(args)
|
|
|
|
build_parsed_args(args)
|
|
|
|
validate_time_difference!
|
|
|
|
apply_time_filter
|
|
|
|
end
|
2021-04-29 21:17:54 +05:30
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
apply_lookahead(timelogs)
|
2021-04-29 21:17:54 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
attr_reader :parsed_args, :timelogs
|
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
def preloads
|
|
|
|
{
|
|
|
|
note: [:note]
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
def validate_args!(args)
|
|
|
|
if 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
|
2021-04-29 21:17:54 +05:30
|
|
|
end
|
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
def build_parsed_args(args)
|
|
|
|
if times_provided?(args)
|
|
|
|
@parsed_args = args
|
|
|
|
else
|
|
|
|
@parsed_args = args.except(:start_date, :end_date)
|
2021-04-29 21:17:54 +05:30
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
@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]
|
|
|
|
end
|
2021-04-29 21:17:54 +05:30
|
|
|
end
|
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
def times_provided?(args)
|
|
|
|
args[:start_time] && args[:end_time]
|
2021-04-29 21:17:54 +05:30
|
|
|
end
|
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
def validate_time_difference!
|
|
|
|
return unless end_time_before_start_time?
|
2021-04-29 21:17:54 +05:30
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
raise_argument_error('Start argument must be before End argument')
|
|
|
|
end
|
2021-04-29 21:17:54 +05:30
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
def end_time_before_start_time?
|
|
|
|
times_provided?(parsed_args) && parsed_args[:end_time] < parsed_args[:start_time]
|
2021-04-29 21:17:54 +05:30
|
|
|
end
|
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
def build_timelogs
|
|
|
|
@timelogs = Timelog.in_group(object)
|
2021-04-29 21:17:54 +05:30
|
|
|
end
|
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
def apply_time_filter
|
|
|
|
@timelogs = timelogs.at_or_after(parsed_args[:start_time]) if parsed_args[:start_time]
|
|
|
|
@timelogs = timelogs.at_or_before(parsed_args[:end_time]) if parsed_args[:end_time]
|
2021-04-29 21:17:54 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def raise_argument_error(message)
|
|
|
|
raise Gitlab::Graphql::Errors::ArgumentError, message
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|