Sha256: a159789eb5dc6efcf6d9c059bd72c7e8d60bce304052eea80499352483382122

Contents?: true

Size: 1.08 KB

Versions: 10

Compression:

Stored size: 1.08 KB

Contents

# frozen_string_literal: true

require_dependency "renalware/clinical"
require "success"
require "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)
          ::Success.new(allergy)
        else
          ::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

10 entries across 10 versions & 1 rubygems

Version Path
renalware-core-2.1.1 app/models/renalware/clinical/create_allergy.rb
renalware-core-2.1.0 app/models/renalware/clinical/create_allergy.rb
renalware-core-2.0.167 app/models/renalware/clinical/create_allergy.rb
renalware-core-2.0.166 app/models/renalware/clinical/create_allergy.rb
renalware-core-2.0.165 app/models/renalware/clinical/create_allergy.rb
renalware-core-2.0.164 app/models/renalware/clinical/create_allergy.rb
renalware-core-2.0.163 app/models/renalware/clinical/create_allergy.rb
renalware-core-2.0.162 app/models/renalware/clinical/create_allergy.rb
renalware-core-2.0.161 app/models/renalware/clinical/create_allergy.rb
renalware-core-2.0.160 app/models/renalware/clinical/create_allergy.rb