Skip to content

Commit

Permalink
Add "Send claims to ESFA" flow
Browse files Browse the repository at this point in the history
  • Loading branch information
Nitemaeric committed Dec 20, 2024
1 parent 0817e1b commit c746340
Show file tree
Hide file tree
Showing 31 changed files with 624 additions and 6 deletions.
1 change: 1 addition & 0 deletions .env.test
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ CLAIMS_HOST=claims.localhost
CLAIMS_HOSTS=claims.localhost
CLAIMS_DFE_SIGN_IN_CLIENT_ID=123
CLAIMS_DFE_SIGN_IN_SECRET=secret
CLAIMS_ESFA_EMAIL_ADDRESSES=esfa@example.com

PLACEMENTS_HOST=placements.localhost
PLACEMENTS_HOSTS=placements.localhost
Expand Down
29 changes: 29 additions & 0 deletions app/controllers/claims/payments/claims_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Claims::Payments::ClaimsController < Claims::ApplicationController
skip_before_action :authenticate_user!

before_action :skip_authorization
before_action :validate_token
before_action :set_payment

def download
send_data @payment.csv_file.download, filename: "payments-claims-#{Time.current.iso8601}.csv"
end

private

def validate_token
@payment_id = Rails.application.message_verifier(:payments).verify(token_param)
rescue ActiveSupport::MessageVerifier::InvalidSignature
render "error"
end

def set_payment
@payment = Claims::Payment.find(@payment_id)
rescue ActiveRecord::RecordNotFound
render "error"
end

def token_param
params.fetch(:token, nil)
end
end
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class Claims::Support::Claims::Payments::ClaimsController < Claims::Support::ApplicationController
append_pundit_namespace :claims, :payments

before_action :set_claim, only: %i[show]
before_action :authorize_claim

Expand All @@ -11,6 +13,6 @@ def set_claim
end

def authorize_claim
authorize [:payments, @claim || Claims::Claim]
authorize @claim || Claims::Claim
end
end
18 changes: 17 additions & 1 deletion app/controllers/claims/support/claims/payments_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class Claims::Support::Claims::PaymentsController < Claims::Support::ApplicationController
append_pundit_namespace :claims

before_action :authorize_claims

helper_method :filter_form
Expand All @@ -7,6 +9,20 @@ def index
@pagy, @claims = pagy(filtered_claims)
end

def new
@submitted_claims = Claims::Claim.submitted

unless policy(Claims::Payment).create?
render "new_not_permitted"
end
end

def create
Claims::Payment::CreateAndDeliver.call(current_user:)

redirect_to claims_support_claims_payments_path, flash: { success: true, heading: "Claims sent to ESFA" }
end

private

def filtered_claims
Expand Down Expand Up @@ -40,6 +56,6 @@ def filter_params
end

def authorize_claims
authorize filtered_claims
authorize [:payments, filtered_claims]
end
end
24 changes: 24 additions & 0 deletions app/mailers/claims/payment_mailer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Claims::PaymentMailer < Claims::ApplicationMailer
def payment_created_notification(payment)
@payment = payment

notify_email to: esfa_email_addresses,
from: support_email,
subject: t(".subject"),
body: t(
".body",
download_page_url: claims_payments_claims_url(token:),
support_email:,
)
end

private

def esfa_email_addresses
ENV.fetch("CLAIMS_ESFA_EMAIL_ADDRESSES").split(",")
end

def token
Rails.application.message_verifier(:payments).generate(@payment.id, expires_in: 7.days)
end
end
4 changes: 3 additions & 1 deletion app/models/claims/payment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,7 @@ class Claims::Payment < ApplicationRecord
belongs_to :sent_by, class_name: "Claims::SupportUser"

has_many :payment_claims, dependent: :destroy
has_many :claims, through: :payment_claims
has_many :claims, through: :payment_claims, class_name: "Claims::Claim"

