diff --git a/lib/appropriate_bodies/importers/induction_period_importer.rb b/lib/appropriate_bodies/importers/induction_period_importer.rb index 3394afe4..ffe4f4d6 100644 --- a/lib/appropriate_bodies/importers/induction_period_importer.rb +++ b/lib/appropriate_bodies/importers/induction_period_importer.rb @@ -16,6 +16,14 @@ def length (finished_on || Time.zone.today) - started_on end + def finished? + finished_on.present? && number_of_terms.present? + end + + def ongoing? + !finished? + end + # used in notes def to_h { appropriate_body_id:, started_on:, finished_on:, induction_programme:, number_of_terms: } @@ -83,6 +91,33 @@ def periods_by_trn if sibling.appropriate_body_id == current.appropriate_body_id && sibling.induction_programme == current.induction_programme case + when current.started_on == sibling.started_on && current.finished? && sibling.ongoing? + # ┌─────────────────────────────────────────┐ + # Current │ KEEP │ + # └─────────────────────────────────────────┘ + # ┌──────────────────────────────────────────────────────────> + # Sibling │ DISCARD + # └──────────────────────────────────────────────────────────> + current.notes << { + heading: "Imported from DQT", + body: "DQT held 2 induction periods for this teacher/appropriate body combination with the same start date. The ongoing one was discarded.", + data: { originals: [original_sibling, original_current], combined: current.to_h } + } + keep.delete(sibling) + keep << current + when current.started_on == sibling.started_on && sibling.finished? && current.ongoing? + # ┌──────────────────────────────────────────────────────────> + # Current │ DISCARD + # └──────────────────────────────────────────────────────────> + # ┌─────────────────────────────────────────┐ + # Sibling │ KEEP │ + # └─────────────────────────────────────────┘ + sibling.notes << { + heading: "Imported from DQT", + body: "DQT held 2 induction periods for this teacher/appropriate body combination with the same start date. The ongoing one was discarded.", + data: { originals: [original_sibling, original_current], combined: sibling.to_h } + } + next when sibling.range.cover?(current.range) # ┌─────────────────────────────┐ # Current │ DISCARD │ @@ -120,6 +155,7 @@ def periods_by_trn # ┌─────────────────────────────────────────┬───┐ # Sibling │ EXTEND │╳╳╳│ # └─────────────────────────────────────────┴───┘ + current.number_of_terms = [sibling.number_of_terms, current.number_of_terms].max sibling.finished_on = current.finished_on sibling.notes << { diff --git a/spec/lib/appropriate_bodies/importers/induction_period_importer_spec.rb b/spec/lib/appropriate_bodies/importers/induction_period_importer_spec.rb index 690de623..7ec3f084 100644 --- a/spec/lib/appropriate_bodies/importers/induction_period_importer_spec.rb +++ b/spec/lib/appropriate_bodies/importers/induction_period_importer_spec.rb @@ -128,6 +128,66 @@ end end + context 'and the first period has an end date and terms but the second does not' do + let(:sample_csv_data) do + <<~CSV + appropriate_body_id,started_on,finished_on,induction_programme_choice,number_of_terms,trn + 025e61e7-ec32-eb11-a813-000d3a228dfc,01/01/2012 00:00:00,03/03/2012 00:00:00,Full Induction Programme,3,2600071 + 025e61e7-ec32-eb11-a813-000d3a228dfc,01/01/2012 00:00:00,,Full Induction Programme,,2600071 + CSV + end + + subject { AppropriateBodies::Importers::InductionPeriodImporter.new(nil, csv: sample_csv) } + + it 'it keeps the finished (first) period' do + expect(subject.periods_as_hashes_by_trn).to eql( + { + '2600071' => [ + AppropriateBodies::Importers::InductionPeriodImporter::Row.new( + started_on: Date.new(2012, 1, 1), + finished_on: Date.new(2012, 3, 3), + induction_programme: 'Full Induction Programme', + appropriate_body_id: ab_1.legacy_id, + trn: '2600071', + number_of_terms: 3, + notes: [] + ).to_hash, + ] + } + ) + end + end + + context 'and the second period has an end date and terms but the first does not' do + let(:sample_csv_data) do + <<~CSV + appropriate_body_id,started_on,finished_on,induction_programme_choice,number_of_terms,trn + 025e61e7-ec32-eb11-a813-000d3a228dfc,01/01/2012 00:00:00,,Full Induction Programme,,2600071 + 025e61e7-ec32-eb11-a813-000d3a228dfc,01/01/2012 00:00:00,03/03/2012 00:00:00,Full Induction Programme,3,2600071 + CSV + end + + subject { AppropriateBodies::Importers::InductionPeriodImporter.new(nil, csv: sample_csv) } + + it 'it keeps the finished (second) period' do + expect(subject.periods_as_hashes_by_trn).to eql( + { + '2600071' => [ + AppropriateBodies::Importers::InductionPeriodImporter::Row.new( + started_on: Date.new(2012, 1, 1), + finished_on: Date.new(2012, 3, 3), + induction_programme: 'Full Induction Programme', + appropriate_body_id: ab_1.legacy_id, + trn: '2600071', + number_of_terms: 3, + notes: [] + ).to_hash, + ] + } + ) + end + end + context 'and the second period ends after the first period has started' do let(:sample_csv_data) do <<~CSV