Sha256: 5f7d2fb9d144609bffe13b8f2879d3b059e248acf041a0d9212e885ed27257b8

Contents?: true

Size: 1.2 KB

Versions: 20

Compression:

Stored size: 1.2 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&.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/generate_monthly_statistics_for_patient.rb
renalware-core-2.0.7 app/models/renalware/hd/generate_monthly_statistics_for_patient.rb
renalware-core-2.0.5 app/models/renalware/hd/generate_monthly_statistics_for_patient.rb
renalware-core-2.0.4 app/models/renalware/hd/generate_monthly_statistics_for_patient.rb
renalware-core-2.0.3 app/models/renalware/hd/generate_monthly_statistics_for_patient.rb
renalware-core-2.0.2 app/models/renalware/hd/generate_monthly_statistics_for_patient.rb
renalware-core-2.0.1 app/models/renalware/hd/generate_monthly_statistics_for_patient.rb
renalware-core-2.0.0 app/models/renalware/hd/generate_monthly_statistics_for_patient.rb
renalware-core-2.0.0.pre.rc13 app/models/renalware/hd/generate_monthly_statistics_for_patient.rb
renalware-core-2.0.0.pre.rc11 app/models/renalware/hd/generate_monthly_statistics_for_patient.rb
renalware-core-2.0.0.pre.rc10 app/models/renalware/hd/generate_monthly_statistics_for_patient.rb
renalware-core-2.0.0.pre.rc9 app/models/renalware/hd/generate_monthly_statistics_for_patient.rb
renalware-core-2.0.0.pre.rc8 app/models/renalware/hd/generate_monthly_statistics_for_patient.rb
renalware-core-2.0.0.pre.rc7 app/models/renalware/hd/generate_monthly_statistics_for_patient.rb
renalware-core-2.0.0.pre.rc6 app/models/renalware/hd/generate_monthly_statistics_for_patient.rb
renalware-core-2.0.0.pre.rc5 app/models/renalware/hd/generate_monthly_statistics_for_patient.rb
renalware-core-2.0.0.pre.rc4 app/models/renalware/hd/generate_monthly_statistics_for_patient.rb
renalware-core-2.0.0.pre.rc3 app/models/renalware/hd/generate_monthly_statistics_for_patient.rb
renalware-core-2.0.0.pre.rc1 app/models/renalware/hd/generate_monthly_statistics_for_patient.rb
renalware-core-2.0.0.pre.beta12 app/models/renalware/hd/generate_monthly_statistics_for_patient.rb