From b1c6768114763ad6f652788d91e34333df53ec39 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Wed, 8 Feb 2017 00:38:19 +0100 Subject: [PATCH] Use real macros, users and groups in batch action. --- .../app/controllers/ticket_overview.coffee | 186 +++++++++++++----- .../app/controllers/ticket_zoom.coffee | 38 ++-- .../app/controllers/widget/tag.coffee | 35 +--- .../app/models/_application_model.coffee | 35 ++++ .../javascripts/app/models/ticket.coffee | 32 +++ 5 files changed, 221 insertions(+), 105 deletions(-) diff --git a/app/assets/javascripts/app/controllers/ticket_overview.coffee b/app/assets/javascripts/app/controllers/ticket_overview.coffee index 5cce0b5cc..bed37ffd7 100644 --- a/app/assets/javascripts/app/controllers/ticket_overview.coffee +++ b/app/assets/javascripts/app/controllers/ticket_overview.coffee @@ -24,57 +24,6 @@ class App.TicketOverview extends App.Controller @batchSupport = @permissionCheck('ticket.agent') @render() - return if !@batchSupport - users = [ - App.User.find(2), - App.User.find(2), - App.User.find(2), - ] - groups = App.Group.all() - - @batchAssign.html $(App.view('ticket_overview/batch_overlay_user_group')( - users: users - groups: groups - )) - macros = [ - { - name: 'Close Beispiel für eine besonders' - }, - { - name: 'Close Beispiel für eine besonders' - }, - { - name: 'Close Beispiel für eine besonders' - }, - { - name: 'Close Beispiel für eine besonders' - }, - { - name: 'Close Beispiel für eine besonders' - }, - { - name: 'Close Beispiel für eine besonders' - }, - { - name: 'Close Beispiel für eine besonders' - }, - { - name: 'Close & Tag as Spam' - }, - { - name: 'Close & Reply we\'re on Holidays' - }, - { - name: 'Escalate to 2nd level' - }, - { - name: '1st Close' - }, - ] - @batchMacro.html $(App.view('ticket_overview/batch_overlay_macro')( - macros: macros - )) - startDragItem: (event) => return if !@batchSupport @grabbedItem = $(event.currentTarget) @@ -126,6 +75,8 @@ class App.TicketOverview extends App.Controller if(additionalItemsClones.length) @batchDragger.find('.js-batch-dragger-count').text(@draggedItems.length) + @renderOptions() + $('#app').append @batchDragger @draggedItems.each (i, item) -> @@ -219,6 +170,61 @@ class App.TicketOverview extends App.Controller performBatchAction: (items, action, id) -> console.log "perform action #{action} with id #{id} on #{items.length} checked items" + if action is 'macro' + @batchCount = items.length + @batchCountIndex = 0 + macro = App.Macro.find(id) + for item in items + #console.log "perform action #{action} with id #{id} on ", $(item).val() + ticket = App.Ticket.find($(item).val()) + App.Ticket.macro( + macro: macro.perform + ticket: ticket + ) + ticket.save( + done: (r) => + @batchCountIndex++ + + # refresh view after all tickets are proceeded + if @batchCountIndex == @batchCount + App.Event.trigger('overview:fetch') + ) + return + + if action is 'user_assign' + @batchCount = items.length + @batchCountIndex = 0 + for item in items + #console.log "perform action #{action} with id #{id} on ", $(item).val() + ticket = App.Ticket.find($(item).val()) + ticket.owner_id = id + ticket.save( + done: (r) => + @batchCountIndex++ + + # refresh view after all tickets are proceeded + if @batchCountIndex == @batchCount + App.Event.trigger('overview:fetch') + ) + return + + if action is 'group_assign' + @batchCount = items.length + @batchCountIndex = 0 + for item in items + #console.log "perform action #{action} with id #{id} on ", $(item).val() + ticket = App.Ticket.find($(item).val()) + ticket.group_id = id + ticket.save( + done: (r) => + @batchCountIndex++ + + # refresh view after all tickets are proceeded + if @batchCountIndex == @batchCount + App.Event.trigger('overview:fetch') + ) + return + showBatchOverlay: -> @batchOverlay.show() @batchOverlayBackdrop.velocity { opacity: [1, 0] }, { duration: 500 } @@ -463,6 +469,84 @@ class App.TicketOverview extends App.Controller App.OverviewListCollection.fetch(@view) @delay(update, 2800, 'overview:fetch') + renderOptions: => + macros = App.Macro.all() + groups = App.Group.all() + users = [] + items = @el.find('[name="bulk"]:checked') + + # find all possible owners for selected tickets + possibleUsers = {} + possibleUserGroups = {} + for item in items + #console.log "selected items with id ", $(item).val() + ticket = App.Ticket.find($(item).val()) + if !possibleUserGroups[ticket.group_id.toString()] + group = App.Group.find(ticket.group_id) + for user_id in group.user_ids + if !possibleUserGroups[ticket.group_id.toString()] + possibleUsers[user_id.toString()] = true + else + hit = false + for user_id, exists of possibleUsers + if possibleUsers[user_id.toString()] + hit = true + if !hit + delete possibleUsers[user_id.toString()] + possibleUserGroups[ticket.group_id.toString()] = true + for user_id, _exists of possibleUsers + user = App.User.find(user_id) + users.push user + ### + users = [ + App.User.find(2), + App.User.find(2), + App.User.find(2), + ] + macros = [ + { + name: 'Close Beispiel für eine besonders' + }, + { + name: 'Close Beispiel für eine besonders' + }, + { + name: 'Close Beispiel für eine besonders' + }, + { + name: 'Close Beispiel für eine besonders' + }, + { + name: 'Close Beispiel für eine besonders' + }, + { + name: 'Close Beispiel für eine besonders' + }, + { + name: 'Close Beispiel für eine besonders' + }, + { + name: 'Close & Tag as Spam' + }, + { + name: 'Close & Reply we\'re on Holidays' + }, + { + name: 'Escalate to 2nd level' + }, + { + name: '1st Close' + }, + ] + ### + @batchAssign.html $(App.view('ticket_overview/batch_overlay_user_group')( + users: users + groups: groups + )) + @batchMacro.html $(App.view('ticket_overview/batch_overlay_macro')( + macros: macros + )) + active: (state) => return @shown if state is undefined @shown = state diff --git a/app/assets/javascripts/app/controllers/ticket_zoom.coffee b/app/assets/javascripts/app/controllers/ticket_zoom.coffee index f9e9681a2..178e223d2 100644 --- a/app/assets/javascripts/app/controllers/ticket_zoom.coffee +++ b/app/assets/javascripts/app/controllers/ticket_zoom.coffee @@ -672,31 +672,19 @@ class App.TicketZoom extends App.Controller for key, value of ticketParams ticket[key] = value - # apply macro - for key, content of macro - attributes = key.split('.') - if attributes[0] is 'ticket' - - # apply tag changes - if attributes[1] is 'tags' - if @sidebar && @sidebar.tagWidget - tags = content.value.split(',') - for tag in tags - if content.operator is 'remove' - @sidebar.tagWidget.remove(tag) - else - @sidebar.tagWidget.add(tag) - - # apply user changes - else if attributes[1] is 'owner_id' - if content.pre_condition is 'current_user.id' - ticket[attributes[1]] = App.Session.get('id') - else - ticket[attributes[1]] = content.value - - # apply direct value changes - else - ticket[attributes[1]] = content.value + App.Ticket.macro( + macro: macro + ticket: ticket + callback: + tagAdd: (tag) => + return if !@sidebar + return if !@sidebar.tagWidget + @sidebar.tagWidget.add(tag) + tagRemove: (tag) => + return if !@sidebar + return if !@sidebar.tagWidget + @sidebar.tagWidget.remove(tag) + ) # set defaults if !@permissionCheck('ticket.customer') diff --git a/app/assets/javascripts/app/controllers/widget/tag.coffee b/app/assets/javascripts/app/controllers/widget/tag.coffee index 12d6a0408..774e62eaa 100644 --- a/app/assets/javascripts/app/controllers/widget/tag.coffee +++ b/app/assets/javascripts/app/controllers/widget/tag.coffee @@ -32,15 +32,10 @@ class App.WidgetTag extends App.Controller fetch: => @pendingRefresh = false - @ajax( - id: @key - type: 'GET' - url: "#{@apiPath}/tags" - data: - object: @object_type - o_id: @object.id - processData: true - success: (data, status, xhr) => + App[@object_type].tagGet( + @object.id, + @key, + (data) => @localTags = data.tags @render() ) @@ -104,16 +99,7 @@ class App.WidgetTag extends App.Controller return if App.Config.get('tag_new') is false && !@possibleTags[item] @localTags.push item @render() - - @ajax( - type: 'GET' - url: "#{@apiPath}/tags/add" - data: - object: @object_type - o_id: @object.id - item: item - processData: true, - ) + App[@object_type].tagAdd(@object.id, item) onRemoveTag: (e) => e.preventDefault() @@ -124,16 +110,7 @@ class App.WidgetTag extends App.Controller remove: (item) => @localTags = _.filter(@localTags, (tagItem) -> return tagItem if tagItem isnt item) @render() - - @ajax( - type: 'GET' - url: "#{@apiPath}/tags/remove" - data: - object: @object_type - o_id: @object.id - item: item - processData: true - ) + App[@object_type].tagRemove(@object.id, item) searchTag: (e) -> e.preventDefault() diff --git a/app/assets/javascripts/app/models/_application_model.coffee b/app/assets/javascripts/app/models/_application_model.coffee index a261d9255..456c15a9b 100644 --- a/app/assets/javascripts/app/models/_application_model.coffee +++ b/app/assets/javascripts/app/models/_application_model.coffee @@ -737,3 +737,38 @@ class App.Model extends Spine.Model @findNative: (id) -> @irecords[id] or notFound?(id) + + @tagGet: (id, key, callback) -> + App.Ajax.request( + id: key + type: 'GET' + url: "#{@apiPath}/tags" + data: + object: @.className + o_id: id + processData: true + success: (data, status, xhr) -> + callback(data) + ) + + @tagAdd: (id, item) -> + App.Ajax.request( + type: 'GET' + url: "#{@apiPath}/tags/add" + data: + object: @.className + o_id: id + item: item + processData: true + ) + + @tagRemove: (id, item) -> + App.Ajax.request( + type: 'GET' + url: "#{@apiPath}/tags/remove" + data: + object: @.className + o_id: id + item: item + processData: true + ) diff --git a/app/assets/javascripts/app/models/ticket.coffee b/app/assets/javascripts/app/models/ticket.coffee index 608a69638..af478516f 100644 --- a/app/assets/javascripts/app/models/ticket.coffee +++ b/app/assets/javascripts/app/models/ticket.coffee @@ -92,3 +92,35 @@ class App.Ticket extends App.Model else if item.type is 'escalation_warning' return App.i18n.translateContent('Ticket |%s| will escalate soon!', item.title) return "Unknow action for (#{@objectDisplayName()}/#{item.type}), extend activityMessage() of model." + + # apply macro + @macro: (params) -> + for key, content of params.macro + attributes = key.split('.') + if attributes[0] is 'ticket' + + # apply tag changes + if attributes[1] is 'tags' + tags = content.value.split(',') + for tag in tags + if content.operator is 'remove' + if params.callback && params.callback.tagRemove + params.callback.tagRemove(tag) + else + @tagRemove(params.ticket.id, tag) + else + if params.callback && params.callback.tagAdd + params.callback.tagAdd(tag) + else + @tagAdd(params.ticket.id, tag) + + # apply user changes + else if attributes[1] is 'owner_id' + if content.pre_condition is 'current_user.id' + params.ticket[attributes[1]] = App.Session.get('id') + else + params.ticket[attributes[1]] = content.value + + # apply direct value changes + else + params.ticket[attributes[1]] = content.value