From 15b781b271e6db02040e8cf7c4ff9d561f2c52ca Mon Sep 17 00:00:00 2001 From: Matt-Yorkley Date: Mon, 31 Jul 2017 16:19:42 +0100 Subject: [PATCH] Update enterprise managers and contact role --- .../enterprise_controller.js.coffee | 6 +-- .../admin/enterprises/enterprises.js.coffee | 2 +- .../stylesheets/admin/enterprises.css.scss | 10 ++++- .../admin/enterprises_controller.rb | 14 +++++++ app/models/enterprise_role.rb | 19 ++++++++++ .../api/admin/enterprise_role_serializer.rb | 2 +- .../admin/enterprises/_form_data.html.haml | 1 + .../admin/enterprises/form/_users.html.haml | 38 +++++++++---------- config/locales/en.yml | 2 + 9 files changed, 67 insertions(+), 27 deletions(-) diff --git a/app/assets/javascripts/admin/enterprises/controllers/enterprise_controller.js.coffee b/app/assets/javascripts/admin/enterprises/controllers/enterprise_controller.js.coffee index 0bbe03d060..7e36a498fd 100644 --- a/app/assets/javascripts/admin/enterprises/controllers/enterprise_controller.js.coffee +++ b/app/assets/javascripts/admin/enterprises/controllers/enterprise_controller.js.coffee @@ -1,12 +1,12 @@ angular.module("admin.enterprises") - .controller "enterpriseCtrl", ($scope, $window, NavigationCheck, enterprise, EnterprisePaymentMethods, EnterpriseShippingMethods, SideMenu, StatusMessage) -> + .controller "enterpriseCtrl", ($scope, $window, NavigationCheck, enterprise, enterpriseRoles, EnterprisePaymentMethods, EnterpriseShippingMethods, SideMenu, StatusMessage) -> $scope.Enterprise = enterprise + $scope.EnterpriseRoles = enterpriseRoles $scope.PaymentMethods = EnterprisePaymentMethods.paymentMethods $scope.ShippingMethods = EnterpriseShippingMethods.shippingMethods $scope.navClear = NavigationCheck.clear $scope.menu = SideMenu $scope.newManager = { id: '', email: (t('add_manager')) } - $scope.StatusMessage = StatusMessage $scope.$watch 'enterprise_form.$dirty', (newValue) -> @@ -35,7 +35,7 @@ angular.module("admin.enterprises") $scope.removeManager = (manager) -> if manager.id? - if manager.id == $scope.Enterprise.owner.id + if manager.id == $scope.Enterprise.owner.id or manager.id == parseInt($scope.receivesNotifications) return for i, user of $scope.Enterprise.users when user.id == manager.id $scope.Enterprise.users.splice i, 1 diff --git a/app/assets/javascripts/admin/enterprises/enterprises.js.coffee b/app/assets/javascripts/admin/enterprises/enterprises.js.coffee index da122761b8..0443c2003d 100644 --- a/app/assets/javascripts/admin/enterprises/enterprises.js.coffee +++ b/app/assets/javascripts/admin/enterprises/enterprises.js.coffee @@ -1 +1 @@ -angular.module("admin.enterprises", [ "admin.paymentMethods", "admin.utils", "admin.shippingMethods", "admin.users", "textAngular", "admin.side_menu", "admin.taxons", 'admin.indexUtils', 'admin.tagRules', 'admin.dropdown', 'ngSanitize'] ) \ No newline at end of file +angular.module("admin.enterprises", [ "admin.paymentMethods", "admin.utils", "ofn.admin", "admin.shippingMethods", "admin.users", "textAngular", "admin.side_menu", "admin.taxons", 'admin.indexUtils', 'admin.tagRules', 'admin.dropdown', 'ngSanitize'] ) \ No newline at end of file diff --git a/app/assets/stylesheets/admin/enterprises.css.scss b/app/assets/stylesheets/admin/enterprises.css.scss index 4fdd14e3b7..c389913773 100644 --- a/app/assets/stylesheets/admin/enterprises.css.scss +++ b/app/assets/stylesheets/admin/enterprises.css.scss @@ -4,10 +4,18 @@ form[name="enterprise_form"] { } table.managers { - i.confirmation { + i.role { float: right; + margin-left: 0.5em; font-size: 1.5em; cursor: pointer; + } + + i.confirmation { + margin-left: 0.2em; + font-size: 1.2em; + cursor: pointer; + vertical-align: bottom; &.confirmed { color: #1ece1e; diff --git a/app/controllers/admin/enterprises_controller.rb b/app/controllers/admin/enterprises_controller.rb index 1e8a4f0968..673f5c31c1 100644 --- a/app/controllers/admin/enterprises_controller.rb +++ b/app/controllers/admin/enterprises_controller.rb @@ -11,6 +11,7 @@ module Admin before_filter :load_methods_and_fees, :only => [:edit, :update] before_filter :load_groups, :only => [:new, :edit, :update, :create] before_filter :load_taxons, :only => [:new, :edit, :update, :create] + before_filter :load_roles, only: :edit before_filter :check_can_change_sells, only: :update before_filter :check_can_change_bulk_sells, only: :bulk_update before_filter :check_can_change_owner, only: :update @@ -39,6 +40,7 @@ module Admin invoke_callbacks(:update, :before) tag_rules_attributes = params[object_name].delete :tag_rules_attributes update_tag_rules(tag_rules_attributes) if tag_rules_attributes.present? + update_enterprise_notifications if @object.update_attributes(params[object_name]) invoke_callbacks(:update, :after) flash[:success] = flash_message_for(@object, :successfully_updated) @@ -184,6 +186,11 @@ module Admin @taxons = Spree::Taxon.order(:name) end + def load_roles + @enterprise_roles = EnterpriseRole.find_all_by_enterprise_id(@enterprise.id) + @notification_user = EnterpriseRole.receives_notifications_for(@enterprise.id).try(:id) + end + def update_tag_rules(tag_rules_attributes) # Due to the combination of trying to use nested attributes and type inheritance # we cannot apply all attributes to tag rules in one hit because mass assignment @@ -198,6 +205,13 @@ module Admin end end + def update_enterprise_notifications + notify_user = params[:receives_notifications] + if notify_user.present? + EnterpriseRole.set_notification_user notify_user, @object.id + end + end + def create_calculator_for(rule, attrs) if attrs[:calculator_type].present? && attrs[:calculator_attributes].present? rule.update_attributes(calculator_type: attrs[:calculator_type]) diff --git a/app/models/enterprise_role.rb b/app/models/enterprise_role.rb index e69d439c40..9c99c4ffe8 100644 --- a/app/models/enterprise_role.rb +++ b/app/models/enterprise_role.rb @@ -6,4 +6,23 @@ 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) + 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.user_id != user_id.to_i && 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 diff --git a/app/serializers/api/admin/enterprise_role_serializer.rb b/app/serializers/api/admin/enterprise_role_serializer.rb index 3b5f889f8c..f05097edf9 100644 --- a/app/serializers/api/admin/enterprise_role_serializer.rb +++ b/app/serializers/api/admin/enterprise_role_serializer.rb @@ -1,5 +1,5 @@ class Api::Admin::EnterpriseRoleSerializer < ActiveModel::Serializer - attributes :id, :user_id, :enterprise_id, :user_email, :enterprise_name + attributes :id, :user_id, :enterprise_id, :user_email, :enterprise_name, :receives_notifications def user_email object.user.email diff --git a/app/views/admin/enterprises/_form_data.html.haml b/app/views/admin/enterprises/_form_data.html.haml index ee3d17d4aa..edd45d6ebd 100644 --- a/app/views/admin/enterprises/_form_data.html.haml +++ b/app/views/admin/enterprises/_form_data.html.haml @@ -1,4 +1,5 @@ = admin_inject_enterprise += admin_inject_enterprise_roles = admin_inject_taxons = admin_inject_payment_methods = admin_inject_shipping_methods diff --git a/app/views/admin/enterprises/form/_users.html.haml b/app/views/admin/enterprises/form/_users.html.haml index 02b6bca253..39fbd7fb55 100644 --- a/app/views/admin/enterprises/form/_users.html.haml +++ b/app/views/admin/enterprises/form/_users.html.haml @@ -10,30 +10,23 @@ %a= t('admin.whats_this') .eight.columns.omega - if full_permissions - = f.hidden_field :owner_id, class: "select2 fullwidth", 'user-select' => 'Enterprise.owner' + = f.hidden_field :owner_id, class: "select2 fullwidth", 'user-select' => 'Enterprise.owner', 'ng-model' => 'Enterprise.owner' - else = owner_email +.row + .three.columns.alpha + =f.label :user_ids, t('.contact') + - if full_permissions + %span.required * + %div{'ofn-with-tip' => t('.contact_tip')} + %a= t('admin.whats_this') --# TODO: add contact field, possibly re-use some of these tooltip keys - --#.row --# .three.columns.alpha --# = f.label :email, t('.notifications') --# - if full_permissions --# %span.required * --# .with-tip{'data-powertip' => t('.notifications_tip')} --# %a= t('admin.whats_this') --# .eight.columns.omega --# - if full_permissions --# = f.text_field :email, { placeholder: t('.notifications_placeholder'), "ng-model" => "Enterprise.email" } --# - else --# = @enterprise.email --#.row{ ng: { hide: "pristineEmail == null || pristineEmail == Enterprise.email"} } --# .alpha.three.columns --#   --# .omega.eight.columns --# = t('.notifications_note') + - if full_permissions + .eight.columns.omega + %select.select2.fullwidth{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}}'} + {{user.email}} .row .three.columns.alpha @@ -57,8 +50,11 @@ {{ manager.email }} %i.confirmation.confirmed.fa.fa-check-circle{ 'ofn-with-tip' => t('.email_confirmed'), ng: {show: 'manager.confirmed'} } %i.confirmation.unconfirmed.fa.fa-exclamation-triangle{ 'ofn-with-tip' => t('.email_not_confirmed'), ng: {show: '!manager.confirmed'} } + %i.role.contact.fa.fa-envelope-o{ 'ofn-with-tip' => t('.contact'), ng: {show: 'manager.id == receivesNotifications'} } + %i.role.owner.fa.fa-star{ 'ofn-with-tip' => t('.owner'), ng: {show: 'manager.id == Enterprise.owner.id'} } %td.actions - %a{ ng: {click: 'removeManager(manager)', class: "{disabled: manager.id == Enterprise.owner.id}"}, :class => "icon-trash no-text" } + %a{ ng: {click: 'removeManager(manager)', class: "{disabled: manager.id == Enterprise.owner.id || manager.id == receivesNotifications}"}, :class => "icon-trash no-text" } + - else - @enterprise.users.each do |manager| = manager.email diff --git a/config/locales/en.yml b/config/locales/en.yml index 50f687f18c..f67900e6b2 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -563,6 +563,8 @@ en: email_confirmation_notice_html: "Email confirmation is pending. We've sent a confirmation email to %{email}." resend: Resend owner: 'Owner' + contact: "Contact" + contact_tip: "The manager who will receive enterprise emails for orders and notifications. Must have a confirmed email adress." owner_tip: The primary user responsible for this enterprise. notifications: Notifications notifications_tip: Notifications about orders will be send to this email address.