Sha256: dfef999e664b295fad10440beb77fc17d0b0555cf54f9d326ce297d5888803c2

Contents?: true

Size: 1.32 KB

Versions: 6

Compression:

Stored size: 1.32 KB

Contents

# frozen_string_literal: true

require_dependency "renalware/letters"

# Represents an Electronic CC - a reference to a letter sent to a recipient (another user on the
# system) as a 'CC'. We use the concept of a 'receipt' which, while mainly being a cross table
# between letter and recipient user, is also a well-named place to store related information
# for example whether the message has been read (e.g. the letter viewed).
module Renalware
  module Letters
    class ElectronicReceipt < ApplicationRecord
      belongs_to :letter, touch: true
      belongs_to :recipient, class_name: "Renalware::User"
      validates :letter, presence: true
      validates :recipient_id, presence: true

      # Merge scope here to make sure we only get approved letters
      scope :sent_by, lambda{ |user_id|
        joins(:letter)
          .merge(Letter::Approved.all)
          .where(letter_letters: { author_id: user_id })
      }
      scope :unread, -> { where(read_at: nil).joins(:letter).merge(Letter::Approved.all) }
      scope :read, -> { where.not(read_at: nil).joins(:letter).merge(Letter::Approved.all) }
      scope :for_recipient, ->(user_id) { where(recipient_id: user_id) }
      scope :ordered, -> { order(created_at: :desc) }

      def read?
        read_at.present?
      end

      def unread?
        !read?
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
renalware-core-2.0.15 app/models/renalware/letters/electronic_receipt.rb
renalware-core-2.0.14 app/models/renalware/letters/electronic_receipt.rb
renalware-core-2.0.13 app/models/renalware/letters/electronic_receipt.rb
renalware-core-2.0.12 app/models/renalware/letters/electronic_receipt.rb
renalware-core-2.0.11 app/models/renalware/letters/electronic_receipt.rb
renalware-core-2.0.9 app/models/renalware/letters/electronic_receipt.rb