From 3ca0b26fc19c8ae462619c5a0aabf774b8463306 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Fri, 11 Sep 2015 15:44:05 +0200 Subject: [PATCH] Allow to delete email addresses. --- .../app/controllers/_channel/email.js.coffee | 39 +++-- .../channel/email_account_overview.jst.eco | 159 +++++++++++++++--- app/controllers/channels_controller.rb | 2 +- app/controllers/email_addresses_controller.rb | 5 +- app/models/email_address.rb | 27 ++- config/routes/email_address.rb | 9 +- test/unit/email_address_test.rb | 99 +++++++++++ 7 files changed, 292 insertions(+), 48 deletions(-) create mode 100644 test/unit/email_address_test.rb diff --git a/app/assets/javascripts/app/controllers/_channel/email.js.coffee b/app/assets/javascripts/app/controllers/_channel/email.js.coffee index 52fbcf571..31665de21 100644 --- a/app/assets/javascripts/app/controllers/_channel/email.js.coffee +++ b/app/assets/javascripts/app/controllers/_channel/email.js.coffee @@ -215,18 +215,19 @@ class App.ChannelEmailSignatureEdit extends App.ControllerModal class App.ChannelEmailAccountOverview extends App.Controller events: - 'click [data-type="new"]': 'wizard' - 'click [data-type="delete"]': 'delete' - 'click [data-type="edit-inbound"]': 'edit_inbound' - 'click [data-type="edit-outbound"]': 'edit_outbound' - 'click [data-type="email-address-new"]': 'email_address_new' - 'click [data-type="email-address-edit"]': 'email_address_edit' - 'click [data-type="edit-notification-outbound"]': 'edit_notification_outbound' + 'click .js-channelNew': 'wizard' + 'click .js-channelDelete': 'delete' + 'click .js-editInbound': 'edit_inbound' + 'click .js-editOutbound': 'edit_outbound' + 'click .js-emailAddressNew': 'email_address_new' + 'click .js-emailAddressEdit': 'email_address_edit' + 'click .js-emailAddressDelete': 'email_address_delete', + 'click .js-editNotificationOutbound': 'edit_notification_outbound' constructor: -> super - # @interval(@load, 20000) - @load() + @interval(@load, 30000) + #@load() load: => @ajax( @@ -282,7 +283,7 @@ class App.ChannelEmailAccountOverview extends App.Controller edit_inbound: (e) => e.preventDefault() - id = $(e.target).closest('tr').data('id') + id = $(e.target).closest('.action').data('id') channel = App.Channel.find(id) slide = 'js-inbound' new App.ChannelEmailAccountWizard( @@ -295,7 +296,7 @@ class App.ChannelEmailAccountOverview extends App.Controller edit_outbound: (e) => e.preventDefault() - id = $(e.target).closest('tr').data('id') + id = $(e.target).closest('.action').data('id') channel = App.Channel.find(id) slide = 'js-outbound' new App.ChannelEmailAccountWizard( @@ -308,7 +309,7 @@ class App.ChannelEmailAccountOverview extends App.Controller delete: (e) => e.preventDefault() - id = $(e.target).closest('tr').data('id') + id = $(e.target).closest('.action').data('id') item = App.Channel.find(id) new App.ControllerGenericDestroyConfirm( item: item @@ -318,7 +319,7 @@ class App.ChannelEmailAccountOverview extends App.Controller email_address_new: (e) => e.preventDefault() - channel_id = $(e.target).closest('tr').data('id') + channel_id = $(e.target).closest('.action').data('id') new App.ControllerGenericNew( pageData: object: 'Email Address' @@ -341,9 +342,19 @@ class App.ChannelEmailAccountOverview extends App.Controller callback: @load ) + email_address_delete: (e) => + e.preventDefault() + id = $(e.target).closest('li').data('id') + item = App.EmailAddress.find(id) + new App.ControllerGenericDestroyConfirm( + item: item + container: @el.closest('.content') + callback: @load + ) + edit_notification_outbound: (e) => e.preventDefault() - id = $(e.target).closest('tr').data('id') + id = $(e.target).closest('.action').data('id') channel = App.Channel.find(id) slide = 'js-outbound' new App.ChannelEmailNotificationWizard( diff --git a/app/assets/javascripts/app/views/channel/email_account_overview.jst.eco b/app/assets/javascripts/app/views/channel/email_account_overview.jst.eco index 8776fb59f..4e141f717 100644 --- a/app/assets/javascripts/app/views/channel/email_account_overview.jst.eco +++ b/app/assets/javascripts/app/views/channel/email_account_overview.jst.eco @@ -1,24 +1,110 @@ -

<%- @T('Email Accounts') %>

<% if !_.isEmpty(@accounts_fixed): %> -

<%- @T('Fixed email addresses') %>

- +

<%- @T('Fixed Email Accounts') %>

+
+
+
+
    + <% for email_address in @accounts_fixed: %> +
  • <%= email_address.realname %> <<%= email_address.email %>> + <% end %> +
