Refactor enterprise contact user assignment

This commit is contained in:
Matt-Yorkley
2017-12-31 00:17:01 +00:00
committed by Rob Harrington
parent c34570154c
commit cc5d0c35dd
6 changed files with 13 additions and 39 deletions

View File

@@ -188,7 +188,7 @@ module Admin
def load_roles
@enterprise_roles = EnterpriseRole.find_all_by_enterprise_id(@enterprise.id)
@notification_user = EnterpriseRole.receives_notifications_for(@enterprise.id).try(:id) || @enterprise.owner.id
@current_contact = @enterprise.contact.id
end
def update_tag_rules(tag_rules_attributes)
@@ -206,9 +206,8 @@ module Admin
end
def update_enterprise_notifications
notify_user = params[:receives_notifications]
if notify_user.present?
EnterpriseRole.set_notification_user notify_user, @object.id
if params.key? :receives_notifications
@enterprise.update_contact params[:receives_notifications]
end
end

View File

@@ -178,7 +178,12 @@ class Enterprise < ActiveRecord::Base
end
def contact
EnterpriseRole.receives_notifications_for id || owner
contact = users.where(enterprise_roles: {receives_notifications: true}).first
contact || owner
end
def update_contact(user_id)
enterprise_roles.update_all(["receives_notifications=(user_id=?)", user_id])
end
def activated?
@@ -387,7 +392,7 @@ class Enterprise < ActiveRecord::Base
end
def set_default_contact
EnterpriseRole.set_notification_user self.owner_id, self.id
update_contact self.owner_id
end
def relate_to_owners_enterprises

View File

@@ -6,24 +6,4 @@ class EnterpriseRole < ActiveRecord::Base
validates_uniqueness_of :enterprise_id, scope: :user_id, message: I18n.t(:enterprise_role_uniqueness_error)
scope :by_user_email, joins(:user).order('spree_users.email ASC')
def self.receives_notifications_for(enterprise_id)
manager = EnterpriseRole.find_by_enterprise_id_and_receives_notifications(enterprise_id, true)
return nil if manager.blank?
Spree::User.find(manager.user_id)
end
def self.set_notification_user(user_id, enterprise_id)
managers_for(enterprise_id).map do |m|
if m.user_id == user_id.to_i
m.update_attributes receives_notifications: true
elsif m.receives_notifications
m.update_attributes receives_notifications: false
end
end
end
def self.managers_for(enterprise_id)
EnterpriseRole.where(enterprise_id: enterprise_id)
end
end

View File

@@ -24,8 +24,8 @@
- if full_permissions
.eight.columns.omega
%select.select2.fullwidth{id: 'receives_notifications_dropdown', name: 'receives_notifications', ng: {model: 'receivesNotifications', init: "receivesNotifications = '#{@notification_user}'"}}
%option{ng: {repeat: 'user in Enterprise.users', selected: "user.id == #{@notification_user}", hide: '!user.confirmed'}, value: '{{user.id}}'}
%select.select2.fullwidth{id: 'receives_notifications_dropdown', name: 'receives_notifications', ng: {model: 'receivesNotifications', init: "receivesNotifications = '#{@current_contact}'"}}
%option{ng: {repeat: 'user in Enterprise.users', selected: "user.id == #{@current_contact}", hide: '!user.confirmed'}, value: '{{user.id}}'}
{{user.email}}
.row

View File

@@ -32,16 +32,6 @@ module Admin
distributor_manager.enterprise_roles.where(enterprise_id: enterprise).first.should be
end
it "does not grant management permission to admins" do
controller.stub spree_current_user: admin_user
enterprise_params[:enterprise][:owner_id] = admin_user
spree_put :create, enterprise_params
enterprise = Enterprise.find_by_name 'zzz'
response.should redirect_to edit_admin_enterprise_path enterprise
admin_user.enterprise_roles.where(enterprise_id: enterprise).should be_empty
end
it "overrides the owner_id submitted by the user (when not super admin)" do
controller.stub spree_current_user: distributor_manager
enterprise_params[:enterprise][:owner_id] = user

View File

@@ -345,7 +345,7 @@ feature %q{
# And I should be managing it
Enterprise.managed_by(enterprise_user).should include enterprise
expect(EnterpriseRole.receives_notifications_for(enterprise.id)).to eq enterprise.owner
expect(enterprise.contact).to eq enterprise.owner
end
context "overstepping my owned enterprises limit" do