Sha256: 3507a1d84e8d28168c801d725e65757935231451f1329acc091e9670283020c9

Contents?: true

Size: 1.2 KB

Versions: 35

Compression:

Stored size: 1.2 KB

Contents

# frozen_string_literal: true

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

35 entries across 35 versions & 1 rubygems

Version Path
renalware-core-2.0.46 app/models/renalware/hd/update_rolling_patient_statistics.rb
renalware-core-2.0.45 app/models/renalware/hd/update_rolling_patient_statistics.rb
renalware-core-2.0.44 app/models/renalware/hd/update_rolling_patient_statistics.rb
renalware-core-2.0.43 app/models/renalware/hd/update_rolling_patient_statistics.rb
renalware-core-2.0.42 app/models/renalware/hd/update_rolling_patient_statistics.rb
renalware-core-2.0.41 app/models/renalware/hd/update_rolling_patient_statistics.rb
renalware-core-2.0.40 app/models/renalware/hd/update_rolling_patient_statistics.rb
renalware-core-2.0.39 app/models/renalware/hd/update_rolling_patient_statistics.rb
renalware-core-2.0.38 app/models/renalware/hd/update_rolling_patient_statistics.rb
renalware-core-2.0.37 app/models/renalware/hd/update_rolling_patient_statistics.rb
renalware-core-2.0.36 app/models/renalware/hd/update_rolling_patient_statistics.rb
renalware-core-2.0.35 app/models/renalware/hd/update_rolling_patient_statistics.rb
renalware-core-2.0.34 app/models/renalware/hd/update_rolling_patient_statistics.rb
renalware-core-2.0.33 app/models/renalware/hd/update_rolling_patient_statistics.rb
renalware-core-2.0.32 app/models/renalware/hd/update_rolling_patient_statistics.rb
renalware-core-2.0.31 app/models/renalware/hd/update_rolling_patient_statistics.rb
renalware-core-2.0.30 app/models/renalware/hd/update_rolling_patient_statistics.rb
renalware-core-2.0.28 app/models/renalware/hd/update_rolling_patient_statistics.rb
renalware-core-2.0.27 app/models/renalware/hd/update_rolling_patient_statistics.rb
renalware-core-2.0.26 app/models/renalware/hd/update_rolling_patient_statistics.rb