Skip to content

Commit

Permalink
Add docs for StudentMerger
Browse files Browse the repository at this point in the history
  • Loading branch information
pskl committed Feb 17, 2025
1 parent 9a05629 commit 01dc86d
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 31 deletions.
25 changes: 25 additions & 0 deletions docs/fusion_élèves.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Fusion élèves

## Description

Dans les SI du système éducatif français il n'est pas impossible qu'un élève physique se retrouve avec 2 INE. Lorsque c'est le cas il est possible qu'un retour d'intégration ASP échoue car il existe une contrainte d'unicité sur l'attribut `asp_individu_id`. Pour remédier ces cas et relancer l'intégration il est nécessaire de dédoublonner l'objet `Student` qui pose problème. Il existe à cet effet la classe `StudentMerger`.

## Mode d'emploi

## Etape 1: selectionner l'étudiant

En utilisant l'id qui remonte dans Sentry ou Sidekiq:

`student = Student.find_by!(asp_individu_id: asp_id)`

## Etape 2: Verifier si il y a des doublons

`students = Student.where(last_name: student.last_name, first_name: student.first_name, birthplace_city_inseecode: student.birthplace_city_inseecode, birthdate: student.birthdate)`

## Etape 3: Fusionner les étudiants

`StudentMerger.new(students.to_a).merge!`

## Etape 4: Relancer les fichiers d'intégration

Cliquer "Retry Now" pour le job dans le panneau d'administration de Sidekiq.
73 changes: 42 additions & 31 deletions spec/services/student_merger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,47 +9,58 @@
let(:students) { [source_student, target_student] }
let(:merger) { described_class.new(students) }

context "with invalid inputs" do
it "raises error when not given exactly two students" do
merger = described_class.new([source_student])
expect { merger.merge! }.to raise_error(StudentMerger::InvalidStudentsArrayError)
end
end

context "when merging students with payment requests" do # rubocop:disable RSpec/MultipleMemoizedHelpers
let(:older_payment_request) { create(:asp_payment_request, created_at: 1.month.ago) }
let(:newer_payment_request) { create(:asp_payment_request, created_at: 1.day.ago) }
let(:source_student) { older_payment_request.student }
let(:target_student) { newer_payment_request.student }

context "when students are identical" do
before do
source_student.current_schooling.update!(end_date: 2.days.ago)
target_student.update!(
first_name: source_student.first_name,
last_name: source_student.last_name,
birthdate: source_student.birthdate,
birthplace_city_insee_code: source_student.birthplace_city_insee_code
)
end

it "keeps the student with the most recent payment request" do
merger.merge!
expect(Student.exists?(source_student.id)).to be false
context "with invalid inputs" do
it "raises error when not given exactly two students" do
merger = described_class.new([source_student])
expect { merger.merge! }.to raise_error(StudentMerger::InvalidStudentsArrayError)
end
end

it "raises error when trying to transfer active schoolings" do
create(:schooling, student: source_student)
expect { merger.merge! }.to raise_error(StudentMerger::ActiveSchoolingError)
end
end
context "when merging students with payment requests" do # rubocop:disable RSpec/MultipleMemoizedHelpers
let(:older_payment_request) { create(:asp_payment_request, created_at: 1.month.ago) }
let(:newer_payment_request) { create(:asp_payment_request, created_at: 1.day.ago) }
let(:source_student) { older_payment_request.student }
let(:target_student) { newer_payment_request.student }

context "when transferring asp_individu_id" do
let(:source_student) { create(:schooling, :closed).student }
let(:target_student) { create(:schooling, :closed).student }
before do
source_student.current_schooling.update!(end_date: 2.days.ago)
end

before do
source_student.update!(asp_individu_id: "123ABC")
target_student.update!(asp_individu_id: nil)
it "keeps the student with the most recent payment request" do
merger.merge!
expect(Student.exists?(source_student.id)).to be false
end

it "raises error when trying to transfer active schoolings" do
create(:schooling, student: source_student)
expect { merger.merge! }.to raise_error(StudentMerger::ActiveSchoolingError)
end
end

it "transfers asp_individu_id from source to target student" do
merger.merge!
context "when transferring asp_individu_id" do
let(:source_student) { create(:schooling, :closed).student }
let(:target_student) { create(:schooling, :closed).student }

before do
source_student.update!(asp_individu_id: "123ABC")
target_student.update!(asp_individu_id: nil)
end

it "transfers asp_individu_id from source to target student" do
merger.merge!

expect(target_student.reload.asp_individu_id).to eq("123ABC")
expect(target_student.reload.asp_individu_id).to eq("123ABC")
end
end
end
end
Expand Down

0 comments on commit 01dc86d

Please sign in to comment.