ticket_checklist_controller.rb 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class TicketChecklistController < ApplicationController
  3. prepend_before_action :authenticate_and_authorize!
  4. def show
  5. if checklist
  6. render json: { id: checklist.id, assets: checklist.assets({}) }
  7. return
  8. end
  9. render json: {}
  10. end
  11. def create
  12. if checklist
  13. raise Exceptions::UnprocessableEntity, __('Checklist is already created for this ticket')
  14. end
  15. new_checklist = if params[:template_id].present?
  16. ChecklistTemplate.find_by(id: params[:template_id]).create_from_template!(ticket_id: params[:ticket_id])
  17. else
  18. Checklist.create!(name: '', ticket_id: params[:ticket_id]).tap do |checklist|
  19. Checklist::Item.create!(checklist_id: checklist.id, text: '')
  20. end
  21. end
  22. new_checklist.reload
  23. render json: { id: new_checklist.id, assets: new_checklist.assets({}) }
  24. end
  25. def update
  26. checklist.update! params.permit(:name, sorted_item_ids: [])
  27. render json: { id: checklist.id, assets: checklist.assets({}) }
  28. end
  29. def destroy
  30. checklist.destroy!
  31. render json: { success: true }
  32. end
  33. def completed
  34. render json: {
  35. completed: checklist&.completed?
  36. }
  37. end
  38. private
  39. def checklist
  40. @checklist ||= Checklist.find_by(ticket: params[:ticket_id])
  41. end
  42. end