Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not include main email step when logged in and build as current_user #1420

Merged
merged 13 commits into from
Dec 18, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,14 @@ module SacCas::Groups::SelfRegistrationController

def restrict_access
return redirect_to_login if !signed_in? && email_exists?
return redirect_to_memberships_tab if member_or_applied?
return redirect_to_memberships_tab if wizard.member_or_applied?
redirect_to_person_show if family?
end

def email_exists? = email.present? && Person.exists?(email: email)

def family? = current_user&.household&.present?

def member_or_applied?
current_user&.sac_membership&.stammsektion_role ||
current_user&.sac_membership&.neuanmeldung_stammsektion_role
end

def model_class
case group
when Group::AboBasicLogin
Expand Down
23 changes: 18 additions & 5 deletions app/models/wizards/signup/abo_basic_login_wizard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,20 @@ class AboBasicLoginWizard < Wizards::RegisterNewUserWizard

public :group

delegate :email, to: :main_email_field
delegate :newsletter, to: :person_fields

self.asides = ["aside_abo_basic_login"]

def member_or_applied?
current_user&.login? # do not allow if person already has a login
end

def save!
super
if current_user
person.save!
else
super
end

mailing_list&.subscribe_if(person, newsletter)
end
Expand All @@ -34,9 +41,15 @@ def build_person
end

def person_attributes
person_fields
.person_attributes
.merge(main_email_field.attributes)
person_fields.person_attributes.merge(email:)
end

def step_after(step_class_or_name)
if step_class_or_name == :_start && current_user
Wizards::Steps::Signup::AboBasicLogin::PersonFields.step_name
else
super
end
end

def mailing_list = MailingList.find_by(id: Group.root.sac_newsletter_mailing_list_id)
Expand Down
10 changes: 10 additions & 0 deletions app/models/wizards/signup/abo_magazin_wizard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,18 @@ class AboMagazinWizard < AboBasicLoginWizard

self.asides = ["aside_abo"]

RESTRICTED_ROLES = [
Group::AboMagazin::Abonnent.sti_name,
Group::AboMagazin::Neuanmeldung.sti_name,
Group::AboMagazin::Gratisabonnent.sti_name
].freeze

delegate :newsletter, to: :summary

def member_or_applied?
current_user&.roles&.map(&:type)&.any? { |type| RESTRICTED_ROLES.include?(type) }
end

def costs = SacCas::ABO_COSTS[:magazin]

def requires_policy_acceptance? = false
Expand Down
9 changes: 9 additions & 0 deletions app/models/wizards/signup/abo_touren_portal_wizard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ class AboTourenPortalWizard < AboBasicLoginWizard

self.asides = ["aside_abo"]

RESTRICTED_ROLES = [
Group::AboTourenPortal::Abonnent.sti_name,
Group::AboTourenPortal::Neuanmeldung.sti_name
].freeze

def member_or_applied?
current_user&.roles&.map(&:type)&.any? { |type| RESTRICTED_ROLES.include?(type) }
end

def costs = SacCas::ABO_COSTS[:tourenportal]
end
end
5 changes: 3 additions & 2 deletions app/models/wizards/signup/sektion_wizard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ class SektionWizard < Wizards::RegisterNewUserWizard

public :group

def email
current_user&.email || step(:main_email_field)&.email
def member_or_applied?
current_user&.sac_membership&.stammsektion_role ||
current_user&.sac_membership&.neuanmeldung_stammsektion_role
end

def save!
Expand Down
3 changes: 2 additions & 1 deletion app/views/groups/self_registration/_aside_abo.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@

= turbo_frame_tag(partial_name, class: 'col-md') do
= render "abo_infos", costs: wizard.costs
= render(SelfRegistration::InfosComponent.new)
- unless current_user
= render(SelfRegistration::InfosComponent.new)
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
%td=person.last_name
%tr
%td=t("activemodel.attributes.wizards/steps/signup/main_email_field.email")
%td=wizard.main_email_field.email
%td=wizard.email
%tr
%td=person.class.human_attribute_name(:birthday)
%td=person.birthday.strftime("%d.%m.%Y")
Expand Down
1 change: 0 additions & 1 deletion lib/hitobito_sac_cas/wagon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ class Wagon < Rails::Engine
QualificationKind.include SacCas::QualificationKind
Contactable.prepend SacCas::Contactable
Wizards::Steps::NewUserForm.support_company = false
Wizards::RegisterNewUserWizard.delegate :email, to: :new_user_form

