From 928ba23fec730bcb329d8d06df6363f9ade1dd88 Mon Sep 17 00:00:00 2001 From: Martin van IJcken Date: Wed, 5 Nov 2025 09:39:27 +0000 Subject: [PATCH] Add script to fix which schools are attributed to BLMK and N&W These SAIS teams are currently configured with all teams in their area wheareas they should be configured with only a limited selection of schools. This change amends them to have only and exactly the schools that they should have. Jira-Issue: MAV-2352 Jira-Issue: MAV-2353 --- app/lib/school_removal.rb | 619 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 619 insertions(+) create mode 100644 app/lib/school_removal.rb diff --git a/app/lib/school_removal.rb b/app/lib/school_removal.rb new file mode 100644 index 0000000000..6534c1a1df --- /dev/null +++ b/app/lib/school_removal.rb @@ -0,0 +1,619 @@ +# frozen_string_literal: true + +class SchoolRemoval + include ActiveModel::Model + + def self.perform + new.perform + end + + def perform + ActiveRecord::Base.transaction do + remove_existing_schools!( + subteams: [bedfordshire_luton_and_milton_keynes, norfolk_and_waveney] + ) + add_correct_schools!( + subteam: bedfordshire_luton_and_milton_keynes, + schools: correct_blkm_schools + ) + add_correct_schools!( + subteam: norfolk_and_waveney, + schools: correct_n_and_w_schools + ) + remove_orphan_sessions( + teams: [ + bedfordshire_luton_and_milton_keynes.team, + norfolk_and_waveney.team + ] + ) + end + end + + private + + def remove_existing_schools!(subteams:) + Location.school.where(subteam: subteams).update_all(subteam_id: nil) + end + + def add_correct_schools!(subteam:, schools:) + schools.each do |location| + if location.subteam + warn "URN #{location.urn} previously belonged to #{location.subteam.name}" + end + + location.update!(subteam:) + location.sessions.update_all(team_id: subteam.team.id) + end + end + + def remove_orphan_sessions(teams:) + Session + .where(team: teams) + .to_a + .select { it.location.subteam.nil? } + .each do |session| + if session.session_dates.any?(&:has_been_attended?) + warn "Session #{session.id} has been attended, refusing to delete session" + else + session.destroy! + end + end + end + + def warn_for_schools_with_no_sessions(subteams:) + subteams.each do |subteam| + subteam.schools.each do |school| + if school.sessions.empty? + warn "No sessions for #{school.name} (URN #{school.urn})" + end + end + end + end + + def bedfordshire_luton_and_milton_keynes + @bedfordshire_luton_and_milton_keynes ||= + Subteam.find_by( + name: + "Bedfordshire, Luton and Milton Keynes School Age Immunisation Service" + ) + end + + def norfolk_and_waveney + @norfolk_and_waveney ||= + Subteam.find_by( + name: "Norfolk and Waveney School Age Immunisation Service" + ) + end + + def correct_blkm_schools + @correct_blkm_schools ||= + [ + 140_281, + 140_286, + 140_734, + 140_835, + 141_271, + 141_520, + 142_036, + 142_263, + 142_264, + 142_310, + 142_322, + 142_387, + 142_672, + 142_907, + 143_084, + 143_263, + 143_265, + 143_561, + 143_766, + 143_770, + 143_850, + 143_926, + 144_010, + 144_039, + 144_137, + 144_311, + 144_312, + 144_342, + 144_357, + 144_424, + 144_424, + 144_458, + 144_528, + 144_545, + 144_546, + 144_595, + 144_746, + 145_043, + 145_063, + 145_219, + 145_324, + 145_325, + 145_426, + 145_427, + 145_736, + 145_805, + 145_861, + 145_872, + 146_269, + 146_462, + 146_685, + 146_714, + 146_751, + 147_081, + 147_112, + 147_154, + 147_183, + 147_269, + 147_376, + 147_376, + 147_380, + 147_381, + 147_690, + 147_803, + 147_804, + 147_860, + 147_891, + 147_891, + 147_994, + 148_104, + 148_193, + 148_227, + 148_229, + 148_234, + 148_297, + 148_334, + 148_411, + 148_420, + 148_551, + 148_640, + 148_692, + 148_835, + 148_982, + 149_106, + 149_107, + 149_208, + 149_324, + 149_404, + 149_460, + 149_470, + 149_542, + 149_662, + 150_145, + 150_335, + 150_413, + 150_474, + 150_593, + 150_595, + 150_629, + 150_792, + 150_796, + 151_041, + 151_042, + 151_047, + 151_125, + 151_126, + 151_151, + 151_210, + 151_218, + 151_293, + 151_368, + 151_371, + 151_372, + 151_526, + 151_796, + 151_840, + 151_866, + 152_290, + 152_290 + ].map { |urn| Location.school.find_by_urn_and_site!(urn) } + end + + def correct_n_and_w_schools + @correct_n_and_w_schools ||= + [ + 141_663, + 144_427, + 140_914, + 140_990, + 141_050, + 141_161, + 141_168, + 141_172, + 141_192, + 141_226, + 141_227, + 141_228, + 141_314, + 141_356, + 141_357, + 141_358, + 141_359, + 141_360, + 145_629, + 138_797, + 139_491, + 120_765, + 120_766, + 120_767, + 120_777, + 120_781, + 120_783, + 120_784, + 120_788, + 120_789, + 120_794, + 120_795, + 120_796, + 120_797, + 120_798, + 120_808, + 120_809, + 120_812, + 120_815, + 120_816, + 120_817, + 120_818, + 120_821, + 120_822, + 120_825, + 120_827, + 120_830, + 120_835, + 120_836, + 120_840, + 120_841, + 120_843, + 120_847, + 120_848, + 120_851, + 120_852, + 120_853, + 120_856, + 120_859, + 120_862, + 120_863, + 120_866, + 120_867, + 120_883, + 120_884, + 120_887, + 120_890, + 120_896, + 120_899, + 120_903, + 120_904, + 120_905, + 120_906, + 120_908, + 120_910, + 120_911, + 120_912, + 120_913, + 120_914, + 120_917, + 120_918, + 120_920, + 120_922, + 120_928, + 120_932, + 120_954, + 120_958, + 120_972, + 120_973, + 120_982, + 120_984, + 120_987, + 120_988, + 120_990, + 120_995, + 120_999, + 121_000, + 121_011, + 121_013, + 121_019, + 121_022, + 121_026, + 121_027, + 121_035, + 121_036, + 121_037, + 121_040, + 121_041, + 121_042, + 121_050, + 121_051, + 121_053, + 121_054, + 121_059, + 121_061, + 121_062, + 121_063, + 121_066, + 121_067, + 121_069, + 121_081, + 121_082, + 121_083, + 121_086, + 121_087, + 121_090, + 121_092, + 121_093, + 121_095, + 121_096, + 121_097, + 121_108, + 121_110, + 121_113, + 121_117, + 121_123, + 121_128, + 121_134, + 121_135, + 121_146, + 121_147, + 121_149, + 121_150, + 121_190, + 121_192, + 121_197, + 121_199, + 121_202, + 121_203, + 121_205, + 121_206, + 121_239, + 121_260, + 121_265, + 141_464, + 141_465, + 141_496, + 141_584, + 141_640, + 141_661, + 141_662, + 141_691, + 141_702, + 141_721, + 141_736, + 141_758, + 141_778, + 141_780, + 141_845, + 141_895, + 141_918, + 141_983, + 141_984, + 141_991, + 142_005, + 142_016, + 142_017, + 142_057, + 124_574, + 124_676, + 124_727, + 124_748, + 124_751, + 124_868, + 124_885, + 142_187, + 142_395, + 142_465, + 142_466, + 142_467, + 142_468, + 142_526, + 142_580, + 142_588, + 142_589, + 142_599, + 142_633, + 142_634, + 142_730, + 142_732, + 142_737, + 142_751, + 142_770, + 142_786, + 142_806, + 142_837, + 142_840, + 142_842, + 142_852, + 142_871, + 142_968, + 143_053, + 143_066, + 143_135, + 110_643, + 121_017, + 121_018, + 143_279, + 143_280, + 143_459, + 143_460, + 143_461, + 143_462, + 143_463, + 143_491, + 143_492, + 143_522, + 143_671, + 143_824, + 143_949, + 143_956, + 143_961, + 143_988, + 143_990, + 144_017, + 144_019, + 144_020, + 144_021, + 144_143, + 144_144, + 144_145, + 144_146, + 144_147, + 144_313, + 144_314, + 144_315, + 144_317, + 144_360, + 144_361, + 144_425, + 144_426, + 144_428, + 144_442, + 144_443, + 144_444, + 144_445, + 144_532, + 144_565, + 144_574, + 144_640, + 144_825, + 144_826, + 144_827, + 144_828, + 144_839, + 144_849, + 144_884, + 144_972, + 144_973, + 144_974, + 144_975, + 145_028, + 145_029, + 145_159, + 145_209, + 145_301, + 145_299, + 145_304, + 145_441, + 145_442, + 145_530, + 145_531, + 145_532, + 145_533, + 145_534, + 145_535, + 145_541, + 145_559, + 145_628, + 145_694, + 145_695, + 145_737, + 145_738, + 145_749, + 145_914, + 145_975, + 146_060, + 146_061, + 146_062, + 146_065, + 146_084, + 146_319, + 146_479, + 146_550, + 146_558, + 146_684, + 146_692, + 146_711, + 146_757, + 146_880, + 147_092, + 147_247, + 147_321, + 147_322, + 147_415, + 147_567, + 147_857, + 147_940, + 147_969, + 148_119, + 148_151, + 148_152, + 148_232, + 148_237, + 148_425, + 148_668, + 148_821, + 148_822, + 148_994, + 148_995, + 148_996, + 149_415, + 149_502, + 149_516, + 149_636, + 149_744, + 149_877, + 149_878, + 149_879, + 149_896, + 150_063, + 150_067, + 150_160, + 150_175, + 150_249, + 150_451, + 150_490, + 150_582, + 150_583, + 150_625, + 150_665, + 150_666, + 150_734, + 150_806, + 151_195, + 151_308, + 151_432, + 151_635, + 151_650, + 130_819, + 133_239, + 133_505, + 133_506, + 133_507, + 133_508, + 134_043, + 134_958, + 134_960, + 134_964, + 134_965, + 131_287, + 135_148, + 136_356, + 136_536, + 137_311, + 137_459, + 137_541, + 138_138, + 138_642, + 138_793, + 138_795, + 138_796, + 138_880, + 139_058, + 139_580, + 139_736, + 139_804, + 139_929, + 140_016, + 140_033, + 140_189, + 140_363, + 140_545, + 140_573, + 140_663, + 140_681, + 140_712, + 140_819, + 140_823, + 140_852, + 140_870 + ].map { |urn| Location.school.find_by_urn_and_site!(urn) } + end +end