Skip to content

Commit

Permalink
Assurer le lien CLASSE MEF sur school_year (#1141)
Browse files Browse the repository at this point in the history
Co-authored-by: pskl <[email protected]>
  • Loading branch information
tnicolas1 and pskl authored Oct 15, 2024
1 parent fd3c43c commit 6d135ff
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 33 deletions.
8 changes: 8 additions & 0 deletions app/models/classe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ class Classe < ApplicationRecord
belongs_to :mef
belongs_to :school_year

validate :mef_matching_school_year

has_many :schoolings, dependent: :destroy

has_many :students, -> { order("last_name", "first_name") }, dependent: nil, through: :schoolings
Expand Down Expand Up @@ -90,4 +92,10 @@ def closed_schooling_of(student_id)
def closed_schoolings_per_student_id
@closed_schoolings_per_student_id ||= inactive_schoolings.index_by(&:student_id)
end

def mef_matching_school_year
return if mef.present? && mef.school_year.eql?(school_year)

errors.add(:mef, "doit avoir la même année scolaire que la classe.")
end
end
6 changes: 4 additions & 2 deletions app/services/updaters/student_schoolings_updater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ def initialize(student)

def call
mapped_schooling_data.each do |attributes|
school_year = SchoolYear.find_by(start_year: attributes[:school_year])

classe = Classe.find_by(
establishment: Establishment.find_by(uai: attributes[:uai]),
school_year: SchoolYear.find_by(start_year: attributes[:school_year]),
school_year:,
label: attributes[:label],
mef: Mef.find_by(code: attributes[:mef_code])
mef: Mef.find_by(code: attributes[:mef_code], school_year:)
)

next if classe.nil?
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddNotNullConstraintForMefsSchoolYear < ActiveRecord::Migration[7.2]
def change
change_column_null(:mefs, :school_year_id, false)
end
end
4 changes: 2 additions & 2 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 2 additions & 7 deletions spec/facades/establishment_facade_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@

let(:establishment) { build(:establishment, :sygne_provider) }
let(:school_year) { create(:school_year, start_year: 2020) }
let(:classe) { build(:classe, establishment: establishment, school_year: school_year) }

before do
payment_requests.each do |pr|
pr.schooling.classe.update!(establishment: establishment, school_year: school_year)
pr.schooling.update!(classe: classe)
end
end

Expand All @@ -31,12 +32,6 @@
let(:payment_requests) { create_list(:asp_payment_request, 2, :pending) }
let(:new_school_year) { create(:school_year, start_year: 2021) }

before do
payment_requests.each do |pr|
pr.schooling.classe.update!(establishment: establishment, school_year: school_year)
end
end

it "doesn't account for them" do
expect(payment_requests_counts[:pending]).to eq 0
end
Expand Down
2 changes: 1 addition & 1 deletion spec/factories/classes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
establishment factory: %i[establishment with_fim_user]

school_year { SchoolYear.current }
mef { Mef.take }
mef { Mef.find_by(school_year:) || association(:mef, school_year:) }

sequence(:label) { |n| "2NDE#{n}" }

Expand Down
2 changes: 1 addition & 1 deletion spec/factories/mefs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
transient do
daily_rate { 1 }
yearly_cap { 100 }
wage { create(:wage, daily_rate: daily_rate, yearly_cap: yearly_cap) } # rubocop:disable FactoryBot/FactoryAssociationWithStrategy
wage { create(:wage, daily_rate: daily_rate, yearly_cap: yearly_cap, school_year: school_year) } # rubocop:disable FactoryBot/FactoryAssociationWithStrategy
end

after :create do |mef, evaluator|
Expand Down
2 changes: 2 additions & 0 deletions spec/models/classe_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require "rails_helper"

RSpec.describe Classe do
subject(:classe) { build(:classe) }

describe "associations" do
it { is_expected.to belong_to(:establishment).class_name("Establishment") }
it { is_expected.to belong_to(:mef).class_name("Mef") }
Expand Down
36 changes: 17 additions & 19 deletions spec/models/concerns/pfmp_amount_calculator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
)
end

