Commit 231051d5 authored by David F.'s avatar David F.

Merge branch '98-bloquage-renouvellement-adhesions-dans-un-autre-club-partenaire' into 'master'

Allow subscription of an adherent previously within another club: refs #98.

See merge request !99
parents 573770c8 a69ac280
...@@ -25,14 +25,19 @@ class Adherent < ApplicationRecord ...@@ -25,14 +25,19 @@ class Adherent < ApplicationRecord
belongs_to :medico_social_org, class_name: 'Member', optional: true belongs_to :medico_social_org, class_name: 'Member', optional: true
validates :last_name, :first_name, :born_on, :permit_number, presence: true validates :last_name, :first_name, :born_on, :permit_number, presence: true
validates_uniqueness_of :permit_number, message: "Ce numéro de licence est déjà pris. Si vous pensez qu'il s'agit du bon numéro, merci de prendre contact avec le CDSA33." validates_uniqueness_of :permit_number, message: "Ce numéro de licence est déjà pris pour cette saison. Si vous pensez qu'il s'agit du bon numéro, merci de prendre contact avec le CDSA33."
validate :at_least_one_medicosocial_org validate :at_least_one_medicosocial_org
before_validation :cleanup_medicosocial_org before_validation :cleanup_medicosocial_org
def self.available_for_current_season(params) def self.available_for_current_season(params)
current_season = Season.new(Date.today) current_season = Season.new(Date.today)
adherent = Adherent.find(params[:id]) if params[:id].present? adherent = if (id = params[:id]).present?
Adherent.find(id)
elsif (permit_number = params[:permit_number]).present?
Adherent.find_by_permit_number(permit_number)
end
if adherent && !adherent.subscriptions.map(&:season).map(&:shorten).uniq.include?(current_season.shorten) if adherent && !adherent.subscriptions.map(&:season).map(&:shorten).uniq.include?(current_season.shorten)
adherent adherent
else else
......
...@@ -39,7 +39,7 @@ RSpec.describe Adherent, type: :model do ...@@ -39,7 +39,7 @@ RSpec.describe Adherent, type: :model do
create :adherent, permit_number: "123456" create :adherent, permit_number: "123456"
new_adherent = build :adherent, permit_number: "123456" new_adherent = build :adherent, permit_number: "123456"
expect(new_adherent).not_to be_valid expect(new_adherent).not_to be_valid
expect(new_adherent.errors[:permit_number].first).to eq "Ce numéro de licence est déjà pris. Si vous pensez qu'il s'agit du bon numéro, merci de prendre contact avec le CDSA33." expect(new_adherent.errors[:permit_number].first).to eq "Ce numéro de licence est déjà pris pour cette saison. Si vous pensez qu'il s'agit du bon numéro, merci de prendre contact avec le CDSA33."
end end
context "regarding medicosocial org" do context "regarding medicosocial org" do
it "can be associated to an existing one" do it "can be associated to an existing one" do
...@@ -119,15 +119,29 @@ RSpec.describe Adherent, type: :model do ...@@ -119,15 +119,29 @@ RSpec.describe Adherent, type: :model do
after { Timecop.return } after { Timecop.return }
it "returns the adherent" do context "when :id provided (renewal)" do
# Given it "returns the adherent" do
Timecop.travel(Date.new(2017,9,10)) # Given
params[:id] = 42 Timecop.travel(Date.new(2017,9,10))
# When params[:id] = 42
result = Adherent.available_for_current_season(params) # When
# Then result = Adherent.available_for_current_season(params)
expect(result).to eq existing_adherent # Then
expect(result).to be_persisted expect(result).to eq existing_adherent
expect(result).to be_persisted
end
end
context "when :id not provided (subscription wihtin another club)" do
it "returns the adherent" do
# Given
Timecop.travel(Date.new(2017,9,10))
# When
result = Adherent.available_for_current_season(params)
# Then
expect(result).to eq existing_adherent
expect(result).to be_persisted
end
end end
end end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment