item_upsert.rb 1.3 KB

1234567891011121314151617181920212223242526272829303132
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. module Gql::Mutations
  3. class Ticket::Checklist::ItemUpsert < Ticket::Checklist::Base
  4. description 'Update or create a ticket checklist item.'
  5. argument :checklist_id, GraphQL::Types::ID, required: true, loads: Gql::Types::ChecklistType, description: 'ID of the ticket checklist to update or create an item for.'
  6. argument :checklist_item_id, GraphQL::Types::ID, required: false, loads: Gql::Types::Checklist::ItemType, description: 'ID of the ticket checklist item to update.'
  7. argument :input, Gql::Types::Input::Ticket::Checklist::ItemInputType, required: true, description: 'Input field values of the ticket checklist item.'
  8. field :checklist_item, Gql::Types::Checklist::ItemType, null: true, description: 'Updated or created checklist item.'
  9. def authorized?(checklist:, input:, checklist_item: nil)
  10. if checklist_item
  11. Pundit.authorize(context.current_user, checklist_item, :update?)
  12. end
  13. Pundit.authorize(context.current_user, Checklist::Item.new(checklist:), :create?)
  14. end
  15. def resolve(checklist:, input:, checklist_item: nil)
  16. if checklist_item
  17. checklist_item.update!(**input)
  18. else
  19. checklist_item = checklist.items.create!(input.to_h)
  20. end
  21. {
  22. checklist_item:
  23. }
  24. end
  25. end
  26. end