20200716124141_issue_3123_external_sync_ticket_merge.rb 963 B

123456789101112131415161718192021222324252627282930313233
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. class Issue3123ExternalSyncTicketMerge < ActiveRecord::Migration[5.2]
  3. def change
  4. # return if it's a new setup
  5. return if !Setting.exists?(name: 'system_init_done')
  6. merged_ticket_ids_with_external_sync.each do |id_from|
  7. id_to = merged_ticket_ids_map[id_from]
  8. ExternalSync.migrate('Ticket', id_from, id_to)
  9. end
  10. end
  11. private
  12. # reduce to the ones with an ExternalSync entry
  13. def merged_ticket_ids_with_external_sync
  14. @merged_ticket_ids_with_external_sync ||= ExternalSync.where(
  15. object: 'Ticket',
  16. o_id: merged_ticket_ids_map.keys,
  17. ).pluck(:o_id).uniq
  18. end
  19. # get all merged tickets
  20. def merged_ticket_ids_map
  21. @merged_ticket_ids_map ||= History.where(
  22. history_type_id: History.type_lookup('merged_into').id,
  23. history_object_id: History.object_lookup('Ticket').id,
  24. ).pluck(:id_from, :id_to).to_h
  25. end
  26. end