trabajo-afectivo/app/models/cti/caller_id.rb

238 lines
4.7 KiB
Ruby
Raw Normal View History

2016-04-28 13:15:50 +00:00
module Cti
class CallerId < ApplicationModel
self.table_name = 'cti_caller_ids'
=begin
Cti::CallerId.maybe_add(
caller_id: '49123456789',
comment: 'Hairdresser Bob Smith, San Francisco', #optional
level: 'maybe', # known|maybe
user_id: 1, # optional
object: 'Ticket',
o_id: 123,
)
=end
def self.maybe_add(data)
records = Cti::CallerId.where(
caller_id: data[:caller_id],
level: data[:level],
object: data[:object],
o_id: data[:o_id],
user_id: data[:user_id],
)
return if records[0]
Cti::CallerId.create(
caller_id: data[:caller_id],
comment: data[:comment],
level: data[:level],
object: data[:object],
o_id: data[:o_id],
user_id: data[:user_id],
)
end
=begin
caller_id_records = Cti::CallerId.lookup('49123456789')
returns
[record1, record2, ...]
=end
def self.lookup(caller_id)
2016-04-29 08:23:12 +00:00
result = Cti::CallerId.where(
2016-04-28 13:15:50 +00:00
caller_id: caller_id,
2016-04-29 17:38:07 +00:00
level: 'known',
2016-04-29 17:52:45 +00:00
).group(:user_id, :id).order(id: 'DESC').limit(20)
2016-04-29 17:38:07 +00:00
if !result[0]
result = Cti::CallerId.where(
caller_id: caller_id,
level: 'maybe',
2016-04-29 17:52:45 +00:00
).group(:user_id, :id).order(id: 'DESC').limit(20)
2016-04-29 17:38:07 +00:00
end
if !result[0]
result = Cti::CallerId.where(
caller_id: caller_id,
).order('id DESC').limit(20)
end
2016-04-29 08:23:12 +00:00
2016-04-29 17:38:07 +00:00
# in case do lookups in external sources
if !result[0]
2016-04-29 08:23:12 +00:00
# ...
end
result
2016-04-28 13:15:50 +00:00
end
=begin
Cti::CallerId.build(ticket)
=end
def self.build(record)
map = config
level = nil
model = nil
2016-06-30 20:04:48 +00:00
map.each { |item|
2016-04-28 13:15:50 +00:00
next if item[:model] != record.class
level = item[:level]
model = item[:model]
}
return if !level || !model
build_item(record, model, level)
end
=begin
Cti::CallerId.build_item(record, model, level)
=end
def self.build_item(record, model, level)
# use first customer article
if model == Ticket
article = record.articles.first
return if !article
return if article.sender.name != 'Customer'
record = article
end
# set user id
user_id = record[:created_by_id]
if model == User
user_id = record.id
end
return if !user_id
# get caller ids
caller_ids = []
attributes = record.attributes
2016-06-30 20:04:48 +00:00
attributes.each { |_attribute, value|
2016-04-28 13:15:50 +00:00
next if value.class != String
next if value.empty?
local_caller_ids = Cti::CallerId.parse_text(value)
next if local_caller_ids.empty?
caller_ids = caller_ids.concat(local_caller_ids)
}
# store caller ids
Cti::CallerId.where(object: model.to_s, o_id: record.id).destroy_all
2016-06-30 20:04:48 +00:00
caller_ids.each { |caller_id|
2016-04-28 13:15:50 +00:00
Cti::CallerId.maybe_add(
caller_id: caller_id,
level: level,
object: model.to_s,
o_id: record.id,
user_id: user_id,
)
}
true
end
=begin
Cti::CallerId.rebuild
=end
def self.rebuild
2016-04-29 08:23:12 +00:00
Cti::CallerId.delete_all
2016-04-28 13:15:50 +00:00
map = config
2016-06-30 20:04:48 +00:00
map.each { |item|
2016-04-28 13:15:50 +00:00
level = item[:level]
model = item[:model]
item[:model].find_each(batch_size: 500) do |record|
build_item(record, model, level)
end
}
end
=begin
Cti::CallerId.config
returns
[
{
model: User,
level: 'known',
},
{
model: Ticket,
level: 'maybe',
},
]
=end
def self.config
[
{
model: User,
level: 'known',
},
{
model: Ticket,
level: 'maybe',
},
]
end
=begin
caller_ids = Cti::CallerId.parse_text('...')
returns
['49123456789', '49987654321']
=end
def self.parse_text(text)
caller_ids = []
# 022 1234567
# 021 123 2345
# 0271233211
# 021-233-9123
# 09 123 32112
# 021 2331231 or 021 321123123
# 622 32281
# 5754321
# 092213212
# (09)1234321
# +41 30 53 00 00 000
# +42 160 0000000
# +43 (0) 30 60 00 00 00-0
# 0043 (0) 30 60 00 00 00-0
default_country_id = '49'
text.gsub!(/([\d|\s|\-|\(|\)]{6,26})/) {
number = $1.strip
number.sub!(/^00/, '')
number.sub!(/\(0\)/, '')
number.gsub!(/(\s|\-|\(|\))/, '')
if !Phony.plausible?(number)
if number =~ /^0/
number.gsub!(/^0/, default_country_id)
else
number = "#{default_country_id}#{number}"
end
next if !Phony.plausible?(number)
end
caller_ids.push number
}
caller_ids
end
end
end