Sha256: 79fe7f8c4c880cfdd0292bfe3a3d60ba9a6b035b6fa2b58833d5c7d774830e42

Contents?: true

Size: 1.89 KB

Versions: 110

Compression:

Stored size: 1.89 KB

Contents

# frozen_string_literal: true

require_dependency "renalware/letters"
require "collection_presenter"

module Renalware
  module Letters
    class ElectronicReceiptsController < BaseController
      include Renalware::Concerns::Pageable

      # PATCH
      def mark_as_read
        authorize receipt
        receipt.update(read_at: Time.zone.now)
        render locals: {
          receipt: ElectronicReceiptPresenter.new(receipt),
          layout: false
        }
      end

      # GET all unread electronic CCs
      def unread
        render_receipts(received_receipts.unread)
      end

      # GET all read electronic CCs
      def read
        render_receipts(received_receipts.read)
      end

      # GET all electronic CCs sent by the current user
      def sent
        render_receipts(sent_receipts)
      end

      private

      def render_receipts(receipts)
        patient_filter = Patients::SearchFilter.new(search_term, request)
        receipts = receipts.joins(letter: [:patient])
        receipts = patient_filter.call(receipts).ordered.page(page).per(per_page)
        authorize receipts

        render locals: {
          receipts: present_receipts(receipts),
          search_form: patient_filter.search_form
        }
      end

      def build_search_form_object
        Patients::SearchForm.new(
          term: search_term,
          url: request.path
        )
      end

      def search_term
        params.fetch(:patient_search, {}).fetch(:term, nil)
      end

      def receipt
        ElectronicReceipt.find_by!(letter_id: params[:letter_id], id: params[:id])
      end

      def received_receipts
        ElectronicReceipt.where(recipient_id: current_user.id)
      end

      def sent_receipts
        ElectronicReceipt.sent_by(current_user.id)
      end

      def present_receipts(receipts)
        CollectionPresenter.new(receipts, ElectronicReceiptPresenter)
      end
    end
  end
end

Version data entries

110 entries across 110 versions & 1 rubygems

Version Path
renalware-core-2.1.1 app/controllers/renalware/letters/electronic_receipts_controller.rb
renalware-core-2.1.0 app/controllers/renalware/letters/electronic_receipts_controller.rb
renalware-core-2.0.167 app/controllers/renalware/letters/electronic_receipts_controller.rb
renalware-core-2.0.166 app/controllers/renalware/letters/electronic_receipts_controller.rb
renalware-core-2.0.165 app/controllers/renalware/letters/electronic_receipts_controller.rb
renalware-core-2.0.164 app/controllers/renalware/letters/electronic_receipts_controller.rb
renalware-core-2.0.163 app/controllers/renalware/letters/electronic_receipts_controller.rb
renalware-core-2.0.162 app/controllers/renalware/letters/electronic_receipts_controller.rb
renalware-core-2.0.161 app/controllers/renalware/letters/electronic_receipts_controller.rb
renalware-core-2.0.160 app/controllers/renalware/letters/electronic_receipts_controller.rb
renalware-core-2.0.159 app/controllers/renalware/letters/electronic_receipts_controller.rb
renalware-core-2.0.158 app/controllers/renalware/letters/electronic_receipts_controller.rb
renalware-core-2.0.157 app/controllers/renalware/letters/electronic_receipts_controller.rb
renalware-core-2.0.156 app/controllers/renalware/letters/electronic_receipts_controller.rb
renalware-core-2.0.155 app/controllers/renalware/letters/electronic_receipts_controller.rb
renalware-core-2.0.153 app/controllers/renalware/letters/electronic_receipts_controller.rb
renalware-core-2.0.152 app/controllers/renalware/letters/electronic_receipts_controller.rb
renalware-core-2.0.151 app/controllers/renalware/letters/electronic_receipts_controller.rb
renalware-core-2.0.149 app/controllers/renalware/letters/electronic_receipts_controller.rb
renalware-core-2.0.148 app/controllers/renalware/letters/electronic_receipts_controller.rb