Sha256: 2174616a736d046973be46ddaf7ad6867e2b0de01e8673ec6961b99cb11af382

Contents?: true

Size: 1.37 KB

Versions: 7

Compression:

Stored size: 1.37 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

        def email_letter_to_the_patients_practice
          Letter.transaction do
            PracticeMailer.patient_letter(
              letter: letter,
              to: practice_email_address,
              recipient: nil # TODO
            ).deliver_later

            # 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

7 entries across 7 versions & 1 rubygems

Version Path
renalware-core-2.0.17 app/models/renalware/letters/delivery/email_letter_to_practice.rb
renalware-core-2.0.16 app/models/renalware/letters/delivery/email_letter_to_practice.rb
renalware-core-2.0.15 app/models/renalware/letters/delivery/email_letter_to_practice.rb
renalware-core-2.0.14 app/models/renalware/letters/delivery/email_letter_to_practice.rb
renalware-core-2.0.13 app/models/renalware/letters/delivery/email_letter_to_practice.rb
renalware-core-2.0.12 app/models/renalware/letters/delivery/email_letter_to_practice.rb
renalware-core-2.0.11 app/models/renalware/letters/delivery/email_letter_to_practice.rb