Skip to content

Commit

Permalink
Send mails if a manager can receive them (#47)
Browse files Browse the repository at this point in the history
* Managers might have valid emails as well

* Conform to linter and styleguide
  • Loading branch information
kronn committed Feb 9, 2024
1 parent b09d836 commit 18c6313
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 22 deletions.
20 changes: 14 additions & 6 deletions app/models/youth/person.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# encoding: utf-8
# frozen_string_literal: true

# Copyright (c) 2012-2015, Pfadibewegung Schweiz. This file is part of
# Copyright (c) 2012-2024, Pfadibewegung Schweiz. This file is part of
# hitobito_youth and licensed under the Affero General Public License version 3
# or later. See the COPYING file at the top-level directory or at
# https://github.com/hitobito/hitobito_youth.
Expand Down Expand Up @@ -38,7 +38,8 @@ module Youth::Person
def validate_ahv_number
# Allow changing the password, even if there is an invalid AHV number in the database
return if will_save_change_to_encrypted_password? && !will_save_change_to_ahv_number?
return unless ahv_number.present?
return if ahv_number.blank?

if ahv_number !~ AHV_NUMBER_REGEX
errors.add(:ahv_number, :must_be_social_security_number_with_correct_format)
return
Expand All @@ -48,10 +49,10 @@ def validate_ahv_number
end
end

# rubocop:disable Metrics/CyclomaticComplexity
def assert_either_only_managers_or_manageds
def assert_either_only_managers_or_manageds # rubocop:disable Metrics/CyclomaticComplexity,Metrics/AbcSize,Metrics/PerceivedComplexity
existent_managers = people_managers.reject { |pm| pm.marked_for_destruction? }
existent_manageds = people_manageds.reject { |pm| pm.marked_for_destruction? }

if existent_managers.any? && existent_manageds.any?
errors.add(:base, :cannot_have_managers_and_manageds)
elsif PeopleManager.exists?(managed: existent_managers.map(&:manager_id))
Expand All @@ -60,7 +61,6 @@ def assert_either_only_managers_or_manageds
errors.add(:base, :managed_already_manager)
end
end
# rubocop:enable Metrics/CyclomaticComplexity

def and_manageds
return [self] unless FeatureGate.enabled?('people.people_managers')
Expand All @@ -71,4 +71,12 @@ def and_manageds
def checksum_validate(ahv_number)
SocialSecurityNumber::Validator.new(number: ahv_number.to_s, country_code: 'ch')
end

def valid_email?(email = self.email)
if FeatureGate.enabled?('people.people_managers')
Person.mailing_emails_for(self).any? { |mail| super(mail) }
else
super(email)
end
end
end
7 changes: 4 additions & 3 deletions lib/hitobito_youth/wagon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ class Wagon < Rails::Engine
#{config.root}/app/domain
#{config.root}/app/jobs
]
# rubocop:disable Metrics/BlockLength,Metrics/LineLength
config.to_prepare do

# rubocop:disable Layout/LineLength
config.to_prepare do # rubocop:disable Metrics/BlockLength
# extend application classes here

# models
Expand Down Expand Up @@ -109,6 +109,7 @@ class Wagon < Rails::Engine
# serializer
PersonSerializer.include Youth::PersonSerializer
end
# rubocop:enable Layout/LineLength

initializer 'youth.add_settings' do |_app|
Settings.add_source!(File.join(paths['config'].existent, 'settings.yml'))
Expand Down
36 changes: 23 additions & 13 deletions spec/models/person_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# encoding: utf-8

# Copyright (c) 2012-2015, Pfadibewegung Schweiz. This file is part of
# Copyright (c) 2012-2024, Pfadibewegung Schweiz. This file is part of
# hitobito_youth and licensed under the Affero General Public License version 3
# or later. See the COPYING file at the top-level directory or at
# https://github.com/hitobito/hitobito_youth.
Expand Down Expand Up @@ -63,21 +63,31 @@
end

describe 'people managers' do
it 'does not allow for someone to be both manager and managed' do
top_leader.managers = [bottom_member]
top_leader.manageds = [Fabricate(:person)]
it 'does not allow for someone to be both manager and managed' do
top_leader.managers = [bottom_member]
top_leader.manageds = [Fabricate(:person)]

expect(top_leader).to_not be_valid
end
it 'does not allow to manage someone who is manager' do
bottom_member.manageds = [Fabricate(:person)]
bottom_member.save
expect(top_leader).to_not be_valid
end

it 'does not allow to manage someone who is manager' do
bottom_member.manageds = [Fabricate(:person)]
bottom_member.save

top_leader.manageds = [bottom_member]
top_leader.manageds = [bottom_member]

expect(top_leader).to_not be_valid
end

expect(top_leader).to_not be_valid
end
it 'can provide a mail to a managed person' do
managed = Fabricate(:person, email: nil)
expect(managed).to_not be_valid_email
expect(bottom_member).to be_valid_email

expect do
managed.managers = [bottom_member]
end.to change(managed, :valid_email?).from(false).to(true)
end
end

end

0 comments on commit 18c6313

Please sign in to comment.