diff --git a/app/models/exclusion.rb b/app/models/exclusion.rb index 5c705c8ab..4afc3898f 100644 --- a/app/models/exclusion.rb +++ b/app/models/exclusion.rb @@ -11,16 +11,16 @@ class Exclusion < ApplicationRecord scope :outside_contract, -> { where.not(mef_code: nil) } class << self - def outside_contract?(uai, mef_code) - exists?(uai: uai, mef_code: mef_code) + def outside_contract?(uai, mef_code, start_year) + exists?(uai:, mef_code:, start_year:) end - def establishment_excluded?(uai) - whole_establishment.exists?(uai: uai) + def establishment_excluded?(uai, start_year) + whole_establishment.exists?(uai:, start_year:) end - def excluded?(uai, mef_code) - establishment_excluded?(uai) || outside_contract?(uai, mef_code) + def excluded?(uai, mef_code, start_year = nil) + establishment_excluded?(uai, start_year) || outside_contract?(uai, mef_code, start_year) end end diff --git a/app/models/schooling.rb b/app/models/schooling.rb index 43fc35bb0..4e9a226c9 100644 --- a/app/models/schooling.rb +++ b/app/models/schooling.rb @@ -97,7 +97,7 @@ def max_end_date end def excluded? - Exclusion.excluded?(establishment.uai, mef.code) + Exclusion.excluded?(establishment.uai, mef.code, classe.school_year.start_year) end def removed? diff --git a/app/models/student/mappers/base.rb b/app/models/student/mappers/base.rb index 57ad034b5..b200082ac 100644 --- a/app/models/student/mappers/base.rb +++ b/app/models/student/mappers/base.rb @@ -74,7 +74,7 @@ def map_classe!(entry) return if label.nil? || mef.nil? || school_year.nil? - return if Exclusion.excluded?(uai, mef_code) + return if Exclusion.excluded?(uai, mef_code, school_year.start_year) Classe.find_or_create_by!( label:, diff --git a/db/exclusion_seeder.rb b/db/exclusion_seeder.rb index b00ffb23b..17c5eb776 100644 --- a/db/exclusion_seeder.rb +++ b/db/exclusion_seeder.rb @@ -22,16 +22,16 @@ class ExclusionSeeder %w[0442083A 2473121131], %w[0442083A 2473121332], %w[0442083A 2473121333], - %w[0442227G 2403320511], %w[0910838S 2473000433], - %w[0910838S 2473121432] + %w[0910838S 2473121432], + %w[0442227G 2403320511 2023] ].freeze def self.seed logger = ActiveSupport::TaggedLogging.new(Logger.new($stdout)) - EXCLUSIONS.each do |uai, mef_code| - Exclusion.find_or_create_by!(uai: uai, mef_code: mef_code) + EXCLUSIONS.each do |uai, mef_code, start_year| + Exclusion.find_or_create_by!(uai:, mef_code:, start_year:) end logger.info { "[seeds] upserted #{Exclusion.count} exclusions" } diff --git a/db/migrate/20250127092313_add_school_year_to_exclusions.rb b/db/migrate/20250127092313_add_school_year_to_exclusions.rb new file mode 100644 index 000000000..83327e89d --- /dev/null +++ b/db/migrate/20250127092313_add_school_year_to_exclusions.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +class AddSchoolYearToExclusions < ActiveRecord::Migration[8.0] + def change + add_column :exclusions, :start_year, :int, default: nil + + remove_index :exclusions, %i[uai mef_code] + add_index :exclusions, %i[uai mef_code start_year], unique: true + end +end diff --git a/db/schema.rb b/db/schema.rb index b1a7cbdbd..da6b17b32 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[8.0].define(version: 2025_01_13_111229) do +ActiveRecord::Schema[8.0].define(version: 2025_01_27_092313) do # These are extensions that must be enabled in order to support this database enable_extension "pg_catalog.plpgsql" enable_extension "unaccent" @@ -149,7 +149,8 @@ t.string "mef_code" t.datetime "created_at", null: false t.datetime "updated_at", null: false - t.index ["uai", "mef_code"], name: "index_exclusions_on_uai_and_mef_code", unique: true + t.integer "start_year" + t.index ["uai", "mef_code", "start_year"], name: "index_exclusions_on_uai_and_mef_code_and_start_year", unique: true end create_table "invitations", force: :cascade do |t|