debian-mirror-gitlab/app/graphql/resolvers/timelog_resolver.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

158 lines
4.6 KiB
Ruby
Raw Normal View History

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,
2021-10-27 15:23:28 +05:30
description: 'List timelogs 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,
2021-10-27 15:23:28 +05:30
description: 'List timelogs 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,
2021-10-27 15:23:28 +05:30
description: 'List timelogs 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,
2021-10-27 15:23:28 +05:30
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.'
2021-04-29 21:17:54 +05:30
2023-03-17 16:20:25 +05:30
argument :sort, Types::TimeTracking::TimelogSortEnum,
description: 'List timelogs in a particular order.',
required: false,
default_value: { field: 'spent_at', direction: :asc }
2021-04-29 21:17:54 +05:30
def resolve_with_lookahead(**args)
2021-10-27 15:23:28 +05:30
validate_args!(object, args)
2023-03-17 16:20:25 +05:30
timelogs = object&.timelogs || Timelog.all
2021-04-29 21:17:54 +05:30
2023-03-17 16:20:25 +05:30
args = parse_datetime_args(args)
2021-10-27 15:23:28 +05:30
2023-03-17 16:20:25 +05:30
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)
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
def preloads
{
note: [:note]
}
end
2021-10-27 15:23:28 +05:30
def validate_args!(object, args)
2023-03-17 16:20:25 +05:30
# 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?
2021-10-27 15:23:28 +05:30
raise_argument_error('Provide at least one argument')
elsif args[:start_time] && args[:start_date]
2021-09-04 01:27:46 +05:30
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-10-27 15:23:28 +05:30
def parse_datetime_args(args)
2021-09-04 01:27:46 +05:30
if times_provided?(args)
2021-10-27 15:23:28 +05:30
args
2021-09-04 01:27:46 +05:30
else
2021-10-27 15:23:28 +05:30
parsed_args = args.except(:start_date, :end_date)
2021-04-29 21:17:54 +05:30
2021-10-27 15:23:28 +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]
parsed_args
2021-09-04 01:27:46 +05:30
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-10-27 15:23:28 +05:30
def validate_time_difference!(args)
return unless end_time_before_start_time?(args)
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-10-27 15:23:28 +05:30
def end_time_before_start_time?(args)
times_provided?(args) && args[:end_time] < args[:start_time]
2021-04-29 21:17:54 +05:30
end
2021-10-27 15:23:28 +05:30
def apply_project_filter(timelogs, args)
return timelogs unless args[:project_id]
2022-07-16 23:28:13 +05:30
timelogs.in_project(args[:project_id].model_id)
2021-04-29 21:17:54 +05:30
end
2021-10-27 15:23:28 +05:30
def apply_group_filter(timelogs, args)
return timelogs unless args[:group_id]
2022-07-16 23:28:13 +05:30
group = Group.find_by_id(args[:group_id].model_id)
2021-10-27 15:23:28 +05:30
timelogs.in_group(group)
end
def apply_user_filter(timelogs, args)
return timelogs unless args[:username]
2023-06-20 00:43:36 +05:30
user = UserFinder.new(args[:username]).find_by_username
2021-10-27 15:23:28 +05:30
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
2021-04-29 21:17:54 +05:30
end
2023-03-17 16:20:25 +05:30
def apply_sorting(timelogs, args)
return timelogs unless args[:sort]
field = args[:sort][:field]
direction = args[:sort][:direction]
timelogs.sort_by_field(field, direction)
end
2021-04-29 21:17:54 +05:30
def raise_argument_error(message)
raise Gitlab::Graphql::Errors::ArgumentError, message
end
end
end