out_of_office.rb 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. module User::OutOfOffice
  3. extend ActiveSupport::Concern
  4. included do
  5. validates_with Validations::OutOfOfficeValidator
  6. belongs_to :out_of_office_replacement, class_name: 'User', optional: true
  7. scope :out_of_office, lambda { |user, interval_start = Time.zone.today, interval_end = Time.zone.today|
  8. where(active: true, out_of_office: true, out_of_office_replacement_id: user)
  9. .where('out_of_office_start_at <= ? AND out_of_office_end_at >= ?', interval_start, interval_end)
  10. }
  11. end
  12. =begin
  13. check if user is in role
  14. user = User.find(123)
  15. result = user.out_of_office?
  16. returns
  17. result = true|false
  18. =end
  19. def out_of_office?
  20. return false if out_of_office != true
  21. return false if out_of_office_start_at.blank?
  22. return false if out_of_office_end_at.blank?
  23. Time.use_zone(Setting.get('timezone_default')) do
  24. start = out_of_office_start_at.beginning_of_day
  25. finish = out_of_office_end_at.end_of_day
  26. Time.zone.now.between? start, finish
  27. end
  28. end
  29. =begin
  30. check if user is in role
  31. user = User.find(123)
  32. result = user.out_of_office_agent
  33. returns
  34. result = user_model
  35. =end
  36. def out_of_office_agent(loop_user_ids: [], stack_depth: 10)
  37. return if !out_of_office?
  38. return if out_of_office_replacement_id.blank?
  39. if stack_depth.zero?
  40. Rails.logger.warn("Found more than 10 replacement levels for agent #{self}.")
  41. return self
  42. end
  43. user = User.find_by(id: out_of_office_replacement_id)
  44. # stop if users are occuring multiple times to prevent endless loops
  45. return user if loop_user_ids.include?(out_of_office_replacement_id)
  46. loop_user_ids |= [out_of_office_replacement_id]
  47. ooo_agent = user.out_of_office_agent(loop_user_ids: loop_user_ids, stack_depth: stack_depth - 1)
  48. return ooo_agent if ooo_agent.present?
  49. user
  50. end
  51. =begin
  52. gets users where user is replacement
  53. user = User.find(123)
  54. result = user.out_of_office_agent_of
  55. returns
  56. result = [user_model1, user_model2]
  57. =end
  58. def out_of_office_agent_of
  59. self.class.where(id: out_of_office_agent_of_recursive(user_id: id))
  60. end
  61. def someones_out_of_office_replacement?
  62. self.class.out_of_office(self).exists?
  63. end
  64. private
  65. def out_of_office_agent_of_recursive(user_id:, result: [])
  66. self.class.out_of_office(user_id).each do |user|
  67. # stop if users are occuring multiple times to prevent endless loops
  68. break if result.include?(user.id)
  69. result |= [user.id]
  70. result |= out_of_office_agent_of_recursive(user_id: user.id, result: result)
  71. end
  72. result
  73. end
  74. end