Sha256: 9e2d93d60163a655ade0a0779d4c2c5c7faa46a250f66bb392ae71955f78b617

Contents?: true

Size: 1.23 KB

Versions: 9

Compression:

Stored size: 1.23 KB

Contents

# frozen_string_literal: true

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

9 entries across 9 versions & 1 rubygems

Version Path
renalware-core-2.0.18 app/models/renalware/hd/generate_monthly_statistics_for_patient.rb
renalware-core-2.0.17 app/models/renalware/hd/generate_monthly_statistics_for_patient.rb
renalware-core-2.0.16 app/models/renalware/hd/generate_monthly_statistics_for_patient.rb
renalware-core-2.0.15 app/models/renalware/hd/generate_monthly_statistics_for_patient.rb
renalware-core-2.0.14 app/models/renalware/hd/generate_monthly_statistics_for_patient.rb
renalware-core-2.0.13 app/models/renalware/hd/generate_monthly_statistics_for_patient.rb
renalware-core-2.0.12 app/models/renalware/hd/generate_monthly_statistics_for_patient.rb
renalware-core-2.0.11 app/models/renalware/hd/generate_monthly_statistics_for_patient.rb
renalware-core-2.0.9 app/models/renalware/hd/generate_monthly_statistics_for_patient.rb