HouseholdAsideComponent.prepend SacCas::HouseholdAsideComponent
HouseholdAsideMemberComponent.prepend SacCas::HouseholdAsideMemberComponent
Expand Down
6 changes: 6 additions & 0 deletions spec/features/signup/abo_basic_login_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,10 @@ def expect_validation_error(message)
expect(page).to have_link("Anmelden")
end
end

it "redirects if logged in" do
sign_in(people(:admin))
visit group_self_registration_path(group_id: group)
expect(page).to have_content("Du besitzt bereits eine SAC-Mitgliedschaft. Wenn du diese anpassen möchtest, kontaktiere bitte die SAC-Geschäftsstelle.")
end
end
22 changes: 22 additions & 0 deletions spec/features/signup/abo_magazin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,26 @@ def complete_last_page(date: Date.tomorrow, submit: true)
expect(page).to have_field("Geburtsdatum", with: "03.01.1924")
end
end

it "has prefilled form when logged in" do
people(:admin).update!(country: "CH")
sign_in(people(:admin))
visit group_self_registration_path(group_id: group)
expect_active_step "Personendaten"
click_button "Weiter"
expect(page).to have_text "[email protected]"
check "Ich habe die AGB gelesen und stimme diesen zu"
check "Ich habe die Datenschutzerklärung gelesen und stimme dieser zu"
expect do
click_button "ABO KOSTENPFLICHTIG BESTELLEN"
expect(page).to have_css "#error_explanation, #flash > .alert"
end.to change { Role.count }.by(1)
end

it "redirects if already abonnent of magazin" do
sign_in(people(:mitglied))
Group::AboMagazin::Abonnent.create!(person: people(:mitglied), group: group)
visit group_self_registration_path(group_id: group)
expect(page).to have_content("Du besitzt bereits eine SAC-Mitgliedschaft. Wenn du diese anpassen möchtest, kontaktiere bitte die SAC-Geschäftsstelle.")
end
end
21 changes: 21 additions & 0 deletions spec/features/signup/abo_touren_portal_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,25 @@ def expect_validation_error(message)
expect(page).to have_text "Person muss 18 Jahre oder älter sein"
end.not_to change { Person.count }
end

it "has prefilled form when logged in" do
people(:admin).update!(country: "CH")
sign_in(people(:admin))
visit group_self_registration_path(group_id: group)
expect(page).to have_text "Registrieren"
expect(page).not_to have_text "Weiter"
check "Ich habe die Statuten gelesen und stimme diesen zu"
check "Ich habe die Datenschutzerklärung gelesen und stimme dieser zu"
expect do
click_button "Registrieren"
expect(page).to have_css "#error_explanation, #flash > .alert"
end.to change { Role.count }.by(1)
end

it "redirects if already abonnent of touren portal" do
sign_in(people(:mitglied))
Group::AboTourenPortal::Abonnent.create!(person: people(:mitglied), group: group)
visit group_self_registration_path(group_id: group)
expect(page).to have_content("Du besitzt bereits eine SAC-Mitgliedschaft. Wenn du diese anpassen möchtest, kontaktiere bitte die SAC-Geschäftsstelle.")
end
end
34 changes: 34 additions & 0 deletions spec/models/wizards/signup/abo_basic_login_wizard_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,39 @@ def build(params = required_attrs)
expect(max.subscriptions).to be_empty
expect(newsletter.people).to be_empty
end

it "saves role for current_user when logged in" do
allow_any_instance_of(Wizards::Signup::AboBasicLoginWizard).to receive(:current_user).and_return(people(:admin))
expect(wizard).to be_valid
expect { wizard.save! }.not_to change { Person.count }
expect(people(:admin).roles.last.type).to eq Group::AboBasicLogin::BasicLogin.sti_name
end
end

describe "steps" do
it "starts at main email field step when not logged in" do
expect(wizard.step_at(0)).to be_instance_of(Wizards::Steps::Signup::MainEmailField)
expect(wizard.step_at(1)).to be_instance_of(Wizards::Steps::Signup::AboBasicLogin::PersonFields)
end

it "starts at person fields step when logged in" do
allow_any_instance_of(Wizards::Signup::AboBasicLoginWizard).to receive(:current_user).and_return(people(:admin))
expect(wizard.step_at(0)).to be_instance_of(Wizards::Steps::Signup::AboBasicLogin::PersonFields)
end
end

describe "#member_or_applied?" do
before do
allow_any_instance_of(Wizards::Signup::AboBasicLoginWizard).to receive(:current_user).and_return(people(:mitglied))
end

it "returns true when user has login" do
expect(wizard.member_or_applied?).to be_truthy
end

