Sha256: da0de8c03ffce42361aafe0af644e16f0d7e95c1d5cc862bc252d2dda8931d38

Contents?: true

Size: 1.2 KB

Versions: 58

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

58 entries across 58 versions & 1 rubygems

Version Path
renalware-core-2.0.109 app/models/renalware/hd/update_rolling_patient_statistics.rb
renalware-core-2.0.108 app/models/renalware/hd/update_rolling_patient_statistics.rb
renalware-core-2.0.106 app/models/renalware/hd/update_rolling_patient_statistics.rb
renalware-core-2.0.105 app/models/renalware/hd/update_rolling_patient_statistics.rb
renalware-core-2.0.104 app/models/renalware/hd/update_rolling_patient_statistics.rb
renalware-core-2.0.103 app/models/renalware/hd/update_rolling_patient_statistics.rb
renalware-core-2.0.102 app/models/renalware/hd/update_rolling_patient_statistics.rb
renalware-core-2.0.101 app/models/renalware/hd/update_rolling_patient_statistics.rb
renalware-core-2.0.100 app/models/renalware/hd/update_rolling_patient_statistics.rb
renalware-core-2.0.99 app/models/renalware/hd/update_rolling_patient_statistics.rb
renalware-core-2.0.98 app/models/renalware/hd/update_rolling_patient_statistics.rb
renalware-core-2.0.97 app/models/renalware/hd/update_rolling_patient_statistics.rb
renalware-core-2.0.96 app/models/renalware/hd/update_rolling_patient_statistics.rb
renalware-core-2.0.95 app/models/renalware/hd/update_rolling_patient_statistics.rb
renalware-core-2.0.94 app/models/renalware/hd/update_rolling_patient_statistics.rb
renalware-core-2.0.93 app/models/renalware/hd/update_rolling_patient_statistics.rb
renalware-core-2.0.92 app/models/renalware/hd/update_rolling_patient_statistics.rb
renalware-core-2.0.91 app/models/renalware/hd/update_rolling_patient_statistics.rb
renalware-core-2.0.90 app/models/renalware/hd/update_rolling_patient_statistics.rb
renalware-core-2.0.89 app/models/renalware/hd/update_rolling_patient_statistics.rb