+
+
+
<% end %> +

<%- @T('Email Accounts') %>

+ <% if !_.isEmpty(@not_used_email_addresses): %> -

<%- @T('Unlinked Email Addresses') %>

- +
+
+
+ <%- @T('Notice') %>: <%- @T('Unlinked Email Addresses, assign it to a channel or delete it.') %> + +
+
+
<% end %> -

<%- @T('Linked Email Addresses') %>

+<% if _.isEmpty(@account_channels): %> +

<%- @T('You have no configured account right now.') %>

+<% else: %> + <% for channel in @account_channels: %> +
+
+
+ <%- @Icon('status', channel.status_in + " inline") %> <%- @T('Inbound') %>:
+ + <%- @T('User') %>: <%= channel.options.inbound.options.user %>
+ <%- @T('Host') %>: <%= channel.options.inbound.options.host %>
+ <%- @T('Protocol') %>: <%= channel.options.inbound.adapter %> +
+ + <% if !_.isEmpty(channel.last_log_in): %> +
+ <%- @T('Notice') %>:
+ <%= channel.last_log_in %> +
+ <% end %> + +
+ + <%- @Icon('status', channel.status_out + " inline") %> <%- @T('Outbound') %>:
+ + <% if channel.options.outbound && channel.options.outbound.options: %> + <%- @T('User') %>: <%= channel.options.outbound.options.user %>
+ <%- @T('Host') %>: <%= channel.options.outbound.options.host %>
+ <% end %> + <%- @T('Protocol') %>: <%= channel.options.outbound.adapter %> +
+ <% if !_.isEmpty(channel.last_log_out): %> +
+ <%- @T('Notice') %>:
+ <%= channel.last_log_out %> +
+ <% end %> +
+
+ <%- @T('Email Adresses') %>: +
    + <% if !_.isEmpty(channel.email_addresses): %> + <% for email_address in channel.email_addresses: %> +
  • + + <% if channel.email_addresses.length > 1: %> +
    + <%- @Icon('diagonal-cross') %> +
    + <% end %> + <% end %> + <% else: %> +
  • <%- @T('none') %> + <% end %> +
+ + <%- @T('Add Email') %> +
+
+
+
<%- @T('Delete') %>
+
+
+ <% end %> +<% end %> + + +<%- @T('New') %> <% if !_.isEmpty(@notification_channels) && !App.Config.get('system_online_service'): %> -

<%- @T('Notification Email Account') %>

+

<%- @T('Email Notification') %>

