2020-06-23 00:09:42 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Mutations
|
|
|
|
module AlertManagement
|
|
|
|
module Alerts
|
|
|
|
class SetAssignees < Base
|
|
|
|
graphql_name 'AlertSetAssignees'
|
|
|
|
|
|
|
|
argument :assignee_usernames,
|
2021-10-27 15:23:28 +05:30
|
|
|
[GraphQL::Types::String],
|
2020-06-23 00:09:42 +05:30
|
|
|
required: true,
|
2021-10-27 15:23:28 +05:30
|
|
|
description: 'Usernames to assign to the alert. Replaces existing assignees by default.'
|
2020-06-23 00:09:42 +05:30
|
|
|
|
|
|
|
argument :operation_mode,
|
|
|
|
Types::MutationOperationModeEnum,
|
|
|
|
required: false,
|
2021-10-27 15:23:28 +05:30
|
|
|
description: 'Operation to perform. Defaults to REPLACE.'
|
2020-06-23 00:09:42 +05:30
|
|
|
|
|
|
|
def resolve(args)
|
|
|
|
alert = authorized_find!(project_path: args[:project_path], iid: args[:iid])
|
|
|
|
result = set_assignees(alert, args[:assignee_usernames], args[:operation_mode])
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
track_usage_event(:incident_management_alert_assigned, current_user.id)
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
prepare_response(result)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def set_assignees(alert, assignee_usernames, operation_mode)
|
|
|
|
operation_mode ||= Types::MutationOperationModeEnum.enum[:replace]
|
|
|
|
|
|
|
|
original_assignees = alert.assignees
|
|
|
|
target_users = find_target_users(assignee_usernames)
|
|
|
|
|
|
|
|
assignees = case Types::MutationOperationModeEnum.enum.key(operation_mode).to_sym
|
|
|
|
when :replace then target_users.uniq
|
|
|
|
when :append then (original_assignees + target_users).uniq
|
|
|
|
when :remove then (original_assignees - target_users)
|
|
|
|
end
|
|
|
|
|
|
|
|
::AlertManagement::Alerts::UpdateService.new(alert, current_user, assignees: assignees).execute
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_target_users(assignee_usernames)
|
|
|
|
UsersFinder.new(current_user, username: assignee_usernames).execute
|
|
|
|
end
|
|
|
|
|
|
|
|
def prepare_response(result)
|
|
|
|
{
|
|
|
|
alert: result.payload[:alert],
|
|
|
|
errors: result.error? ? [result.message] : []
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|