class Onboarding
Constants
- CLINIC_ATTRIBUTES
- DEFAULT_PROGRAMMES
- ORGANISATION_ATTRIBUTES
- SUBTEAM_ATTRIBUTES
- TEAM_ATTRIBUTES
- USER_ATTRIBUTES
Public Class Methods
Source
# File app/models/onboarding.rb, line 78 def initialize(hash) config = hash.deep_symbolize_keys team_type = config.fetch(:team, {})&.fetch(:type, "point_of_care")&.to_sym @organisation = Organisation.find_or_initialize_by( config.fetch(:organisation, {}).slice( *ORGANISATION_ATTRIBUTES.fetch(team_type) ) ) @team = Team.new( **config.fetch(:team, {}).slice(*TEAM_ATTRIBUTES.fetch(team_type)), organisation: @organisation, programme_types: [] ) @programmes = config .fetch(:programmes, DEFAULT_PROGRAMMES.fetch(team_type, [])) .map { |type| ExistingProgramme.new(type:, team:) } subteams_by_name = config .fetch(:subteams, {}) .transform_values { it.slice(*SUBTEAM_ATTRIBUTES.fetch(team_type)) } .transform_values { Subteam.new(**it, team:) } @subteams = subteams_by_name.values @users = config .fetch(:users, []) .map { it.slice(*USER_ATTRIBUTES.fetch(team_type)) } .map do |attributes| User .find_or_initialize_by(email: attributes[:email]) .tap { it.assign_attributes(attributes) } end @schools = config .fetch(:schools, {}) .flat_map do |team_name, schools| subteam = subteams_by_name[team_name] schools.map do |config| if config.is_a?(Hash) urn = config.fetch(:urn) name = config.fetch(:name, nil) site = config.fetch(:site, nil) address_line_1 = config.fetch(:address_line_1, nil) address_line_2 = config.fetch(:address_line_2, nil) address_town = config.fetch(:address_town, nil) address_postcode = config.fetch(:address_postcode, nil) NewSchoolSite.new( urn:, name:, site:, address_line_1:, address_line_2:, address_town:, address_postcode:, subteam:, programmes: ) else urn = config ExistingSchool.new(urn:, subteam:, programmes:) end end end @clinics = config .fetch(:clinics, {}) .each_with_object({}) do |(team_name, clinic_configs), hash| subteam = subteams_by_name[team_name] clinic_configs .map { it.slice(*CLINIC_ATTRIBUTES.fetch(team_type)) } .each do hash[Location.new(**it, type: :community_clinic)] = subteam end end end
Public Instance Methods
Source
# File app/models/onboarding.rb, line 214 def errors super.tap do |errors| merge_errors_from([organisation], errors:, name: "organisation") merge_errors_from([team], errors:, name: "team") merge_errors_from(programmes, errors:, name: "programme") merge_errors_from(subteams, errors:, name: "subteam") merge_errors_from(users, errors:, name: "user") merge_errors_from(schools, errors:, name: "school") merge_errors_from(clinics.keys, errors:, name: "clinic") end end
Calls superclass method
Source
# File app/models/onboarding.rb, line 210 def invalid?(context = nil) ([super] + models.map(&:invalid?)).any? end
Calls superclass method
Source
# File app/models/onboarding.rb, line 165 def no_duplicate_urns_across_school_types existing_school_urns = schools.select { it.is_a?(ExistingSchool) }.map(&:urn) site_urns = schools.select { it.is_a?(NewSchoolSite) }.map(&:urn) overlapping_urns = existing_school_urns & site_urns if overlapping_urns.any? errors.add( :schools, "URN(s) #{overlapping_urns.join(", ")} cannot appear as both a regular school and a site" ) end end
Source
# File app/models/onboarding.rb, line 181 def no_schools_with_existing_team_attachments schools.each_with_index do |school, index| urn = school.urn next if urn.blank? locations_with_teams = Location.gias_school.where(urn:).joins(:teams).distinct next unless locations_with_teams.exists? site_codes = locations_with_teams.pluck(:site).compact.sort team_names = locations_with_teams.flat_map { it.teams.pluck(:name) }.uniq.sort message = if site_codes.any? "URN #{urn} has sites (#{site_codes.join(", ")}) already attached to teams: #{team_names.join(", ")}" else "URN #{urn} is already attached to teams: #{team_names.join(", ")}" end errors.add("school.#{index}.urn", message) end end
Source
# File app/models/onboarding.rb, line 226 def save!(include_previous_academic_year: false) academic_years = [AcademicYear.pending] academic_years << AcademicYear.pending - 1 if include_previous_academic_year ActiveRecord::Base.transaction do models.each(&:save!) academic_years.each do |academic_year| schools.each { |school| school.attach_to_team!(academic_year:) } clinics.each do |clinic, subteam| clinic.attach_to_team!(team, academic_year:, subteam:) end end # Reload to ensure the programmes are loaded. team.reload @users.each { |user| user.teams << team } academic_years.each do |academic_year| GenericLocationFactory.call(team:, academic_year:) end PatientTeamUpdater.call(team_scope: Team.where(id: team.id)) end end
Source
# File app/models/onboarding.rb, line 206 def valid?(context = nil) ([super] + models.map(&:valid?)).all? end
Calls superclass method