diff --git a/app/assets/javascripts/app/controllers/agent_ticket_create.coffee b/app/assets/javascripts/app/controllers/agent_ticket_create.coffee index 930577cde..c011be7bb 100644 --- a/app/assets/javascripts/app/controllers/agent_ticket_create.coffee +++ b/app/assets/javascripts/app/controllers/agent_ticket_create.coffee @@ -377,6 +377,12 @@ class App.TicketCreate extends App.Controller if params.group_id group = App.Group.find(params.group_id) + # add linked objects if ticket got splited + if @ticket_id + params['links'] = + Ticket: + child: [@ticket_id] + # allow cc only on email tickets if @currentChannel() isnt 'email-out' delete params.cc diff --git a/app/assets/javascripts/app/models/ticket.coffee b/app/assets/javascripts/app/models/ticket.coffee index 9720b7786..a037ff143 100644 --- a/app/assets/javascripts/app/models/ticket.coffee +++ b/app/assets/javascripts/app/models/ticket.coffee @@ -1,5 +1,5 @@ class App.Ticket extends App.Model - @configure 'Ticket', 'number', 'title', 'group_id', 'owner_id', 'customer_id', 'state_id', 'priority_id', 'article', 'tags', 'updated_at' + @configure 'Ticket', 'number', 'title', 'group_id', 'owner_id', 'customer_id', 'state_id', 'priority_id', 'article', 'tags', 'links', 'updated_at' @extend Spine.Model.Ajax @url: @apiPath + '/tickets' @configure_attributes = [ diff --git a/app/controllers/tickets_controller.rb b/app/controllers/tickets_controller.rb index 1effa6207..ffb87d88d 100644 --- a/app/controllers/tickets_controller.rb +++ b/app/controllers/tickets_controller.rb @@ -134,6 +134,33 @@ class TicketsController < ApplicationController return 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] + raise 'Invalid link structure' if params[:links].to_h.class != Hash + params[:links].each { |target_object, link_types_with_object_ids| + raise 'Invalid link structure (Object)' if link_types_with_object_ids.to_h.class != Hash + link_types_with_object_ids.each { |link_type, object_ids| + raise 'Invalid link structure (Object->LinkType)' if object_ids.class != Array + object_ids.each { |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 + render json: ticket, status: :created end diff --git a/test/controllers/tickets_controller_test.rb b/test/controllers/tickets_controller_test.rb new file mode 100644 index 000000000..87b2bd1db --- /dev/null +++ b/test/controllers/tickets_controller_test.rb @@ -0,0 +1,91 @@ +# encoding: utf-8 +require 'test_helper' + +class TicketsControllerTest < ActionDispatch::IntegrationTest + setup do + + # set accept header + @headers = { 'ACCEPT' => 'application/json', 'CONTENT_TYPE' => 'application/json' } + + # create agent + roles = Role.where(name: %w(Admin Agent)) + groups = Group.all + + UserInfo.current_user_id = 1 + @admin = User.create_or_update( + login: 'tickets-admin', + firstname: 'Tickets', + lastname: 'Admin', + email: 'tickets-admin@example.com', + password: 'adminpw', + active: true, + roles: roles, + groups: groups, + ) + + # create agent + roles = Role.where(name: 'Agent') + @agent = User.create_or_update( + login: 'tickets-agent@example.com', + firstname: 'Tickets', + lastname: 'Agent', + email: 'tickets-agent@example.com', + password: 'agentpw', + active: true, + roles: roles, + groups: groups, + ) + + # create customer without org + roles = Role.where(name: 'Customer') + @customer_without_org = User.create_or_update( + login: 'tickets-customer1@example.com', + firstname: 'Tickets', + lastname: 'Customer1', + email: 'tickets-customer1@example.com', + password: 'customer1pw', + active: true, + roles: roles, + ) + + end + + test '01 ticket create with agent' do + + credentials = ActionController::HttpAuthentication::Basic.encode_credentials('tickets-agent@example.com', 'agentpw') + + params = { + title: 'a new ticket #1', + state: 'new', + priority: '2 normal', + group: 'Users', + customer: 'tickets-customer1@example.com', + article: { + content_type: 'text/plain', # or text/html + body: 'some body', + sender: 'Customer', + type: 'note', + }, + links: { + Ticket: { + parent: [1], + } + } + } + + post '/api/v1/tickets', params.to_json, @headers.merge('Authorization' => credentials) + + assert_response(201) + result = JSON.parse(@response.body) + assert_equal(Hash, result.class) + assert_equal(Ticket::State.lookup(name: 'new').id, result['state_id']) + assert_equal('a new ticket #1', result['title']) + + links = Link.list( + link_object: 'Ticket', + link_object_value: result['id'], + ) + p links.inspect + end + +end