From 6d135ffe90dbeb28cd552da7ccc0f5b1ca576a55 Mon Sep 17 00:00:00 2001 From: tnicolas1 <166516439+tnicolas1@users.noreply.github.com> Date: Tue, 15 Oct 2024 16:21:14 +0200 Subject: [PATCH] Assurer le lien CLASSE MEF sur school_year (#1141) Co-authored-by: pskl --- app/models/classe.rb | 8 +++++ .../updaters/student_schoolings_updater.rb | 6 ++-- ...ot_null_constraint_for_mefs_school_year.rb | 5 +++ db/schema.rb | 4 +-- spec/facades/establishment_facade_spec.rb | 9 ++--- spec/factories/classes.rb | 2 +- spec/factories/mefs.rb | 2 +- spec/models/classe_spec.rb | 2 ++ .../concerns/pfmp_amount_calculator_spec.rb | 36 +++++++++---------- spec/models/mef_spec.rb | 1 + spec/services/csv_importer_fixture.csv | 2 +- 11 files changed, 44 insertions(+), 33 deletions(-) create mode 100644 db/migrate/20241014094756_add_not_null_constraint_for_mefs_school_year.rb diff --git a/app/models/classe.rb b/app/models/classe.rb index 488c760aa..021bbe733 100644 --- a/app/models/classe.rb +++ b/app/models/classe.rb @@ -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 @@ -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 diff --git a/app/services/updaters/student_schoolings_updater.rb b/app/services/updaters/student_schoolings_updater.rb index eef103b7e..2e813cad8 100644 --- a/app/services/updaters/student_schoolings_updater.rb +++ b/app/services/updaters/student_schoolings_updater.rb @@ -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? diff --git a/db/migrate/20241014094756_add_not_null_constraint_for_mefs_school_year.rb b/db/migrate/20241014094756_add_not_null_constraint_for_mefs_school_year.rb new file mode 100644 index 000000000..ce0822011 --- /dev/null +++ b/db/migrate/20241014094756_add_not_null_constraint_for_mefs_school_year.rb @@ -0,0 +1,5 @@ +class AddNotNullConstraintForMefsSchoolYear < ActiveRecord::Migration[7.2] + def change + change_column_null(:mefs, :school_year_id, false) + end +end diff --git a/db/schema.rb b/db/schema.rb index 459871f17..5204a52f4 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.2].define(version: 2024_10_11_131127) do +ActiveRecord::Schema[7.2].define(version: 2024_10_14_094756) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -166,7 +166,7 @@ t.datetime "updated_at", null: false t.string "mefstat11", null: false t.integer "ministry", null: false - t.bigint "school_year_id" + t.bigint "school_year_id", null: false t.index ["code", "school_year_id"], name: "index_mefs_on_code_and_school_year", unique: true t.index ["mefstat11"], name: "index_mefs_on_mefstat11" t.index ["school_year_id"], name: "index_mefs_on_school_year_id" diff --git a/spec/facades/establishment_facade_spec.rb b/spec/facades/establishment_facade_spec.rb index 41af57667..4646eb7a6 100644 --- a/spec/facades/establishment_facade_spec.rb +++ b/spec/facades/establishment_facade_spec.rb @@ -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 @@ -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 diff --git a/spec/factories/classes.rb b/spec/factories/classes.rb index aca408717..814ede3e6 100644 --- a/spec/factories/classes.rb +++ b/spec/factories/classes.rb @@ -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}" } diff --git a/spec/factories/mefs.rb b/spec/factories/mefs.rb index 465ef191c..31b3bc519 100644 --- a/spec/factories/mefs.rb +++ b/spec/factories/mefs.rb @@ -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| diff --git a/spec/models/classe_spec.rb b/spec/models/classe_spec.rb index ec12eda9d..9ded82691 100644 --- a/spec/models/classe_spec.rb +++ b/spec/models/classe_spec.rb @@ -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") } diff --git a/spec/models/concerns/pfmp_amount_calculator_spec.rb b/spec/models/concerns/pfmp_amount_calculator_spec.rb index b8511219b..dc4227b9c 100644 --- a/spec/models/concerns/pfmp_amount_calculator_spec.rb +++ b/spec/models/concerns/pfmp_amount_calculator_spec.rb @@ -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") @@ -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 @@ -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 @@ -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 diff --git a/spec/models/mef_spec.rb b/spec/models/mef_spec.rb index d62dd48fc..b667331db 100644 --- a/spec/models/mef_spec.rb +++ b/spec/models/mef_spec.rb @@ -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) } diff --git a/spec/services/csv_importer_fixture.csv b/spec/services/csv_importer_fixture.csv index 70092fc08..3e6a42f7a 100644 --- a/spec/services/csv_importer_fixture.csv +++ b/spec/services/csv_importer_fixture.csv @@ -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