tickets_shared_draft_starts_controller.rb 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class TicketsSharedDraftStartsController < ApplicationController
  3. prepend_before_action :authenticate_and_authorize!
  4. def index
  5. drafts = scope
  6. render json: {
  7. shared_draft_ids: drafts.map(&:id),
  8. assets: ApplicationModel::CanAssets.reduce(drafts),
  9. }
  10. end
  11. def show
  12. object = scope.find params[:id]
  13. render json: {
  14. shared_draft_id: object.id,
  15. shared_draft_content: object.content_with_base64,
  16. assets: object.assets,
  17. }
  18. end
  19. def create
  20. object = Service::Ticket::SharedDraft::Start::Create
  21. .new(current_user, params[:form_id], **safe_params)
  22. .execute
  23. render json: {
  24. shared_draft_id: object.id,
  25. assets: object.assets,
  26. }
  27. end
  28. def update
  29. object = scope.find params[:id]
  30. Service::Ticket::SharedDraft::Start::Update
  31. .new(current_user, object, params[:form_id], **safe_params)
  32. .execute
  33. render json: {
  34. shared_draft_id: object.id,
  35. assets: object.assets,
  36. }
  37. end
  38. def destroy
  39. object = scope.find params[:id]
  40. object.destroy!
  41. render json: {
  42. shared_draft_id: object.id
  43. }
  44. end
  45. def import_attachments
  46. object = scope.find params[:id]
  47. new_attachments = object.clone_attachments 'UploadCache', params[:form_id], only_attached_attachments: true
  48. render json: {
  49. attachments: new_attachments
  50. }
  51. end
  52. private
  53. def scope
  54. Ticket::SharedDraftStartPolicy::Scope
  55. .new(current_user, Ticket::SharedDraftStart)
  56. .resolve
  57. end
  58. def safe_params
  59. @safe_params ||= params
  60. .permit(:name, :group_id, content: {})
  61. .to_hash
  62. .to_options
  63. .tap { |elem| elem[:group] = Group.find_by(id: elem.delete(:group_id)) }
  64. end
  65. end