Skip to content

Commit

Permalink
3013: Consolidate users (#3050)
Browse files Browse the repository at this point in the history
* Update users in DB to match partner users

* Change all Partner User instances to User

* Add new routes and clean up sign in process

* Clean up and remove files that are no longer needed

* Fix specs

* Updates per code review

* Missing factory

* Fix last spec
  • Loading branch information
Daniel Orner authored Jul 30, 2022
1 parent 40bf819 commit 838d511
Show file tree
Hide file tree
Showing 98 changed files with 343 additions and 1,570 deletions.
4 changes: 0 additions & 4 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ Layout/ArgumentAlignment:
- 'app/models/product_drive.rb'
- 'app/models/distribution.rb'
- 'app/models/organization_stats.rb'
- 'app/models/partner_user.rb'
- 'app/models/partners/user.rb'
- 'app/models/storage_location.rb'
- 'app/models/user.rb'
- 'app/pdfs/distribution_pdf.rb'
Expand Down Expand Up @@ -727,15 +725,13 @@ Style/StringLiterals:
- 'spec/models/organization_spec.rb'
- 'spec/models/partner_group_spec.rb'
- 'spec/models/partner_spec.rb'
- 'spec/models/partner_user_spec.rb'
- 'spec/models/partners/authorized_family_member_spec.rb'
- 'spec/models/partners/child_item_request_spec.rb'
- 'spec/models/partners/child_spec.rb'
- 'spec/models/partners/item_request_spec.rb'
- 'spec/models/partners/partner_form_spec.rb'
- 'spec/models/partners/partner_spec.rb'
- 'spec/models/partners/request_spec.rb'
- 'spec/models/partners/user_spec.rb'
- 'spec/models/request_item_spec.rb'
- 'spec/models/user_spec.rb'
- 'spec/rails_helper.rb'
Expand Down
59 changes: 0 additions & 59 deletions app/controllers/consolidated_logins_controller.rb

This file was deleted.

4 changes: 1 addition & 3 deletions app/controllers/partners/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ module Partners
class BaseController < ApplicationController
layout 'partners/application'

skip_before_action :authenticate_user!
skip_before_action :authorize_user
before_action :authenticate_partner_user!

private

Expand All @@ -14,7 +12,7 @@ def redirect_to_root

helper_method :current_partner
def current_partner
current_partner_user.partner
current_user.partner
end

def verify_partner_is_active
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/partners/family_requests_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def create
end

create_service = Partners::FamilyRequestCreateService.new(
partner_user_id: current_partner_user.id,
partner_user_id: current_user.id,
family_requests_attributes: family_requests_attributes,
for_families: true
)
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/partners/individuals_requests_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def new

def create
create_service = Partners::FamilyRequestCreateService.new(
partner_user_id: current_partner_user.id,
partner_user_id: current_user.id,
comments: individuals_request_params[:comments],
family_requests_attributes: individuals_request_params[:items_attributes]&.values
)
Expand All @@ -28,7 +28,7 @@ def create
[item[:name], item[:id]]
end.sort

Rails.logger.info("[Request Creation Failure] partner_user_id=#{current_partner_user.id} reason=#{@errors.full_messages}")
Rails.logger.info("[Request Creation Failure] partner_user_id=#{current_user.id} reason=#{@errors.full_messages}")

render :new, status: :unprocessable_entity
end
Expand Down
10 changes: 0 additions & 10 deletions app/controllers/partners/invitations_controller.rb

This file was deleted.

41 changes: 0 additions & 41 deletions app/controllers/partners/passwords_controller.rb

This file was deleted.

4 changes: 2 additions & 2 deletions app/controllers/partners/requests_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def show

def create
create_service = Partners::RequestCreateService.new(
partner_user_id: current_partner_user.id,
partner_user_id: current_user.id,
comments: partner_request_params[:comments],
item_requests_attributes: partner_request_params[:item_requests_attributes]&.values || []
)
Expand All @@ -39,7 +39,7 @@ def create
[item[:name], item[:id]]
end.sort

Rails.logger.info("[Request Creation Failure] partner_user_id=#{current_partner_user.id} reason=#{@errors.full_messages}")
Rails.logger.info("[Request Creation Failure] partner_user_id=#{current_user.id} reason=#{@errors.full_messages}")

render :new, status: :unprocessable_entity
end
Expand Down
39 changes: 0 additions & 39 deletions app/controllers/partners/sessions_controller.rb

This file was deleted.

14 changes: 12 additions & 2 deletions app/controllers/partners/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def new
end

def create
user = PartnerUser.invite!(
user = ::User.invite!(
email: user_params[:email],
name: user_params[:name],
partner: current_partner,
Expand All @@ -19,10 +19,20 @@ def create
redirect_to partners_users_path
end

def switch_to_bank_role
if current_user.organization.nil?
error_message = "Attempted to switch to a bank role but you have no bank associated with your account!"
redirect_back(fallback_location: root_path, alert: error_message)
return
end

redirect_to dashboard_path(current_user.organization)
end

private

def user_params
params.require(:partners_user).permit(:name, :email)
params.require(:user).permit(:name, :email)
end
end
end
2 changes: 1 addition & 1 deletion app/controllers/requests_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def index
@items = current_organization.items.alphabetized
@partners = current_organization.partners.order(:name)
@statuses = Request.statuses.transform_keys(&:humanize)
@partner_users = Partners::User.where(id: @paginated_requests.pluck(:partner_user_id))
@partner_users = User.where(id: @paginated_requests.pluck(:partner_user_id))
@selected_request_item = filter_params[:by_request_item_id]
@selected_partner = filter_params[:by_partner]
@selected_status = filter_params[:by_status]
Expand Down
2 changes: 2 additions & 0 deletions app/controllers/static_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ def index
if current_user
if current_user.organization.present?
redirect_to dashboard_url(current_user.organization)
elsif current_user.partner.present?
redirect_to partners_dashboard_path
elsif current_user.super_admin?
redirect_to admin_dashboard_url if current_user.super_admin?
end
Expand Down
10 changes: 10 additions & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,14 @@ def index
def new
@user = User.new
end

def switch_to_partner_role
if current_user.partner.nil?
error_message = "Attempted to switch to a partner role but you have no partner associated with your account!"
redirect_back(fallback_location: root_path, alert: error_message)
return
end

redirect_to partner_user_root_path
end
end
4 changes: 2 additions & 2 deletions app/mailers/custom_devise_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ def subject_for(key)
# Replace the invitation instruction subject for partner users
# that were invited by other partner users.

if resource.is_a?(PartnerUser) && resource.id == resource.partner.primary_user&.id
if resource.try(:partner_id) && resource.id == resource.partner.primary_user&.id
"You've been invited to be a partner with #{resource.partner.organization.name}"
elsif resource.is_a?(PartnerUser) && resource.id != resource.partner.primary_user&.id
elsif resource.try(:partner_id) && resource.id != resource.partner.primary_user&.id
"You've been invited to #{resource.partner.name}'s partnerbase account"
else
super
Expand Down
4 changes: 2 additions & 2 deletions app/models/partner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def profile
end

#
# Returns the primary Partners::User record which is the
# Returns the primary User record which is the
# first & main user associated to a partner agency.
def primary_partner_user
profile&.primary_user
Expand Down Expand Up @@ -169,7 +169,7 @@ def correct_document_mime_type
end

def invite_new_partner
PartnerUser.invite!(email: email, partner: profile)
User.invite!(email: email, partner: profile)
end

def should_invite_because_email_changed?
Expand Down
42 changes: 0 additions & 42 deletions app/models/partner_user.rb

This file was deleted.

4 changes: 2 additions & 2 deletions app/models/partners/partner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ module Partners
class Partner < Base
self.table_name = "partner_profiles"

has_one :primary_user, -> { order('created_at ASC') }, class_name: 'Partners::User', inverse_of: :partner
has_many :users, dependent: :destroy
has_one :primary_user, -> { order('created_at ASC') }, class_name: '::User', inverse_of: :partner
has_many :users, class_name: '::User', dependent: :destroy
has_many :requests, dependent: :destroy
has_many :families, dependent: :destroy
has_many :children, through: :families
Expand Down
2 changes: 1 addition & 1 deletion app/models/partners/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Request < Base
self.table_name = "partner_requests"

belongs_to :partner, class_name: "Partners::Partner", dependent: :destroy
belongs_to :partner_user, class_name: "Partners::User", optional: true
belongs_to :partner_user, class_name: "::User", optional: true

has_many :item_requests, class_name: 'Partners::ItemRequest', foreign_key: :partner_request_id, dependent: :destroy, inverse_of: :request
accepts_nested_attributes_for :item_requests, allow_destroy: true, reject_if: proc { |attributes| attributes["quantity"].blank? }
Expand Down
Loading

0 comments on commit 838d511

Please sign in to comment.