Skip to content

Commit

Permalink
Allow emailing the PDF report to caseworkers
Browse files Browse the repository at this point in the history
George/ffs 777 (#60)

* Consolidating commits as to not expose the email address previously hardcoded, but has since replaced with an env var

* fixing rubocop issues

* formatting variable

* addressing some feedback, cleaning up en translations, updated ApplicantMailer to use in-memory pdf attachment rather than a temp file

* removed comment about retrieving the case_number from the session, it's irrelevant

* no need for an http [PATCH] route

* updating mailer attachment to be properly rendered

* tests passing for pdf email attachment to caseworker

* added body to caseworker summary email

* updated applicant_mailer preview so that the view inherits the appropriate variables

* updated share endpoint test to use pinwheel stub... merging main

* cbv flows controller tests passing

* following a few best practices for applicant_mailer, consolidated view related helpers into a single ViewHelper class, normalized en.yml

* updated how the test email is resolved \n Added controller POST /share test

* removed SLACK_TEST_EMAIL env var from tests

* ran rubocop for linting
  • Loading branch information
GeorgeCodes19 committed Jun 18, 2024
1 parent c83417a commit b36f114
Show file tree
Hide file tree
Showing 17 changed files with 294 additions and 50 deletions.
1 change: 1 addition & 0 deletions app/.env
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ NGROK_URL=
CBV_INVITE_SECRET=development
DOMAIN_NAME=localhost
PINWHEEL_API_TOKEN=API secret
SLACK_TEST_EMAIL=
1 change: 1 addition & 0 deletions app/app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class ApplicationController < ActionController::Base
helper :view
around_action :switch_locale

def switch_locale(&action)
Expand Down
34 changes: 20 additions & 14 deletions app/app/controllers/cbv_flows_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class CbvFlowsController < ApplicationController
before_action :set_cbv_flow
before_action :set_payments, only: [:summary, :share]

def entry
end
Expand All @@ -15,17 +16,6 @@ def summary
return redirect_to next_path
end

@payments = fetch_payroll.map do |payment|
{
employer: payment["employer_name"],
amount: payment["net_pay_amount"].to_i,
start: payment["pay_period_start"],
end: payment["pay_period_end"],
hours: payment["earnings"][0]["hours"],
rate: payment["earnings"][0]["rate"]
}
end

respond_to do |format|
format.html
format.pdf do
Expand All @@ -35,6 +25,8 @@ def summary
end

def share
email_address = ENV["SLACK_TEST_EMAIL"]
ApplicantMailer.with(email_address: email_address, cbv_flow: @cbv_flow, payments: @payments).caseworker_summary_email.deliver_now
end

def reset
Expand Down Expand Up @@ -66,6 +58,19 @@ def set_cbv_flow
session[:cbv_flow_id] = @cbv_flow.id
end

def set_payments
@payments = fetch_payroll.map do |payment|
{
employer: payment["employer_name"],
amount: payment["net_pay_amount"].to_i,
start: payment["pay_period_start"],
end: payment["pay_period_end"],
hours: payment["earnings"][0]["hours"],
rate: payment["earnings"][0]["rate"]
}
end
end

def next_path
case params[:action]
when "entry"
Expand All @@ -78,21 +83,22 @@ def next_path
root_url
end
end

helper_method :next_path

def fetch_employers(query = "")
request_params = {
q: query,
supported_jobs: [ "paystubs" ]
supported_jobs: ["paystubs"]
}

provider.fetch_items(request_params)["data"]
end

def fetch_payroll
account_ids = provider.fetch_accounts(end_user_id: @cbv_flow.pinwheel_end_user_id)["data"].map { |account| account["id"] }
end_user_account_ids = provider.fetch_accounts(end_user_id: @cbv_flow.pinwheel_end_user_id)["data"].map { |account| account["id"] }

account_ids.map do |account_id|
end_user_account_ids.map do |account_id|
provider.fetch_paystubs(account_id: account_id)["data"]
end.flatten
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module ApplicationHelper
module ViewHelper
def format_active_locale(locale_string)
link_classes = "usa-nav__link"
if locale_string.to_sym == I18n.locale
Expand Down
33 changes: 30 additions & 3 deletions app/app/mailers/applicant_mailer.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,35 @@
class ApplicantMailer < ApplicationMailer
attr_reader :email_address
before_action :set_params
helper :view

def invitation_email
@link = params[:link]
mail(to: params[:email_address], subject: I18n.t("applicant_mailer.invitation_email.subject"))
mail(
to: @email_address,
subject: I18n.t("applicant_mailer.invitation_email.subject")
)
end

def caseworker_summary_email
attachments["income_verification.pdf"] = generate_pdf
mail(
to: @email_address,
subject: I18n.t("applicant_mailer.caseworker_summary_email.subject", case_number: @cbv_flow.case_number),
body: I18n.t("applicant_mailer.caseworker_summary_email.body")
)
end

private

def set_params
@cbv_flow = params[:cbv_flow]
@email_address = params[:email_address]
@link = params[:link] if params[:link]
@payments = params[:payments] if params[:payments]
end

def generate_pdf
WickedPdf.new.pdf_from_string(
render_to_string(template: "cbv_flows/summary", formats: [ :pdf ])
)
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<%= t('.greeting') %>
<%= t('.body') %>
<%= t('.thank_you') %>
7 changes: 4 additions & 3 deletions app/app/views/cbv_flows/share.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
<%= t('.body') %>
</p>

<button class="usa-button" type="button">
<%= t('.share_with_caseworker') %>
</button>
<%= form_with url: cbv_flow_share_path(@cbv_flow), method: :post, local: true do |form| %>
<%= form.button t('.share_with_caseworker'), type: 'submit', class: 'usa-button' %>
<% end %>
<%= link_to cbv_flow_summary_path(format: 'pdf'), target: '_blank', class: 'usa-button margin-top-3 usa-button--outline' do %>
<%= t('.download_pdf') %>
<% end %>
3 changes: 3 additions & 0 deletions app/config/locales/en.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
---
en:
applicant_mailer:
caseworker_summary_email:
body: Applicant has submitted their income verification. Please find the attached PDF.
subject: 'Applicant Income Verification: %{case_number}'
invitation_email:
body: A government agency has requested that you verify your income in connection with your application for benefits. Please click this link to begin the process.
greeting: Hello,
Expand Down
4 changes: 2 additions & 2 deletions app/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
get "/employer_search" => "cbv_flows#employer_search"
patch "/summary" => "cbv_flows#summary"
get "/summary" => "cbv_flows#summary"
patch "/share" => "cbv_flows#share"
# share
get "/share" => "cbv_flows#share"

post "/share" => "cbv_flows#share"
# Utility route to clear your session; useful during development
get "/reset" => "cbv_flows#reset"

Expand Down
52 changes: 36 additions & 16 deletions app/spec/controllers/cbv_flows_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
require "rails_helper"

RSpec.describe CbvFlowsController do
include PinwheelApiHelper

around do |ex|
stub_environment_variable("PINWHEEL_API_TOKEN", "foobar", &ex)
stub_environment_variable("SLACK_TEST_EMAIL", "[email protected]", &ex)
end

describe "#entry" do
Expand All @@ -17,8 +19,8 @@
it "sets a CbvFlow object in the session" do
expect { get :entry }
.to change { session[:cbv_flow_id] }
.from(nil)
.to(be_an(Integer))
.from(nil)
.to(be_an(Integer))
end

context "when following a link from a flow invitation" do
Expand All @@ -27,14 +29,14 @@
it "sets a CbvFlow object based on the invitation" do
expect { get :entry, params: { token: invitation.auth_token } }
.to change { session[:cbv_flow_id] }
.from(nil)
.to(be_an(Integer))
.from(nil)
.to(be_an(Integer))

cbv_flow = CbvFlow.find(session[:cbv_flow_id])
expect(cbv_flow).to have_attributes(
case_number: "ABC1234",
cbv_flow_invitation: invitation
)
case_number: "ABC1234",
cbv_flow_invitation: invitation
)
end

