Skip to content

Commit

Permalink
Add special cases for periods that start on the same day
Browse files Browse the repository at this point in the history
When one has an end date and number of terms it should be favoured over
the other (ongoing one) that doesn't.
  • Loading branch information
peteryates committed Jan 29, 2025
1 parent 05e701a commit 950b8b0
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
36 changes: 36 additions & 0 deletions lib/appropriate_bodies/importers/induction_period_importer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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: }
Expand Down Expand Up @@ -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 │
Expand Down Expand Up @@ -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 << {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 950b8b0

Please sign in to comment.