From 9374e93ea82635d0fcff5a70f49f178263dac7ce Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Mon, 16 Apr 2012 13:58:15 +0200 Subject: [PATCH] Added article data to ticket creation (improve performance). --- .../controllers/agent_ticket_create.js.coffee | 38 +++++++++--------- .../javascripts/app/models/ticket.js.coffee | 2 +- app/controllers/tickets_controller.rb | 39 +++++++++++++++++-- 3 files changed, 55 insertions(+), 24 deletions(-) diff --git a/app/assets/javascripts/app/controllers/agent_ticket_create.js.coffee b/app/assets/javascripts/app/controllers/agent_ticket_create.js.coffee index 3c6b40808..44be7db59 100644 --- a/app/assets/javascripts/app/controllers/agent_ticket_create.js.coffee +++ b/app/assets/javascripts/app/controllers/agent_ticket_create.js.coffee @@ -83,8 +83,26 @@ class Index extends App.Controller # create ticket object = new App.Ticket @log 'updateAttributes', params - object.load(params) + # find sender_id + sender = App.TicketArticleSender.findByAttribute("name", "Customer") + type = App.TicketArticleType.findByAttribute("name", "phone") + group = App.Group.find(params.group_id) + + # create article + params['article'] = { + from: params.customer_id_autocompletion, + to: group.name, + subject: params.subject, + body: params.body, + ticket_article_type_id: type.id, + ticket_article_sender_id: sender.id, + created_by_id: params.customer_id, + } +# console.log('params', params) + + object.load(params) + # validate form errors = object.validate() @@ -101,24 +119,6 @@ class Index extends App.Controller object.save( success: (r) => - - # find sender_id - sender = App.TicketArticleSender.findByAttribute("name", "Customer") - type = App.TicketArticleType.findByAttribute("name", "phone") - - # create article - article = new App.TicketArticle - article.load( - from: 'some guy', - to: 'some group', - subject: params.subject, - body: params.body, - ticket_id: r.id, - ticket_article_type_id: type.id, - ticket_article_sender_id: sender.id, - ) - article.save() -# console.log('params', params) # notify UI @notify diff --git a/app/assets/javascripts/app/models/ticket.js.coffee b/app/assets/javascripts/app/models/ticket.js.coffee index b6069b34a..0c581606f 100644 --- a/app/assets/javascripts/app/models/ticket.js.coffee +++ b/app/assets/javascripts/app/models/ticket.js.coffee @@ -1,5 +1,5 @@ class App.Ticket extends App.Model - @configure 'Ticket', 'number', 'title', 'group_id', 'owner_id', 'customer_id', 'ticket_state_id', 'ticket_priority_id' + @configure 'Ticket', 'number', 'title', 'group_id', 'owner_id', 'customer_id', 'ticket_state_id', 'ticket_priority_id', 'article' @extend Spine.Model.Ajax # @url: '/tickets' @configure_attributes = [ diff --git a/app/controllers/tickets_controller.rb b/app/controllers/tickets_controller.rb index 3c51d8243..0c6b9cc3d 100644 --- a/app/controllers/tickets_controller.rb +++ b/app/controllers/tickets_controller.rb @@ -20,11 +20,42 @@ class TicketsController < ApplicationController @ticket = Ticket.new(params[:ticket]) @ticket.created_by_id = current_user.id - if @ticket.save - render :json => @ticket, :status => :created - else - render :json => @ticket.errors, :status => :unprocessable_entity + # 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 + + # create article if given + if params[:article] + @article = Ticket::Article.new(params[:article]) + @article.created_by_id = params[:article][:created_by_id] || current_user.id + @article.ticket_id = @ticket.id + + # find attachments in upload cache + @article['attachments'] = Store.list( + :object => 'UploadCache::TicketZoom::' + current_user.id.to_s, + :o_id => @article.ticket_id + ) + if !@article.save + render :json => @article.errors, :status => :unprocessable_entity + return + end + + # remove attachments from upload cache + Store.remove( + :object => 'UploadCache::TicketZoom::' + current_user.id.to_s, + :o_id => @article.ticket_id + ) + end + + render :json => @ticket, :status => :created end # PUT /tickets/1