Sha256: 7e1e1cfefdeb317a6db597d35715092481ab93ad669c4872dd9ae335324e9554

Contents?: true

Size: 1.99 KB

Versions: 19

Compression:

Stored size: 1.99 KB

Contents

require "renalware/letters/part"
require "ostruct"

module Renalware
  module Letters
    class Part::Prescriptions < DumbDelegator
      include ::PresenterHelper

      def initialize(patient, _letter, _event = Event::Unknown.new)
        @patient = patient
        super(prescriptions)
      end

      def to_partial_path
        "renalware/letters/parts/prescriptions"
      end

      private

      def prescriptions
        presenter_klass = ::Renalware::Medications::PrescriptionPresenter
        ::OpenStruct.new(
          current: present(current_prescriptions, presenter_klass),
          recently_changed: present(recently_changed_current_prescriptions, presenter_klass),
          recently_stopped: present(recently_stopped_prescriptions, presenter_klass)
        )
      end

      def current_prescriptions
        @current_prescriptions ||= patient_prescriptions.current
      end

      # Prescriptions created or with dosage changed in the last 14 days.
      # Because we terminated a prescription if the dosage changes, and create a new one,
      # we just need to search for prescriptions created in the last 14 days.
      def recently_changed_current_prescriptions
        @recently_changed_prescriptions ||= begin
          current_prescriptions.prescribed_between(from: 14.days.ago, to: ::Time.zone.now)
        end
      end

      # Find prescriptions terminated within 14 days
      def recently_stopped_prescriptions
        @recently_stopped_prescriptions ||= begin
          patient_prescriptions
            .terminated
            .terminated_between(from: 14.days.ago, to: ::Time.zone.now)
            .where.not(drug_id: current_prescriptions.map(&:drug_id))
        end
      end

      def patient_prescriptions
        @patient_prescriptions ||= begin
          @patient.prescriptions
                   .with_created_by
                   .with_medication_route
                   .with_drugs
                   .with_termination
                   .ordered
        end
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
renalware-core-2.0.8 app/models/renalware/letters/part/prescriptions.rb
renalware-core-2.0.7 app/models/renalware/letters/part/prescriptions.rb
renalware-core-2.0.5 app/models/renalware/letters/part/prescriptions.rb
renalware-core-2.0.4 app/models/renalware/letters/part/prescriptions.rb
renalware-core-2.0.3 app/models/renalware/letters/part/prescriptions.rb
renalware-core-2.0.2 app/models/renalware/letters/part/prescriptions.rb
renalware-core-2.0.1 app/models/renalware/letters/part/prescriptions.rb
renalware-core-2.0.0 app/models/renalware/letters/part/prescriptions.rb
renalware-core-2.0.0.pre.rc13 app/models/renalware/letters/part/prescriptions.rb
renalware-core-2.0.0.pre.rc11 app/models/renalware/letters/part/prescriptions.rb
renalware-core-2.0.0.pre.rc10 app/models/renalware/letters/part/prescriptions.rb
renalware-core-2.0.0.pre.rc9 app/models/renalware/letters/part/prescriptions.rb
renalware-core-2.0.0.pre.rc8 app/models/renalware/letters/part/prescriptions.rb
renalware-core-2.0.0.pre.rc7 app/models/renalware/letters/part/prescriptions.rb
renalware-core-2.0.0.pre.rc6 app/models/renalware/letters/part/prescriptions.rb
renalware-core-2.0.0.pre.rc5 app/models/renalware/letters/part/prescriptions.rb
renalware-core-2.0.0.pre.rc4 app/models/renalware/letters/part/prescriptions.rb
renalware-core-2.0.0.pre.rc3 app/models/renalware/letters/part/prescriptions.rb
renalware-core-2.0.0.pre.rc1 app/models/renalware/letters/part/prescriptions.rb