12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
- module Gql::Queries
- class AutocompleteSearch::Generic < BaseQuery
- description 'Generic autocomplete search'
- argument :input, Gql::Types::Input::AutocompleteSearch::GenericInputType, required: true, description: 'The input object for the autocomplete search'
- type [Gql::Types::AutocompleteSearch::GenericEntryType], null: false
- def resolve(input:)
- input = input.to_h
- query = input[:query]
- limit = input[:limit] || 50
- return [] if query.blank?
- objects = Service::Search.new(current_user: context.current_user).execute(
- term: query,
- objects: input[:only_in] || Gql::Types::SearchResultType.searchable_models,
- options: { limit: limit }
- )
- post_process(objects, input:)
- end
- def post_process(results, input:)
- results.map { |object| coerce_to_result(object) }
- end
- def coerce_to_result(object)
- {
- value: object.id,
- label: label(object),
- heading: heading(object),
- heading_placeholder: heading_placeholder(object),
- object: object,
- }
- end
- private
- def label(object)
- case object
- when ::User
- label_user(object)
- when ::Organization
- object.name
- when ::Ticket
- "##{object.number} - #{object.title}"
- end
- end
- def label_user(user)
- user.fullname.presence || user.phone.presence || user.login
- end
- def heading(object)
- case object
- when ::User
- object.organization&.name
- when ::Organization
- __('%s people')
- when ::Ticket
- label_user(object.customer)
- end
- end
- def heading_placeholder(object)
- case object
- when ::Organization
- [object.members.size]
- else
- []
- end
- end
- end
- end
|