tickets_mass_controller.rb 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class TicketsMassController < ApplicationController
  3. include CreatesTicketArticles
  4. prepend_before_action :authentication_check
  5. before_action :fetch_tickets
  6. def macro
  7. macro = Macro.find params[:macro_id]
  8. applicable = macro.applicable_on? @tickets
  9. if !applicable
  10. render json: {
  11. error: __('Macro group restrictions do not cover all tickets'),
  12. blocking_tickets: applicable.blocking_tickets.map(&:id)
  13. }, status: :unprocessable_entity
  14. return
  15. end
  16. execute_transaction(@tickets) do |ticket|
  17. ticket.screen = 'edit'
  18. ticket.perform_changes macro, 'macro', ticket, current_user.id
  19. end
  20. end
  21. def update
  22. clean_params = clean_update_params
  23. execute_transaction(@tickets) do |ticket|
  24. ticket.update!(clean_params) if clean_params
  25. if params[:article].present?
  26. article_create(ticket, params[:article])
  27. end
  28. end
  29. end
  30. private
  31. def fetch_tickets
  32. @tickets = Ticket.find params[:ticket_ids]
  33. @tickets.each do |elem|
  34. authorize!(elem, :agent_update_access?)
  35. end
  36. rescue Pundit::NotAuthorizedError => e
  37. render json: { error: true, ticket_id: e.record.id }, status: :unprocessable_entity
  38. end
  39. def clean_update_params
  40. return if params[:attributes].blank?
  41. clean_params = Ticket.association_name_to_id_convert(params.require(:attributes))
  42. clean_params = Ticket.param_cleanup(clean_params, true)
  43. clean_params.compact_blank!
  44. clean_params[:screen] = 'edit'
  45. clean_params.delete('number')
  46. clean_params
  47. end
  48. def execute_transaction(tickets, &)
  49. failed_record = nil
  50. ActiveRecord::Base.transaction do
  51. tickets.each(&)
  52. assets = ApplicationModel::CanAssets.reduce tickets
  53. render json: { ticket_ids: tickets.map(&:id), assets: assets }, status: :ok
  54. rescue => e
  55. raise e if !e.try(:record)
  56. failed_record = e.record
  57. raise ActiveRecord::Rollback
  58. end
  59. render json: { error: true, ticket_id: failed_record.id }, status: :unprocessable_entity if failed_record
  60. end
  61. end