Sha256: 939f5e31024ca62618ca3713249cb1a9b2d0dfa5b0fa7c8d154772e03e6e99d3

Contents?: true

Size: 1.93 KB

Versions: 93

Compression:

Stored size: 1.93 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
    # TODO: not sure this needs to be a job - doesn't seem to be used as such
    # instead is invoked directly
    class GenerateMonthlyStatistics < ApplicationJob
      queue_as :hd_patient_statistics
      queue_with_priority 1
      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

93 entries across 93 versions & 1 rubygems

Version Path
renalware-core-2.0.159 app/jobs/renalware/hd/generate_monthly_statistics.rb
renalware-core-2.0.158 app/jobs/renalware/hd/generate_monthly_statistics.rb
renalware-core-2.0.157 app/jobs/renalware/hd/generate_monthly_statistics.rb
renalware-core-2.0.156 app/jobs/renalware/hd/generate_monthly_statistics.rb
renalware-core-2.0.155 app/jobs/renalware/hd/generate_monthly_statistics.rb
renalware-core-2.0.153 app/jobs/renalware/hd/generate_monthly_statistics.rb
renalware-core-2.0.152 app/jobs/renalware/hd/generate_monthly_statistics.rb
renalware-core-2.0.151 app/jobs/renalware/hd/generate_monthly_statistics.rb
renalware-core-2.0.149 app/jobs/renalware/hd/generate_monthly_statistics.rb
renalware-core-2.0.148 app/jobs/renalware/hd/generate_monthly_statistics.rb
renalware-core-2.0.147 app/jobs/renalware/hd/generate_monthly_statistics.rb
renalware-core-2.0.146 app/jobs/renalware/hd/generate_monthly_statistics.rb
renalware-core-2.0.145 app/jobs/renalware/hd/generate_monthly_statistics.rb
renalware-core-2.0.144 app/jobs/renalware/hd/generate_monthly_statistics.rb
renalware-core-2.0.143 app/jobs/renalware/hd/generate_monthly_statistics.rb
renalware-core-2.0.142 app/jobs/renalware/hd/generate_monthly_statistics.rb
renalware-core-2.0.141 app/jobs/renalware/hd/generate_monthly_statistics.rb
renalware-core-2.0.140 app/jobs/renalware/hd/generate_monthly_statistics.rb
renalware-core-2.0.139 app/jobs/renalware/hd/generate_monthly_statistics.rb
renalware-core-2.0.138 app/jobs/renalware/hd/generate_monthly_statistics.rb