mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-01 02:03:22 +00:00
Refactor enterprise contact user assignment
This commit is contained in:
committed by
Rob Harrington
parent
c34570154c
commit
cc5d0c35dd
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user