Sha256: a289ed5fefb2b2337c1c0036bc235418e801b4d9b26c3befe1388c07cbaabb4e

Contents?: true

Size: 1.14 KB

Versions: 139

Compression:

Stored size: 1.14 KB

Contents

# frozen_string_literal: true

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

139 entries across 139 versions & 1 rubygems

Version Path
renalware-core-2.0.51 app/models/renalware/clinical/create_allergy.rb
renalware-core-2.0.50 app/models/renalware/clinical/create_allergy.rb
renalware-core-2.0.48 app/models/renalware/clinical/create_allergy.rb
renalware-core-2.0.47 app/models/renalware/clinical/create_allergy.rb
renalware-core-2.0.46 app/models/renalware/clinical/create_allergy.rb
renalware-core-2.0.45 app/models/renalware/clinical/create_allergy.rb
renalware-core-2.0.44 app/models/renalware/clinical/create_allergy.rb
renalware-core-2.0.43 app/models/renalware/clinical/create_allergy.rb
renalware-core-2.0.42 app/models/renalware/clinical/create_allergy.rb
renalware-core-2.0.41 app/models/renalware/clinical/create_allergy.rb
renalware-core-2.0.40 app/models/renalware/clinical/create_allergy.rb
renalware-core-2.0.39 app/models/renalware/clinical/create_allergy.rb
renalware-core-2.0.38 app/models/renalware/clinical/create_allergy.rb
renalware-core-2.0.37 app/models/renalware/clinical/create_allergy.rb
renalware-core-2.0.36 app/models/renalware/clinical/create_allergy.rb
renalware-core-2.0.35 app/models/renalware/clinical/create_allergy.rb
renalware-core-2.0.34 app/models/renalware/clinical/create_allergy.rb
renalware-core-2.0.33 app/models/renalware/clinical/create_allergy.rb
renalware-core-2.0.32 app/models/renalware/clinical/create_allergy.rb
renalware-core-2.0.31 app/models/renalware/clinical/create_allergy.rb