let(:mef) { create(:mef, daily_rate: 1, yearly_cap: 10) }
let(:mef) { create(:mef, daily_rate: 1, yearly_cap: 10, school_year: SchoolYear.current) }
let(:classe) { create(:classe, school_year: SchoolYear.current, mef: mef) }

RSpec.configure do |config|
config.alias_it_behaves_like_to(:it_calculates, "calculates")
Expand All @@ -39,7 +40,7 @@
end

before do
pfmp.schooling.classe.update!(mef: mef)
pfmp.schooling.update!(classe: classe)
end

context "when the PFMP doesn't have a day count" do
Expand Down Expand Up @@ -75,9 +76,12 @@
it_calculates "a limited amount", 2

context "when the classe is from another year" do
let(:school_year) { create(:school_year, start_year: 2022) }
before do
old_school_year = create(:school_year, start_year: 2022)
old_classe = create(:classe, school_year: old_school_year)

before { schooling.classe.update!(school_year: school_year) }
schooling.update!(classe: old_classe)
end

it_calculates "the original amount"
end
Expand All @@ -98,33 +102,27 @@
end
end

describe "#pfmps_for_mef_and_school_year" do # rubocop:disable RSpec/MultipleMemoizedHelpers
let(:mef) { create(:mef, daily_rate: 20, yearly_cap: 400) }
let(:school_year) { create(:school_year, start_year: 2022) }
let(:classe) { create(:classe, mef: mef, school_year: school_year) }
describe "#pfmps_for_mef_and_school_year" do
let(:student) { create(:student, :with_all_asp_info) }
let(:schooling) { create(:schooling, student: student, classe: classe) }
let(:pfmp) do
create(:pfmp,
:validated,
start_date: "#{school_year.start_year}-09-03",
end_date: "#{school_year.start_year}-09-28",
start_date: "2024-09-03",
end_date: "2024-09-28",
schooling: schooling,
day_count: 3)
end

before do
school_year = create(:school_year, start_year: 2020)
classe = create(:classe, school_year: school_year, mef: mef)
schooling = create(:schooling,
student: student,
classe: classe,
end_date: "#{SchoolYear.current.start_year}-08-27")
old_school_year = create(:school_year, start_year: 2022)
old_classe = create(:classe, school_year: old_school_year)
old_schooling = create(:schooling, :closed, student: student, classe: old_classe)
create(:pfmp,
:validated,
start_date: "#{school_year.start_year}-09-03",
end_date: "#{school_year.start_year}-09-28",
schooling: schooling,
start_date: "#{old_school_year.start_year}-09-03",
end_date: "#{old_school_year.start_year}-09-28",
schooling: old_schooling,
day_count: 1)
end

Expand Down
1 change: 1 addition & 0 deletions spec/models/mef_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
it { is_expected.to validate_presence_of(:mefstat11) }
it { is_expected.to validate_presence_of(:label) }
it { is_expected.to validate_presence_of(:short) }
it { is_expected.to belong_to(:school_year).class_name("SchoolYear") }

describe "bop" do
subject(:code) { mef.bop(establishment) }
Expand Down
2 changes: 1 addition & 1 deletion spec/services/csv_importer_fixture.csv
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
ine;prénom;nom;date_naissance;label_classe;mef_code;année_scolaire;date_début;date_fin;Sexe biologique;Code INSEE de ville de naissance;Code INSEE de pays de naissance;Code postal de résidence;Code INSEE de ville de résidence;Code INSEE de pays de résidence
ABCDEF034;MARIE;CURIE;12/03/2006;BAC PRO CHIMIE : 1ERE PRO;2473000432;2023;28/06/2023;;f;56162;99100;29570;29238;99100
ABCDEF034;MARIE;CURIE;12/03/2006;BAC PRO CHIMIE : 1ERE PRO;2473000432;2024;28/06/2024;;f;56162;99100;29570;29238;99100

0 comments on commit 6d135ff

Please sign in to comment.