Sha256: 8d2100f8793eaa2cb40a6e9be87de55ccda889ae5455422344e8bebbd370dfed

Contents?: true

Size: 1.43 KB

Versions: 47

Compression:

Stored size: 1.43 KB

Contents

# frozen_string_literal: true

require "attr_extras"

module Renalware
  module HD
    class ProfilesInDateRangeQuery
      pattr_initialize [:patient!, :from!, :to!]

      # We are looking for HD Profiles within a certain period.
      # Some HD profiles have no prescribed_on populated (and it is not always acurate - if the
      # profile is edited and a copy made, they sometimes to do not change the prescribed_on date)
      # so for the start date of the profile we always use created_at and ignore prescribed_on,
      # even if it is present.
      def call
        # p "  from #{from}"
        # p "  to #{to}"

        # If to is nil it is because modality we are targetting is a current one.
        # We need a date for the from..to range to work so use a far future one.
        @to ||= Date.parse("3000-01-01")

        # Be sure not to reselect profiles we have already used in a previous treatment otherwise
        # we will have duplicate Treatments with odd state/end date ordering
        used_profiles_ids = UKRDC::Treatment
                             .where(patient: patient)
                             .where("hd_profile_id is not null")
                             .pluck(:hd_profile_id)
        HD::Profile
          .with_deactivated
          .order(created_at: :asc, deactivated_at: :desc)
          .where(patient_id: patient.id, created_at: from..to)
          .where.not(id: used_profiles_ids)
      end
    end
  end
end

Version data entries

47 entries across 47 versions & 1 rubygems

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