Skip to content

Commit

Permalink
feat: add default ahv question for #58
Browse files Browse the repository at this point in the history
  • Loading branch information
diegosteiner committed Aug 21, 2024
1 parent 011d3c9 commit d1b6256
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 24 deletions.
25 changes: 1 addition & 24 deletions app/models/youth/person.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
module Youth::Person
extend ActiveSupport::Concern

require_dependency "social_security_number"
include ::SocialSecurityNumber

NATIONALITIES_J_S = %w[CH FL ANDERE].freeze

included do
Expand All @@ -24,25 +21,9 @@ module Youth::Person
has_many :manageds, through: :people_manageds

validates :nationality_j_s, inclusion: {in: NATIONALITIES_J_S, allow_blank: true}
validates :ahv_number, ahv_number: true # AhvNumberValidator in core

validate :assert_either_only_managers_or_manageds
validate :validate_ahv_number
end

AHV_NUMBER_REGEX = /\A\d{3}\.\d{4}\.\d{4}\.\d{2}\z/

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 if ahv_number.blank?

if !AHV_NUMBER_REGEX.match?(ahv_number)
errors.add(:ahv_number, :must_be_social_security_number_with_correct_format)
return
end
unless checksum_validate(ahv_number).valid?
errors.add(:ahv_number, :must_be_social_security_number_with_correct_checksum)
end
end

def assert_either_only_managers_or_manageds # rubocop:disable Metrics/CyclomaticComplexity,Metrics/AbcSize,Metrics/PerceivedComplexity
Expand All @@ -64,10 +45,6 @@ def and_manageds
[self, manageds].flatten
end

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")
super || Person.mailing_emails_for(self).any? { |mail| super(mail) }
Expand Down
13 changes: 13 additions & 0 deletions db/seeds/event_questions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# encoding: utf-8

# Copyright (c) 2012-2024, Jungwacht Blauring Schweiz. This file is part of
# hitobito_jubla 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_jubla.

Event::Question.seed_global([
{ question: 'AHV-Nummer',
disclosure: nil, # Has to be chosen for every event
event_type: nil, # Is derived for every event
type: Event::Question::AhvNumber.sti_name },
])
12 changes: 12 additions & 0 deletions spec/fixtures/event/questions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,31 @@

top_ov:
event: top_course
disclosure: optional
type: Event::Question::Default

top_vegi:
event: top_course
disclosure: optional
type: Event::Question::Default

top_more:
event: top_course
disclosure: optional
type: Event::Question::Default

# global questions (not assigned to event)
ga:
event: {}
disclosure: optional
type: Event::Question::Default

vegi:
event: {}
disclosure: optional
type: Event::Question::Default

schub:
event: {}
disclosure: optional
type: Event::Question::Default

0 comments on commit d1b6256

Please sign in to comment.