Sha256: 5272cd820968fb7dc8666f329b417c3b6a90cce3b4596dc273d3182d5d2034ae

Contents?: true

Size: 1.17 KB

Versions: 4

Compression:

Stored size: 1.17 KB

Contents

module Renalware
  module HD
    class UpdateRollingPatientStatistics < ActiveJob::Base

      def initialize(patient:)
        @patient = patient
      end

      def call
        return unless recent_sessions.any?
        stats = rolling_stats_for_this_patient
        stats.hospital_unit = most_recently_used_hospital_unit
        stats.assign_attributes(auditable_sessions.to_h)
        stats.session_count = recent_sessions.count
        stats.save!
      end

      private

      attr_reader :patient

      def recent_sessions
        @recent_sessions ||= Sessions::LatestPatientSessionsQuery.new(patient: patient).call
      end

      def auditable_sessions
        @auditable_sessions ||= Sessions:: AuditableSessionCollection.new(recent_sessions)
      end

      def rolling_stats_for_this_patient
        @patient_rolling_stats ||= begin
          PatientStatistics.where(patient: patient,
                                  rolling: true,
                                  year: nil,
                                  month: nil).first_or_initialize
        end
      end

      def most_recently_used_hospital_unit
        recent_sessions.last.hospital_unit
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
renalware-core-2.0.0.pre.beta7 app/models/renalware/hd/update_rolling_patient_statistics.rb
renalware-core-2.0.0.pre.beta6 app/models/renalware/hd/update_rolling_patient_statistics.rb
renalware-core-2.0.0.pre.beta5 app/models/renalware/hd/update_rolling_patient_statistics.rb
renalware-core-2.0.0.pre.beta4 app/models/renalware/hd/update_rolling_patient_statistics.rb