Sha256: 3915dc6f7a4b5b2aaac19a4cdb03641c1c2faed7bcdf01c87306f5c3ff94a662

Contents?: true

Size: 1.66 KB

Versions: 104

Compression:

Stored size: 1.66 KB

Contents

# frozen_string_literal: true

require_dependency "renalware/reporting"

module Renalware
  module Reporting
    class ReportMailer < ApplicationMailer
      def daily_summary(to: "dev@airslie.com")
        mail(
          to: Array(to),
          subject: "Renalware daily summary #{I18n.l(Time.zone.today)}"
        ) do |format|
          format.text { render(locals: { view_data: view_data }) }
          format.html { render(locals: { view_data: view_data }) }
        end
      end

      private

      # Piggybacks on the reporting mechanism used elswhere for generating reports from json.
      #
      # returns e.g.
      # {
      #   "reporting_daily_letters" => {
      #     "runat" => "2018-08-31T16:51:58.939876+00:00",
      #      "data" => [
      #         [0] {
      #             "letters_created_today" => 0,
      #             "letters_printed_today" => 0
      #         }
      #     ]
      #   },
      #   "another_view" :...
      # }
      def view_data
        reporting_daily_views.each_with_object({}) do |view_name, hash|
          hash[view_name] = JSON.parse(
            Reporting::FetchAuditJson.call(view_name)
          ).with_indifferent_access
        end
      end

      # Returns the names of views that start with 'reporting_daily_', regardless of namespace
      # e.g.
      # ["reporting_daily_pathology", "reporting_daily_letters"]
      def reporting_daily_views
        result = ActiveRecord::Base.connection.execute(<<-SQL)
          SELECT relname FROM pg_class
          WHERE relkind = 'v'
          AND relname like 'reporting_daily_%'
          order by relname desc;
        SQL
        result.values.flatten
      end
    end
  end
end

Version data entries

104 entries across 104 versions & 1 rubygems

Version Path
renalware-core-2.0.62 app/mailers/renalware/reporting/report_mailer.rb
renalware-core-2.0.61 app/mailers/renalware/reporting/report_mailer.rb
renalware-core-2.0.60 app/mailers/renalware/reporting/report_mailer.rb
renalware-core-2.0.58 app/mailers/renalware/reporting/report_mailer.rb