Sha256: fc8c3d607b78d993afa7209cf6d96662f743d699716489aac4dcbb2b8c60576e

Contents?: true

Size: 1.75 KB

Versions: 20

Compression:

Stored size: 1.75 KB

Contents

# frozen_string_literal: true

require "month_period"

# This job when executed will store a snapshot of last month's HD session statistics
# for each HD patient.
module Renalware
  module HD
    class GenerateMonthlyStatistics < ApplicationJob
      attr_reader :period

      def initialize(month: nil, year: nil)
        validate_args(month: month, year: year)

        @period = Renalware::MonthPeriod.new(
          month: (month || default_month).to_i,
          year:  (year || default_year).to_i
        )
      end

      # :reek:UtilityFunction
      def call
        patients_with_a_closed_hd_session_in_period.each do |patient|
          GenerateMonthlyStatisticsForPatientJob.perform_later(
            patient: patient,
            month: period.month,
            year: period.year
          )
        end
        Rails.logger.info(
          "Enqueued GenerateMonthlyStatisticsForPatientJob jobs for "\
          "#{patients_with_a_closed_hd_session_in_period.length} patients"
        )
      end

      private

      def validate_args(month:, year:)
        if (month.present? && year.blank?) || (month.blank? && year.present?)
          raise(ArgumentError, "Must supply both month and year if supplying one or the other")
        end
      end

      def patients_with_a_closed_hd_session_in_period
        @patients_with_a_closed_hd_session_in_period ||= begin
          Sessions::AuditablePatientsInPeriodQuery.new(period: period).call
        end
      end

      def default_month
        date_falling_in_the_previous_month.month
      end

      def default_year
        date_falling_in_the_previous_month.year
      end

      def date_falling_in_the_previous_month
        @date_falling_in_the_previous_month ||= Time.zone.today - 1.month
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
renalware-core-2.0.43 app/jobs/renalware/hd/generate_monthly_statistics.rb
renalware-core-2.0.42 app/jobs/renalware/hd/generate_monthly_statistics.rb
renalware-core-2.0.41 app/jobs/renalware/hd/generate_monthly_statistics.rb
renalware-core-2.0.40 app/jobs/renalware/hd/generate_monthly_statistics.rb
renalware-core-2.0.39 app/jobs/renalware/hd/generate_monthly_statistics.rb
renalware-core-2.0.38 app/jobs/renalware/hd/generate_monthly_statistics.rb
renalware-core-2.0.37 app/jobs/renalware/hd/generate_monthly_statistics.rb
renalware-core-2.0.36 app/jobs/renalware/hd/generate_monthly_statistics.rb
renalware-core-2.0.35 app/jobs/renalware/hd/generate_monthly_statistics.rb
renalware-core-2.0.34 app/jobs/renalware/hd/generate_monthly_statistics.rb
renalware-core-2.0.33 app/jobs/renalware/hd/generate_monthly_statistics.rb
renalware-core-2.0.32 app/jobs/renalware/hd/generate_monthly_statistics.rb
renalware-core-2.0.31 app/jobs/renalware/hd/generate_monthly_statistics.rb
renalware-core-2.0.30 app/jobs/renalware/hd/generate_monthly_statistics.rb
renalware-core-2.0.28 app/jobs/renalware/hd/generate_monthly_statistics.rb
renalware-core-2.0.27 app/jobs/renalware/hd/generate_monthly_statistics.rb
renalware-core-2.0.26 app/jobs/renalware/hd/generate_monthly_statistics.rb
renalware-core-2.0.25 app/jobs/renalware/hd/generate_monthly_statistics.rb
renalware-core-2.0.24 app/jobs/renalware/hd/generate_monthly_statistics.rb
renalware-core-2.0.23 app/jobs/renalware/hd/generate_monthly_statistics.rb