integrity.rb 932 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Sequencer::Unit::Import::Common::Model::ExternalSync::Integrity < Sequencer::Unit::Base
  3. uses :instance, :remote_id, :dry_run, :external_sync_source, :model_class
  4. def process
  5. return if dry_run
  6. return if instance.blank?
  7. return if instance.id.blank?
  8. return if up_to_date?
  9. create
  10. end
  11. private
  12. def up_to_date?
  13. return false if entry.blank?
  14. return true if entry.source_id == remote_id
  15. entry.update!(source_id: remote_id)
  16. true
  17. end
  18. def entry
  19. @entry ||= begin
  20. ::ExternalSync.find_by(
  21. source: external_sync_source,
  22. object: model_class.name,
  23. o_id: instance.id
  24. )
  25. end
  26. end
  27. def create
  28. ::ExternalSync.create(
  29. source: external_sync_source,
  30. source_id: remote_id,
  31. object: model_class.name,
  32. o_id: instance.id
  33. )
  34. end
  35. end