state.rb 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. module Gql::Queries
  3. class System::Import::State < BaseQuery
  4. description 'Fetch system import state'
  5. type Gql::Types::ImportJobType, null: true
  6. def self.authorize(...)
  7. true
  8. end
  9. def resolve
  10. begin
  11. status = Service::System::Import::CheckStatus.new.execute
  12. rescue Service::System::Import::Run::ExecuteError => e
  13. return build_error(e.message)
  14. end
  15. return status if status.is_a?(ImportJob)
  16. transform_otrs_status(status)
  17. end
  18. private
  19. def build_error(message, name = 'Import::None')
  20. {
  21. name: name,
  22. result: {
  23. error: message
  24. },
  25. started_at: nil,
  26. finished_at: nil
  27. }
  28. end
  29. def transform_otrs_status(status)
  30. return build_error(status[:message] || __('The OTRS import failed.'), 'Import::Otrs') if status[:result] == 'error'
  31. started_at = status.present? ? DateTime.now : nil
  32. finished_at = status[:result] == 'import_done' ? DateTime.now : nil
  33. data = status[:data] || {}
  34. data.deep_transform_keys! { |key| key.to_s.eql?('done') ? :sum : key }
  35. if data.present?
  36. data[:Configuration] = {
  37. sum: 1,
  38. total: 1,
  39. }
  40. end
  41. {
  42. name: 'Import::Otrs',
  43. result: data,
  44. started_at: started_at,
  45. finished_at: finished_at
  46. }
  47. end
  48. end
  49. end