Sha256: 9828579f5908bd0908bde3dcb7ee0cdab3d9f1c66fea636efebf2aa67f100bb2

Contents?: true

Size: 1.11 KB

Versions: 20

Compression:

Stored size: 1.11 KB

Contents

require_dependency "renalware/clinical"
require_dependency "renalware/success"
require_dependency "renalware/failure"

module Renalware
  module Clinical
    class CreateAllergy
      def initialize(patient, user)
        @patient = patient
        @user = user
      end

      def call(params)
        allergy = build_allergy(params)
        yield allergy if block_given?
        if allergy.valid?
          save_allergy(allergy)
          ::Renalware::Success.new(allergy)
        else
          ::Renalware::Failure.new(allergy)
        end
      end

      private

      attr_reader :patient, :user

      def build_allergy(params)
        params[:by] = user
        params[:recorded_at] ||= Time.zone.now
        patient.allergies.build(params)
      end

      def save_allergy(allergy)
        Allergy.transaction do
          allergy.save!
          unless patient.allergy_status&.known_allergies?
            patient.allergy_status = :known_allergies
            patient.allergy_status_updated_at = Time.zone.now
            patient.by = user
            patient.save!
          end
        end
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

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