it "returns true if user does not have login" do
allow(people(:mitglied)).to receive(:login?).and_return(false)
expect(wizard.member_or_applied?).to be_falsy
end
end
end
49 changes: 49 additions & 0 deletions spec/models/wizards/signup/abo_magazin_wizard_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@ def build(params = required_attrs)
.with(kind_of(Person), group, true)
end
end

it "saves role for current_user when logged in" do
allow_any_instance_of(Wizards::Signup::AboBasicLoginWizard).to receive(:current_user).and_return(people(:admin))
expect(wizard).to be_valid
expect { wizard.save! }.not_to change { Person.count }
expect(people(:admin).roles.last.type).to eq Group::AboMagazin::Abonnent.sti_name
end
end

describe "#calculate_costs" do
Expand All @@ -141,4 +148,46 @@ def build(params = required_attrs)
expect(wizard.calculated_costs).to eq(76)
end
end

describe "steps" do
it "starts at main email field step when not logged in" do
expect(wizard.step_at(0)).to be_instance_of(Wizards::Steps::Signup::MainEmailField)
expect(wizard.step_at(1)).to be_instance_of(Wizards::Steps::Signup::AboMagazin::PersonFields)
expect(wizard.step_at(2)).to be_instance_of(Wizards::Steps::Signup::AboMagazin::Summary)
end

it "starts at person fields step when logged in" do
allow_any_instance_of(Wizards::Signup::AboBasicLoginWizard).to receive(:current_user).and_return(people(:admin))
expect(wizard.step_at(0)).to be_instance_of(Wizards::Steps::Signup::AboMagazin::PersonFields)
expect(wizard.step_at(1)).to be_instance_of(Wizards::Steps::Signup::AboMagazin::Summary)
end
end

describe "#member_or_applied?" do
let(:person) { people(:mitglied) }
let(:group) { groups(:abo_die_alpen) }

before do
allow_any_instance_of(Wizards::Signup::AboBasicLoginWizard).to receive(:current_user).and_return(person)
end

it "returns true when user has abonnent role" do
Group::AboMagazin::Abonnent.create!(person:, group:)
expect(wizard.member_or_applied?).to be_truthy
end

it "returns true when user has neuanmeldung role" do
Group::AboMagazin::Neuanmeldung.create!(person:, group:)
expect(wizard.member_or_applied?).to be_truthy
end

it "returns true when user has gratis abonnent role" do
Group::AboMagazin::Gratisabonnent.create!(person:, group:)
expect(wizard.member_or_applied?).to be_truthy
end

it "returns false if user does not have role" do
expect(wizard.member_or_applied?).to be_falsy
end
end
end
41 changes: 41 additions & 0 deletions spec/models/wizards/signup/abo_touren_portal_wizard_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,5 +114,46 @@ def build(params = required_attrs)
expect(max.subscriptions).to have(1).item
expect(newsletter.people).to eq [max]
end

it "saves role for current_user when logged in" do
allow_any_instance_of(Wizards::Signup::AboBasicLoginWizard).to receive(:current_user).and_return(people(:admin))
expect(wizard).to be_valid
expect { wizard.save! }.not_to change { Person.count }
expect(people(:admin).roles.last.type).to eq Group::AboTourenPortal::Abonnent.sti_name
end
end

describe "steps" do
it "starts at main email field step when not logged in" do
expect(wizard.step_at(0)).to be_instance_of(Wizards::Steps::Signup::MainEmailField)
expect(wizard.step_at(1)).to be_instance_of(Wizards::Steps::Signup::AboTourenPortal::PersonFields)
end

it "starts at person fields step when logged in" do
allow_any_instance_of(Wizards::Signup::AboBasicLoginWizard).to receive(:current_user).and_return(people(:admin))
expect(wizard.step_at(0)).to be_instance_of(Wizards::Steps::Signup::AboTourenPortal::PersonFields)
end
end

describe "#member_or_applied?" do
let(:person) { people(:mitglied) }

before do
allow_any_instance_of(Wizards::Signup::AboBasicLoginWizard).to receive(:current_user).and_return(person)
end

it "returns true when user has abonnent role" do
Group::AboTourenPortal::Abonnent.create!(person:, group:)
expect(wizard.member_or_applied?).to be_truthy
end

it "returns true when user has neuanmeldung role" do
Group::AboTourenPortal::Neuanmeldung.create!(person:, group:)
expect(wizard.member_or_applied?).to be_truthy
end

it "returns false if user does not have role" do
expect(wizard.member_or_applied?).to be_falsy
end
end
end
Loading