Sha256: e373b94d62a670d13774524af2f143d5ccd5fc931e5cd8d567c5da938b6a8394

Contents?: true

Size: 1.72 KB

Versions: 27

Compression:

Stored size: 1.72 KB

Contents

# frozen_string_literal: true

require "renalware/letters"
require "attr_extras"

module Renalware
  module Letters
    module Delivery
      # This is a utility class that a host app might want to use to send an approved letter
      # to the patient's practice.
      class EmailLetterToPractice
        pattr_initialize [:letter!]
        delegate :email_letter_to_practice?, :gp_recipient, to: :policy

        def self.call(letter)
          new(letter: letter).call
        end

        # Returns
        #   true: we have sent an email to the patient's practice
        #   false: the patient does not have a practice or the practice has no email address
        def call
          return false unless email_letter_to_practice?

          email_letter_to_the_patients_practice
          true
        end

        private

        # Note we are already in a tx here from the letterlistener, so for safety we call
        # call #deliver_later and not #deliver; its possible we could get a
        # LetterIsNotApprovedOrCompletedError error in the mailer we and we don't want that to
        # roll back the txn. We could possibly use an async Listener and remove the
        # deliver_later here.
        def email_letter_to_the_patients_practice
          Letter.transaction do
            PracticeMailer.patient_letter(
              letter: letter,
              to: practice_email_address
            ).deliver_later # ! see comment

            # Flag as sent
            gp_recipient.update(emailed_at: Time.zone.now)
          end
        end

        def practice_email_address
          PracticeEmail.new(letter).address
        end

        def policy
          @policy ||= DeliveryPolicy.new(letter)
        end
      end
    end
  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

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