33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
# File 'lib/telephony/conversation_data.rb', line 33
def self.search(args = {})
conversations = Conversation.scoped
.includes(:events, calls: [:recordings, :voicemail])
.order("telephony_conversations.created_at")
.reverse_order
.page(args[:page])
.per(10)
if args[:csr_id].present?
agent = Agent.find_by_csr_id args[:csr_id]
agent_id = agent ? agent.id : -1
conversations = conversations.where("telephony_conversations.id in (#{Call.select(:conversation_id).where(agent_id: agent_id).to_sql})")
end
if args[:q].present?
query = args[:q].to_s.strip
condition = "telephony_conversations.loan_id = ? OR telephony_conversations.id = ? OR telephony_calls.number like ?"
subquery = Conversation.select("telephony_conversations.id").joins(:calls).where(condition, query, query, "%#{query}%").to_sql
conversations = conversations.where("telephony_conversations.id in (#{subquery})")
end
if args[:start_date].present?
conversations = conversations.where('telephony_conversations.created_at >= ?',
DateTime.parse(args[:start_date]).utc)
end
if args[:end_date].present?
conversations = conversations.where('telephony_conversations.created_at <= ?',
DateTime.parse(args[:end_date]).utc)
end
conversations
end
|