recent_by_customer.rb 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. module Gql::Queries
  3. class Tickets::RecentByCustomer < BaseQuery
  4. description 'Fetch recent customer tickets'
  5. argument :customer_id, GraphQL::Types::ID, description: 'Customer to find tickets for', loads: Gql::Types::UserType
  6. argument :except_ticket_internal_id, Integer, required: false, description: 'Optional ticket ID to be filtered out from results'
  7. argument :limit, Integer, required: false, description: 'Limit for the amount of entries'
  8. type [Gql::Types::TicketType], null: false
  9. def self.authorize(_obj, ctx)
  10. ctx.current_user.permissions?(['ticket.agent'])
  11. end
  12. def resolve(customer:, limit: 6, except_ticket_internal_id: nil)
  13. open_by_customer(customer:, except_ticket_internal_id:, limit:).all.presence ||
  14. all_by_customer(customer:, except_ticket_internal_id:, limit:).all
  15. end
  16. private
  17. def open_by_customer(customer:, except_ticket_internal_id:, limit:)
  18. scope = ::TicketPolicy::ReadScope.new(context.current_user).resolve
  19. .where(
  20. customer_id: customer.id,
  21. state_id: ::Ticket::State.by_category(:open).select(:id),
  22. )
  23. .reorder(created_at: :desc)
  24. .limit(limit)
  25. return scope if !except_ticket_internal_id
  26. scope.where.not(id: except_ticket_internal_id)
  27. end
  28. def all_by_customer(customer:, except_ticket_internal_id:, limit:)
  29. scope = ::TicketPolicy::ReadScope.new(context.current_user).resolve
  30. .where(customer_id: customer.id)
  31. .where.not(state_id: ::Ticket::State.by_category_ids(:merged))
  32. .reorder(created_at: :desc)
  33. .limit(limit)
  34. return scope if !except_ticket_internal_id
  35. scope.where.not(id: except_ticket_internal_id)
  36. end
  37. end
  38. end