Sha256: 19a62594645773ca171c27c9523c5675402065e98a089a06038153343776fbb0

Contents?: true

Size: 1006 Bytes

Versions: 9

Compression:

Stored size: 1006 Bytes

Contents

# frozen_string_literal: true

require_dependency "renalware/ukrdc"

module Renalware
  module UKRDC
    class TransmissionLog < ApplicationRecord
      validates :sent_at, presence: true
      validates :status, presence: true
      belongs_to :patient, class_name: "Renalware::Patient"
      enum status: { undefined: 0, error: 1, unsent_no_change_since_last_send: 2, sent: 3 }
      scope :ordered, -> { order(sent_at: :asc) }

      def self.with_logging(patient, request_uuid)
        log = new(patient: patient, sent_at: Time.zone.now, request_uuid: request_uuid)
        yield log if block_given?
        log.save!
      rescue StandardError => e
        log.error << formatted_exception(e)
        log.status = :error
        log.save!
        raise e
      end

      def self.formatted_exception(error)
        [
          "#{error.backtrace.first}: #{error.message} (#{error.class})",
          error.backtrace.drop(1).map { |line| "\t#{line}" }
        ].join("\n")
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
renalware-core-2.0.111 app/models/renalware/ukrdc/transmission_log.rb
renalware-core-2.0.110 app/models/renalware/ukrdc/transmission_log.rb
renalware-core-2.0.109 app/models/renalware/ukrdc/transmission_log.rb
renalware-core-2.0.108 app/models/renalware/ukrdc/transmission_log.rb
renalware-core-2.0.106 app/models/renalware/ukrdc/transmission_log.rb
renalware-core-2.0.105 app/models/renalware/ukrdc/transmission_log.rb
renalware-core-2.0.104 app/models/renalware/ukrdc/transmission_log.rb
renalware-core-2.0.103 app/models/renalware/ukrdc/transmission_log.rb
renalware-core-2.0.102 app/models/renalware/ukrdc/transmission_log.rb