base.rb 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Channel::Driver::Sms::Base
  3. def user_by_mobile(mobile)
  4. User.by_mobile(number: mobile) || User.create!(
  5. firstname: mobile,
  6. mobile: mobile,
  7. )
  8. end
  9. def article_type_sms
  10. Ticket::Article::Type.find_by(name: 'sms')
  11. end
  12. def closed_ids
  13. Ticket::State.where(name: %w[closed merged removed]).pluck(:id)
  14. end
  15. def ensure_ticket_followup_state(ticket)
  16. return if !ticket
  17. new_state = Ticket::State.find_by(default_create: true)
  18. return if ticket.state_id == new_state.id
  19. ticket.state = Ticket::State.find_by(default_follow_up: true)
  20. ticket.save!
  21. end
  22. def find_open_sms_ticket(user)
  23. ticket = Ticket.where(customer_id: user.id, create_article_type_id: article_type_sms.id).where.not(state_id: closed_ids).reorder(:updated_at).first
  24. ensure_ticket_followup_state(ticket)
  25. ticket
  26. end
  27. def ensure_group!(channel)
  28. raise Exceptions::UnprocessableEntity, __('Group needed in channel definition!') if channel.group_id.blank?
  29. group = Group.find_by(id: channel.group_id)
  30. raise Exceptions::UnprocessableEntity, __('Group is invalid!') if !group
  31. end
  32. def cut_title(title)
  33. if title.length > 40
  34. title = "#{title[0, 40]}..."
  35. end
  36. title
  37. end
  38. def process_ticket(attr, channel, user)
  39. ticket = find_open_sms_ticket(user)
  40. if !ticket
  41. ensure_group!(channel)
  42. ticket = create_ticket(attr, channel, user)
  43. end
  44. create_article(attr, channel, ticket)
  45. end
  46. end