2014-02-03 19:24:49 +00:00
|
|
|
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
|
2013-06-12 15:59:58 +00:00
|
|
|
|
2012-04-10 14:06:46 +00:00
|
|
|
class TicketsController < ApplicationController
|
2015-05-07 11:23:55 +00:00
|
|
|
before_action :authentication_check
|
2012-04-10 14:06:46 +00:00
|
|
|
|
2013-08-06 22:10:28 +00:00
|
|
|
# GET /api/v1/tickets/1
|
2012-04-10 14:06:46 +00:00
|
|
|
def show
|
2012-11-13 10:34:45 +00:00
|
|
|
@ticket = Ticket.find( params[:id] )
|
2012-04-10 14:06:46 +00:00
|
|
|
|
2016-01-27 18:26:10 +00:00
|
|
|
# permission check
|
2012-09-04 21:28:49 +00:00
|
|
|
return if !ticket_permission(@ticket)
|
|
|
|
|
2015-04-27 13:42:53 +00:00
|
|
|
render json: @ticket
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
|
|
|
|
2013-08-06 22:10:28 +00:00
|
|
|
# POST /api/v1/tickets
|
2012-04-10 14:06:46 +00:00
|
|
|
def create
|
2014-09-23 05:37:43 +00:00
|
|
|
ticket = Ticket.new( Ticket.param_validation( params[:ticket] ) )
|
2012-04-10 14:06:46 +00:00
|
|
|
|
2012-04-16 11:58:15 +00:00
|
|
|
# check if article is given
|
|
|
|
if !params[:article]
|
2015-04-27 13:42:53 +00:00
|
|
|
render json: 'article hash is missing', status: :unprocessable_entity
|
2012-04-16 11:58:15 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
# create ticket
|
2014-09-23 05:37:43 +00:00
|
|
|
if !ticket.save
|
2015-04-27 13:42:53 +00:00
|
|
|
render json: ticket.errors, status: :unprocessable_entity
|
2012-04-16 11:58:15 +00:00
|
|
|
return
|
|
|
|
end
|
2012-10-02 05:46:08 +00:00
|
|
|
|
2012-11-18 11:06:55 +00:00
|
|
|
# create tags if given
|
|
|
|
if params[:tags] && !params[:tags].empty?
|
2015-05-05 13:43:00 +00:00
|
|
|
tags = params[:tags].split(/,/)
|
2012-11-18 11:06:55 +00:00
|
|
|
tags.each {|tag|
|
|
|
|
Tag.tag_add(
|
2015-04-27 13:42:53 +00:00
|
|
|
object: 'Ticket',
|
|
|
|
o_id: ticket.id,
|
|
|
|
item: tag,
|
|
|
|
created_by_id: current_user.id,
|
2012-11-18 11:06:55 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2012-04-16 11:58:15 +00:00
|
|
|
# create article if given
|
|
|
|
if params[:article]
|
2016-05-22 09:37:44 +00:00
|
|
|
article_create(ticket, params[:article])
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
2012-04-16 12:56:04 +00:00
|
|
|
|
2015-04-27 13:42:53 +00:00
|
|
|
render json: ticket, status: :created
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
|
|
|
|
2013-08-06 22:10:28 +00:00
|
|
|
# PUT /api/v1/tickets/1
|
2012-04-10 14:06:46 +00:00
|
|
|
def update
|
2014-09-23 05:37:43 +00:00
|
|
|
ticket = Ticket.find(params[:id])
|
2012-04-10 14:06:46 +00:00
|
|
|
|
2016-01-27 18:26:10 +00:00
|
|
|
# permission check
|
2014-09-23 05:37:43 +00:00
|
|
|
return if !ticket_permission(ticket)
|
|
|
|
|
2016-05-22 09:37:44 +00:00
|
|
|
if ticket.update_attributes(Ticket.param_validation(params[:ticket]))
|
2012-09-04 21:28:49 +00:00
|
|
|
|
2014-09-23 05:37:43 +00:00
|
|
|
if params[:article]
|
2016-05-22 09:37:44 +00:00
|
|
|
article_create(ticket, params[:article])
|
2014-09-23 05:37:43 +00:00
|
|
|
end
|
2012-09-04 21:28:49 +00:00
|
|
|
|
2015-04-27 13:42:53 +00:00
|
|
|
render json: ticket, status: :ok
|
2012-04-12 11:27:01 +00:00
|
|
|
else
|
2015-04-27 13:42:53 +00:00
|
|
|
render json: ticket.errors, status: :unprocessable_entity
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-08-06 22:10:28 +00:00
|
|
|
# DELETE /api/v1/tickets/1
|
2012-04-10 14:06:46 +00:00
|
|
|
def destroy
|
2016-05-22 09:37:44 +00:00
|
|
|
ticket = Ticket.find(params[:id])
|
2012-09-04 21:28:49 +00:00
|
|
|
|
2015-02-15 09:23:55 +00:00
|
|
|
# permission check
|
2014-09-23 05:37:43 +00:00
|
|
|
return if !ticket_permission(ticket)
|
2012-09-04 21:28:49 +00:00
|
|
|
|
2014-09-23 05:37:43 +00:00
|
|
|
ticket.destroy
|
2012-04-10 14:06:46 +00:00
|
|
|
|
2012-04-12 11:27:01 +00:00
|
|
|
head :ok
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
2012-09-20 12:08:02 +00:00
|
|
|
|
2013-08-06 22:10:28 +00:00
|
|
|
# GET /api/v1/ticket_customer
|
|
|
|
# GET /api/v1/tickets_customer
|
2012-09-20 12:08:02 +00:00
|
|
|
def ticket_customer
|
2012-10-02 05:46:08 +00:00
|
|
|
|
2012-09-20 12:08:02 +00:00
|
|
|
# return result
|
2013-08-17 21:10:11 +00:00
|
|
|
result = Ticket::ScreenOptions.list_by_customer(
|
2015-04-27 13:42:53 +00:00
|
|
|
customer_id: params[:customer_id],
|
|
|
|
limit: 15,
|
2013-08-06 09:23:25 +00:00
|
|
|
)
|
2015-04-27 13:42:53 +00:00
|
|
|
render json: result
|
2012-09-20 12:08:02 +00:00
|
|
|
end
|
|
|
|
|
2013-08-06 22:10:28 +00:00
|
|
|
# GET /api/v1/ticket_history/1
|
2012-09-20 12:08:02 +00:00
|
|
|
def ticket_history
|
|
|
|
|
|
|
|
# get ticket data
|
2016-05-22 09:37:44 +00:00
|
|
|
ticket = Ticket.find(params[:id])
|
2012-09-20 12:08:02 +00:00
|
|
|
|
2016-01-27 18:26:10 +00:00
|
|
|
# permission check
|
2016-05-22 09:37:44 +00:00
|
|
|
return if !ticket_permission(ticket)
|
2012-09-20 12:08:02 +00:00
|
|
|
|
|
|
|
# get history of ticket
|
2013-09-29 16:40:42 +00:00
|
|
|
history = ticket.history_get(true)
|
2012-09-20 12:08:02 +00:00
|
|
|
|
|
|
|
# return result
|
2015-04-27 13:42:53 +00:00
|
|
|
render json: history
|
2012-09-20 12:08:02 +00:00
|
|
|
end
|
|
|
|
|
2014-09-21 19:58:07 +00:00
|
|
|
# GET /api/v1/ticket_related/1
|
|
|
|
def ticket_related
|
2012-10-18 19:23:05 +00:00
|
|
|
|
2016-05-22 09:37:44 +00:00
|
|
|
ticket = Ticket.find(params[:ticket_id])
|
2013-08-19 08:21:52 +00:00
|
|
|
assets = ticket.assets({})
|
|
|
|
|
|
|
|
# open tickets by customer
|
2016-05-22 09:37:44 +00:00
|
|
|
group_ids = Group.select('groups.id')
|
2016-01-15 17:22:57 +00:00
|
|
|
.joins(:users)
|
2016-05-22 09:37:44 +00:00
|
|
|
.where('groups_users.user_id = ?', current_user.id)
|
|
|
|
.map(&:id)
|
2015-04-30 15:53:03 +00:00
|
|
|
|
2014-09-21 19:58:07 +00:00
|
|
|
access_condition = [ 'group_id IN (?)', group_ids ]
|
|
|
|
|
2015-04-30 15:53:03 +00:00
|
|
|
ticket_lists = Ticket
|
|
|
|
.where(
|
|
|
|
customer_id: ticket.customer_id,
|
|
|
|
state_id: Ticket::State.by_category( 'open' )
|
|
|
|
)
|
|
|
|
.where(access_condition)
|
|
|
|
.where( 'id != ?', [ ticket.id ] )
|
|
|
|
.order('created_at DESC')
|
|
|
|
.limit(6)
|
2012-10-18 19:23:05 +00:00
|
|
|
|
2013-08-19 08:21:52 +00:00
|
|
|
# get related assets
|
2014-02-03 19:24:49 +00:00
|
|
|
ticket_ids_by_customer = []
|
2015-04-30 15:53:03 +00:00
|
|
|
ticket_lists.each {|ticket_list|
|
|
|
|
ticket_ids_by_customer.push ticket_list.id
|
|
|
|
assets = ticket_list.assets(assets)
|
2012-10-18 19:23:05 +00:00
|
|
|
}
|
|
|
|
|
2013-08-19 08:21:52 +00:00
|
|
|
ticket_ids_recent_viewed = []
|
2016-05-22 09:37:44 +00:00
|
|
|
recent_views = RecentView.list(current_user, 8, 'Ticket')
|
2015-04-30 15:53:03 +00:00
|
|
|
recent_views.each {|recent_view|
|
|
|
|
next if recent_view['object'] != 'Ticket'
|
|
|
|
ticket_ids_recent_viewed.push recent_view['o_id']
|
2016-05-22 09:37:44 +00:00
|
|
|
recent_view_ticket = Ticket.find(recent_view['o_id'])
|
2015-04-30 15:53:03 +00:00
|
|
|
assets = recent_view_ticket.assets(assets)
|
2013-08-19 08:21:52 +00:00
|
|
|
}
|
2012-10-18 19:23:05 +00:00
|
|
|
|
|
|
|
# return result
|
2015-04-27 13:42:53 +00:00
|
|
|
render json: {
|
|
|
|
assets: assets,
|
|
|
|
ticket_ids_by_customer: ticket_ids_by_customer,
|
|
|
|
ticket_ids_recent_viewed: ticket_ids_recent_viewed,
|
2012-10-18 19:23:05 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2013-08-06 22:10:28 +00:00
|
|
|
# GET /api/v1/ticket_merge/1/1
|
2012-09-20 12:08:02 +00:00
|
|
|
def ticket_merge
|
|
|
|
|
|
|
|
# check master ticket
|
2015-11-17 14:04:36 +00:00
|
|
|
ticket_master = Ticket.find_by(number: params[:master_ticket_number])
|
2012-09-20 12:08:02 +00:00
|
|
|
if !ticket_master
|
2015-04-27 13:42:53 +00:00
|
|
|
render json: {
|
|
|
|
result: 'faild',
|
|
|
|
message: 'No such master ticket number!',
|
2012-09-20 12:08:02 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2016-01-27 18:26:10 +00:00
|
|
|
# permission check
|
2012-09-20 12:08:02 +00:00
|
|
|
return if !ticket_permission(ticket_master)
|
|
|
|
|
|
|
|
# check slave ticket
|
2015-11-17 14:04:36 +00:00
|
|
|
ticket_slave = Ticket.find_by(id: params[:slave_ticket_id] )
|
2012-09-20 12:08:02 +00:00
|
|
|
if !ticket_slave
|
2015-04-27 13:42:53 +00:00
|
|
|
render json: {
|
|
|
|
result: 'faild',
|
|
|
|
message: 'No such slave ticket!',
|
2012-09-20 12:08:02 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2016-01-27 18:26:10 +00:00
|
|
|
# permission check
|
2016-05-22 09:37:44 +00:00
|
|
|
return if !ticket_permission(ticket_slave)
|
2012-09-20 12:08:02 +00:00
|
|
|
|
|
|
|
# check diffetent ticket ids
|
|
|
|
if ticket_slave.id == ticket_master.id
|
2015-04-27 13:42:53 +00:00
|
|
|
render json: {
|
|
|
|
result: 'faild',
|
|
|
|
message: 'Can\'t merge ticket with it self!',
|
2012-09-20 12:08:02 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
end
|
2013-06-12 15:59:58 +00:00
|
|
|
|
2012-09-20 12:08:02 +00:00
|
|
|
# merge ticket
|
2015-04-30 15:53:03 +00:00
|
|
|
ticket_slave.merge_to(
|
|
|
|
ticket_id: ticket_master.id,
|
|
|
|
created_by_id: current_user.id,
|
2013-06-12 15:59:58 +00:00
|
|
|
)
|
2012-09-20 12:08:02 +00:00
|
|
|
|
|
|
|
# return result
|
2015-04-27 13:42:53 +00:00
|
|
|
render json: {
|
|
|
|
result: 'success',
|
|
|
|
master_ticket: ticket_master.attributes,
|
|
|
|
slave_ticket: ticket_slave.attributes,
|
2012-09-20 12:08:02 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2013-08-06 22:10:28 +00:00
|
|
|
# GET /api/v1/ticket_full/1
|
2012-09-20 12:08:02 +00:00
|
|
|
def ticket_full
|
|
|
|
|
|
|
|
# permission check
|
2016-05-22 09:37:44 +00:00
|
|
|
ticket = Ticket.find(params[:id])
|
2015-11-20 11:06:50 +00:00
|
|
|
return if !ticket_permission(ticket)
|
2012-09-20 12:08:02 +00:00
|
|
|
|
2013-01-04 08:09:59 +00:00
|
|
|
# get attributes to update
|
2015-11-20 11:06:50 +00:00
|
|
|
attributes_to_change = Ticket::ScreenOptions.attributes_to_change(user: current_user, ticket: ticket)
|
2013-01-04 08:09:59 +00:00
|
|
|
|
2014-09-09 23:42:20 +00:00
|
|
|
# get related users
|
|
|
|
assets = attributes_to_change[:assets]
|
|
|
|
assets = ticket.assets(assets)
|
2013-01-04 08:09:59 +00:00
|
|
|
|
2012-09-20 12:08:02 +00:00
|
|
|
# get related articles
|
2016-05-22 09:37:44 +00:00
|
|
|
articles = Ticket::Article.where(ticket_id: params[:id]).order('created_at ASC, id ASC')
|
2012-09-20 12:08:02 +00:00
|
|
|
|
|
|
|
# get related users
|
2013-08-19 06:29:49 +00:00
|
|
|
article_ids = []
|
2012-09-20 12:08:02 +00:00
|
|
|
articles.each {|article|
|
|
|
|
|
|
|
|
# ignore internal article if customer is requesting
|
2015-05-08 08:15:45 +00:00
|
|
|
next if article.internal == true && role?(Z_ROLENAME_CUSTOMER)
|
2012-09-20 12:08:02 +00:00
|
|
|
|
|
|
|
# load article ids
|
2013-08-19 06:29:49 +00:00
|
|
|
article_ids.push article.id
|
2012-09-20 12:08:02 +00:00
|
|
|
|
2013-08-19 06:29:49 +00:00
|
|
|
# load assets
|
|
|
|
assets = article.assets(assets)
|
2012-09-20 12:08:02 +00:00
|
|
|
}
|
|
|
|
|
2014-09-22 15:47:33 +00:00
|
|
|
# get links
|
|
|
|
links = Link.list(
|
2015-04-27 13:42:53 +00:00
|
|
|
link_object: 'Ticket',
|
|
|
|
link_object_value: ticket.id,
|
2014-09-22 15:47:33 +00:00
|
|
|
)
|
|
|
|
link_list = []
|
|
|
|
links.each { |item|
|
|
|
|
link_list.push item
|
|
|
|
if item['link_object'] == 'Ticket'
|
2016-05-10 13:06:51 +00:00
|
|
|
linked_ticket = Ticket.lookup(id: item['link_object_value'])
|
2014-09-24 19:37:06 +00:00
|
|
|
assets = linked_ticket.assets(assets)
|
2014-09-22 15:47:33 +00:00
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
# get tags
|
|
|
|
tags = Tag.tag_list(
|
2015-04-27 13:42:53 +00:00
|
|
|
object: 'Ticket',
|
|
|
|
o_id: ticket.id,
|
2014-09-22 15:47:33 +00:00
|
|
|
)
|
|
|
|
|
2012-09-20 12:08:02 +00:00
|
|
|
# return result
|
2015-04-27 13:42:53 +00:00
|
|
|
render json: {
|
|
|
|
ticket_id: ticket.id,
|
|
|
|
ticket_article_ids: article_ids,
|
|
|
|
assets: assets,
|
|
|
|
links: link_list,
|
|
|
|
tags: tags,
|
2015-11-20 11:06:50 +00:00
|
|
|
form_meta: attributes_to_change[:form_meta],
|
2012-09-20 12:08:02 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2015-11-17 14:04:36 +00:00
|
|
|
# GET /api/v1/ticket_split
|
|
|
|
def ticket_split
|
|
|
|
|
|
|
|
# permission check
|
|
|
|
ticket = Ticket.find(params[:ticket_id])
|
|
|
|
return if !ticket_permission(ticket)
|
|
|
|
assets = ticket.assets({})
|
|
|
|
|
|
|
|
# get related articles
|
|
|
|
article = Ticket::Article.find(params[:article_id])
|
|
|
|
assets = article.assets(assets)
|
|
|
|
|
|
|
|
render json: {
|
|
|
|
assets: assets
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
# GET /api/v1/ticket_create
|
2012-09-20 12:08:02 +00:00
|
|
|
def ticket_create
|
|
|
|
|
2013-01-04 08:09:59 +00:00
|
|
|
# get attributes to update
|
2013-08-17 21:10:11 +00:00
|
|
|
attributes_to_change = Ticket::ScreenOptions.attributes_to_change(
|
2015-04-27 13:42:53 +00:00
|
|
|
user: current_user,
|
2012-09-20 12:08:02 +00:00
|
|
|
)
|
2015-11-17 14:04:36 +00:00
|
|
|
render json: attributes_to_change
|
2012-09-20 12:08:02 +00:00
|
|
|
end
|
|
|
|
|
2013-08-06 22:10:28 +00:00
|
|
|
# GET /api/v1/tickets/search
|
2012-11-14 01:05:53 +00:00
|
|
|
def search
|
2013-06-12 15:59:58 +00:00
|
|
|
|
2014-09-01 13:09:43 +00:00
|
|
|
# permit nested conditions
|
|
|
|
params.require(:condition).permit!
|
|
|
|
|
2012-11-14 01:05:53 +00:00
|
|
|
# build result list
|
2013-05-21 22:30:09 +00:00
|
|
|
tickets = Ticket.search(
|
2015-04-27 13:42:53 +00:00
|
|
|
limit: params[:limit],
|
|
|
|
query: params[:term],
|
|
|
|
condition: params[:condition],
|
|
|
|
current_user: current_user,
|
2013-05-21 22:30:09 +00:00
|
|
|
)
|
2013-08-19 06:29:49 +00:00
|
|
|
assets = {}
|
2013-05-21 22:30:09 +00:00
|
|
|
ticket_result = []
|
|
|
|
tickets.each do |ticket|
|
|
|
|
ticket_result.push ticket.id
|
2013-08-19 06:29:49 +00:00
|
|
|
assets = ticket.assets(assets)
|
2012-11-14 01:05:53 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# return result
|
2015-04-27 13:42:53 +00:00
|
|
|
render json: {
|
|
|
|
tickets: ticket_result,
|
|
|
|
tickets_count: tickets.count,
|
|
|
|
assets: assets,
|
2012-11-14 01:05:53 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2015-09-17 01:04:16 +00:00
|
|
|
# GET /api/v1/tickets/selector
|
|
|
|
def selector
|
|
|
|
return if deny_if_not_role(Z_ROLENAME_ADMIN)
|
|
|
|
|
|
|
|
ticket_count, tickets = Ticket.selectors(params[:condition], 6)
|
|
|
|
|
|
|
|
assets = {}
|
|
|
|
ticket_ids = []
|
|
|
|
if tickets
|
|
|
|
tickets.each do |ticket|
|
|
|
|
ticket_ids.push ticket.id
|
|
|
|
assets = ticket.assets(assets)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# return result
|
|
|
|
render json: {
|
|
|
|
ticket_ids: ticket_ids,
|
|
|
|
ticket_count: ticket_count || 0,
|
|
|
|
assets: assets,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2014-11-10 07:34:20 +00:00
|
|
|
# GET /api/v1/ticket_stats
|
2014-11-09 23:42:17 +00:00
|
|
|
def stats
|
2014-11-10 07:34:20 +00:00
|
|
|
|
|
|
|
if !params[:user_id] && !params[:organization_id]
|
2016-03-01 14:26:46 +00:00
|
|
|
raise 'Need user_id or organization_id as param'
|
2014-11-10 07:34:20 +00:00
|
|
|
end
|
2014-11-09 23:42:17 +00:00
|
|
|
|
2016-01-27 18:26:10 +00:00
|
|
|
# permission check
|
2014-11-09 23:42:17 +00:00
|
|
|
#return if !ticket_permission(ticket)
|
|
|
|
|
|
|
|
# lookup open user tickets
|
2014-11-10 07:34:20 +00:00
|
|
|
limit = 100
|
|
|
|
assets = {}
|
2015-11-30 10:50:02 +00:00
|
|
|
access_condition = Ticket.access_condition(current_user)
|
2015-04-27 20:49:17 +00:00
|
|
|
now = Time.zone.now
|
2014-11-10 07:34:20 +00:00
|
|
|
user_tickets_open_ids = []
|
|
|
|
user_tickets_closed_ids = []
|
|
|
|
user_ticket_volume_by_year = []
|
|
|
|
if params[:user_id]
|
2015-11-30 10:50:02 +00:00
|
|
|
user = User.lookup(id: params[:user_id])
|
2014-11-10 07:34:20 +00:00
|
|
|
condition = {
|
2015-09-23 14:25:06 +00:00
|
|
|
'ticket.state_id' => {
|
2015-09-17 18:39:51 +00:00
|
|
|
operator: 'is',
|
|
|
|
value: Ticket::State.by_category('open').map(&:id),
|
|
|
|
},
|
2015-09-23 14:25:06 +00:00
|
|
|
'ticket.customer_id' => {
|
2015-09-17 18:39:51 +00:00
|
|
|
operator: 'is',
|
|
|
|
value: user.id,
|
|
|
|
},
|
2014-11-10 07:34:20 +00:00
|
|
|
}
|
|
|
|
user_tickets_open = Ticket.search(
|
2015-04-27 13:42:53 +00:00
|
|
|
limit: limit,
|
|
|
|
condition: condition,
|
|
|
|
current_user: current_user,
|
2014-11-10 07:34:20 +00:00
|
|
|
)
|
|
|
|
user_tickets_open_ids = assets_of_tickets(user_tickets_open, assets)
|
2014-11-09 23:42:17 +00:00
|
|
|
|
2014-11-10 07:34:20 +00:00
|
|
|
# lookup closed user tickets
|
|
|
|
condition = {
|
2015-09-23 14:25:06 +00:00
|
|
|
'ticket.state_id' => {
|
2015-09-17 18:39:51 +00:00
|
|
|
operator: 'is',
|
|
|
|
value: Ticket::State.by_category('closed').map(&:id),
|
|
|
|
},
|
2015-09-23 14:25:06 +00:00
|
|
|
'ticket.customer_id' => {
|
2015-09-17 18:39:51 +00:00
|
|
|
operator: 'is',
|
|
|
|
value: user.id,
|
|
|
|
},
|
2014-11-10 07:34:20 +00:00
|
|
|
}
|
|
|
|
user_tickets_closed = Ticket.search(
|
2015-04-27 13:42:53 +00:00
|
|
|
limit: limit,
|
|
|
|
condition: condition,
|
|
|
|
current_user: current_user,
|
2014-11-10 07:34:20 +00:00
|
|
|
)
|
|
|
|
user_tickets_closed_ids = assets_of_tickets(user_tickets_closed, assets)
|
2014-11-09 23:42:17 +00:00
|
|
|
|
2014-11-10 07:34:20 +00:00
|
|
|
# generate stats by user
|
|
|
|
(0..11).each {|month_back|
|
2015-04-30 15:53:03 +00:00
|
|
|
date_to_check = now - month_back.month
|
2015-04-27 20:49:17 +00:00
|
|
|
date_start = "#{date_to_check.year}-#{date_to_check.month}-01 00:00:00"
|
2014-11-10 07:34:20 +00:00
|
|
|
date_end = "#{date_to_check.year}-#{date_to_check.month}-#{date_to_check.end_of_month.day} 00:00:00"
|
|
|
|
|
|
|
|
condition = {
|
|
|
|
'tickets.customer_id' => user.id,
|
|
|
|
}
|
|
|
|
|
|
|
|
# created
|
2015-04-30 15:53:03 +00:00
|
|
|
created = Ticket.where('created_at > ? AND created_at < ?', date_start, date_end )
|
2016-01-15 17:22:57 +00:00
|
|
|
.where(access_condition)
|
|
|
|
.where(condition)
|
|
|
|
.count
|
2014-11-10 07:34:20 +00:00
|
|
|
|
|
|
|
# closed
|
2015-04-30 15:53:03 +00:00
|
|
|
closed = Ticket.where('close_time > ? AND close_time < ?', date_start, date_end )
|
2016-01-15 17:22:57 +00:00
|
|
|
.where(access_condition)
|
|
|
|
.where(condition)
|
|
|
|
.count
|
2014-11-10 07:34:20 +00:00
|
|
|
|
|
|
|
data = {
|
2015-04-27 13:42:53 +00:00
|
|
|
month: date_to_check.month,
|
|
|
|
year: date_to_check.year,
|
|
|
|
text: Date::MONTHNAMES[date_to_check.month],
|
|
|
|
created: created,
|
|
|
|
closed: closed,
|
2014-11-10 07:34:20 +00:00
|
|
|
}
|
|
|
|
user_ticket_volume_by_year.push data
|
|
|
|
}
|
|
|
|
end
|
2014-11-09 23:42:17 +00:00
|
|
|
|
|
|
|
# lookup open org tickets
|
2014-11-10 07:34:20 +00:00
|
|
|
org_tickets_open_ids = []
|
|
|
|
org_tickets_closed_ids = []
|
|
|
|
org_ticket_volume_by_year = []
|
|
|
|
if params[:organization_id] && !params[:organization_id].empty?
|
|
|
|
|
2014-11-09 23:42:17 +00:00
|
|
|
condition = {
|
2015-09-23 14:25:06 +00:00
|
|
|
'ticket.state_id' => {
|
2015-09-17 18:39:51 +00:00
|
|
|
operator: 'is',
|
|
|
|
value: Ticket::State.by_category('open').map(&:id),
|
|
|
|
},
|
2015-09-23 14:25:06 +00:00
|
|
|
'ticket.organization_id' => {
|
2015-09-17 18:39:51 +00:00
|
|
|
operator: 'is',
|
|
|
|
value: params[:organization_id],
|
|
|
|
},
|
2014-11-09 23:42:17 +00:00
|
|
|
}
|
|
|
|
org_tickets_open = Ticket.search(
|
2015-04-27 13:42:53 +00:00
|
|
|
limit: limit,
|
|
|
|
condition: condition,
|
|
|
|
current_user: current_user,
|
2014-11-09 23:42:17 +00:00
|
|
|
)
|
|
|
|
org_tickets_open_ids = assets_of_tickets(org_tickets_open, assets)
|
|
|
|
|
2014-11-10 07:34:20 +00:00
|
|
|
# lookup closed org tickets
|
2014-11-09 23:42:17 +00:00
|
|
|
condition = {
|
2015-09-23 14:25:06 +00:00
|
|
|
'ticket.state_id' => {
|
2015-09-17 18:39:51 +00:00
|
|
|
operator: 'is',
|
|
|
|
value: Ticket::State.by_category('closed').map(&:id),
|
|
|
|
},
|
2015-09-23 14:25:06 +00:00
|
|
|
'ticket.organization_id' => {
|
2015-09-17 18:39:51 +00:00
|
|
|
operator: 'is',
|
|
|
|
value: params[:organization_id],
|
|
|
|
},
|
2014-11-09 23:42:17 +00:00
|
|
|
}
|
|
|
|
org_tickets_closed = Ticket.search(
|
2015-04-27 13:42:53 +00:00
|
|
|
limit: limit,
|
|
|
|
condition: condition,
|
|
|
|
current_user: current_user,
|
2014-11-09 23:42:17 +00:00
|
|
|
)
|
|
|
|
org_tickets_closed_ids = assets_of_tickets(org_tickets_closed, assets)
|
|
|
|
|
2014-11-10 07:34:20 +00:00
|
|
|
# generate stats by org
|
|
|
|
(0..11).each {|month_back|
|
2015-04-30 15:53:03 +00:00
|
|
|
date_to_check = now - month_back.month
|
2015-04-27 20:49:17 +00:00
|
|
|
date_start = "#{date_to_check.year}-#{date_to_check.month}-01 00:00:00"
|
2014-11-10 07:34:20 +00:00
|
|
|
date_end = "#{date_to_check.year}-#{date_to_check.month}-#{date_to_check.end_of_month.day} 00:00:00"
|
2014-11-09 23:42:17 +00:00
|
|
|
|
2014-11-10 07:34:20 +00:00
|
|
|
condition = {
|
|
|
|
'tickets.organization_id' => params[:organization_id],
|
|
|
|
}
|
2014-11-09 23:42:17 +00:00
|
|
|
|
2014-11-10 07:34:20 +00:00
|
|
|
# created
|
|
|
|
created = Ticket.where('created_at > ? AND created_at < ?', date_start, date_end ).where(condition).count
|
2014-11-09 23:42:17 +00:00
|
|
|
|
2014-11-10 07:34:20 +00:00
|
|
|
# closed
|
|
|
|
closed = Ticket.where('close_time > ? AND close_time < ?', date_start, date_end ).where(condition).count
|
2014-11-09 23:42:17 +00:00
|
|
|
|
2014-11-10 07:34:20 +00:00
|
|
|
data = {
|
2015-04-27 13:42:53 +00:00
|
|
|
month: date_to_check.month,
|
|
|
|
year: date_to_check.year,
|
|
|
|
text: Date::MONTHNAMES[date_to_check.month],
|
|
|
|
created: created,
|
|
|
|
closed: closed,
|
2014-11-10 07:34:20 +00:00
|
|
|
}
|
|
|
|
org_ticket_volume_by_year.push data
|
2014-11-09 23:42:17 +00:00
|
|
|
}
|
2014-11-10 07:34:20 +00:00
|
|
|
end
|
2014-11-09 23:42:17 +00:00
|
|
|
|
|
|
|
# return result
|
2015-04-27 13:42:53 +00:00
|
|
|
render json: {
|
|
|
|
user_tickets_open_ids: user_tickets_open_ids,
|
|
|
|
user_tickets_closed_ids: user_tickets_closed_ids,
|
|
|
|
org_tickets_open_ids: org_tickets_open_ids,
|
|
|
|
org_tickets_closed_ids: org_tickets_closed_ids,
|
|
|
|
user_ticket_volume_by_year: user_ticket_volume_by_year,
|
|
|
|
org_ticket_volume_by_year: org_ticket_volume_by_year,
|
|
|
|
assets: assets,
|
2014-11-09 23:42:17 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2014-09-23 05:37:43 +00:00
|
|
|
private
|
|
|
|
|
2014-11-09 23:42:17 +00:00
|
|
|
def assets_of_tickets(tickets, assets)
|
|
|
|
ticket_ids = []
|
|
|
|
tickets.each do |ticket|
|
|
|
|
ticket_ids.push ticket.id
|
|
|
|
assets = ticket.assets(assets)
|
|
|
|
end
|
2015-04-27 20:49:17 +00:00
|
|
|
ticket_ids
|
2014-11-09 23:42:17 +00:00
|
|
|
end
|
|
|
|
|
2014-09-23 05:37:43 +00:00
|
|
|
def article_create(ticket, params)
|
2014-11-09 23:42:17 +00:00
|
|
|
|
2014-09-23 05:37:43 +00:00
|
|
|
# create article if given
|
2016-01-15 17:22:57 +00:00
|
|
|
form_id = params[:form_id]
|
2014-09-23 05:37:43 +00:00
|
|
|
params.delete(:form_id)
|
2016-05-10 13:06:51 +00:00
|
|
|
article = Ticket::Article.new(Ticket::Article.param_validation( params ))
|
2014-09-23 05:37:43 +00:00
|
|
|
article.ticket_id = ticket.id
|
|
|
|
|
2016-05-10 13:06:51 +00:00
|
|
|
# store dataurl images to store
|
|
|
|
if form_id && article.body && article.content_type =~ %r{text/html}i
|
|
|
|
article.body.gsub!( %r{(<img\s.+?src=")(data:image/(jpeg|png);base64,.+?)">}i ) { |_item|
|
|
|
|
file_attributes = StaticAssets.data_url_attributes($2)
|
|
|
|
cid = "#{ticket.id}.#{form_id}.#{rand(999_999)}@#{Setting.get('fqdn')}"
|
|
|
|
headers_store = {
|
|
|
|
'Content-Type' => file_attributes[:mime_type],
|
|
|
|
'Mime-Type' => file_attributes[:mime_type],
|
|
|
|
'Content-ID' => cid,
|
|
|
|
'Content-Disposition' => 'inline',
|
|
|
|
}
|
|
|
|
store = Store.add(
|
|
|
|
object: 'UploadCache',
|
|
|
|
o_id: form_id,
|
|
|
|
data: file_attributes[:content],
|
|
|
|
filename: cid,
|
|
|
|
preferences: headers_store
|
|
|
|
)
|
|
|
|
"#{$1}cid:#{cid}\">"
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2014-09-23 05:37:43 +00:00
|
|
|
# find attachments in upload cache
|
|
|
|
if form_id
|
|
|
|
article.attachments = Store.list(
|
2015-04-27 13:42:53 +00:00
|
|
|
object: 'UploadCache',
|
|
|
|
o_id: form_id,
|
2014-09-23 05:37:43 +00:00
|
|
|
)
|
|
|
|
end
|
|
|
|
if !article.save
|
2015-04-27 13:42:53 +00:00
|
|
|
render json: article.errors, status: :unprocessable_entity
|
2014-09-23 05:37:43 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
# remove attachments from upload cache
|
2015-04-30 15:53:03 +00:00
|
|
|
return if !form_id
|
2015-04-30 15:25:04 +00:00
|
|
|
|
2015-04-30 15:53:03 +00:00
|
|
|
Store.remove(
|
|
|
|
object: 'UploadCache',
|
|
|
|
o_id: form_id,
|
|
|
|
)
|
2014-09-23 05:37:43 +00:00
|
|
|
end
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|