import_job_base.rb 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. module Integration::ImportJobBase
  3. extend ActiveSupport::Concern
  4. def job_try_index
  5. job_index(
  6. dry_run: true,
  7. take_finished: params[:finished] == 'true'
  8. )
  9. end
  10. def job_try_create
  11. ImportJob.dry_run(name: backend, payload: payload_dry_run)
  12. render json: {
  13. result: 'ok',
  14. }
  15. end
  16. def job_start_index
  17. job_index(dry_run: false)
  18. end
  19. def job_start_create
  20. if !ImportJob.exists?(name: backend, finished_at: nil)
  21. job = ImportJob.create(name: backend)
  22. AsyncImportJob.perform_later(job)
  23. end
  24. render json: {
  25. result: 'ok',
  26. }
  27. end
  28. def payload_dry_run
  29. clean_payload(params.permit!.to_h)
  30. end
  31. private
  32. def clean_payload(payload)
  33. payload.except(:wizardData, :action, :controller)
  34. end
  35. def answer_with
  36. result = yield
  37. render json: result.merge(result: 'ok')
  38. rescue => e
  39. logger.error(e)
  40. render json: {
  41. result: 'failed',
  42. message: e.message,
  43. }
  44. end
  45. def backend
  46. "Import::#{controller_name.classify}"
  47. end
  48. def job_index(dry_run:, take_finished: true)
  49. job = ImportJob.find_by(
  50. name: backend,
  51. dry_run: dry_run,
  52. finished_at: nil
  53. )
  54. if !job && take_finished
  55. job = ImportJob.where(
  56. name: backend,
  57. dry_run: dry_run
  58. ).reorder(created_at: :desc).limit(1).first
  59. end
  60. if job
  61. model_show_render_item(job)
  62. else
  63. render json: {}
  64. end
  65. end
  66. end