whatsapp.rb 1016 B

1234567891011121314151617181920212223242526272829303132333435
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. module Channel::Area::Whatsapp
  3. extend ActiveSupport::Concern
  4. included do
  5. validate :validate_whatsapp_phone_number, if: :area_whatsapp?
  6. end
  7. private
  8. def validate_whatsapp_phone_number
  9. return if Channel
  10. .in_area('WhatsApp::Business')
  11. .then { |query| persisted? ? query.where.not(id:) : query }
  12. .none? { |elem| identical_whatsapp_phone_numbers?(elem, self) }
  13. errors.add :base, __('Phone number is already in use by another WhatsApp account.')
  14. end
  15. def identical_whatsapp_phone_numbers?(one, another)
  16. one_phone_number = extract_whatsapp_phone_number(one)
  17. another_phone_number = extract_whatsapp_phone_number(another)
  18. one_phone_number && another_phone_number && one_phone_number == another_phone_number
  19. end
  20. def extract_whatsapp_phone_number(channel)
  21. channel.options[:phone_number_id]&.to_s&.downcase
  22. end
  23. def area_whatsapp?
  24. area == 'WhatsApp::Business'
  25. end
  26. end