Sha256: a3273735dbe334951a9c7d5f57a54399dc3ae9d99cc770b04938bfe3e43a318b

Contents?: true

Size: 1.17 KB

Versions: 20

Compression:

Stored size: 1.17 KB

Contents

module Renalware
  module HD
    class UpdateRollingPatientStatistics < ApplicationJob
      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

20 entries across 20 versions & 1 rubygems

Version Path
renalware-core-2.0.8 app/models/renalware/hd/update_rolling_patient_statistics.rb
renalware-core-2.0.7 app/models/renalware/hd/update_rolling_patient_statistics.rb
renalware-core-2.0.5 app/models/renalware/hd/update_rolling_patient_statistics.rb
renalware-core-2.0.4 app/models/renalware/hd/update_rolling_patient_statistics.rb
renalware-core-2.0.3 app/models/renalware/hd/update_rolling_patient_statistics.rb
renalware-core-2.0.2 app/models/renalware/hd/update_rolling_patient_statistics.rb
renalware-core-2.0.1 app/models/renalware/hd/update_rolling_patient_statistics.rb
renalware-core-2.0.0 app/models/renalware/hd/update_rolling_patient_statistics.rb
renalware-core-2.0.0.pre.rc13 app/models/renalware/hd/update_rolling_patient_statistics.rb
renalware-core-2.0.0.pre.rc11 app/models/renalware/hd/update_rolling_patient_statistics.rb
renalware-core-2.0.0.pre.rc10 app/models/renalware/hd/update_rolling_patient_statistics.rb
renalware-core-2.0.0.pre.rc9 app/models/renalware/hd/update_rolling_patient_statistics.rb
renalware-core-2.0.0.pre.rc8 app/models/renalware/hd/update_rolling_patient_statistics.rb
renalware-core-2.0.0.pre.rc7 app/models/renalware/hd/update_rolling_patient_statistics.rb
renalware-core-2.0.0.pre.rc6 app/models/renalware/hd/update_rolling_patient_statistics.rb
renalware-core-2.0.0.pre.rc5 app/models/renalware/hd/update_rolling_patient_statistics.rb
renalware-core-2.0.0.pre.rc4 app/models/renalware/hd/update_rolling_patient_statistics.rb
renalware-core-2.0.0.pre.rc3 app/models/renalware/hd/update_rolling_patient_statistics.rb
renalware-core-2.0.0.pre.rc1 app/models/renalware/hd/update_rolling_patient_statistics.rb
renalware-core-2.0.0.pre.beta12 app/models/renalware/hd/update_rolling_patient_statistics.rb