has_one_attached :csv_file
end
9 changes: 9 additions & 0 deletions app/policies/claims/support/claims/payment_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Claims::Support::Claims::PaymentPolicy < Claims::ApplicationPolicy
def new?
true
end

def create?
Claims::Claim.submitted.any?
end
end
5 changes: 5 additions & 0 deletions app/policies/claims/support/claims/payments/claim_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Claims::Support::Claims::Payments::ClaimPolicy < Claims::ApplicationPolicy
def update?
false
end
end
2 changes: 0 additions & 2 deletions app/policies/claims/support/payments/claim_policy.rb

This file was deleted.

33 changes: 33 additions & 0 deletions app/services/claims/payment/create_and_deliver.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class Claims::Payment::CreateAndDeliver < ApplicationService
def initialize(current_user:)
@current_user = current_user
end

def call
return if submitted_claims.none?

ActiveRecord::Base.transaction do |transaction|
csv_file = Claims::Payments::Claim::GenerateCSVFile.call(claims: submitted_claims)

payment = Claims::Payment.create!(sent_by: current_user, csv_file: File.open(csv_file.to_io), claims: submitted_claims)

submitted_claims.find_each do |claim|
claim.update!(status: :payment_in_progress, payment_in_progress_at: Time.current)
end

Claims::ClaimActivity.create!(action: :payment_delivered, user: current_user, record: payment)

transaction.after_commit do
Claims::PaymentMailer.payment_created_notification(payment).deliver_later
end
end
end

private

attr_reader :current_user

def submitted_claims
@submitted_claims ||= Claims::Claim.submitted
end
end
50 changes: 50 additions & 0 deletions app/services/claims/payments/claim/generate_csv_file.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
require "csv"

class Claims::Payments::Claim::GenerateCSVFile < ApplicationService
HEADERS = %w[
claim_reference
school_urn
school_name
school_local_authority
claim_amount
school_type_of_establishment
school_group
claim_submission_date
claim_status
claim_unpaid_reason
].freeze

def initialize(claims:)
@claims = claims
end

def call
CSV.open(file_name, "w", headers: true) do |csv|
csv << HEADERS

claims.each do |claim|
csv << [
claim.reference,
claim.school.urn,
claim.school_name,
claim.school.local_authority_name,
claim.amount.format(symbol: false, decimal_mark: ".", no_cents: false),
claim.school.type_of_establishment,
claim.school.group,
claim.submitted_at&.iso8601,
claim.status,
]
end

csv
end
end

private

attr_reader :claims

def file_name
Rails.root.join("tmp/payment-claims-#{Time.current}.csv")
end
end
11 changes: 11 additions & 0 deletions app/views/claims/payments/claims/error.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<div class="govuk-width-container">
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<h1 class="govuk-heading-l"><%= t(".page_title") %></h1>

<p class="govuk-body"><%= t(".description") %></p>

<p class="govuk-body"><%= t(".support_html", mail_to: govuk_mail_to(t("claims.support_email"), t("claims.support_email_html"))) %></p>
</div>
</div>
</div>
13 changes: 13 additions & 0 deletions app/views/claims/payments/claims/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<div class="govuk-width-container">
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<h1 class="govuk-heading-l"><%= t(".page_title") %></h1>

<p class="govuk-body"><%= t(".description_html") %></p>

<p class="govuk-body"><%= t(".support_html", mail_to: govuk_mail_to(t("claims.support_email"), t("claims.support_email_html"))) %></p>

<%= govuk_button_link_to t(".submit"), download_claims_payments_claims_path(token: params[:token]) %>
</div>
</div>
</div>
4 changes: 4 additions & 0 deletions app/views/claims/support/claims/payments/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

<h2 class="govuk-heading-m"><%= t(".sub_heading", count: @pagy.count) %></h2>

<div class="govuk-button-group">
<%= govuk_button_link_to t(".buttons.send_claims_to_esfa"), new_claims_support_claims_payment_path %>
</div>

