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
|
|
|
|
before_filter :authentication_check
|
|
|
|
|
2013-08-06 22:10:28 +00:00
|
|
|
# GET /api/v1/tickets
|
2012-04-10 14:06:46 +00:00
|
|
|
def index
|
|
|
|
@tickets = Ticket.all
|
|
|
|
|
2012-04-12 11:27:01 +00:00
|
|
|
render :json => @tickets
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
|
|
|
|
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
|
|
|
|
2012-09-04 21:28:49 +00:00
|
|
|
# permissin check
|
|
|
|
return if !ticket_permission(@ticket)
|
|
|
|
|
2012-04-12 11:27:01 +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]
|
|
|
|
render :json => 'article hash is missing', :status => :unprocessable_entity
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
# create ticket
|
2014-09-23 05:37:43 +00:00
|
|
|
if !ticket.save
|
|
|
|
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?
|
|
|
|
tags = params[:tags].split /,/
|
|
|
|
tags.each {|tag|
|
|
|
|
Tag.tag_add(
|
|
|
|
:object => 'Ticket',
|
2014-09-23 05:37:43 +00:00
|
|
|
:o_id => ticket.id,
|
2012-11-18 11:06:55 +00:00
|
|
|
:item => tag,
|
|
|
|
:created_by_id => current_user.id,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2012-04-16 11:58:15 +00:00
|
|
|
# create article if given
|
|
|
|
if params[:article]
|
2014-09-23 05:37:43 +00:00
|
|
|
article_create( ticket, params[:article] )
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
2012-04-16 12:56:04 +00:00
|
|
|
|
2014-09-23 05:37:43 +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
|
|
|
|
2012-09-04 21:28:49 +00:00
|
|
|
# permissin check
|
2014-09-23 05:37:43 +00:00
|
|
|
return if !ticket_permission(ticket)
|
|
|
|
|
|
|
|
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]
|
|
|
|
article_create( ticket, params[:article] )
|
|
|
|
end
|
2012-09-04 21:28:49 +00:00
|
|
|
|
2014-09-23 05:37:43 +00:00
|
|
|
render :json => ticket, :status => :ok
|
2012-04-12 11:27:01 +00:00
|
|
|
else
|
2014-09-23 05:37:43 +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
|
2014-09-23 05:37:43 +00:00
|
|
|
ticket = Ticket.find( params[:id] )
|
2012-09-04 21:28:49 +00:00
|
|
|
|
|
|
|
# permissin 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(
|
2013-08-06 09:23:25 +00:00
|
|
|
:customer_id => params[:customer_id],
|
|
|
|
:limit => 15,
|
|
|
|
)
|
2012-09-20 12:08:02 +00:00
|
|
|
render :json => {
|
2013-08-06 09:23:25 +00:00
|
|
|
:tickets => 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
|
2012-10-02 05:46:08 +00:00
|
|
|
ticket = Ticket.find( params[:id] )
|
2012-09-20 12:08:02 +00:00
|
|
|
|
|
|
|
# permissin check
|
2012-10-02 05:46:08 +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
|
2013-09-29 16:40:42 +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
|
|
|
|
2013-08-19 08:21:52 +00:00
|
|
|
ticket = Ticket.find( params[:ticket_id] )
|
|
|
|
assets = ticket.assets({})
|
|
|
|
|
|
|
|
# open tickets by customer
|
2014-09-21 19:58:07 +00:00
|
|
|
group_ids = Group.select( 'groups.id' ).joins(:users).
|
|
|
|
where( 'groups_users.user_id = ?', current_user.id ).
|
|
|
|
where( 'groups.active = ?', true ).
|
|
|
|
map( &:id )
|
|
|
|
access_condition = [ 'group_id IN (?)', group_ids ]
|
2013-08-06 09:23:25 +00:00
|
|
|
ticket_list = Ticket.where(
|
2014-06-08 22:01:20 +00:00
|
|
|
:customer_id => ticket.customer_id,
|
|
|
|
:state_id => Ticket::State.by_category( 'open' )
|
2014-09-21 19:58:07 +00:00
|
|
|
|
2012-10-18 19:23:05 +00:00
|
|
|
)
|
2014-09-21 19:58:07 +00:00
|
|
|
.where(access_condition)
|
2013-06-12 15:59:58 +00:00
|
|
|
.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 = []
|
2012-10-18 19:23:05 +00:00
|
|
|
ticket_list.each {|ticket|
|
2013-08-19 08:21:52 +00:00
|
|
|
ticket_ids_by_customer.push ticket.id
|
2013-08-19 06:29:49 +00:00
|
|
|
assets = ticket.assets(assets)
|
2012-10-18 19:23:05 +00:00
|
|
|
}
|
|
|
|
|
2013-08-19 08:21:52 +00:00
|
|
|
|
|
|
|
ticket_ids_recent_viewed = []
|
2014-09-21 19:58:07 +00:00
|
|
|
ticket_recent_view = RecentView.list( current_user, 8, 'Ticket' )
|
2013-08-19 08:21:52 +00:00
|
|
|
ticket_recent_view.each {|item|
|
2014-09-21 19:58:07 +00:00
|
|
|
if item['object'] == 'Ticket'
|
2013-08-19 08:21:52 +00:00
|
|
|
ticket_ids_recent_viewed.push item['o_id']
|
|
|
|
ticket = Ticket.find( item['o_id'] )
|
|
|
|
assets = ticket.assets(assets)
|
|
|
|
end
|
|
|
|
}
|
2012-10-18 19:23:05 +00:00
|
|
|
|
|
|
|
# return result
|
|
|
|
render :json => {
|
2013-08-19 08:21:52 +00:00
|
|
|
: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
|
|
|
|
ticket_master = Ticket.where( :number => params[:master_ticket_number] ).first
|
|
|
|
if !ticket_master
|
|
|
|
render :json => {
|
|
|
|
:result => 'faild',
|
|
|
|
:message => 'No such master ticket number!',
|
|
|
|
}
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
# permissin check
|
|
|
|
return if !ticket_permission(ticket_master)
|
|
|
|
|
|
|
|
# check slave ticket
|
|
|
|
ticket_slave = Ticket.where( :id => params[:slave_ticket_id] ).first
|
|
|
|
if !ticket_slave
|
|
|
|
render :json => {
|
|
|
|
:result => 'faild',
|
|
|
|
:message => 'No such slave ticket!',
|
|
|
|
}
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
# permissin check
|
2012-10-02 05:46:08 +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
|
|
|
|
render :json => {
|
|
|
|
:result => 'faild',
|
|
|
|
:message => 'Can\'t merge ticket with it self!',
|
|
|
|
}
|
|
|
|
return
|
|
|
|
end
|
2013-06-12 15:59:58 +00:00
|
|
|
|
2012-09-20 12:08:02 +00:00
|
|
|
# merge ticket
|
|
|
|
success = 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
|
|
|
|
render :json => {
|
|
|
|
:result => 'success',
|
|
|
|
:master_ticket => ticket_master.attributes,
|
|
|
|
:slave_ticket => ticket_slave.attributes,
|
|
|
|
}
|
|
|
|
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
|
2012-10-02 05:46:08 +00:00
|
|
|
ticket = Ticket.find( params[:id] )
|
|
|
|
return if !ticket_permission( ticket )
|
2012-09-20 12:08:02 +00:00
|
|
|
|
2012-10-02 05:46:08 +00:00
|
|
|
# get signature
|
2012-10-03 19:45:04 +00:00
|
|
|
signature = {}
|
|
|
|
if ticket.group.signature
|
|
|
|
signature = ticket.group.signature.attributes
|
|
|
|
|
|
|
|
# replace tags
|
|
|
|
signature['body'] = NotificationFactory.build(
|
2013-01-04 14:28:55 +00:00
|
|
|
:locale => current_user.locale,
|
2012-10-03 19:45:04 +00:00
|
|
|
:string => signature['body'],
|
|
|
|
:objects => {
|
|
|
|
:ticket => ticket,
|
|
|
|
:user => current_user,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
end
|
2012-09-20 12:08:02 +00:00
|
|
|
|
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( :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
|
|
|
|
articles = Ticket::Article.where( :ticket_id => params[:id] )
|
|
|
|
|
|
|
|
# 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
|
|
|
|
next if article.internal == true && is_role('Customer')
|
|
|
|
|
|
|
|
# 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(
|
|
|
|
:link_object => 'Ticket',
|
|
|
|
:link_object_value => ticket.id,
|
|
|
|
)
|
|
|
|
link_list = []
|
|
|
|
links.each { |item|
|
|
|
|
link_list.push item
|
|
|
|
if item['link_object'] == 'Ticket'
|
2014-09-24 19:37:06 +00:00
|
|
|
linked_ticket = Ticket.lookup( :id => item['link_object_value'] )
|
|
|
|
assets = linked_ticket.assets(assets)
|
2014-09-22 15:47:33 +00:00
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
# get tags
|
|
|
|
tags = Tag.tag_list(
|
|
|
|
:object => 'Ticket',
|
|
|
|
:o_id => ticket.id,
|
|
|
|
)
|
|
|
|
|
2012-09-20 12:08:02 +00:00
|
|
|
# return result
|
|
|
|
render :json => {
|
2013-08-19 06:29:49 +00:00
|
|
|
:ticket_id => ticket.id,
|
|
|
|
:ticket_article_ids => article_ids,
|
|
|
|
:signature => signature,
|
|
|
|
:assets => assets,
|
2014-09-22 15:47:33 +00:00
|
|
|
:links => link_list,
|
|
|
|
:tags => tags,
|
2014-09-09 23:42:20 +00:00
|
|
|
:form_meta => attributes_to_change,
|
2013-08-19 06:29:49 +00:00
|
|
|
:edit_form => attributes_to_change,
|
2012-09-20 12:08:02 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2013-08-06 22:10:28 +00:00
|
|
|
# GET /api/v1/ticket_create/1
|
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(
|
2013-01-04 08:09:59 +00:00
|
|
|
:user => current_user,
|
2014-09-10 07:45:30 +00:00
|
|
|
:ticket_id => params[:ticket_id],
|
|
|
|
:article_id => params[:article_id]
|
2012-09-20 12:08:02 +00:00
|
|
|
)
|
|
|
|
|
2014-09-09 23:42:20 +00:00
|
|
|
assets = attributes_to_change[:assets]
|
2012-09-20 12:08:02 +00:00
|
|
|
# split data
|
2013-08-19 06:29:49 +00:00
|
|
|
split = {}
|
2012-09-20 12:08:02 +00:00
|
|
|
if params[:ticket_id] && params[:article_id]
|
|
|
|
ticket = Ticket.find( params[:ticket_id] )
|
2013-08-19 06:29:49 +00:00
|
|
|
split[:ticket_id] = ticket.id
|
|
|
|
assets = ticket.assets(assets)
|
2012-09-20 12:08:02 +00:00
|
|
|
|
|
|
|
# get related articles
|
|
|
|
article = Ticket::Article.find( params[:article_id] )
|
2013-08-19 06:29:49 +00:00
|
|
|
split[:article_id] = article.id
|
|
|
|
assets = article.assets(assets)
|
2012-09-20 12:08:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# return result
|
|
|
|
render :json => {
|
2013-08-19 06:29:49 +00:00
|
|
|
:split => split,
|
|
|
|
:assets => assets,
|
2014-09-10 07:45:30 +00:00
|
|
|
:form_meta => {
|
|
|
|
:filter => attributes_to_change[:filter],
|
|
|
|
:dependencies => attributes_to_change[:dependencies],
|
|
|
|
}
|
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(
|
|
|
|
:limit => params[:limit],
|
|
|
|
:query => params[:term],
|
2014-08-18 11:49:08 +00:00
|
|
|
:condition => params[:condition],
|
2013-05-21 22:30:09 +00:00
|
|
|
:current_user => current_user,
|
2014-08-18 11:49:08 +00:00
|
|
|
:detail => params[:detail]
|
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
|
|
|
|
render :json => {
|
2014-08-18 11:49:08 +00:00
|
|
|
:tickets => ticket_result,
|
|
|
|
:tickets_count => tickets.count,
|
|
|
|
:assets => assets,
|
2012-11-14 01:05:53 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2014-09-23 05:37:43 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def article_create(ticket, params)
|
|
|
|
puts params.inspect
|
|
|
|
# create article if given
|
|
|
|
form_id = params[:form_id]
|
|
|
|
params.delete(:form_id)
|
|
|
|
article = Ticket::Article.new( Ticket::Article.param_validation( params ) )
|
|
|
|
article.ticket_id = ticket.id
|
|
|
|
|
|
|
|
# find attachments in upload cache
|
|
|
|
if form_id
|
|
|
|
article.attachments = Store.list(
|
|
|
|
:object => 'UploadCache',
|
|
|
|
:o_id => form_id,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
if !article.save
|
|
|
|
render :json => article.errors, :status => :unprocessable_entity
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
# remove attachments from upload cache
|
|
|
|
if form_id
|
|
|
|
Store.remove(
|
|
|
|
:object => 'UploadCache',
|
|
|
|
:o_id => form_id,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|