Skip to content

Commit

Permalink
Check for active membership to prevent 500 error when opening members…
Browse files Browse the repository at this point in the history
…hip invoice form (#1489)
  • Loading branch information
njaeggi authored Jan 10, 2025
1 parent e1e9bcf commit 975a69f
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 9 deletions.
2 changes: 1 addition & 1 deletion app/domain/people/sac_membership.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def zusatzsektion_roles
active_roles_of_type(mitglied_zusatzsektion_types)
end

def select_currently_paying(roles)
def select_paying(roles)
roles.compact.select { |role| paying_person?(role.beitragskategorie) }
end

Expand Down
14 changes: 7 additions & 7 deletions app/models/people/membership/invoice_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,28 @@ class People::Membership::InvoiceForm
validates :discount, inclusion: {in: DISCOUNTS}, allow_blank: true
validate :assert_active_membership, if: :assert_active_membership?

delegate :stammsektion_role, :zusatzsektion_roles,
:neuanmeldung_nv_zusatzsektion_roles, :select_currently_paying, to:
:sac_membership
delegate :stammsektion_role, :neuanmeldung_stammsektion_role,
:zusatzsektion_roles, :neuanmeldung_nv_zusatzsektion_roles,
:select_paying, to: :sac_membership

def initialize(person, attrs = {})
super(attrs)
@person = person
end

def stammsektion
select_currently_paying([stammsektion_role]).first&.layer_group
select_paying([stammsektion_role, neuanmeldung_stammsektion_role]).first&.layer_group
end

def zusatzsektionen
select_currently_paying(neuanmeldung_nv_zusatzsektion_roles + zusatzsektion_roles).map(&:layer_group)
select_paying(neuanmeldung_nv_zusatzsektion_roles + zusatzsektion_roles).map(&:layer_group)
end

def min_date = today.beginning_of_year

def max_date = today.next_year.end_of_year

def max_send_date = (!already_member_next_year?) ? today.end_of_year : today.next_year.end_of_year
def max_send_date = already_member_next_year? ? today.next_year.end_of_year : today.end_of_year

private

Expand All @@ -64,7 +64,7 @@ def assert_active_membership?
end

def already_member_next_year?
person.sac_membership.stammsektion_role.end_on&.year&.>= today.next_year.year
person.sac_membership.active? && (person.sac_membership.stammsektion_role.end_on&.year&.>= today.next_year.year)
end

def today = Time.zone.today
Expand Down
2 changes: 1 addition & 1 deletion app/views/people/membership_invoices/new.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
= f.date_field(:send_date, minDate: @invoice_form.min_date, maxDate: @invoice_form.max_send_date)

= f.labeled(:section_id) do
= f.inline_radio_button :section_id, @person.sac_membership.stammsektion_role.group.layer_group.id, t(".mv_yearly_invoice"), true, checked: true
= f.inline_radio_button :section_id, @invoice_form.stammsektion.id, t(".mv_yearly_invoice"), true, checked: true

.nested-radio-group
= f.labeled(:new_entry, class: "ms-3") do
Expand Down
26 changes: 26 additions & 0 deletions spec/controllers/people/membership_invoices_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,31 @@
expect(flash[:alert]).to eq "Die Person hat Datenqualitätsprobleme, daher wurde keine Rechnung erstellt."
end
end

context "only neuanmeldung for stammsektion" do
let(:person) do
person = Fabricate(:person, birthday: 42.years.ago)
Fabricate(Group::SektionsNeuanmeldungenNv::Neuanmeldung.sti_name.to_sym,
person: person,
beitragskategorie: :adult,
group: groups(:bluemlisalp_neuanmeldungen_nv))
person
end

it "creates external invoice and enqueues job" do
expect do
post :create, params: params.deep_merge(people_membership_invoice_form: {discount: 50, new_entry: true})
end.to change { ExternalInvoice.count }.by(1)
.and change { Delayed::Job.where("handler like '%CreateMembershipInvoiceJob%'").count }

expect(response).to redirect_to(external_invoices_group_person_path(groups(:bluemlisalp_mitglieder).id, person.id))
expect(flash[:notice]).to eq("Die gewünschte Rechnung wird erzeugt und an Abacus übermittelt")

job = Delayed::Job.last.payload_object
expect(job.new_entry).to eq true
expect(job.discount).to eq 50
expect(job.reference_date).to eq today
end
end
end
end
13 changes: 13 additions & 0 deletions spec/models/people/membership/invoice_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@
expect(form).not_to be_valid
expect(form.errors.full_messages).to eq ["Mitgliedschaft ist nicht gültig"]
end

it "is valid if person has neuanmeldung for stammsektion in chose section" do
person.roles.destroy_all
Fabricate(Group::SektionsNeuanmeldungenNv::Neuanmeldung.sti_name, person: person, group: groups(:bluemlisalp_neuanmeldungen_nv), start_on: 1.year.ago)
form.section_id = groups(:bluemlisalp_neuanmeldungen_nv).layer_group.id.to_s
expect(form).to be_valid
end
end

describe "send_date" do
Expand Down Expand Up @@ -143,6 +150,12 @@
expect(form.max_send_date).to eq Date.new(2024, 12, 31)
end

it "uses end of this year for max_send_date for people with neuanmeldung roles" do
person.roles.destroy_all
Fabricate(Group::SektionsNeuanmeldungenNv::Neuanmeldung.sti_name, person: person, group: groups(:bluemlisalp_neuanmeldungen_nv), start_on: 1.year.ago)
expect(form.max_send_date).to eq Date.new(2024, 12, 31)
end

context "with membership for next year" do
before { roles(:mitglied).update!(end_on: 3.years.from_now) }

Expand Down

0 comments on commit 975a69f

Please sign in to comment.