+ <% for channel in @notification_channels: %> +
+
+
+ <%- @Icon('status', channel.status_out + " inline") %> <%- @T('Outbound') %>:
+ + <% if channel.options.outbound && channel.options.outbound.options: %> + <%- @T('User') %>: <%= channel.options.outbound.options.user %>
+ <%- @T('Host') %>: <%= channel.options.outbound.options.host %>
+ <% end %> + <%- @T('Protocol') %>: <%= channel.options.outbound.adapter %> +
+ + <% if channel.status_in is 'error': %> +
<%= channel.last_log_in %>
+ <% end %> + <% if channel.status_out is 'error': %> +
<%= channel.last_log_out %>
+ <% end %> + +
+
+
+ <% end %> +<% end %> + + \ No newline at end of file diff --git a/app/controllers/channels_controller.rb b/app/controllers/channels_controller.rb index 772f30c44..d35d88b93 100644 --- a/app/controllers/channels_controller.rb +++ b/app/controllers/channels_controller.rb @@ -31,7 +31,7 @@ curl http://localhost/api/v1/channels.json -v -u #{login}:#{password} -H "Conten not_used_email_address_ids = [] accounts_fixed = [] assets = {} - Channel.all.each {|channel| + Channel.order(:id).each {|channel| if system_online_service && channel.preferences && channel.preferences['online_service_disable'] email_addresses = EmailAddress.where(channel_id: channel.id) email_addresses.each {|email_address| diff --git a/app/controllers/email_addresses_controller.rb b/app/controllers/email_addresses_controller.rb index 14ea8b647..9a531b934 100644 --- a/app/controllers/email_addresses_controller.rb +++ b/app/controllers/email_addresses_controller.rb @@ -123,7 +123,7 @@ Response: } Test: -curl http://localhost/api/v1/email_addresses.json -v -u #{login}:#{password} -H "Content-Type: application/json" -X PUT -d '{"name": "some_name","active": true, "note": "some note"}' +curl http://localhost/api/v1/email_addresses/#{id}.json -v -u #{login}:#{password} -H "Content-Type: application/json" -X PUT -d '{"name": "some_name","active": true, "note": "some note"}' =end @@ -135,10 +135,13 @@ curl http://localhost/api/v1/email_addresses.json -v -u #{login}:#{password} -H =begin Resource: +POST /api/v1/email_addresses/{id}.json Response: +{} Test: +curl http://localhost/api/v1/email_addresses/#{id}.json -v -u #{login}:#{password} -H "Content-Type: application/json" -X DELETE =end diff --git a/app/models/email_address.rb b/app/models/email_address.rb index 7e75dbee2..1bac82234 100644 --- a/app/models/email_address.rb +++ b/app/models/email_address.rb @@ -6,8 +6,9 @@ class EmailAddress < ApplicationModel validates :realname, presence: true validates :email, presence: true - before_create :channel_check - before_update :channel_check + before_create :check_if_channel_exists_set_inactive + before_update :check_if_channel_exists_set_inactive + after_destroy :delete_group_reference latest_change_support @@ -21,28 +22,42 @@ check and if channel not exists reset configured channels for email addresses def self.channel_cleanup EmailAddress.all.each {|email_address| + + # set to active if channel exists if email_address.channel_id && Channel.find_by(id: email_address.channel_id) if !email_address.active email_address.save end next end - if email_address.channel_id || email_address.active - email_address.save - end + + # set in inactive if channel not longer exists + next if !email_address.active + email_address.save } end private - def channel_check + # set email address to inactive/active if channel exists or not + def check_if_channel_exists_set_inactive + + # set to active if channel exists if channel_id && Channel.find_by(id: channel_id) self.active = true return true end + + # set in inactive if channel not longer exists self.channel_id = nil self.active = false true end + # delete group.email_address_id reference if email address get's deleted + def delete_group_reference + Group.where(email_address_id: id).each { |group| + group.email_address_id = nil + } + end end diff --git a/config/routes/email_address.rb b/config/routes/email_address.rb index 49542f856..234ed1bf8 100644 --- a/config/routes/email_address.rb +++ b/config/routes/email_address.rb @@ -2,9 +2,10 @@ Zammad::Application.routes.draw do api_path = Rails.configuration.api_path # groups - match api_path + '/email_addresses', to: 'email_addresses#index', via: :get - match api_path + '/email_addresses/:id', to: 'email_addresses#show', via: :get - match api_path + '/email_addresses', to: 'email_addresses#create', via: :post - match api_path + '/email_addresses/:id', to: 'email_addresses#update', via: :put + match api_path + '/email_addresses', to: 'email_addresses#index', via: :get + match api_path + '/email_addresses/:id', to: 'email_addresses#show', via: :get + match api_path + '/email_addresses', to: 'email_addresses#create', via: :post + match api_path + '/email_addresses/:id', to: 'email_addresses#update', via: :put + match api_path + '/email_addresses/:id', to: 'email_addresses#destroy', via: :delete end diff --git a/test/unit/email_address_test.rb b/test/unit/email_address_test.rb new file mode 100644 index 000000000..ac8af3f93 --- /dev/null +++ b/test/unit/email_address_test.rb @@ -0,0 +1,99 @@ +# encoding: utf-8 +require 'test_helper' + +class EmailAddressTest < ActiveSupport::TestCase + test 'basic tests' do + + EmailAddress.delete_all + email_address1 = EmailAddress.create( + realname: 'address #1', + email: 'address1@example.com', + active: true, + updated_by_id: 1, + created_by_id: 1, + ) + assert_not(email_address1.active) + + email_address1.channel_id = Channel.first.id + email_address1.save + + assert(email_address1.active) + end + + test 'group tests' do + + EmailAddress.delete_all + email_address1 = EmailAddress.create( + realname: 'address #1', + email: 'address1@example.com', + active: true, + updated_by_id: 1, + created_by_id: 1, + ) + + group1 = Group.create_or_update( + name: 'group email address 1', + email_address_id: email_address1.id, + active: true, + updated_by_id: 1, + created_by_id: 1, + ) + assert(group1.email_address_id) + email_address1.destroy + + group1 = Group.find(group1.id) + assert(group1.email_address_id) + end + + test 'channel tests' do + + channel1 = Channel.create( + area: 'Email::Account', + options: {}, + active: true, + updated_by_id: 1, + created_by_id: 1, + ) + + EmailAddress.delete_all + email_address1 = EmailAddress.create( + realname: 'address #1', + email: 'address1@example.com', + active: true, + channel_id: channel1.id, + updated_by_id: 1, + created_by_id: 1, + ) + email_address2 = EmailAddress.create( + realname: 'address #2', + email: 'address2@example.com', + active: true, + channel_id: channel1.id, + updated_by_id: 1, + created_by_id: 1, + ) + + channel1.destroy + + email_address1 = EmailAddress.find(email_address1.id) + assert_not(email_address1.channel_id) + + email_address2 = EmailAddress.find(email_address2.id) + assert_not(email_address2.channel_id) + + channel1 = Channel.create( + area: 'Email::Account', + options: {}, + active: true, + updated_by_id: 1, + created_by_id: 1, + ) + + email_address1 = EmailAddress.find(email_address1.id) + assert_not(email_address1.channel_id) + + email_address2 = EmailAddress.find(email_address2.id) + assert_not(email_address2.channel_id) + end + +end