Skip to content

Commit

Permalink
Scope profile to organization (#1993)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwr18 authored Aug 27, 2024
1 parent d616784 commit 8b1f22e
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<%= c 'modal', **attrs do %>
<%= form_with(url: profile_user_path) do %>
<%= form_with(url: organization_profile_user_path(organization)) do %>
<%= c 'button',
styles: [:secondary, :circular],
type: 'button',
Expand Down
10 changes: 9 additions & 1 deletion app/components/create_user_modal/create_user_modal.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# frozen_string_literal: true

module CreateUserModal
class CreateUserModal < ApplicationComponent; end
class CreateUserModal < ApplicationComponent
def initialize(organization:, **)
super

@organization = organization
end

attr_reader :organization
end
end
2 changes: 1 addition & 1 deletion app/components/nav_bar/nav_bar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
</li>
<% end %>
<li class="NavBar-item NavBar-profileLink">
<%= link_to profile_path, aria: { label: t('.profile_path') } do %>
<%= link_to organization_profile_path(organization), aria: { label: t('.profile_path') } do %>
<span>
<%= t '.profile_link' %>
</span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<% end %>
<%= form_with(
model: organization,
url: profile_upgrade_business_plan_path,
url: organization_profile_upgrade_business_plan_path(organization),
local: true,
method: 'PUT',
html: { autocomplete: 'off' }
Expand Down
2 changes: 1 addition & 1 deletion app/components/user_management/user_management.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@
<%= t('.add_users_button') %>
<% end%>
<% end %>
<%= c 'create_user_modal', data: { controller: 'modal', user_management_target: 'modal' } %>
<%= c 'create_user_modal', organization: organization, data: { controller: 'modal', user_management_target: 'modal' } %>
<% end %>
24 changes: 10 additions & 14 deletions app/controllers/profile_controller.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
# frozen_string_literal: true

class ProfileController < ApplicationController
before_action :organization, :business_plans
before_action :business_plans

def index; end

def create_user
password = SecureRandom.alphanumeric(20)
user = User.new(user_params.merge(password: password, organization: organization))
user = User.new(user_params.merge(password: password, organization: @organization))
if user.save
redirect_to profile_path, flash: { success: I18n.t('profile.user.created_successfully') }
redirect_to organization_profile_path(@organization), flash: { success: I18n.t('profile.user.created_successfully') }
else
redirect_to profile_path, flash: { error: user.errors.full_messages.join(' ') }
redirect_to organization_profile_path(@organization), flash: { error: user.errors.full_messages.join(' ') }
end
end

def upgrade_business_plan
organization.business_plan.update(valid_from: nil, valid_until: nil)
@organization.business_plan.update(valid_from: nil, valid_until: nil)
business_plan = BusinessPlan.find(upgrade_business_plan_params[:business_plan_id])
business_plan.update(valid_from: Time.current, valid_until: 1.year.from_now)
organization.business_plan = business_plan
organization.upgraded_business_plan_at = Time.current
@organization.business_plan = business_plan
@organization.upgraded_business_plan_at = Time.current

if organization.save
redirect_to profile_path, flash: { success: I18n.t('profile.business_plan.updated_successfully') }
if @organization.save
redirect_to organization_profile_path(@organization), flash: { success: I18n.t('profile.business_plan.updated_successfully') }
else
redirect_to profile_path, flash: { error: organization.errors.full_messages.join(' ') }
redirect_to organization_profile_path(@organization), flash: { error: @organization.errors.full_messages.join(' ') }
end
end

Expand All @@ -39,10 +39,6 @@ def upgrade_business_plan_params
params.require(:profile).permit(:business_plan_id)
end

def organization
@organization ||= current_user.admin? ? Organization.last : current_user.organization
end

def business_plans
@business_plans ||= BusinessPlan.order(:price_per_month)
end
Expand Down
8 changes: 4 additions & 4 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@
get 'count'
end
end

get '/profile', to: 'profile#index'
post '/profile/user', to: 'profile#create_user'
put '/profile/upgrade_business_plan', to: 'profile#upgrade_business_plan'
end

get '/health', to: 'health#index'
Expand Down Expand Up @@ -132,8 +136,4 @@

resource :otp_setup, controller: :otp_setup, only: %i[show create]
resource :otp_auth, controller: :otp_auth, only: %i[show create]

get '/profile', to: 'profile#index'
post '/profile/user', to: 'profile#create_user'
put '/profile/upgrade_business_plan', to: 'profile#upgrade_business_plan'
end
16 changes: 8 additions & 8 deletions spec/requests/profile_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

describe 'GET /index' do
it 'should be successful' do
get profile_url(as: user)
get organization_profile_url(organization, as: user)
expect(response).to be_successful
end
end
Expand All @@ -23,23 +23,23 @@
let(:params) { { user: { first_name: 'Daniel', last_name: 'Theis', email: 'daniel-theis@example.org' } } }

context 'unauthenticated' do
subject { -> { post profile_user_path, params: params } }
subject { -> { post organization_profile_user_path(organization), params: params } }

it 'does not change user count' do
expect { subject.call }.not_to(change { User.count })
end
end

context 'authenticated' do
subject { -> { post profile_user_path(as: user), params: params } }
subject { -> { post organization_profile_user_path(organization, as: user), params: params } }

it 'creates a user' do
expect { subject.call }.to(change { User.count }.from(2).to(3))
end

it 'redirects to profile page' do
subject.call
expect(response).to redirect_to profile_path
expect(response).to redirect_to organization_profile_path(organization)
end

it 'shows success notification' do
Expand All @@ -58,7 +58,7 @@

it 'redirects to profile page' do
subject.call
expect(response).to redirect_to profile_path
expect(response).to redirect_to organization_profile_path(organization)
end

it 'shows success notification' do
Expand Down Expand Up @@ -86,15 +86,15 @@
let(:params) { { profile: { business_plan_id: business_plan.id } } }

context 'unauthenticated' do
subject { -> { put profile_upgrade_business_plan_path, params: params } }
subject { -> { put organization_profile_upgrade_business_plan_path(organization), params: params } }

it 'does not change user count' do
expect { subject.call }.not_to(change { organization.reload.business_plan })
end
end

context 'authenticated' do
subject { -> { put profile_upgrade_business_plan_path(as: user), params: params } }
subject { -> { put organization_profile_upgrade_business_plan_path(organization, as: user), params: params } }

it 'updates the organization business_plan' do
current_business_plan = organization.business_plan
Expand All @@ -103,7 +103,7 @@

it 'redirects to profile page' do
subject.call
expect(response).to redirect_to profile_path
expect(response).to redirect_to organization_profile_path(organization)
end

it 'shows success notification' do
Expand Down
12 changes: 5 additions & 7 deletions spec/system/profile/index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
find('a[aria-label="Zur Profilseite"]').click
end

expect(page).to have_current_path(profile_path)
expect(page).to have_current_path(organization_profile_path(organization))

# header
expect(page).to have_content("Dein 100eyes Plan: #{current_plan.name}")
Expand Down Expand Up @@ -70,7 +70,7 @@

user_to_be_deactivated.update(deactivated_at: Time.current)

visit profile_path(as: user)
visit organization_profile_path(organization, as: user)
expect(page).to have_content("3 von #{current_plan.number_of_users} Seats genutzt")
expect(page).not_to have_content(user_to_be_deactivated.name)

Expand Down Expand Up @@ -160,7 +160,7 @@
expect(page).to have_content("Mindeslaufzeit: bis #{I18n.l(1.year.from_now, format: '%m/%Y')}")

Timecop.travel(6.months.from_now + 1.minute)
visit profile_path(as: user)
visit organization_profile_path(organization, as: user)
expect(page).to have_content("Preis: #{number_to_currency(editorial_enterprise.price_per_month)}/Monat")

# WhatsApp not set up
Expand All @@ -180,8 +180,7 @@
three_sixty_dialog_client_api_key: nil
)

visit profile_path(as: user)

visit organization_profile_path(organization, as: user)
expect(page).not_to have_selector(:element, 'section', 'data-testid': 'whats-app-setup')

# 360dialog configured
Expand All @@ -192,8 +191,7 @@
three_sixty_dialog_client_api_key: SecureRandom.alphanumeric(26)
)

visit profile_path(as: user)

visit organization_profile_path(organization, as: user)
expect(page).not_to have_selector(:element, 'section', 'data-testid': 'whats-app-setup')
end
end

0 comments on commit 8b1f22e

Please sign in to comment.