2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 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
|
2021-09-07 10:34:10 +00:00
|
|
|
include CanPaginate
|
2017-03-09 11:44:51 +00:00
|
|
|
|
2020-05-05 08:50:03 +00:00
|
|
|
prepend_before_action -> { authorize! }, only: %i[create selector import_example import_start ticket_customer ticket_history ticket_related ticket_recent ticket_merge ticket_split]
|
2017-02-15 12:29:25 +00:00
|
|
|
prepend_before_action :authentication_check
|
2012-04-10 14:06:46 +00:00
|
|
|
|
2016-06-06 15:26:37 +00:00
|
|
|
# GET /api/v1/tickets
|
|
|
|
def index
|
2021-08-17 05:44:54 +00:00
|
|
|
paginate_with(max: 100)
|
2016-09-14 07:21:17 +00:00
|
|
|
|
2021-07-20 17:35:02 +00:00
|
|
|
tickets = TicketPolicy::ReadScope.new(current_user).resolve
|
|
|
|
.order(id: :asc)
|
2021-08-17 05:44:54 +00:00
|
|
|
.offset(pagination.offset)
|
|
|
|
.limit(pagination.limit)
|
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,
|
2018-12-19 17:31:51 +00:00
|
|
|
assets: assets,
|
2016-06-06 15:26:37 +00:00
|
|
|
}, 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])
|
2020-03-19 09:39:51 +00:00
|
|
|
authorize!(ticket)
|
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
|
2021-10-06 05:54:33 +00:00
|
|
|
ticket = nil
|
2016-08-19 11:57:13 +00:00
|
|
|
|
2021-10-06 05:54:33 +00:00
|
|
|
Transaction.execute do # rubocop:disable Metrics/BlockLength
|
|
|
|
customer = {}
|
|
|
|
if params[:customer].instance_of?(ActionController::Parameters)
|
|
|
|
customer = params[:customer]
|
|
|
|
params.delete(:customer)
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2016-08-23 18:46:04 +00:00
|
|
|
|
2021-10-06 05:54:33 +00:00
|
|
|
clean_params = Ticket.association_name_to_id_convert(params)
|
2018-02-02 11:27:19 +00:00
|
|
|
|
2021-10-06 05:54:33 +00:00
|
|
|
# overwrite params
|
|
|
|
if !current_user.permissions?('ticket.agent')
|
|
|
|
%i[owner owner_id customer customer_id organization organization_id preferences].each do |key|
|
|
|
|
clean_params.delete(key)
|
|
|
|
end
|
|
|
|
clean_params[:customer_id] = current_user.id
|
2018-02-02 11:27:19 +00:00
|
|
|
end
|
2021-10-06 05:54:33 +00:00
|
|
|
|
|
|
|
# The parameter :customer_id is 'abused' in cases where it is not an integer, but a string like
|
|
|
|
# 'guess:customers.email@domain.com' which implies that the customer should be looked up.
|
|
|
|
if clean_params[:customer_id].is_a?(String) && clean_params[:customer_id] =~ %r{^guess:(.+?)$}
|
|
|
|
email_address = $1
|
|
|
|
email_address_validation = EmailAddressValidation.new(email_address)
|
|
|
|
if !email_address_validation.valid_format?
|
|
|
|
render json: { error: "Invalid email '#{email_address}' of customer" }, status: :unprocessable_entity
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local_customer = User.find_by(email: email_address.downcase)
|
|
|
|
if !local_customer
|
|
|
|
role_ids = Role.signup_role_ids
|
|
|
|
local_customer = User.create(
|
|
|
|
firstname: '',
|
|
|
|
lastname: '',
|
|
|
|
email: email_address,
|
|
|
|
password: '',
|
|
|
|
active: true,
|
|
|
|
role_ids: role_ids,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
clean_params[:customer_id] = local_customer.id
|
2018-02-02 11:27:19 +00:00
|
|
|
end
|
2016-08-19 11:57:13 +00:00
|
|
|
|
2021-10-06 05:54:33 +00:00
|
|
|
# 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
|
|
|
|
if !local_customer && clean_customer[:email].present?
|
|
|
|
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
|
|
|
|
end
|
2012-04-10 14:06:46 +00:00
|
|
|
|
2021-10-06 05:54:33 +00:00
|
|
|
clean_params = Ticket.param_cleanup(clean_params, true)
|
|
|
|
clean_params[:screen] = 'create_middle'
|
|
|
|
ticket = Ticket.new(clean_params)
|
|
|
|
authorize!(ticket, :create?)
|
2012-04-16 11:58:15 +00:00
|
|
|
|
2021-10-06 05:54:33 +00:00
|
|
|
# create ticket
|
|
|
|
ticket.save!
|
2016-09-08 19:18:26 +00:00
|
|
|
|
|
|
|
# create tags if given
|
2017-09-08 08:28:34 +00:00
|
|
|
if params[:tags].present?
|
2021-03-02 07:26:51 +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
|
|
|
|
2021-03-16 08:59:32 +00:00
|
|
|
# create mentions if given
|
|
|
|
if params[:mentions].present?
|
|
|
|
authorize!(Mention.new, :create?)
|
|
|
|
Array(params[:mentions]).each do |user_id|
|
|
|
|
Mention.where(mentionable: ticket, user_id: user_id).first_or_create(mentionable: ticket, user_id: user_id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-08 19:18:26 +00:00
|
|
|
# create article if given
|
|
|
|
if params[:article]
|
|
|
|
article_create(ticket, params[:article])
|
|
|
|
end
|
2021-10-06 05:54:33 +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, ...]
|
|
|
|
# },
|
|
|
|
# }
|
|
|
|
if params[:links].present?
|
|
|
|
link = params[:links].permit!.to_h
|
|
|
|
raise Exceptions::UnprocessableEntity, __('Invalid link structure') if !link.is_a? Hash
|
|
|
|
|
|
|
|
link.each do |target_object, link_types_with_object_ids|
|
|
|
|
raise Exceptions::UnprocessableEntity, __('Invalid link structure (Object)') if !link_types_with_object_ids.is_a? Hash
|
|
|
|
|
|
|
|
link_types_with_object_ids.each do |link_type, object_ids|
|
2021-12-16 13:42:09 +00:00
|
|
|
raise Exceptions::UnprocessableEntity, __('Invalid link structure (Object → LinkType)') if !object_ids.is_a? Array
|
2021-10-06 05:54:33 +00:00
|
|
|
|
|
|
|
object_ids.each do |local_object_id|
|
|
|
|
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,
|
|
|
|
)
|
|
|
|
end
|
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])
|
2020-03-19 09:39:51 +00:00
|
|
|
authorize!(ticket, :follow_up?)
|
|
|
|
authorize!(ticket)
|
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)
|
2021-08-25 12:24:42 +00:00
|
|
|
clean_params[:screen] = 'edit'
|
2018-07-26 14:24:31 +00:00
|
|
|
|
2020-02-19 11:54:06 +00:00
|
|
|
# disable changes on ticket number
|
|
|
|
clean_params.delete('number')
|
|
|
|
|
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)
|
2021-02-26 13:46:47 +00:00
|
|
|
if params[:article].present?
|
2016-09-08 19:18:26 +00:00
|
|
|
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])
|
2020-03-19 09:39:51 +00:00
|
|
|
authorize!(ticket)
|
2016-08-23 18:46:04 +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(
|
2020-02-13 11:53:09 +00:00
|
|
|
current_user: current_user,
|
|
|
|
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])
|
2020-03-19 09:39:51 +00:00
|
|
|
authorize!(ticket, :show?)
|
2012-09-20 12:08:02 +00:00
|
|
|
|
|
|
|
# get history of ticket
|
2018-12-31 19:15:17 +00:00
|
|
|
render json: ticket.history_get(true)
|
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({})
|
|
|
|
|
2021-07-20 17:35:02 +00:00
|
|
|
tickets = TicketPolicy::ReadScope.new(current_user).resolve
|
|
|
|
.where(
|
|
|
|
customer_id: ticket.customer_id,
|
|
|
|
state_id: Ticket::State.by_category(:open).select(:id),
|
|
|
|
)
|
2021-07-21 14:04:17 +00:00
|
|
|
.where.not(id: ticket.id)
|
|
|
|
.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
|
2021-07-20 17:35:02 +00:00
|
|
|
tickets ||= TicketPolicy::ReadScope.new(current_user).resolve
|
2021-07-21 14:04:17 +00:00
|
|
|
.where(customer_id: ticket.customer_id)
|
|
|
|
.where.not(state_id: Ticket::State.by_category(:merged).pluck(:id))
|
|
|
|
.where.not(id: ticket.id)
|
|
|
|
.order(created_at: :desc)
|
|
|
|
.limit(6)
|
2016-06-08 13:25:39 +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 = []
|
2021-07-20 17:35:02 +00:00
|
|
|
tickets.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: {
|
2018-12-19 17:31:51 +00:00
|
|
|
assets: assets,
|
|
|
|
ticket_ids_by_customer: ticket_ids_by_customer,
|
2015-04-27 13:42:53 +00:00
|
|
|
ticket_ids_recent_viewed: ticket_ids_recent_viewed,
|
2012-10-18 19:23:05 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2019-06-04 03:40:48 +00:00
|
|
|
# GET /api/v1/ticket_recent
|
|
|
|
def ticket_recent
|
|
|
|
ticket_ids = RecentView.list(current_user, 10, Ticket.name).map(&:o_id)
|
|
|
|
tickets = ticket_ids.map { |elem| Ticket.lookup(id: elem) }
|
|
|
|
assets = ApplicationModel::CanAssets.reduce(tickets)
|
|
|
|
|
|
|
|
render json: {
|
|
|
|
assets: assets,
|
|
|
|
ticket_ids_recent_viewed: ticket_ids
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2020-08-31 07:51:56 +00:00
|
|
|
# PUT /api/v1/ticket_merge/1/1
|
2012-09-20 12:08:02 +00:00
|
|
|
def ticket_merge
|
|
|
|
|
2021-07-12 13:18:31 +00:00
|
|
|
# check target ticket
|
|
|
|
target_ticket = Ticket.find_by(number: params[:target_ticket_number])
|
|
|
|
if !target_ticket
|
2015-04-27 13:42:53 +00:00
|
|
|
render json: {
|
2018-12-19 17:31:51 +00:00
|
|
|
result: 'failed',
|
2021-11-15 15:58:19 +00:00
|
|
|
message: __('No such target ticket number!'),
|
2012-09-20 12:08:02 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
end
|
2021-07-12 13:18:31 +00:00
|
|
|
authorize!(target_ticket, :update?)
|
2012-09-20 12:08:02 +00:00
|
|
|
|
2021-07-12 13:18:31 +00:00
|
|
|
# check source ticket
|
|
|
|
source_ticket = Ticket.find_by(id: params[:source_ticket_id])
|
|
|
|
if !source_ticket
|
2015-04-27 13:42:53 +00:00
|
|
|
render json: {
|
2018-12-19 17:31:51 +00:00
|
|
|
result: 'failed',
|
2021-11-15 15:58:19 +00:00
|
|
|
message: __('No such source ticket!'),
|
2012-09-20 12:08:02 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
end
|
2021-07-12 13:18:31 +00:00
|
|
|
authorize!(source_ticket, :update?)
|
2012-09-20 12:08:02 +00:00
|
|
|
|
|
|
|
# merge ticket
|
2021-07-12 13:18:31 +00:00
|
|
|
source_ticket.merge_to(
|
|
|
|
ticket_id: target_ticket.id,
|
2015-04-30 15:53:03 +00:00
|
|
|
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: {
|
2018-12-19 17:31:51 +00:00
|
|
|
result: 'success',
|
2021-07-12 13:18:31 +00:00
|
|
|
target_ticket: target_ticket.attributes,
|
|
|
|
source_ticket: source_ticket.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])
|
2020-03-19 09:39:51 +00:00
|
|
|
authorize!(ticket, :show?)
|
2015-11-17 14:04:36 +00:00
|
|
|
assets = ticket.assets({})
|
|
|
|
|
|
|
|
article = Ticket::Article.find(params[:article_id])
|
2020-03-19 09:39:51 +00:00
|
|
|
authorize!(article.ticket, :show?)
|
2015-11-17 14:04:36 +00:00
|
|
|
assets = article.assets(assets)
|
|
|
|
|
|
|
|
render json: {
|
2018-12-19 17:31:51 +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(
|
2020-11-13 14:10:24 +00:00
|
|
|
view: 'ticket_create',
|
2021-08-25 12:24:42 +00:00
|
|
|
screen: 'create_middle',
|
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
|
|
|
|
2021-08-17 05:44:54 +00:00
|
|
|
paginate_with(max: 200, default: 50)
|
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(
|
2018-12-19 17:31:51 +00:00
|
|
|
query: query,
|
|
|
|
condition: params[:condition].to_h,
|
2021-08-17 05:44:54 +00:00
|
|
|
limit: pagination.limit,
|
|
|
|
offset: pagination.offset,
|
2018-12-19 17:31:51 +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: {
|
2018-12-19 17:31:51 +00:00
|
|
|
tickets: ticket_result,
|
2015-04-27 13:42:53 +00:00
|
|
|
tickets_count: tickets.count,
|
2018-12-19 17:31:51 +00:00
|
|
|
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
|
2020-02-20 12:34:16 +00:00
|
|
|
ticket_count, tickets = Ticket.selectors(params[:condition], limit: 6, execution_time: true)
|
2015-09-17 01:04:16 +00:00
|
|
|
|
|
|
|
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: {
|
2018-12-19 17:31:51 +00:00
|
|
|
ticket_ids: ticket_ids,
|
2015-09-17 01:04:16 +00:00
|
|
|
ticket_count: ticket_count || 0,
|
2018-12-19 17:31:51 +00:00
|
|
|
assets: assets,
|
2015-09-17 01:04:16 +00:00
|
|
|
}
|
|
|
|
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]
|
2021-11-15 15:58:19 +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 = {}
|
|
|
|
|
|
|
|
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: {
|
2018-12-19 17:31:51 +00:00
|
|
|
'ticket.state_id' => {
|
2017-04-10 08:06:11 +00:00
|
|
|
operator: 'is',
|
2018-12-19 17:31:51 +00:00
|
|
|
value: Ticket::State.by_category(:closed).pluck(:id),
|
2017-04-10 08:06:11 +00:00
|
|
|
},
|
|
|
|
'ticket.customer_id' => {
|
|
|
|
operator: 'is',
|
2018-12-19 17:31:51 +00:00
|
|
|
value: user.id,
|
2017-04-10 08:06:11 +00:00
|
|
|
},
|
2015-09-17 18:39:51 +00:00
|
|
|
},
|
2018-12-19 17:31:51 +00:00
|
|
|
open_ids: {
|
|
|
|
'ticket.state_id' => {
|
2017-04-10 08:06:11 +00:00
|
|
|
operator: 'is',
|
2018-12-19 17:31:51 +00:00
|
|
|
value: Ticket::State.by_category(:open).pluck(:id),
|
2017-04-10 08:06:11 +00:00
|
|
|
},
|
|
|
|
'ticket.customer_id' => {
|
|
|
|
operator: 'is',
|
2018-12-19 17:31:51 +00:00
|
|
|
value: user.id,
|
2017-04-10 08:06:11 +00:00
|
|
|
},
|
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
|
|
|
}
|
2021-07-20 17:35:02 +00:00
|
|
|
user_tickets[:volume_by_year] = ticket_stats_last_year(condition)
|
2017-04-10 08:06:11 +00:00
|
|
|
|
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: {
|
2018-12-19 17:31:51 +00:00
|
|
|
'ticket.state_id' => {
|
2017-04-10 08:06:11 +00:00
|
|
|
operator: 'is',
|
2018-12-19 17:31:51 +00:00
|
|
|
value: Ticket::State.by_category(:closed).pluck(:id),
|
2017-04-10 08:06:11 +00:00
|
|
|
},
|
|
|
|
'ticket.organization_id' => {
|
|
|
|
operator: 'is',
|
2018-12-19 17:31:51 +00:00
|
|
|
value: organization.id,
|
2017-04-10 08:06:11 +00:00
|
|
|
},
|
2015-09-17 18:39:51 +00:00
|
|
|
},
|
2018-12-19 17:31:51 +00:00
|
|
|
open_ids: {
|
|
|
|
'ticket.state_id' => {
|
2017-04-10 08:06:11 +00:00
|
|
|
operator: 'is',
|
2018-12-19 17:31:51 +00:00
|
|
|
value: Ticket::State.by_category(:open).pluck(:id),
|
2017-04-10 08:06:11 +00:00
|
|
|
},
|
|
|
|
'ticket.organization_id' => {
|
|
|
|
operator: 'is',
|
2018-12-19 17:31:51 +00:00
|
|
|
value: organization.id,
|
2017-04-10 08:06:11 +00:00
|
|
|
},
|
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
|
|
|
}
|
2021-07-20 17:35:02 +00:00
|
|
|
org_tickets[:volume_by_year] = ticket_stats_last_year(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: {
|
2018-12-19 17:31:51 +00:00
|
|
|
user: user_tickets,
|
2017-04-10 08:06:11 +00:00
|
|
|
organization: org_tickets,
|
2018-12-19 17:31:51 +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.
|
2021-02-04 08:28:41 +00:00
|
|
|
# @response_message 403 Forbidden / Invalid session.
|
2018-02-20 04:29:30 +00:00
|
|
|
def import_example
|
|
|
|
csv_string = Ticket.csv_example(
|
|
|
|
col_sep: ',',
|
|
|
|
)
|
|
|
|
send_data(
|
|
|
|
csv_string,
|
2018-12-19 17:31:51 +00:00
|
|
|
filename: 'example.csv',
|
|
|
|
type: 'text/csv',
|
2018-02-20 04:29:30 +00:00
|
|
|
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.
|
2021-02-04 08:28:41 +00:00
|
|
|
# @response_message 403 Forbidden / Invalid session.
|
2018-02-20 04:29:30 +00:00
|
|
|
def import_start
|
|
|
|
if Setting.get('import_mode') != true
|
2021-11-15 15:58:19 +00:00
|
|
|
raise __('Only can import tickets if system is in import mode.')
|
2018-02-20 04:29:30 +00:00
|
|
|
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
|
2021-11-15 15:58:19 +00:00
|
|
|
raise Exceptions::UnprocessableEntity, __('No source data submitted!') if string.blank?
|
2018-11-06 05:42:52 +00:00
|
|
|
|
2018-02-20 04:29:30 +00:00
|
|
|
result = Ticket.csv_import(
|
2018-12-19 17:31:51 +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
|
|
|
},
|
2018-12-19 17:31:51 +00:00
|
|
|
try: params[:try],
|
2018-02-20 04:29:30 +00:00
|
|
|
)
|
|
|
|
render json: result, status: :ok
|
|
|
|
end
|
|
|
|
|
2014-09-23 05:37:43 +00:00
|
|
|
private
|
|
|
|
|
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,
|
2021-08-25 12:24:42 +00:00
|
|
|
ticket: ticket,
|
|
|
|
screen: 'edit',
|
2017-06-16 20:43:09 +00:00
|
|
|
)
|
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|
|
2020-03-19 09:39:51 +00:00
|
|
|
next if !authorized?(article, :show?)
|
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(
|
2018-12-19 17:31:51 +00:00
|
|
|
link_object: 'Ticket',
|
2016-06-06 15:26:37 +00:00
|
|
|
link_object_value: ticket.id,
|
2021-04-27 08:17:15 +00:00
|
|
|
user: current_user,
|
2016-06-06 15:26:37 +00:00
|
|
|
)
|
2019-06-04 03:40:48 +00:00
|
|
|
|
|
|
|
assets = Link.reduce_assets(assets, links)
|
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
|
|
|
|
2021-03-16 08:59:32 +00:00
|
|
|
# get mentions
|
|
|
|
mentions = Mention.where(mentionable: ticket).order(created_at: :desc)
|
|
|
|
mentions.each do |mention|
|
|
|
|
assets = mention.assets(assets)
|
|
|
|
end
|
|
|
|
|
2016-06-06 15:26:37 +00:00
|
|
|
# return result
|
|
|
|
{
|
2018-12-19 17:31:51 +00:00
|
|
|
ticket_id: ticket.id,
|
2016-06-06 15:26:37 +00:00
|
|
|
ticket_article_ids: article_ids,
|
2018-12-19 17:31:51 +00:00
|
|
|
assets: assets,
|
2019-06-04 03:40:48 +00:00
|
|
|
links: links,
|
2018-12-19 17:31:51 +00:00
|
|
|
tags: tags,
|
2021-03-16 08:59:32 +00:00
|
|
|
mentions: mentions.pluck(:id),
|
2018-12-19 17:31:51 +00:00
|
|
|
form_meta: attributes_to_change[:form_meta],
|
2016-06-06 15:26:37 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|