<% if @claims.any? %>
<h3 class="govuk-heading-s"><%= t(".description", count: @pagy.count) %></h3>

Expand Down
31 changes: 31 additions & 0 deletions app/views/claims/support/claims/payments/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<%= render "claims/support/primary_navigation", current: :claims %>
<% content_for(:page_title) { sanitize t(".page_title") } %>

<% content_for(:before_content) do %>
<%= govuk_back_link href: claims_support_claims_payments_path %>
<% end %>

<div class="govuk-width-container">
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<p class="govuk-caption-l"><%= t(".page_caption") %></p>
<h1 class="govuk-heading-l"><%= t(".page_title") %></h1>

<p class="govuk-body"><%= t(".description", count: @submitted_claims.count) %></p>

<p class="govuk-body"><%= t(".details.heading") %></p>

<%= govuk_list type: :bullet do %>
<% t(".details.list").each do |item| %>
<li><%= item %></li>
<% end %>
<% end %>

<%= govuk_warning_text text: t(".warning") %>

<%= govuk_button_to t(".submit"), claims_support_claims_payments_path %>

<p class="govuk-body"><%= govuk_link_to t(".cancel"), claims_support_claims_payments_path %></p>
</div>
</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<%= render "claims/support/primary_navigation", current: :claims %>
<% content_for(:page_title) { sanitize t(".page_title") } %>

<% content_for(:before_content) do %>
<%= govuk_back_link href: claims_support_claims_payments_path %>
<% end %>

<div class="govuk-width-container">
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<p class="govuk-caption-l"><%= t(".page_caption") %></p>
<h1 class="govuk-heading-l"><%= t(".page_title") %></h1>

<p class="govuk-body"><%= t(".description") %></p>

<p class="govuk-body"><%= govuk_link_to t(".cancel"), claims_support_claims_payments_path %></p>
</div>
</div>
</div>
6 changes: 6 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,11 @@ class Application < Rails::Application

# Store user sessions in the database
config.session_store :active_record_store

# Configure message verifiers to generate URL-safe tokens.
config.before_initialize do |app|
app.message_verifiers.clear_rotations
app.message_verifiers.rotate(url_safe: true)
end
end
end
30 changes: 30 additions & 0 deletions config/locales/en/claims/payment_mailer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
en:
claims:
payment_mailer:
payment_created_notification:
subject: Claims ready for payment - Claim funding for mentor training
body: |
To ESFA,
These claims from the Claim funding for mentor training service (Claim) are ready for payment — the link to the latest CSV file is valid for 7 days:
[%{download_page_url}](%{download_page_url})
What you need to do:
1. Check and validate the claims in the CSV file by marking them as ‘paid’ or ‘unpaid’ in the ‘claim_status’ column.
2. If you mark a claim as ‘unpaid’, add the reason in the ‘claim_unpaid_reason’ column.
3. Reply to this email and attach the updated CSV file.
The Claim Support team will follow up on the reasons for unpaid claims and email back an updated version.
If you have a problem opening the link or it has expired, reply to this email and request that it be sent again.
# Give feedback or report a problem
If you have any questions or feedback, please contact the team at [%{support_email}](%{support_email}).
Regards
Claim funding for mentor training team
13 changes: 13 additions & 0 deletions config/locales/en/claims/payments/claims.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
en:
claims:
payments:
claims:
index:
page_title: Download payments file
description_html: Download the Claim funding for mentor training payments <span title="Comma separated values">CSV</span> file.
support_html: If you have any questions, email %{mail_to}.
submit: Download CSV file
error:
page_title: Sorry, there is a problem with the download link
description: You are seeing this page because the download link is not working. It may have timed out or contained an invalid security token.
support_html: Email %{mail_to} to request a new download link.
Loading

0 comments on commit c746340

Please sign in to comment.