From e2eef259b40fa30e24143931acd1d32efa410678 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Fri, 19 Aug 2016 13:57:13 +0200 Subject: [PATCH] Added feature to create a ticket without existing customer, just enter email address in customer search field. --- .../user_organization_autocompletion.coffee | 9 +++++ app/controllers/tickets_controller.rb | 23 +++++++++++++ ...00001_update_setting_postmaster_filter2.rb | 34 +++++++++++++++++++ db/seeds.rb | 1 + 4 files changed, 67 insertions(+) diff --git a/app/assets/javascripts/app/lib/app_post/user_organization_autocompletion.coffee b/app/assets/javascripts/app/lib/app_post/user_organization_autocompletion.coffee index b542fc746..1496e31c6 100644 --- a/app/assets/javascripts/app/lib/app_post/user_organization_autocompletion.coffee +++ b/app/assets/javascripts/app/lib/app_post/user_organization_autocompletion.coffee @@ -63,6 +63,15 @@ class App.UserOrganizationAutocompletion extends App.Controller @userSelect.focus() if not @formControl.hasClass 'focus' onBlur: => + selectUser = @userSelect.val() + if _.isEmpty(selectUser) + @userId.val('') + return + if @attribute.guess is true + currentUserId = @userId.val() + if _.isEmpty(currentUserId) || currentUserId.match(/^guess:/) + if !_.isEmpty(selectUser) + @userId.val("guess:#{selectUser}") @formControl.removeClass 'focus' onUserClick: (e) => diff --git a/app/controllers/tickets_controller.rb b/app/controllers/tickets_controller.rb index 0f0d71ad4..1effa6207 100644 --- a/app/controllers/tickets_controller.rb +++ b/app/controllers/tickets_controller.rb @@ -73,6 +73,29 @@ class TicketsController < ApplicationController def create clean_params = Ticket.param_association_lookup(params) clean_params = Ticket.param_cleanup(clean_params, true) + + # try to create customer if needed + if clean_params[:customer_id] && clean_params[:customer_id] =~ /^guess:(.+?)$/ + email = $1 + if email !~ /@/ || email =~ /(>|<|\||\!|"|ยง|'|\$|%|&|\(|\)|\?|\s)/ + render json: { error: 'Invalid email' }, status: :unprocessable_entity + return + end + customer = User.find_by(email: email) + if !customer + role_ids = Role.signup_role_ids + customer = User.create( + firstname: '', + lastname: '', + email: email, + password: '', + active: true, + role_ids: role_ids, + ) + end + clean_params[:customer_id] = customer.id + end + ticket = Ticket.new(clean_params) # check if article is given diff --git a/db/migrate/20160819000001_update_setting_postmaster_filter2.rb b/db/migrate/20160819000001_update_setting_postmaster_filter2.rb index d204ec165..589698778 100644 --- a/db/migrate/20160819000001_update_setting_postmaster_filter2.rb +++ b/db/migrate/20160819000001_update_setting_postmaster_filter2.rb @@ -11,5 +11,39 @@ class UpdateSettingPostmasterFilter2 < ActiveRecord::Migration state: 'Channel::Filter::SenderIsSystemAddress', frontend: false ) + ObjectManager::Attribute.add( + force: true, + object: 'Ticket', + name: 'customer_id', + display: 'Customer', + data_type: 'user_autocompletion', + data_option: { + relation: 'User', + autocapitalize: false, + multiple: false, + guess: true, + null: false, + limit: 200, + placeholder: 'Enter Person or Organization/Company', + minLengt: 2, + translate: false, + }, + editable: false, + active: true, + screens: { + create_top: { + Agent: { + null: false, + }, + }, + edit: {}, + }, + to_create: false, + to_migrate: false, + to_delete: false, + position: 10, + updated_by_id: 1, + created_by_id: 1, + ) end end diff --git a/db/seeds.rb b/db/seeds.rb index 4ab4d21d4..990524999 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -3296,6 +3296,7 @@ ObjectManager::Attribute.add( relation: 'User', autocapitalize: false, multiple: false, + guess: true, null: false, limit: 200, placeholder: 'Enter Person or Organization/Company',