Skip to content

Commit

Permalink
initial spike
Browse files Browse the repository at this point in the history
  • Loading branch information
alkesh committed Oct 7, 2024
1 parent 94d94a1 commit 8f49feb
Show file tree
Hide file tree
Showing 11 changed files with 177 additions and 2 deletions.
18 changes: 18 additions & 0 deletions app/controllers/admin/reports_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module Admin
class ReportsController < BaseAdminController
before_action :ensure_service_operator

def index
@reports = Report.all
end

def show
respond_to do |format|
format.csv {
report = Report.find(params[:id])
send_data report.csv, filename: "#{report.name.parameterize(separator: "_")}_#{report.created_at.iso8601}.csv"
}
end
end
end
end
9 changes: 9 additions & 0 deletions app/jobs/reports_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class ReportsJob < CronJob
self.cron_expression = "0 6 * * 2#2" # second Tuesday of the month

def perform
Rails.logger.info "Generating Ops reports"

Report.create!(name: Reports::DuplicateClaims::NAME, csv: Reports::DuplicateClaims.new.to_csv)
end
end
2 changes: 2 additions & 0 deletions app/models/report.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Report < ApplicationRecord
end
47 changes: 47 additions & 0 deletions app/models/reports/duplicate_claims.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require "csv"
require "excel_utils"

module Reports
class DuplicateClaims
include Admin::ClaimsHelper

NAME = "Duplicate Approved Claims"
HEADERS = [
"Claim reference",
"Teacher reference number",
"Full name",
"Policy name",
"Claim amount",
"Claim status",
"Decision date",
"Decision agent"
].freeze

def initialize
@claims = Claim.approved.select { |claim| Claim::MatchingAttributeFinder.new(claim).matching_claims.any? }
end

def to_csv
CSV.generate(write_headers: true, headers: HEADERS) do |csv|
@claims.each do |claim|
csv << row(
claim.reference,
claim.eligibility.teacher_reference_number,
claim.full_name,
claim.policy,
claim.award_amount,
status(claim),
claim.latest_decision.created_at,
claim.latest_decision.created_by.full_name
)
end
end
end

private

def row(*entries)
entries.map { |entry| ExcelUtils.escape_formulas(entry) }
end
end
end
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require "csv"
require "excel_utils"

module Reports
class FailedQualification
include Admin::ClaimsHelper

NAME = "Duplicate Approved Claims"
HEADERS = [
"Claim reference",
"Teacher reference number",
"Full name",
"Policy name",
"Claim amount",
"Claim status",
"Decision date",
"Decision agent"
].freeze

def initialize
@claims = Claim.approved.select { |claim| Claim::MatchingAttributeFinder.new(claim).matching_claims.any? }
end

def to_csv
CSV.generate(write_headers: true, headers: HEADERS) do |csv|
@claims.each do |claim|
csv << row(
claim.reference,
claim.eligibility.teacher_reference_number,
claim.full_name,
claim.policy,
claim.award_amount,
status(claim),
claim.latest_decision.created_at,
claim.latest_decision.created_by.full_name
)
end
end
end

private

def row(*entries)
entries.map { |entry| ExcelUtils.escape_formulas(entry) }
end
end
end

30 changes: 30 additions & 0 deletions app/views/admin/reports/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<% content_for(:page_title) { page_title("Download Reports") } %>

<% content_for :back_link do %>
<%= govuk_back_link href: admin_root_path %>
<% end %>

<div class="govuk-grid-row">
<div class="govuk-grid-column-full">
<h1 class="govuk-heading-xl">
Reports
</h1>

<table class="govuk-table">
<thead class="govuk-table__head">
<tr class="govuk-table__row">
<th scope="col" class="govuk-table__header">Name</th>
<th scope="col" class="govuk-table__header">Created At</th>
</tr>
</thead>
<tbody class="govuk-table__body">
<% @reports.each do |report| %>
<tr class="govuk-table__row">
<td class="govuk-table__cell"><%= govuk_link_to report.name, admin_report_path(report, format: :csv) %></td>
<td class="govuk-table__cell"><%= l(report.created_at) %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
</div>
3 changes: 3 additions & 0 deletions app/views/application/_admin_menu.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
<li class="govuk-header__navigation-item">
<%= link_to "Manage services", admin_journey_configurations_path, class: "govuk-header__link" %>
</li>
<li class="govuk-header__navigation-item">
<%= link_to "Reports", admin_reports_path, class: "govuk-header__link" %>
</li>
<% end %>
<li class="govuk-header__navigation-item">
<%= link_to "Sign out", admin_sign_out_path, method: :delete, class: "govuk-header__link" %>
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ def matches?(request)
resources :levelling_up_premium_payments_awards, only: [:index, :create]
resource :eligible_ey_providers, only: [:create, :show], path: "eligible-early-years-providers"
resource :eligible_fe_providers, only: [:create, :show], path: "eligible-further-education-providers"
resources :reports, only: [:index, :show]

get "refresh-session", to: "sessions#refresh", as: :refresh_session

Expand Down
10 changes: 10 additions & 0 deletions db/migrate/20241007141638_create_reports.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateReports < ActiveRecord::Migration[7.0]
def change
create_table :reports, id: :uuid do |t|
t.string :name
t.text :csv

t.timestamps
end
end
end
11 changes: 9 additions & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.0].define(version: 2024_09_24_113642) do
ActiveRecord::Schema[7.0].define(version: 2024_10_07_141638) do
# These are extensions that must be enabled in order to support this database
enable_extension "citext"
enable_extension "pg_trgm"
Expand Down Expand Up @@ -111,7 +111,7 @@
t.text "onelogin_idv_first_name"
t.text "onelogin_idv_last_name"
t.date "onelogin_idv_date_of_birth"
t.datetime "started_at", precision: nil, null: false
t.datetime "started_at", precision: nil
t.index ["academic_year"], name: "index_claims_on_academic_year"
t.index ["created_at"], name: "index_claims_on_created_at"
t.index ["eligibility_type", "eligibility_id"], name: "index_claims_on_eligibility_type_and_eligibility_id"
Expand Down Expand Up @@ -419,6 +419,13 @@
t.string "itt_subject"
end

create_table "reports", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
t.string "name"
t.text "csv"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "school_workforce_censuses", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
t.string "teacher_reference_number"
t.datetime "created_at", null: false
Expand Down

0 comments on commit 8f49feb

Please sign in to comment.