Sha256: f6b762068d5ec339aa1fc1c5066151db3a9aeceb88c67008d49b206c756fb6cc

Contents?: true

Size: 1.5 KB

Versions: 23

Compression:

Stored size: 1.5 KB

Contents

require_dependency "renalware/transplants"
require_dependency "renalware/success"
require_dependency "renalware/failure"

# Service object responsible for creating a new DonorStage and terminated the previous one
# if found.
module Renalware
  module Transplants
    class CreateDonorStage
      def initialize(patient:, options: {})
        @patient = patient
        @options = options
      end

      def call
        DonorStage.transaction do
          terminate_current_stage if current_stage.present?
          stage = build_new_stage
          if stage.save
            ::Renalware::Success.new(stage)
          else
            ::Renalware::Failure.new(stage)
          end
        end
      end

      private

      attr_reader :patient, :options

      def current_stage
        @current_stage ||= DonorStage.for_patient(patient).current.first
      end

      def terminate_current_stage
        current_stage.terminated_on = started_on_for_new_stage
        current_stage.by = by
        current_stage.save!
        @current_stage = nil
      end

      def build_new_stage
        DonorStage.new(
          patient: patient,
          started_on: started_on_for_new_stage,
          stage_position_id: options.fetch(:stage_position_id),
          stage_status_id: options.fetch(:stage_status_id),
          notes: options.fetch(:notes),
          by: by
        )
      end

      def by
        options.fetch(:by)
      end

      def started_on_for_new_stage
        options.fetch(:started_on)
      end
    end
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

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