trabajo-afectivo/app/controllers/tickets_controller.rb

433 lines
11 KiB
Ruby
Raw Normal View History

# Copyright (C) 2012-2013 Zammad Foundation, http://zammad-foundation.org/
2012-04-10 14:06:46 +00:00
class TicketsController < ApplicationController
before_filter :authentication_check
# GET /api/v1/tickets
2012-04-10 14:06:46 +00:00
def index
@tickets = Ticket.all
render :json => @tickets
2012-04-10 14:06:46 +00:00
end
# GET /api/v1/tickets/1
2012-04-10 14:06:46 +00:00
def show
@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)
render :json => @ticket
2012-04-10 14:06:46 +00:00
end
# POST /api/v1/tickets
2012-04-10 14:06:46 +00:00
def create
@ticket = Ticket.new( Ticket.param_validation( params[:ticket] ) )
2012-04-10 14:06:46 +00:00
# check if article is given
if !params[:article]
render :json => 'article hash is missing', :status => :unprocessable_entity
return
end
# create ticket
if !@ticket.save
render :json => @ticket.errors, :status => :unprocessable_entity
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',
:o_id => @ticket.id,
:item => tag,
:created_by_id => current_user.id,
)
}
end
# create article if given
if params[:article]
2012-12-02 10:18:55 +00:00
form_id = params[:article][:form_id]
params[:article].delete(:form_id)
2013-08-22 20:29:24 +00:00
@article = Ticket::Article.new( Ticket::Article.param_validation( params[:article] ) )
@article.ticket_id = @ticket.id
2012-10-02 05:46:08 +00:00
# find attachments in upload cache
2012-12-02 10:18:55 +00:00
if form_id
@article.attachments = Store.list(
2012-12-02 10:18:55 +00:00
:object => 'UploadCache',
:o_id => form_id,
)
end
if !@article.save
render :json => @article.errors, :status => :unprocessable_entity
return
end
2012-12-02 10:18:55 +00:00
# remove attachments from upload cache
if form_id
2012-12-02 10:18:55 +00:00
Store.remove(
:object => 'UploadCache',
:o_id => form_id,
)
end
2012-04-10 14:06:46 +00:00
end
2012-04-16 12:56:04 +00:00
render :json => @ticket, :status => :created
2012-04-10 14:06:46 +00:00
end
# PUT /api/v1/tickets/1
2012-04-10 14:06:46 +00:00
def update
@ticket = Ticket.find(params[:id])
2012-09-04 21:28:49 +00:00
# permissin check
return if !ticket_permission(@ticket)
if @ticket.update_attributes( Ticket.param_validation( params[:ticket] ) )
render :json => @ticket, :status => :ok
else
render :json => @ticket.errors, :status => :unprocessable_entity
2012-04-10 14:06:46 +00:00
end
end
# DELETE /api/v1/tickets/1
2012-04-10 14:06:46 +00:00
def destroy
@ticket = Ticket.find( params[:id] )
2012-09-04 21:28:49 +00:00
# permissin check
return if !ticket_permission(@ticket)
2012-04-10 14:06:46 +00:00
@ticket.destroy
head :ok
2012-04-10 14:06:46 +00:00
end
# GET /api/v1/ticket_customer
# GET /api/v1/tickets_customer
def ticket_customer
2012-10-02 05:46:08 +00:00
# return result
result = Ticket::ScreenOptions.list_by_customer(
:customer_id => params[:customer_id],
:limit => 15,
)
render :json => {
:tickets => result
}
end
# GET /api/v1/ticket_history/1
def ticket_history
# get ticket data
2012-10-02 05:46:08 +00:00
ticket = Ticket.find( params[:id] )
# permissin check
2012-10-02 05:46:08 +00:00
return if !ticket_permission( ticket )
# get history of ticket
2013-06-04 12:52:56 +00:00
history = History.list( 'Ticket', params[:id], 'Ticket::Article' )
# get related assets
assets = ticket.assets({})
2013-06-14 08:27:01 +00:00
history_list = []
history.each do |item|
2013-06-14 08:06:17 +00:00
assets = item.assets(assets)
2013-06-14 08:06:17 +00:00
item_tmp = item.attributes
if item['history_object'] == 'Ticket::Article'
2013-06-14 08:06:17 +00:00
item_temp['type'] = 'Article ' + item['type'].to_s
else
2013-06-14 08:06:17 +00:00
item_tmp['type'] = 'Ticket ' + item['type'].to_s
end
item_tmp['history_type'] = item.history_type.name
item_tmp['history_object'] = item.history_object.name
if item.history_attribute
item_tmp['history_attribute'] = item.history_attribute.name
end
item_tmp.delete( 'history_attribute_id' )
item_tmp.delete( 'history_object_id' )
item_tmp.delete( 'history_type_id' )
item_tmp.delete( 'o_id' )
item_tmp.delete( 'updated_at' )
if item_tmp['id_to'] == nil && item_tmp['id_from'] == nil
item_tmp.delete( 'id_to' )
item_tmp.delete( 'id_from' )
end
if item_tmp['value_to'] == nil && item_tmp['value_from'] == nil
item_tmp.delete( 'value_to' )
item_tmp.delete( 'value_from' )
end
2013-06-14 08:06:17 +00:00
if item_tmp['related_history_object_id'] == nil
item_tmp.delete( 'related_history_object_id' )
end
if item_tmp['related_o_id'] == nil
item_tmp.delete( 'related_o_id' )
end
history_list.push item_tmp
end
# return result
render :json => {
:assets => assets,
:history => history_list,
}
end
# GET /api/v1/ticket_merge_list/1
2012-10-18 19:23:05 +00:00
def ticket_merge_list
ticket = Ticket.find( params[:ticket_id] )
assets = ticket.assets({})
# open tickets by customer
ticket_list = Ticket.where(
:customer_id => ticket.customer_id,
:ticket_state_id => Ticket::State.by_category( 'open' )
2012-10-18 19:23:05 +00:00
)
.where( 'id != ?', [ ticket.id ] )
.order('created_at DESC')
.limit(6)
2012-10-18 19:23:05 +00:00
# get related assets
ticket_ids_by_customer = []
2012-10-18 19:23:05 +00:00
ticket_list.each {|ticket|
ticket_ids_by_customer.push ticket.id
assets = ticket.assets(assets)
2012-10-18 19:23:05 +00:00
}
ticket_ids_recent_viewed = []
ticket_recent_view = RecentView.list( current_user, 8 )
ticket_recent_view.each {|item|
if item['recent_view_object'] == 'Ticket'
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 => {
: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
# GET /api/v1/ticket_merge/1/1
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 )
# 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
# merge ticket
success = ticket_slave.merge_to(
{
:ticket_id => ticket_master.id,
:created_by_id => current_user.id,
}
)
# return result
render :json => {
:result => 'success',
:master_ticket => ticket_master.attributes,
:slave_ticket => ticket_slave.attributes,
}
end
# GET /api/v1/ticket_full/1
def ticket_full
# permission check
2012-10-02 05:46:08 +00:00
ticket = Ticket.find( params[:id] )
return if !ticket_permission( ticket )
# log object as viewed
if !params[:do_not_log] || params[:do_not_log].to_i == 0
log_view( ticket )
end
2012-10-02 05:46:08 +00:00
# get signature
signature = {}
if ticket.group.signature
signature = ticket.group.signature.attributes
# replace tags
signature['body'] = NotificationFactory.build(
:locale => current_user.locale,
:string => signature['body'],
:objects => {
:ticket => ticket,
:user => current_user,
}
)
end
# get related users
assets = {}
assets[:users] = {}
assets = ticket.assets(assets)
2013-01-04 08:09:59 +00:00
# get attributes to update
attributes_to_change = Ticket::ScreenOptions.attributes_to_change( :user => current_user, :ticket => ticket )
2013-01-04 08:09:59 +00:00
attributes_to_change[:owner_id].each { |user_id|
if !assets[:users][user_id]
assets[:users][user_id] = User.user_data_full( user_id )
2013-01-04 08:09:59 +00:00
end
}
attributes_to_change[:group_id__owner_id].each {|group_id, user_ids|
user_ids.each {|user_id|
if !assets[:users][user_id]
assets[:users][user_id] = User.user_data_full( user_id )
2013-01-04 08:09:59 +00:00
end
}
}
# get related articles
articles = Ticket::Article.where( :ticket_id => params[:id] )
# get related users
article_ids = []
articles.each {|article|
# ignore internal article if customer is requesting
next if article.internal == true && is_role('Customer')
# load article ids
article_ids.push article.id
# load assets
assets = article.assets(assets)
}
# return result
render :json => {
:ticket_id => ticket.id,
:ticket_article_ids => article_ids,
:signature => signature,
:assets => assets,
:edit_form => attributes_to_change,
}
end
# GET /api/v1/ticket_create/1
def ticket_create
2013-01-04 08:09:59 +00:00
# get attributes to update
attributes_to_change = Ticket::ScreenOptions.attributes_to_change(
2013-01-04 08:09:59 +00:00
:user => current_user,
# :ticket_id => params[:ticket_id],
# :article_id => params[:article_id]
)
assets = {}
assets[:users] = {}
2013-01-04 08:09:59 +00:00
attributes_to_change[:owner_id].each { |user_id|
if !assets[:users][user_id]
assets[:users][user_id] = User.user_data_full( user_id )
2013-01-04 08:09:59 +00:00
end
}
attributes_to_change[:group_id__owner_id].each {|group_id, user_ids|
user_ids.each {|user_id|
if !assets[:users][user_id]
assets[:users][user_id] = User.user_data_full( user_id )
2013-01-04 08:09:59 +00:00
end
}
}
# split data
split = {}
if params[:ticket_id] && params[:article_id]
ticket = Ticket.find( params[:ticket_id] )
split[:ticket_id] = ticket.id
assets = ticket.assets(assets)
owner_ids = []
ticket.agent_of_group.each { |user|
owner_ids.push user.id
if !assets[:users][user.id]
assets[:users][user.id] = User.user_data_full( user.id )
end
}
2013-01-04 08:09:59 +00:00
# get related articles
article = Ticket::Article.find( params[:article_id] )
split[:article_id] = article.id
assets = article.assets(assets)
end
# return result
render :json => {
:split => split,
:assets => assets,
2013-01-04 08:09:59 +00:00
:edit_form => attributes_to_change,
}
end
# GET /api/v1/tickets/search
2012-11-14 01:05:53 +00:00
def search
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],
:current_user => current_user,
)
assets = {}
2013-05-21 22:30:09 +00:00
ticket_result = []
tickets.each do |ticket|
ticket_result.push ticket.id
assets = ticket.assets(assets)
2012-11-14 01:05:53 +00:00
end
# return result
render :json => {
2013-05-21 22:30:09 +00:00
:tickets => ticket_result,
:assets => assets,
2012-11-14 01:05:53 +00:00
}
end
2012-04-10 14:06:46 +00:00
end