add.rb 1.2 KB

123456789101112131415161718192021222324252627282930
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. module Gql::Mutations
  3. class Ticket::Checklist::Add < BaseMutation
  4. description 'Create an empty checklist or a checklist based on a template for a ticket.'
  5. argument :ticket_id, GraphQL::Types::ID, loads: Gql::Types::TicketType, description: 'Ticket to create the new checklist for.'
  6. argument :template_id, GraphQL::Types::ID, required: false, description: 'Checklist template ID to base the ticket checklist on.'
  7. field :checklist, Gql::Types::ChecklistType, null: true, description: 'Created checklist'
  8. def resolve(ticket:, template_id: nil)
  9. checklist = if template_id
  10. Gql::ZammadSchema.verified_object_from_id(template_id, type: ::ChecklistTemplate).create_from_template!(ticket_id: ticket.id)
  11. else
  12. ::Checklist.create!(name: '', ticket:).tap do |checklist|
  13. Checklist::Item.create!(checklist_id: checklist.id, text: '')
  14. end
  15. end
  16. checklist.reload
  17. { checklist: }
  18. end
  19. def authorized?(ticket:, template_id: nil)
  20. Pundit.authorize(context.current_user, ticket, :update?)
  21. end
  22. end
  23. end