|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +class TeamMigration::Base |
| 4 | + def call |
| 5 | + ActiveRecord::Base.transaction { perform } |
| 6 | + end |
| 7 | + |
| 8 | + def self.call(...) = new(...).call |
| 9 | + |
| 10 | + private_class_method :new |
| 11 | + |
| 12 | + protected |
| 13 | + |
| 14 | + def log(message) = Rails.logger.debug("#{ods_code}: #{message}") |
| 15 | + |
| 16 | + def create_team(workgroup:, **attributes) |
| 17 | + log("Creating team #{workgroup}") |
| 18 | + team = organisation.teams.create!(workgroup:, **attributes) |
| 19 | + GenericClinicFactory.call(team:) |
| 20 | + team |
| 21 | + end |
| 22 | + |
| 23 | + def destroy_team(team) |
| 24 | + team.programmes.destroy_all |
| 25 | + team.subteams.destroy_all |
| 26 | + team.destroy! |
| 27 | + end |
| 28 | + |
| 29 | + def add_team_programmes(team, *programme_types) |
| 30 | + programme_types.each do |type| |
| 31 | + log("Adding #{type} programme to #{team.workgroup}") |
| 32 | + programme = Programme.find_by!(type:) |
| 33 | + team.programmes << programme |
| 34 | + end |
| 35 | + |
| 36 | + GenericClinicFactory.call(team:) |
| 37 | + end |
| 38 | + |
| 39 | + def set_team_workgroup(team, workgroup) |
| 40 | + log("Setting #{team.name} workgroup to #{workgroup}") |
| 41 | + team.update_column(:workgroup, workgroup) |
| 42 | + end |
| 43 | + |
| 44 | + def attach_school_to_subteam(location, subteam) |
| 45 | + log("Attaching #{location.urn} to #{subteam.name}") |
| 46 | + location.sessions.update_all(team_id: subteam.team_id) |
| 47 | + location.update!(subteam:) |
| 48 | + end |
| 49 | + |
| 50 | + def attach_school_to_team(location, team) |
| 51 | + attach_school_to_subteam(location, team.subteams.first) |
| 52 | + end |
| 53 | + |
| 54 | + def add_school_year_groups(location, programmes, sen:) |
| 55 | + log( |
| 56 | + "Adding default #{programmes.map(&:name).to_sentence} year groups to #{location.urn}" |
| 57 | + ) |
| 58 | + location.location_programme_year_groups.destroy_all |
| 59 | + location.create_default_programme_year_groups!(programmes) |
| 60 | + |
| 61 | + log("Adding additional flu year groups to #{location.urn}") |
| 62 | + if sen && (programme = programmes.find(&:flu?)) |
| 63 | + location |
| 64 | + .year_groups |
| 65 | + .select { it >= 12 } |
| 66 | + .each do |year_group| |
| 67 | + location.location_programme_year_groups.create!( |
| 68 | + programme:, |
| 69 | + year_group: |
| 70 | + ) |
| 71 | + end |
| 72 | + end |
| 73 | + end |
| 74 | + |
| 75 | + def organisation |
| 76 | + @organisation ||= Organisation.find_by!(ods_code:) |
| 77 | + end |
| 78 | +end |
0 commit comments