2016-10-19 03:11:36 +00:00
|
|
|
# Copyright (C) 2012-2016 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
|
2017-03-09 11:44:51 +00:00
|
|
|
include CreatesTicketArticles
|
2017-08-28 21:31:26 +00:00
|
|
|
include ClonesTicketArticleAttachments
|
2018-02-02 11:27:19 +00:00
|
|
|
include ChecksUserAttributesByCurrentUserPermission
|
2017-04-10 08:06:11 +00:00
|
|
|
include TicketStats
|
2017-03-09 11:44:51 +00:00
|
|
|
|
2017-02-15 12:29:25 +00:00
|
|
|
prepend_before_action :authentication_check
|
2018-02-05 13:00:30 +00:00
|
|
|
before_action :follow_up_possible_check, only: :update
|
2012-04-10 14:06:46 +00:00
|
|
|
|
2016-06-06 15:26:37 +00:00
|
|
|
# GET /api/v1/tickets
|
|
|
|
def index
|
|
|
|
offset = 0
|
|
|
|
per_page = 100
|
|
|
|
|
|
|
|
if params[:page] && params[:per_page]
|
|
|
|
offset = (params[:page].to_i - 1) * params[:per_page].to_i
|
|
|
|
per_page = params[:per_page].to_i
|
|
|
|
end
|
|
|
|
|
2016-09-14 07:21:17 +00:00
|
|
|
if per_page > 100
|
|
|
|
per_page = 100
|
|
|
|
end
|
|
|
|
|
2017-06-16 20:43:09 +00:00
|
|
|
access_condition = Ticket.access_condition(current_user, 'read')
|
2016-09-21 17:42:47 +00:00
|
|
|
tickets = Ticket.where(access_condition).order(id: 'ASC').offset(offset).limit(per_page)
|
2016-06-06 15:26:37 +00:00
|
|
|
|
2017-12-14 13:19:24 +00:00
|
|
|
if response_expand?
|
2016-06-08 04:56:05 +00:00
|
|
|
list = []
|
2017-10-01 12:25:52 +00:00
|
|
|
tickets.each do |ticket|
|
2017-01-31 17:13:45 +00:00
|
|
|
list.push ticket.attributes_with_association_names
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2016-06-08 04:56:05 +00:00
|
|
|
render json: list, status: :ok
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2017-12-14 13:19:24 +00:00
|
|
|
if response_full?
|
2016-06-06 15:26:37 +00:00
|
|
|
assets = {}
|
|
|
|
item_ids = []
|
2017-10-01 12:25:52 +00:00
|
|
|
tickets.each do |item|
|
2016-06-06 15:26:37 +00:00
|
|
|
item_ids.push item.id
|
|
|
|
assets = item.assets(assets)
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2016-06-06 15:26:37 +00:00
|
|
|
render json: {
|
|
|
|
record_ids: item_ids,
|
|
|
|
assets: assets,
|
|
|
|
}, status: :ok
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
render json: tickets
|
|
|
|
end
|
|
|
|
|
2013-08-06 22:10:28 +00:00
|
|
|
# GET /api/v1/tickets/1
|
2012-04-10 14:06:46 +00:00
|
|
|
def show
|
2016-06-06 15:26:37 +00:00
|
|
|
ticket = Ticket.find(params[:id])
|
2017-06-16 20:43:09 +00:00
|
|
|
access!(ticket, 'read')
|
2016-06-06 15:26:37 +00:00
|
|
|
|
2017-12-14 13:19:24 +00:00
|
|
|
if response_expand?
|
2017-01-31 17:13:45 +00:00
|
|
|
result = ticket.attributes_with_association_names
|
2016-06-08 04:56:05 +00:00
|
|
|
render json: result, status: :ok
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2017-12-14 13:19:24 +00:00
|
|
|
if response_full?
|
2016-06-07 19:22:08 +00:00
|
|
|
full = Ticket.full(params[:id])
|
|
|
|
render json: full
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2017-12-14 13:19:24 +00:00
|
|
|
if response_all?
|
2016-06-07 19:22:08 +00:00
|
|
|
render json: ticket_all(ticket)
|
2016-06-06 15:26:37 +00:00
|
|
|
return
|
|
|
|
end
|
2012-09-04 21:28:49 +00:00
|
|
|
|
2016-06-06 15:26:37 +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
|
2018-02-02 11:27:19 +00:00
|
|
|
customer = {}
|
|
|
|
if params[:customer].class == ActionController::Parameters
|
|
|
|
customer = params[:customer]
|
|
|
|
params.delete(:customer)
|
|
|
|
end
|
|
|
|
|
2017-01-31 17:13:45 +00:00
|
|
|
clean_params = Ticket.association_name_to_id_convert(params)
|
2016-08-19 11:57:13 +00:00
|
|
|
|
2016-08-23 18:46:04 +00:00
|
|
|
# overwrite params
|
|
|
|
if !current_user.permissions?('ticket.agent')
|
2017-11-23 08:09:44 +00:00
|
|
|
%i[owner owner_id customer customer_id organization organization_id preferences].each do |key|
|
2016-08-23 18:46:04 +00:00
|
|
|
clean_params.delete(key)
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2016-08-23 18:46:04 +00:00
|
|
|
clean_params[:customer_id] = current_user.id
|
|
|
|
end
|
|
|
|
|
2016-08-19 11:57:13 +00:00
|
|
|
# try to create customer if needed
|
2018-02-02 11:27:19 +00:00
|
|
|
if clean_params[:customer_id].present? && clean_params[:customer_id] =~ /^guess:(.+?)$/
|
2016-08-19 11:57:13 +00:00
|
|
|
email = $1
|
|
|
|
if email !~ /@/ || email =~ /(>|<|\||\!|"|§|'|\$|%|&|\(|\)|\?|\s)/
|
2016-08-26 08:30:37 +00:00
|
|
|
render json: { error: 'Invalid email of customer' }, status: :unprocessable_entity
|
2016-08-19 11:57:13 +00:00
|
|
|
return
|
|
|
|
end
|
2018-02-02 11:27:19 +00:00
|
|
|
local_customer = User.find_by(email: email.downcase)
|
|
|
|
if !local_customer
|
2016-08-19 11:57:13 +00:00
|
|
|
role_ids = Role.signup_role_ids
|
2018-02-02 11:27:19 +00:00
|
|
|
local_customer = User.create(
|
2016-08-19 11:57:13 +00:00
|
|
|
firstname: '',
|
|
|
|
lastname: '',
|
|
|
|
email: email,
|
|
|
|
password: '',
|
|
|
|
active: true,
|
|
|
|
role_ids: role_ids,
|
|
|
|
)
|
|
|
|
end
|
2018-02-02 11:27:19 +00:00
|
|
|
clean_params[:customer_id] = local_customer.id
|
|
|
|
end
|
|
|
|
|
|
|
|
# try to create customer if needed
|
|
|
|
if clean_params[:customer_id].blank? && customer.present?
|
|
|
|
check_attributes_by_current_user_permission(customer)
|
|
|
|
clean_customer = User.association_name_to_id_convert(customer)
|
|
|
|
local_customer = nil
|
|
|
|
if !local_customer && clean_customer[:id].present?
|
|
|
|
local_customer = User.find_by(id: clean_customer[:id])
|
|
|
|
end
|
2018-02-05 13:00:30 +00:00
|
|
|
if !local_customer && clean_customer[:email].present?
|
2018-02-02 11:27:19 +00:00
|
|
|
local_customer = User.find_by(email: clean_customer[:email].downcase)
|
|
|
|
end
|
|
|
|
if !local_customer && clean_customer[:login].present?
|
|
|
|
local_customer = User.find_by(login: clean_customer[:login].downcase)
|
|
|
|
end
|
|
|
|
if !local_customer
|
|
|
|
role_ids = Role.signup_role_ids
|
|
|
|
local_customer = User.new(clean_customer)
|
|
|
|
local_customer.role_ids = role_ids
|
|
|
|
local_customer.save!
|
|
|
|
end
|
|
|
|
clean_params[:customer_id] = local_customer.id
|
2016-08-19 11:57:13 +00:00
|
|
|
end
|
|
|
|
|
2017-01-05 15:38:12 +00:00
|
|
|
clean_params = Ticket.param_cleanup(clean_params, true)
|
2016-06-06 15:26:37 +00:00
|
|
|
ticket = Ticket.new(clean_params)
|
2012-04-10 14:06:46 +00:00
|
|
|
|
2012-04-16 11:58:15 +00:00
|
|
|
# check if article is given
|
|
|
|
if !params[:article]
|
2016-06-06 15:26:37 +00:00
|
|
|
render json: { error: 'article hash is missing' }, status: :unprocessable_entity
|
2012-04-16 11:58:15 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
# create ticket
|
2016-08-23 18:46:04 +00:00
|
|
|
ticket.save!
|
2016-09-08 19:18:26 +00:00
|
|
|
ticket.with_lock do
|
|
|
|
|
|
|
|
# create tags if given
|
2017-09-08 08:28:34 +00:00
|
|
|
if params[:tags].present?
|
2016-09-08 19:18:26 +00:00
|
|
|
tags = params[:tags].split(/,/)
|
2017-10-01 12:25:52 +00:00
|
|
|
tags.each do |tag|
|
2017-04-12 07:34:49 +00:00
|
|
|
ticket.tag_add(tag)
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2016-09-08 19:18:26 +00:00
|
|
|
end
|
2012-10-02 05:46:08 +00:00
|
|
|
|
2016-09-08 19:18:26 +00:00
|
|
|
# create article if given
|
|
|
|
if params[:article]
|
|
|
|
article_create(ticket, params[:article])
|
|
|
|
end
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
2016-08-23 07:47:06 +00:00
|
|
|
# create links (e. g. in case of ticket split)
|
|
|
|
# links: {
|
|
|
|
# Ticket: {
|
|
|
|
# parent: [ticket_id1, ticket_id2, ...]
|
|
|
|
# normal: [ticket_id1, ticket_id2, ...]
|
|
|
|
# child: [ticket_id1, ticket_id2, ...]
|
|
|
|
# },
|
|
|
|
# }
|
2017-09-08 08:28:34 +00:00
|
|
|
if params[:links].present?
|
|
|
|
link = params[:links].permit!.to_h
|
|
|
|
raise Exceptions::UnprocessableEntity, 'Invalid link structure' if !link.is_a? Hash
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-10-01 12:25:52 +00:00
|
|
|
link.each do |target_object, link_types_with_object_ids|
|
2017-09-08 08:28:34 +00:00
|
|
|
raise Exceptions::UnprocessableEntity, 'Invalid link structure (Object)' if !link_types_with_object_ids.is_a? Hash
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-10-01 12:25:52 +00:00
|
|
|
link_types_with_object_ids.each do |link_type, object_ids|
|
2017-09-08 08:28:34 +00:00
|
|
|
raise Exceptions::UnprocessableEntity, 'Invalid link structure (Object->LinkType)' if !object_ids.is_a? Array
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-10-01 12:25:52 +00:00
|
|
|
object_ids.each do |local_object_id|
|
2016-08-23 07:47:06 +00:00
|
|
|
link = Link.add(
|
|
|
|
link_type: link_type,
|
|
|
|
link_object_target: target_object,
|
|
|
|
link_object_target_value: local_object_id,
|
|
|
|
link_object_source: 'Ticket',
|
|
|
|
link_object_source_value: ticket.id,
|
|
|
|
)
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-08-23 07:47:06 +00:00
|
|
|
end
|
|
|
|
|
2017-12-14 13:19:24 +00:00
|
|
|
if response_expand?
|
2017-01-31 17:13:45 +00:00
|
|
|
result = ticket.reload.attributes_with_association_names
|
2016-08-23 18:46:04 +00:00
|
|
|
render json: result, status: :created
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2017-12-14 13:19:24 +00:00
|
|
|
if response_full?
|
|
|
|
full = Ticket.full(ticket.id)
|
|
|
|
render json: full, status: :created
|
2016-09-04 21:24:19 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2017-12-14 13:19:24 +00:00
|
|
|
if response_all?
|
|
|
|
render json: ticket_all(ticket.reload), status: :created
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
render json: ticket.reload.attributes_with_association_ids, 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
|
2016-06-06 15:26:37 +00:00
|
|
|
ticket = Ticket.find(params[:id])
|
2017-06-16 20:43:09 +00:00
|
|
|
access!(ticket, 'change')
|
2014-09-23 05:37:43 +00:00
|
|
|
|
2017-01-31 17:13:45 +00:00
|
|
|
clean_params = Ticket.association_name_to_id_convert(params)
|
2016-06-06 15:26:37 +00:00
|
|
|
clean_params = Ticket.param_cleanup(clean_params, true)
|
|
|
|
|
2018-07-26 14:24:31 +00:00
|
|
|
# only apply preferences changes (keep not updated keys/values)
|
|
|
|
clean_params = ticket.param_preferences_merge(clean_params)
|
|
|
|
|
2016-08-23 18:46:04 +00:00
|
|
|
# overwrite params
|
|
|
|
if !current_user.permissions?('ticket.agent')
|
2017-11-23 08:09:44 +00:00
|
|
|
%i[owner owner_id customer customer_id organization organization_id preferences].each do |key|
|
2016-08-23 18:46:04 +00:00
|
|
|
clean_params.delete(key)
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2016-08-23 18:46:04 +00:00
|
|
|
end
|
2012-09-04 21:28:49 +00:00
|
|
|
|
2016-09-08 19:18:26 +00:00
|
|
|
ticket.with_lock do
|
2017-09-11 11:16:08 +00:00
|
|
|
ticket.update!(clean_params)
|
2016-09-08 19:18:26 +00:00
|
|
|
if params[:article]
|
|
|
|
article_create(ticket, params[:article])
|
|
|
|
end
|
2016-08-23 18:46:04 +00:00
|
|
|
end
|
2016-06-08 04:56:05 +00:00
|
|
|
|
2017-12-14 13:19:24 +00:00
|
|
|
if response_expand?
|
2017-01-31 17:13:45 +00:00
|
|
|
result = ticket.reload.attributes_with_association_names
|
2016-08-23 18:46:04 +00:00
|
|
|
render json: result, status: :ok
|
|
|
|
return
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
2016-08-23 18:46:04 +00:00
|
|
|
|
2017-12-14 13:19:24 +00:00
|
|
|
if response_full?
|
|
|
|
full = Ticket.full(params[:id])
|
|
|
|
render json: full, status: :ok
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
if response_all?
|
|
|
|
render json: ticket_all(ticket.reload), status: :ok
|
2016-09-04 21:24:19 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2017-12-14 13:19:24 +00:00
|
|
|
render json: ticket.reload.attributes_with_association_ids, status: :ok
|
2012-04-10 14:06:46 +00:00
|
|
|
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-06-06 15:26:37 +00:00
|
|
|
ticket = Ticket.find(params[:id])
|
2017-06-16 20:43:09 +00:00
|
|
|
access!(ticket, 'delete')
|
2012-09-04 21:28:49 +00:00
|
|
|
|
2016-08-23 18:46:04 +00:00
|
|
|
raise Exceptions::NotAuthorized, 'Not authorized (admin permission required)!' if !current_user.permissions?('admin')
|
|
|
|
|
|
|
|
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])
|
2017-06-16 20:43:09 +00:00
|
|
|
access!(ticket, 'read')
|
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
|
2017-06-16 20:43:09 +00:00
|
|
|
access_condition = Ticket.access_condition(current_user, 'read')
|
2014-09-21 19:58:07 +00:00
|
|
|
|
2015-04-30 15:53:03 +00:00
|
|
|
ticket_lists = Ticket
|
|
|
|
.where(
|
|
|
|
customer_id: ticket.customer_id,
|
2017-03-27 14:06:18 +00:00
|
|
|
state_id: Ticket::State.by_category(:open)
|
2015-04-30 15:53:03 +00:00
|
|
|
)
|
|
|
|
.where(access_condition)
|
2016-05-22 21:17:46 +00:00
|
|
|
.where('id != ?', [ ticket.id ])
|
2015-04-30 15:53:03 +00:00
|
|
|
.order('created_at DESC')
|
|
|
|
.limit(6)
|
2012-10-18 19:23:05 +00:00
|
|
|
|
2016-06-08 13:25:39 +00:00
|
|
|
# if we do not have open related tickets, search for any tickets
|
2017-11-23 08:09:44 +00:00
|
|
|
if ticket_lists.blank?
|
2016-06-08 13:25:39 +00:00
|
|
|
ticket_lists = Ticket
|
|
|
|
.where(
|
|
|
|
customer_id: ticket.customer_id,
|
2017-01-05 15:38:12 +00:00
|
|
|
).where.not(
|
2017-03-27 14:06:18 +00:00
|
|
|
state_id: Ticket::State.by_category(:merged)
|
2016-06-08 13:25:39 +00:00
|
|
|
)
|
|
|
|
.where(access_condition)
|
|
|
|
.where('id != ?', [ ticket.id ])
|
|
|
|
.order('created_at DESC')
|
|
|
|
.limit(6)
|
|
|
|
end
|
|
|
|
|
2013-08-19 08:21:52 +00:00
|
|
|
# get related assets
|
2014-02-03 19:24:49 +00:00
|
|
|
ticket_ids_by_customer = []
|
2017-10-01 12:25:52 +00:00
|
|
|
ticket_lists.each do |ticket_list|
|
2015-04-30 15:53:03 +00:00
|
|
|
ticket_ids_by_customer.push ticket_list.id
|
|
|
|
assets = ticket_list.assets(assets)
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2012-10-18 19:23:05 +00:00
|
|
|
|
2013-08-19 08:21:52 +00:00
|
|
|
ticket_ids_recent_viewed = []
|
2018-03-20 12:16:17 +00:00
|
|
|
recent_views = RecentView.list(current_user, 8, 'Ticket')
|
2017-10-01 12:25:52 +00:00
|
|
|
recent_views.each do |recent_view|
|
2018-03-20 12:16:17 +00:00
|
|
|
next if recent_view.object.name != 'Ticket'
|
|
|
|
next if recent_view.o_id == ticket.id
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2018-03-20 12:16:17 +00:00
|
|
|
ticket_ids_recent_viewed.push recent_view.o_id
|
|
|
|
recent_view_ticket = Ticket.find(recent_view.o_id)
|
2017-01-05 15:38:12 +00:00
|
|
|
assets = recent_view_ticket.assets(assets)
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
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: {
|
2017-02-24 14:02:58 +00:00
|
|
|
result: 'failed',
|
2015-04-27 13:42:53 +00:00
|
|
|
message: 'No such master ticket number!',
|
2012-09-20 12:08:02 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
end
|
2018-03-04 04:11:04 +00:00
|
|
|
access!(ticket_master, 'change')
|
2012-09-20 12:08:02 +00:00
|
|
|
|
|
|
|
# check slave ticket
|
2016-05-22 21:17:46 +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: {
|
2017-02-24 14:02:58 +00:00
|
|
|
result: 'failed',
|
2015-04-27 13:42:53 +00:00
|
|
|
message: 'No such slave ticket!',
|
2012-09-20 12:08:02 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
end
|
2018-03-04 04:11:04 +00:00
|
|
|
access!(ticket_slave, 'change')
|
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
|
|
|
|
|
2015-11-17 14:04:36 +00:00
|
|
|
# GET /api/v1/ticket_split
|
|
|
|
def ticket_split
|
|
|
|
ticket = Ticket.find(params[:ticket_id])
|
2017-06-16 20:43:09 +00:00
|
|
|
access!(ticket, 'read')
|
2015-11-17 14:04:36 +00:00
|
|
|
assets = ticket.assets({})
|
|
|
|
|
|
|
|
article = Ticket::Article.find(params[:article_id])
|
2017-08-28 21:31:26 +00:00
|
|
|
access!(article.ticket, 'read')
|
2015-11-17 14:04:36 +00:00
|
|
|
assets = article.assets(assets)
|
|
|
|
|
|
|
|
render json: {
|
2017-12-05 14:49:59 +00:00
|
|
|
assets: assets,
|
2017-08-28 21:31:26 +00:00
|
|
|
attachments: article_attachments_clone(article),
|
2015-11-17 14:04:36 +00:00
|
|
|
}
|
|
|
|
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(
|
2017-06-16 20:43:09 +00:00
|
|
|
current_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
|
2016-09-11 08:46:01 +00:00
|
|
|
if params[:condition]
|
|
|
|
params.require(:condition).permit!
|
|
|
|
end
|
2014-09-01 13:09:43 +00:00
|
|
|
|
2018-04-13 07:22:55 +00:00
|
|
|
per_page = params[:per_page] || params[:limit] || 50
|
|
|
|
per_page = per_page.to_i
|
|
|
|
if per_page > 200
|
|
|
|
per_page = 200
|
2016-09-14 07:21:17 +00:00
|
|
|
end
|
2018-04-13 07:22:55 +00:00
|
|
|
page = params[:page] || 1
|
|
|
|
page = page.to_i
|
|
|
|
offset = (page - 1) * per_page
|
2016-09-14 07:21:17 +00:00
|
|
|
|
2017-11-23 08:09:44 +00:00
|
|
|
query = params[:query]
|
|
|
|
if query.respond_to?(:permit!)
|
|
|
|
query = query.permit!.to_h
|
|
|
|
end
|
|
|
|
|
2012-11-14 01:05:53 +00:00
|
|
|
# build result list
|
2013-05-21 22:30:09 +00:00
|
|
|
tickets = Ticket.search(
|
2017-11-23 08:09:44 +00:00
|
|
|
query: query,
|
|
|
|
condition: params[:condition].to_h,
|
2018-04-13 07:22:55 +00:00
|
|
|
limit: per_page,
|
|
|
|
offset: offset,
|
2018-07-18 14:00:06 +00:00
|
|
|
order_by: params[:order_by],
|
|
|
|
sort_by: params[:sort_by],
|
2015-04-27 13:42:53 +00:00
|
|
|
current_user: current_user,
|
2013-05-21 22:30:09 +00:00
|
|
|
)
|
2016-06-08 04:56:05 +00:00
|
|
|
|
2017-12-14 13:19:24 +00:00
|
|
|
if response_expand?
|
2016-06-08 04:56:05 +00:00
|
|
|
list = []
|
2017-10-01 12:25:52 +00:00
|
|
|
tickets.each do |ticket|
|
2017-01-31 17:13:45 +00:00
|
|
|
list.push ticket.attributes_with_association_names
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2016-06-08 04:56:05 +00:00
|
|
|
render json: list, status: :ok
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
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
|
2016-08-12 16:39:09 +00:00
|
|
|
permission_check('admin.*')
|
2015-09-17 01:04:16 +00:00
|
|
|
|
|
|
|
ticket_count, tickets = Ticket.selectors(params[:condition], 6)
|
|
|
|
|
|
|
|
assets = {}
|
|
|
|
ticket_ids = []
|
2017-11-23 08:09:44 +00:00
|
|
|
tickets&.each do |ticket|
|
|
|
|
ticket_ids.push ticket.id
|
|
|
|
assets = ticket.assets(assets)
|
2015-09-17 01:04:16 +00:00
|
|
|
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
|
|
|
|
|
|
|
# lookup open user tickets
|
2017-04-10 08:06:11 +00:00
|
|
|
limit = 100
|
|
|
|
assets = {}
|
2017-06-16 20:43:09 +00:00
|
|
|
access_condition = Ticket.access_condition(current_user, 'read')
|
2017-04-10 08:06:11 +00:00
|
|
|
|
|
|
|
user_tickets = {}
|
2014-11-10 07:34:20 +00:00
|
|
|
if params[:user_id]
|
2015-11-30 10:50:02 +00:00
|
|
|
user = User.lookup(id: params[:user_id])
|
2017-01-05 15:38:12 +00:00
|
|
|
if !user
|
|
|
|
raise "No such user with id #{params[:user_id]}"
|
|
|
|
end
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-04-10 08:06:11 +00:00
|
|
|
conditions = {
|
|
|
|
closed_ids: {
|
|
|
|
'ticket.state_id' => {
|
|
|
|
operator: 'is',
|
|
|
|
value: Ticket::State.by_category(:closed).pluck(:id),
|
|
|
|
},
|
|
|
|
'ticket.customer_id' => {
|
|
|
|
operator: 'is',
|
|
|
|
value: user.id,
|
|
|
|
},
|
2015-09-17 18:39:51 +00:00
|
|
|
},
|
2017-04-10 08:06:11 +00:00
|
|
|
open_ids: {
|
|
|
|
'ticket.state_id' => {
|
|
|
|
operator: 'is',
|
|
|
|
value: Ticket::State.by_category(:open).pluck(:id),
|
|
|
|
},
|
|
|
|
'ticket.customer_id' => {
|
|
|
|
operator: 'is',
|
|
|
|
value: user.id,
|
|
|
|
},
|
2015-09-17 18:39:51 +00:00
|
|
|
},
|
2014-11-10 07:34:20 +00:00
|
|
|
}
|
2017-10-01 12:25:52 +00:00
|
|
|
conditions.each do |key, local_condition|
|
2017-04-10 08:06:11 +00:00
|
|
|
user_tickets[key] = ticket_ids_and_assets(local_condition, current_user, limit, assets)
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2014-11-09 23:42:17 +00:00
|
|
|
|
2014-11-10 07:34:20 +00:00
|
|
|
# generate stats by user
|
2017-04-10 08:06:11 +00:00
|
|
|
condition = {
|
|
|
|
'tickets.customer_id' => user.id,
|
2014-11-10 07:34:20 +00:00
|
|
|
}
|
2017-04-10 08:06:11 +00:00
|
|
|
user_tickets[:volume_by_year] = ticket_stats_last_year(condition, access_condition)
|
|
|
|
|
2014-11-10 07:34:20 +00:00
|
|
|
end
|
2014-11-09 23:42:17 +00:00
|
|
|
|
|
|
|
# lookup open org tickets
|
2017-04-10 08:06:11 +00:00
|
|
|
org_tickets = {}
|
2017-11-23 08:09:44 +00:00
|
|
|
if params[:organization_id].present?
|
2017-04-10 08:06:11 +00:00
|
|
|
organization = Organization.lookup(id: params[:organization_id])
|
|
|
|
if !organization
|
|
|
|
raise "No such organization with id #{params[:organization_id]}"
|
|
|
|
end
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-04-10 08:06:11 +00:00
|
|
|
conditions = {
|
|
|
|
closed_ids: {
|
|
|
|
'ticket.state_id' => {
|
|
|
|
operator: 'is',
|
|
|
|
value: Ticket::State.by_category(:closed).pluck(:id),
|
|
|
|
},
|
|
|
|
'ticket.organization_id' => {
|
|
|
|
operator: 'is',
|
|
|
|
value: organization.id,
|
|
|
|
},
|
2015-09-17 18:39:51 +00:00
|
|
|
},
|
2017-04-10 08:06:11 +00:00
|
|
|
open_ids: {
|
|
|
|
'ticket.state_id' => {
|
|
|
|
operator: 'is',
|
|
|
|
value: Ticket::State.by_category(:open).pluck(:id),
|
|
|
|
},
|
|
|
|
'ticket.organization_id' => {
|
|
|
|
operator: 'is',
|
|
|
|
value: organization.id,
|
|
|
|
},
|
2015-09-17 18:39:51 +00:00
|
|
|
},
|
2014-11-09 23:42:17 +00:00
|
|
|
}
|
2017-10-01 12:25:52 +00:00
|
|
|
conditions.each do |key, local_condition|
|
2017-04-10 08:06:11 +00:00
|
|
|
org_tickets[key] = ticket_ids_and_assets(local_condition, current_user, limit, assets)
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2014-11-09 23:42:17 +00:00
|
|
|
|
2014-11-10 07:34:20 +00:00
|
|
|
# generate stats by org
|
2017-04-10 08:06:11 +00:00
|
|
|
condition = {
|
|
|
|
'tickets.organization_id' => organization.id,
|
2014-11-09 23:42:17 +00:00
|
|
|
}
|
2017-04-10 08:06:11 +00:00
|
|
|
org_tickets[:volume_by_year] = ticket_stats_last_year(condition, access_condition)
|
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: {
|
2017-04-10 08:06:11 +00:00
|
|
|
user: user_tickets,
|
|
|
|
organization: org_tickets,
|
2015-04-27 13:42:53 +00:00
|
|
|
assets: assets,
|
2014-11-09 23:42:17 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2018-02-20 04:29:30 +00:00
|
|
|
# @path [GET] /tickets/import_example
|
|
|
|
#
|
|
|
|
# @summary Download of example CSV file.
|
|
|
|
# @notes The requester have 'admin' permissions to be able to download it.
|
|
|
|
# @example curl -u 'me@example.com:test' http://localhost:3000/api/v1/tickets/import_example
|
|
|
|
#
|
|
|
|
# @response_message 200 File download.
|
|
|
|
# @response_message 401 Invalid session.
|
|
|
|
def import_example
|
|
|
|
permission_check('admin')
|
|
|
|
csv_string = Ticket.csv_example(
|
|
|
|
col_sep: ',',
|
|
|
|
)
|
|
|
|
send_data(
|
|
|
|
csv_string,
|
|
|
|
filename: 'example.csv',
|
|
|
|
type: 'text/csv',
|
|
|
|
disposition: 'attachment'
|
|
|
|
)
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
# @path [POST] /tickets/import
|
|
|
|
#
|
|
|
|
# @summary Starts import.
|
|
|
|
# @notes The requester have 'admin' permissions to be create a new import.
|
|
|
|
# @example curl -u 'me@example.com:test' -F 'file=@/path/to/file/tickets.csv' 'https://your.zammad/api/v1/tickets/import?try=true'
|
|
|
|
# @example curl -u 'me@example.com:test' -F 'file=@/path/to/file/tickets.csv' 'https://your.zammad/api/v1/tickets/import'
|
|
|
|
#
|
|
|
|
# @response_message 201 Import started.
|
|
|
|
# @response_message 401 Invalid session.
|
|
|
|
def import_start
|
|
|
|
permission_check('admin')
|
|
|
|
if Setting.get('import_mode') != true
|
|
|
|
raise 'Only can import tickets if system is in import mode.'
|
|
|
|
end
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2018-11-06 05:42:52 +00:00
|
|
|
string = params[:data]
|
|
|
|
if string.blank? && params[:file].present?
|
|
|
|
string = params[:file].read.force_encoding('utf-8')
|
|
|
|
end
|
|
|
|
raise Exceptions::UnprocessableEntity, 'No source data submitted!' if string.blank?
|
|
|
|
|
2018-02-20 04:29:30 +00:00
|
|
|
result = Ticket.csv_import(
|
2018-06-06 01:30:17 +00:00
|
|
|
string: string,
|
2018-02-20 04:29:30 +00:00
|
|
|
parse_params: {
|
2018-06-06 01:30:17 +00:00
|
|
|
col_sep: params[:col_sep] || ',',
|
2018-02-20 04:29:30 +00:00
|
|
|
},
|
|
|
|
try: params[:try],
|
|
|
|
)
|
|
|
|
render json: result, status: :ok
|
|
|
|
end
|
|
|
|
|
2014-09-23 05:37:43 +00:00
|
|
|
private
|
|
|
|
|
2018-02-05 13:00:30 +00:00
|
|
|
def follow_up_possible_check
|
|
|
|
ticket = Ticket.find(params[:id])
|
|
|
|
|
|
|
|
return true if ticket.group.follow_up_possible != 'new_ticket' # check if the setting for follow_up_possible is disabled
|
|
|
|
return true if ticket.state.name != 'closed' # check if the ticket state is already closed
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2018-02-05 13:00:30 +00:00
|
|
|
raise Exceptions::UnprocessableEntity, 'Cannot follow up on a closed ticket. Please create a new ticket.'
|
|
|
|
end
|
|
|
|
|
2016-06-07 19:22:08 +00:00
|
|
|
def ticket_all(ticket)
|
2016-06-06 15:26:37 +00:00
|
|
|
|
|
|
|
# get attributes to update
|
2017-06-16 20:43:09 +00:00
|
|
|
attributes_to_change = Ticket::ScreenOptions.attributes_to_change(
|
|
|
|
current_user: current_user,
|
|
|
|
ticket: ticket
|
|
|
|
)
|
2016-06-06 15:26:37 +00:00
|
|
|
|
|
|
|
# get related users
|
|
|
|
assets = attributes_to_change[:assets]
|
|
|
|
assets = ticket.assets(assets)
|
|
|
|
|
|
|
|
# get related users
|
|
|
|
article_ids = []
|
2017-10-01 12:25:52 +00:00
|
|
|
ticket.articles.each do |article|
|
2016-06-06 15:26:37 +00:00
|
|
|
|
|
|
|
# ignore internal article if customer is requesting
|
2016-08-12 16:39:09 +00:00
|
|
|
next if article.internal == true && current_user.permissions?('ticket.customer')
|
2016-06-06 15:26:37 +00:00
|
|
|
|
|
|
|
article_ids.push article.id
|
|
|
|
assets = article.assets(assets)
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2016-06-06 15:26:37 +00:00
|
|
|
|
|
|
|
# get links
|
|
|
|
links = Link.list(
|
|
|
|
link_object: 'Ticket',
|
|
|
|
link_object_value: ticket.id,
|
|
|
|
)
|
|
|
|
link_list = []
|
2017-10-01 12:25:52 +00:00
|
|
|
links.each do |item|
|
2016-06-06 15:26:37 +00:00
|
|
|
link_list.push item
|
|
|
|
if item['link_object'] == 'Ticket'
|
|
|
|
linked_ticket = Ticket.lookup(id: item['link_object_value'])
|
|
|
|
assets = linked_ticket.assets(assets)
|
|
|
|
end
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2016-06-06 15:26:37 +00:00
|
|
|
|
|
|
|
# get tags
|
2017-04-12 07:34:49 +00:00
|
|
|
tags = ticket.tag_list
|
2016-06-06 15:26:37 +00:00
|
|
|
|
|
|
|
# return result
|
|
|
|
{
|
|
|
|
ticket_id: ticket.id,
|
|
|
|
ticket_article_ids: article_ids,
|
|
|
|
assets: assets,
|
|
|
|
links: link_list,
|
|
|
|
tags: tags,
|
|
|
|
form_meta: attributes_to_change[:form_meta],
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|