organization.rb 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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
  13. .new(
  14. current_user: context.current_user,
  15. query: query,
  16. objects: [::Organization],
  17. options: { limit: limit, ids: customer_ids(input[:customer]) },
  18. )
  19. .execute
  20. .flattened
  21. .map { |organization| coerce_to_result(organization) }
  22. end
  23. def coerce_to_result(organization)
  24. {
  25. value: organization.id,
  26. label: organization.name,
  27. organization: organization,
  28. }
  29. end
  30. private
  31. def customer_ids(customer)
  32. return nil if customer.blank?
  33. return nil if customer.all_organization_ids.blank?
  34. customer.all_organization_ids
  35. end
  36. end
  37. end