integrity.rb 1.4 KB

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