organization.rb 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. module Gql::Queries
  3. class AutocompleteSearch::Organization < BaseQuery
  4. description 'Search for organizations'
  5. argument :input, Gql::Types::Input::AutocompleteSearch::OrganizationInputType, required: true, description: 'The input object for the autocomplete search'
  6. type [Gql::Types::AutocompleteSearch::OrganizationEntryType], null: false
  7. def resolve(input:)
  8. input = input.to_h
  9. query = input[:query]
  10. limit = input[:limit] || 50
  11. return [] if query.strip.empty?
  12. Service::Search.new(current_user: context.current_user).execute(
  13. term: query,
  14. objects: [::Organization],
  15. options: { limit: limit, ids: customer_ids(input[:customer]) },
  16. ).map { |organization| coerce_to_result(organization) }
  17. end
  18. def coerce_to_result(organization)
  19. {
  20. value: organization.id,
  21. label: organization.name,
  22. organization: organization,
  23. }
  24. end
  25. private
  26. def customer_ids(customer)
  27. return nil if customer.blank?
  28. return nil if customer.all_organization_ids.blank?
  29. customer.all_organization_ids
  30. end
  31. end
  32. end