context "when returning to an already-visited flow invitation" do
Expand All @@ -43,8 +45,8 @@
it "uses the existing CbvFlow object" do
expect { get :entry, params: { token: invitation.auth_token } }
.to change { session[:cbv_flow_id] }
.from(nil)
.to(existing_cbv_flow.id)
.from(nil)
.to(existing_cbv_flow.id)
end
end

Expand All @@ -58,8 +60,8 @@
it "replaces the session's CbvFlow id with the one from the link token" do
expect { get :entry, params: { token: invitation.auth_token } }
.to change { session[:cbv_flow_id] }
.from(other_cbv_flow.id)
.to(be_an(Integer))
.from(other_cbv_flow.id)
.to(be_an(Integer))
end
end

Expand Down Expand Up @@ -115,8 +117,8 @@
skip "saves the token in the CbvFlow model" do
expect { get :employer_search }
.to change { cbv_flow.reload.pinwheel_token_id }
.from(nil)
.to(pinwheel_token_id)
.from(nil)
.to(pinwheel_token_id)
end
end
end
Expand All @@ -128,6 +130,8 @@

before do
session[:cbv_flow_id] = cbv_flow.id
stub_request_end_user_accounts_response
stub_request_end_user_paystubs_response
end

skip "renders properly" do
Expand All @@ -142,8 +146,8 @@
expect do
patch :summary, params: { cbv_flow: { additional_information: additional_information } }
end.to change { cbv_flow.reload.additional_information }
.from(nil)
.to(additional_information)
.from(nil)
.to(additional_information)

