diff --git a/app/controllers/user_projects_controller.rb b/app/controllers/user_projects_controller.rb index fb5aee4b4..9097c2bf1 100644 --- a/app/controllers/user_projects_controller.rb +++ b/app/controllers/user_projects_controller.rb @@ -1,4 +1,6 @@ class UserProjectsController < ApplicationController + include NotificationsHelper + before_action :load_vars before_action :check_view_tab_permissions, only: [ :index ] before_action :check_view_permissions, only: [ :index_edit ] @@ -191,7 +193,7 @@ class UserProjectsController < ApplicationController unassigned_by_user: current_user.full_name ) ) - generate_notification(current_user, @up.user, @project) + generate_notification(current_user, @up.user, false, false, @project) respond_to do |format| format.html { @@ -276,24 +278,6 @@ class UserProjectsController < ApplicationController end end - def generate_notification(user, target_user, project) - title = I18n.t('activities.unassign_user_from_project', - unassigned_user: target_user.full_name, - project: project.name, - unassigned_by_user: user.full_name) - message = "#{I18n.t('search.index.project')} #{@project.name}" - notification = Notification.create( - type_of: :assignment, - title: - ActionController::Base.helpers.sanitize(title), - message: - ActionController::Base.helpers.sanitize(message) - ) - if target_user.assignments_notification - UserNotification.create(notification: notification, user: target_user) - end - end - def init_gui @users = @project.unassigned_users end diff --git a/app/controllers/users/settings_controller.rb b/app/controllers/users/settings_controller.rb index c84c02620..dc8d5a550 100644 --- a/app/controllers/users/settings_controller.rb +++ b/app/controllers/users/settings_controller.rb @@ -1,5 +1,6 @@ class Users::SettingsController < ApplicationController include UsersGenerator + include NotificationsHelper before_action :load_user, only: [ :preferences, @@ -225,7 +226,8 @@ class Users::SettingsController < ApplicationController generate_notification(@user_organization.user, @new_user_org.user, @new_user_org.organization, - @new_user_org.role_str) + @new_user_org.role_str, + false) flash[:notice] = I18n.t( 'users.settings.organizations.edit.modal_add_user.existing_flash_success', @@ -412,6 +414,7 @@ class Users::SettingsController < ApplicationController generate_notification(@user_organization.user, @user_org.user, @user_org.organization, + false, false) format.json { render json: { @@ -582,34 +585,6 @@ class Users::SettingsController < ApplicationController ) end - def generate_notification(user, target_user, org, role) - if role - title = I18n.t('notifications.assign_user_to_organization', - assigned_user: target_user.name, - role: role, - organization: org.name, - assigned_by_user: user.name) - else - title = I18n.t('notifications.unassign_user_from_organization', - unassigned_user: target_user.name, - organization: org.name, - unassigned_by_user: user.name) - end - - message = "#{I18n.t('search.index.organization')} #{org.name}" - notification = Notification.create( - type_of: :assignment, - title: - ActionController::Base.helpers.sanitize(title), - message: - ActionController::Base.helpers.sanitize(message) - ) - - if target_user.assignments_notification - UserNotification.create(notification: notification, user: target_user) - end - end - def reset_user_current_organization(user_org) ids = user_org.user.organizations_ids ids -= [user_org.organization.id] diff --git a/app/helpers/notifications_helper.rb b/app/helpers/notifications_helper.rb index e1f3eb61c..3e2d1132a 100644 --- a/app/helpers/notifications_helper.rb +++ b/app/helpers/notifications_helper.rb @@ -17,4 +17,40 @@ module NotificationsHelper def send_email_notification(user, notification) AppMailer.delay.notification(user, notification) end + + def generate_notification(user, target_user, org, role, project) + if org + if role + title = I18n.t('notifications.assign_user_to_organization', + assigned_user: target_user.name, + role: role, + organization: org.name, + assigned_by_user: user.name) + else + title = I18n.t('notifications.unassign_user_from_organization', + unassigned_user: target_user.name, + organization: org.name, + unassigned_by_user: user.name) + end + message = "#{I18n.t('search.index.organization')} #{org.name}" + elsif project + title = I18n.t('activities.unassign_user_from_project', + unassigned_user: target_user.full_name, + project: project.name, + unassigned_by_user: user.full_name) + message = "#{I18n.t('search.index.project')} #{@project.name}" + end + + notification = Notification.create( + type_of: :assignment, + title: + ActionController::Base.helpers.sanitize(title), + message: + ActionController::Base.helpers.sanitize(message) + ) + + if target_user.assignments_notification + UserNotification.create(notification: notification, user: target_user) + end + end end