Sha256: 0e6a7360b5d5c9cb3f6aaabc868f751e3bbd37271ebce28730025ac76a235999

Contents?: true

Size: 1.21 KB

Versions: 8

Compression:

Stored size: 1.21 KB

Contents

module Renalware
  module HD
    class GenerateMonthlyStatisticsForPatient

      def initialize(patient:, period:)
        @patient = patient
        @period = period
      end

      def call
        options = { patient: patient, period: period }
        sessions = Sessions::AuditablePatientSessionsInPeriodQuery.new(**options).call
        create_patient_statistics(sessions)
      end

      private

      attr_reader :period, :patient

      def create_patient_statistics(sessions)
        return unless sessions.any?
        stats = build_patient_statistics
        stats.hospital_unit = most_recently_used_hospital_unit(sessions)
        stats.assign_attributes(auditable_sessions(sessions).to_h)
        stats.session_count = sessions.length
        stats.save!
      end

      def build_patient_statistics
        PatientStatistics.where(
          patient: patient,
          month: period.month,
          year: period.year,
          rolling: nil
        ).first_or_initialize
      end

      def auditable_sessions(sessions)
        Sessions::AuditableSessionCollection.new(sessions)
      end

      def most_recently_used_hospital_unit(sessions)
        sessions.last.try!(:hospital_unit)
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
renalware-core-2.0.0.pre.beta11 app/models/renalware/hd/generate_monthly_statistics_for_patient.rb
renalware-core-2.0.0.pre.beta10 app/models/renalware/hd/generate_monthly_statistics_for_patient.rb
renalware-core-2.0.0.pre.beta9 app/models/renalware/hd/generate_monthly_statistics_for_patient.rb
renalware-core-2.0.0.pre.beta8 app/models/renalware/hd/generate_monthly_statistics_for_patient.rb
renalware-core-2.0.0.pre.beta7 app/models/renalware/hd/generate_monthly_statistics_for_patient.rb
renalware-core-2.0.0.pre.beta6 app/models/renalware/hd/generate_monthly_statistics_for_patient.rb
renalware-core-2.0.0.pre.beta5 app/models/renalware/hd/generate_monthly_statistics_for_patient.rb
renalware-core-2.0.0.pre.beta4 app/models/renalware/hd/generate_monthly_statistics_for_patient.rb