class ClinicSessionFactory
This class either finds or creates a suitable community clinic session for the team, academic year, and programme type.
Itβs used when recording a vaccination for a patient outside the context of a school, and therefore we need a clinic session to record the vaccination from.
Public Class Methods
Source
# File app/lib/clinic_session_factory.rb, line 37 def self.call(...) = new(...).call private_class_method :new private attr_reader :team, :academic_year, :programme_type, :date def team_location @team_location ||= TeamLocation.find_or_create_by!( team:, academic_year:, location: team.generic_clinic ) end def existing_session Session.has_date(date).find_by(team_location:) end def new_session Session.create!(team_location:, dates: [date], requires_registration: false) end end
Source
# File app/lib/clinic_session_factory.rb, line 11 def initialize(team:, academic_year:, programme_type:, date: nil) @team = team @academic_year = academic_year @programme_type = programme_type @date = date || Date.current end
Public Instance Methods
Source
# File app/lib/clinic_session_factory.rb, line 18 def call ActiveRecord::Base.transaction do session = existing_session || new_session unless session.has_programme_type?(programme_type) session.sync_location_programme_year_groups!( programme_types: (session.programme_types + [programme_type]).sort.uniq ) # This is needed because `programme_types` is memoized when it's called above. session.reload session.instance_variable_set(:@programme_types, nil) end session end end
Source
# File app/lib/clinic_session_factory.rb, line 54 def existing_session Session.has_date(date).find_by(team_location:) end
Source
# File app/lib/clinic_session_factory.rb, line 58 def new_session Session.create!(team_location:, dates: [date], requires_registration: false) end
Source
# File app/lib/clinic_session_factory.rb, line 45 def team_location @team_location ||= TeamLocation.find_or_create_by!( team:, academic_year:, location: team.generic_clinic ) end