expect(response).to redirect_to(cbv_flow_share_path)
end
Expand All @@ -157,11 +161,27 @@

before do
session[:cbv_flow_id] = cbv_flow.id
stub_request_end_user_paystubs_response
stub_request_end_user_accounts_response
end

it "renders" do
get :share
expect(response).to be_successful
end

context "when sending an email to the caseworker" do
let(:email_address) { "[email protected]" }

it "sends the email" do
expect do
post :share
end.to change { ActionMailer::Base.deliveries.count }.by(1)

email = ActionMailer::Base.deliveries.last
expect(email.to).to eq([ email_address ])
expect(email.subject).to eq("Applicant Income Verification: ABC1234")
end
end
end
end
34 changes: 34 additions & 0 deletions app/spec/mailers/applicant_mailer_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
require "rails_helper"

RSpec.describe ApplicantMailer, type: :mailer do

around do |ex|
stub_environment_variable("SLACK_TEST_EMAIL", "[email protected]", &ex)
end

let(:payments) { stub_payments }
let(:email) { '[email protected]' }
let(:link) { 'www.google.com' }
let(:mail) { ApplicantMailer.with(email_address: email, link: link).invitation_email }
Expand All @@ -20,4 +26,32 @@
it "renders the body" do
expect(mail.body.encoded).to match(I18n.t('applicant_mailer.invitation_email.body'))
end

describe 'caseworker_summary_email' do
let(:cbv_flow) { CbvFlow.create(case_number: "ABC1234", argyle_user_id: "abc-def-ghi") }
let(:email_address) { "[email protected]" }
let(:mail) {
ApplicantMailer.with(
email_address: email_address,
cbv_flow: cbv_flow,
payments: payments).caseworker_summary_email.deliver_now
}

it 'renders the subject with case number' do
expect(mail.subject).to eq(I18n.t('applicant_mailer.caseworker_summary_email.subject', case_number: cbv_flow.case_number))
end

it 'sends to the correct email' do
expect(mail.to).to eq([ email_address ])
end

it 'renders the body' do
expect(mail.body.encoded).to match(I18n.t('applicant_mailer.caseworker_summary_email.body'))
end

it 'attaches a PDF' do
expect(mail.attachments['income_verification.pdf']).to be_present
expect(mail.attachments['income_verification.pdf'].content_type).to start_with('application/pdf')
end
end
end
7 changes: 7 additions & 0 deletions app/spec/mailers/previews/applicant_mailer_preview.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,11 @@ class ApplicantMailerPreview < ActionMailer::Preview
def invitation_email
ApplicantMailer.with(email_address: "[email protected]", link: "http://example.com").invitation_email
end

def caseworker_summary_email
payments = stub_payments

cbv_flow = CbvFlow.create(case_number: "ABC1234", argyle_user_id: "abc-def-ghi", email_address: "[email protected]")
ApplicantMailer.with(cbv_flow: cbv_flow, case_number: "12345", payments: payments).caseworker_summary_email
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"data": [
{
"connected": true,
"created_at": "2021-01-06T15:59:13.530178+00:00",
"data_refreshed_at": {
"direct_deposit_allocations": "2021-01-09T00:00:00.000000+00:00"
},
"data_updated_at": {
"direct_deposit_allocations": "2021-01-08T00:00:00.000000+00:00"
},
"end_user_id": "my_user_12345",
"id": "eb240ec6-a227-47ca-b7c7-fbbdfe7170a0",
"link_token_id": "bd2c09e7-1303-46d5-87c0-0ffa572d7ae4",
"monitoring_status": "active",
"platform_id": "fce3eee0-285b-496f-9b36-30e976194736"
}
],
"meta": {
"count": 1,
"next_cursor": "eyJuYW1lIjogImFtYXpvbiJ9"
}
}
Loading

0 comments on commit b36f114

Please sign in to comment.