trabajo-afectivo/lib/sessions/backend/ticket_overview_list.rb

133 lines
3.1 KiB
Ruby
Raw Normal View History

2014-07-13 18:52:32 +00:00
class Sessions::Backend::TicketOverviewList
2016-03-01 14:26:46 +00:00
def initialize(user, client = nil, client_id = nil, ttl = 7)
@user = user
@client = client
@client_id = client_id
@ttl = ttl
@last_change = nil
@last_ticket_change = nil
2014-07-13 18:52:32 +00:00
end
def load
2014-07-13 18:52:32 +00:00
# get whole collection
overviews = Ticket::Overviews.all(
current_user: @user,
)
2016-02-07 17:46:44 +00:00
return if !overviews
result = []
overviews.each { |overview|
overview_data = Ticket::Overviews.list(
view: overview.link,
current_user: @user,
array: true,
)
data = { list: overview_data, index: overview }
result.push data
}
2015-05-10 19:54:57 +00:00
2014-07-13 18:52:32 +00:00
# no data exists
return if !result || result.empty?
2014-07-13 18:52:32 +00:00
# no change exists
return if @last_change == result
2014-07-13 18:52:32 +00:00
# remember last state
@last_change = result
2014-07-13 18:52:32 +00:00
result
end
2014-07-13 18:52:32 +00:00
def client_key
"as::load::#{self.class}::#{@user.id}::#{@client_id}"
2014-07-13 18:52:32 +00:00
end
def push
# check interval
2016-03-01 14:26:46 +00:00
return if Sessions::CacheIn.get(client_key)
2014-07-13 18:52:32 +00:00
# reset check interval
2016-03-01 14:26:46 +00:00
Sessions::CacheIn.set(client_key, true, { expires_in: @ttl.seconds })
2014-07-13 18:52:32 +00:00
# check if min one ticket has changed
last_ticket_change = Ticket.latest_change
return if last_ticket_change == @last_ticket_change
@last_ticket_change = last_ticket_change
# load current data
items = load
return if !items
2014-07-13 18:52:32 +00:00
# push overviews
results = []
items.each { |item|
overview_data = item[:list]
2014-07-13 18:52:32 +00:00
assets = {}
overview_data[:ticket_ids].each {|ticket_id|
2016-03-01 14:26:46 +00:00
ticket = Ticket.find(ticket_id)
2014-07-13 18:52:32 +00:00
assets = ticket.assets(assets)
}
2014-07-13 18:52:32 +00:00
# get groups
group_ids = []
Group.where(active: true).each { |group|
2014-07-13 18:52:32 +00:00
group_ids.push group.id
}
agents = {}
User.of_role('Agent').each { |user|
2014-07-13 18:52:32 +00:00
agents[ user.id ] = 1
}
users = {}
groups_users = {}
groups_users[''] = []
group_ids.each {|group_id|
groups_users[ group_id ] = []
Group.find(group_id).users.each {|user|
next if !agents[ user.id ]
groups_users[ group_id ].push user.id
if !users[user.id]
users[user.id] = User.find(user.id)
assets = users[user.id].assets(assets)
end
}
2014-07-13 18:52:32 +00:00
}
if !@client
result = {
event: 'navupdate_ticket_overview',
data: item[:index],
}
results.push result
else
@client.log "push overview_list for user #{@user.id}"
# send update to browser
2015-04-27 15:21:17 +00:00
@client.send(
data: assets,
2015-12-09 13:09:37 +00:00
event: 'loadAssets'
2015-04-27 15:21:17 +00:00
)
@client.send(
data: {
view: item[:index].link.to_s,
overview: overview_data[:overview],
ticket_ids: overview_data[:ticket_ids],
tickets_count: overview_data[:tickets_count],
bulk: {
group_id__owner_id: groups_users,
owner_id: [],
},
},
2015-12-09 13:09:37 +00:00
event: 'ticket_overview_rebuild',
2015-04-27 15:21:17 +00:00
)
end
}
return results if !@client
nil
end
end