diff --git a/app/assets/javascripts/admins/salesman_customers/index.js b/app/assets/javascripts/admins/salesman_customers/index.js index 607e01f2e..f6ed67f12 100644 --- a/app/assets/javascripts/admins/salesman_customers/index.js +++ b/app/assets/javascripts/admins/salesman_customers/index.js @@ -58,7 +58,7 @@ $(document).on('turbolinks:load', function() { $addMemberModal.modal('hide'); setTimeout(function(){ - window.location.reload(); + listForm(); }, 500); }, error: function(res){ @@ -70,5 +70,13 @@ $(document).on('turbolinks:load', function() { $addMemberModal.modal('hide'); } }); + + + var listForm = function(){ + $.ajax({ + url: '/admins/salesman_customers?salesman_id='+ $salesmanIdInput.data("salesman-id"), + dataType: "script" + }); + }; } }); \ No newline at end of file diff --git a/app/controllers/admins/salesman_channels_controller.rb b/app/controllers/admins/salesman_channels_controller.rb index 385503d3a..44928253c 100644 --- a/app/controllers/admins/salesman_channels_controller.rb +++ b/app/controllers/admins/salesman_channels_controller.rb @@ -15,7 +15,7 @@ class Admins::SalesmanChannelsController < Admins::BaseController school_ids = params[:school_ids] - channel_ids school_ids.each do |school_id| school = School.find_by(id: school_id) - next if school.blank? + next if school.blank? || @salesman.salesman_channels.where(school_id: school.id).exists? @salesman.salesman_channels.create!(school_id: school.id) end render_ok diff --git a/app/controllers/admins/salesman_customers_controller.rb b/app/controllers/admins/salesman_customers_controller.rb index 3fa63f147..15645fe6f 100644 --- a/app/controllers/admins/salesman_customers_controller.rb +++ b/app/controllers/admins/salesman_customers_controller.rb @@ -10,7 +10,7 @@ class Admins::SalesmanCustomersController < Admins::BaseController user_ids = params[:user_ids] - customer_ids user_ids.each do |user_id| user = UserExtension.find_by(user_id: user_id) - next if user.blank? + next if user.blank? || @salesman.salesman_customers.where(user_id: user.user_id).exists? @salesman.salesman_customers.create!(user_id: user.user_id, school_id: user.school_id) end render_ok diff --git a/app/models/salesman_customer.rb b/app/models/salesman_customer.rb index c5e84650b..07f2ee83c 100644 --- a/app/models/salesman_customer.rb +++ b/app/models/salesman_customer.rb @@ -1,6 +1,6 @@ class SalesmanCustomer < ApplicationRecord belongs_to :salesman, :touch => true, counter_cache: true - belongs_to :school + belongs_to :school, optional: true belongs_to :user def name @@ -8,7 +8,7 @@ class SalesmanCustomer < ApplicationRecord end def school_name - school.name + school&.name end def courses_count diff --git a/app/views/admins/salesman_customers/index.js.erb b/app/views/admins/salesman_customers/index.js.erb new file mode 100644 index 000000000..ed90f5394 --- /dev/null +++ b/app/views/admins/salesman_customers/index.js.erb @@ -0,0 +1 @@ + $(".salesman-customer-list-container").html("<%= j(render partial: 'admins/salesman_customers/shared/list') %>") \ No newline at end of file diff --git a/db/migrate/20200225092846_add_uniq_index_to_salesman_channel.rb b/db/migrate/20200225092846_add_uniq_index_to_salesman_channel.rb new file mode 100644 index 000000000..96540d64e --- /dev/null +++ b/db/migrate/20200225092846_add_uniq_index_to_salesman_channel.rb @@ -0,0 +1,10 @@ +class AddUniqIndexToSalesmanChannel < ActiveRecord::Migration[5.2] + def change + sql = %Q(delete from salesman_channels where (salesman_id, school_id) in + (select * from (select salesman_id, school_id from salesman_channels group by salesman_id, school_id having count(*) > 1) a) + and id not in (select * from (select min(id) from salesman_channels group by salesman_id, school_id having count(*) > 1 order by id) b)) + ActiveRecord::Base.connection.execute sql + + add_index :salesman_channels, [:salesman_id, :school_id], unique: true + end +end diff --git a/db/migrate/20200225093944_add_uniq_index_to_salesman_customer.rb b/db/migrate/20200225093944_add_uniq_index_to_salesman_customer.rb new file mode 100644 index 000000000..a0067a45f --- /dev/null +++ b/db/migrate/20200225093944_add_uniq_index_to_salesman_customer.rb @@ -0,0 +1,10 @@ +class AddUniqIndexToSalesmanCustomer < ActiveRecord::Migration[5.2] + def change + sql = %Q(delete from salesman_customers where (salesman_id, user_id) in + (select * from (select salesman_id, user_id from salesman_customers group by salesman_id, user_id having count(*) > 1) a) + and id not in (select * from (select min(id) from salesman_customers group by salesman_id, user_id having count(*) > 1 order by id) b)) + ActiveRecord::Base.connection.execute sql + + add_index :salesman_customers, [:salesman_id, :user_id], unique: true + end +end