|
@@ -72,83 +72,79 @@ class TicketsController < ApplicationController
|
|
|
|
|
|
# POST /api/v1/tickets
|
|
|
def create
|
|
|
- customer = {}
|
|
|
- if params[:customer].instance_of?(ActionController::Parameters)
|
|
|
- customer = params[:customer]
|
|
|
- params.delete(:customer)
|
|
|
- end
|
|
|
-
|
|
|
- clean_params = Ticket.association_name_to_id_convert(params)
|
|
|
+ ticket = nil
|
|
|
|
|
|
- # 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)
|
|
|
+ Transaction.execute do # rubocop:disable Metrics/BlockLength
|
|
|
+ customer = {}
|
|
|
+ if params[:customer].instance_of?(ActionController::Parameters)
|
|
|
+ customer = params[:customer]
|
|
|
+ params.delete(:customer)
|
|
|
end
|
|
|
- clean_params[:customer_id] = current_user.id
|
|
|
- end
|
|
|
|
|
|
- # 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
|
|
|
- end
|
|
|
+ clean_params = Ticket.association_name_to_id_convert(params)
|
|
|
|
|
|
- # 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)
|
|
|
+ # 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
|
|
|
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!
|
|
|
+
|
|
|
+ # 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
|
|
|
end
|
|
|
- clean_params[:customer_id] = local_customer.id
|
|
|
- end
|
|
|
|
|
|
- clean_params = Ticket.param_cleanup(clean_params, true)
|
|
|
- clean_params[:screen] = 'create_middle'
|
|
|
- ticket = Ticket.new(clean_params)
|
|
|
- authorize!(ticket, :create?)
|
|
|
+ # 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
|
|
|
|
|
|
- # check if article is given
|
|
|
- if !params[:article]
|
|
|
- render json: { error: 'article hash is missing' }, status: :unprocessable_entity
|
|
|
- return
|
|
|
- end
|
|
|
+ clean_params = Ticket.param_cleanup(clean_params, true)
|
|
|
+ clean_params[:screen] = 'create_middle'
|
|
|
+ ticket = Ticket.new(clean_params)
|
|
|
+ authorize!(ticket, :create?)
|
|
|
|
|
|
- # create ticket
|
|
|
- ticket.save!
|
|
|
- ticket.with_lock do
|
|
|
+ # create ticket
|
|
|
+ ticket.save!
|
|
|
|
|
|
# create tags if given
|
|
|
if params[:tags].present?
|
|
@@ -170,33 +166,34 @@ class TicketsController < ApplicationController
|
|
|
if params[:article]
|
|
|
article_create(ticket, params[:article])
|
|
|
end
|
|
|
- end
|
|
|
- # 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|
|
|
|
- raise Exceptions::UnprocessableEntity, __('Invalid link structure (Object->LinkType)') if !object_ids.is_a? Array
|
|
|
-
|
|
|
- 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,
|
|
|
- )
|
|
|
+
|
|
|
+ # 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|
|
|
|
+ raise Exceptions::UnprocessableEntity, __('Invalid link structure (Object->LinkType)') if !object_ids.is_a? Array
|
|
|
+
|
|
|
+ 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
|
|
|
end
|
|
|
end
|
|
|
end
|