Skip to content

Commit

Permalink
Merge pull request #7 from DSACMS/add-cbv-flow-controller
Browse files Browse the repository at this point in the history
Add CbvFlows model and controller for flow logic (FFS-679)
  • Loading branch information
allthesignals authored Apr 29, 2024
2 parents 534b1a7 + 73cb59f commit 57f0683
Show file tree
Hide file tree
Showing 18 changed files with 234 additions and 40 deletions.
69 changes: 69 additions & 0 deletions app/controllers/cbv_flows_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
class CbvFlowsController < ApplicationController
USER_TOKEN_ENDPOINT = 'https://api-sandbox.argyle.com/v2/users';

before_action :set_cbv_flow

def entry
end

def employer_search
@argyle_user_token = fetch_and_store_argyle_token
end

def summary
@employer = summary_employer_params[:employer]
@payments = [
{ amount: 810, start: 'March 25', end: 'June 15', hours: 54, rate: 15 },
{ amount: 195, start: 'January 1', end: 'February 23', hours: 13, rate: 15 }
]
end

def reset
session[:cbv_flow_id] = nil
session[:argyle_user_token] = nil
redirect_to root_url
end

private

def set_cbv_flow
if session[:cbv_flow_id]
@cbv_flow = CbvFlow.find(session[:cbv_flow_id])
else
# TODO: This case_number would be provided by the case worker when they send the initial invite
@cbv_flow = CbvFlow.create(case_number: 'ABC1234')
session[:cbv_flow_id] = @cbv_flow.id
end
end

def next_path
case params[:action]
when 'entry'
cbv_flow_employer_search_path
when 'employer_search'
cbv_flow_summary_path
when 'summary'
root_url
end
end
helper_method :next_path

def fetch_and_store_argyle_token
return session[:argyle_user_token] if session[:argyle_user_token].present?

raise "ARGYLE_API_TOKEN environment variable is blank. Make sure you have the .env.local from 1Password." if ENV['ARGYLE_API_TOKEN'].blank?

res = Net::HTTP.post(URI.parse(USER_TOKEN_ENDPOINT), "", {"Authorization" => "Basic #{ENV['ARGYLE_API_TOKEN']}"})
parsed = JSON.parse(res.body)
raise "Argyle API error: #{parsed['detail']}" if res.code.to_i >= 400

@cbv_flow.update(argyle_user_id: parsed['id'])
session[:argyle_user_token] = parsed['user_token']

parsed['user_token']
end

def summary_employer_params
params.permit(:employer)
end
end
2 changes: 1 addition & 1 deletion app/controllers/pages_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class PagesController < ApplicationController
def welcome
def home
end
end
22 changes: 0 additions & 22 deletions app/controllers/providers_controller.rb
Original file line number Diff line number Diff line change
@@ -1,26 +1,4 @@
class ProvidersController < ApplicationController
USER_TOKEN_ENDPOINT = "https://api-sandbox.argyle.com/v2/users"

def index
res = Net::HTTP.post(URI.parse(USER_TOKEN_ENDPOINT), "", { "Authorization" => "Basic #{ENV['ARGYLE_API_TOKEN']}" })

@user_token = JSON.parse(res.body)["user_token"]
end

def search
end

def confirm
@employer = employer_params[:employer]
@payments = [
{ amount: 810, start: "March 25", end: "June 15", hours: 54, rate: 15 },
{ amount: 195, start: "January 1", end: "February 23", hours: 13, rate: 15 }
]
end

private

def employer_params
params.permit(:employer)
end
end
5 changes: 2 additions & 3 deletions app/javascript/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@

import { application } from "./application"

import ProvidersController from "./providers_controller"
application.register("providers", ProvidersController)

import CbvFlowsController from "./cbv_flows_controller"
application.register("cbv-flows", CbvFlowsController)
2 changes: 2 additions & 0 deletions app/models/cbv_flow.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class CbvFlow < ApplicationRecord
end
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<% content_for :head do %>
<%= tag :meta, name: :argyle_user_token, content: @user_token %>
<%= tag :meta, name: :argyle_user_token, content: @argyle_user_token %>
<%= tag :meta, name: :argyle_sandbox, content: ENV['ARGYLE_SANDBOX'] %>
<% end %>

<h2>Get payment info from your employer.</h2>

<form data-controller="providers" action="/providers/confirm" method="get">
<form data-controller="cbv-flows" action="<%= next_path %>" method="get">
<label class="usa-label" for="employer">Search for an employer</label>
<div class="usa-combo-box margin-bottom-3" data-action="input->providers#search">
<select class="usa-select" name="employer" id="employer" data-providers-target="options" data-action="change->providers#select">
<div class="usa-combo-box margin-bottom-3" data-action="input->cbv-flows#search">
<select class="usa-select" name="employer" id="employer" data-cbv-flows-target="options" data-action="change->cbv-flows#select">
<option value>Select an employer</option>
</select>
</div>
<button data-action="click->providers#submit" class="usa-button usa-button--outline" disabled="disabled" type="submit" data-providers-target="continue">Continue</button>
<button data-action="click->cbv-flows#submit" class="usa-button usa-button--outline" disabled="disabled" type="submit" data-cbv-flows-target="continue">Continue</button>
</form>
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<h1>Hello, Matt</h1>
<h1>Hello</h1>
<h2>Let's verify your payment information.</h2>

<p>We'll connect to your payment records to get proof of payments to your caseworker and get benfits faster.</p>

<p>Case Number: <strong><%= @cbv_flow.case_number %></strong></p>

<div class="usa-alert usa-alert--info margin-bottom-3">
<div class="usa-alert__body">
<p class="usa-alert__text">
Expand All @@ -11,6 +13,6 @@
</div>
</div>

<%= link_to '/providers' do %>
<%= link_to next_path do %>
<button class="usa-button usa-button--outline" type="button">Get Started</button>
<% end %>
File renamed without changes.
8 changes: 8 additions & 0 deletions app/views/pages/home.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<h1>Welcome to Verify.gov</h1>
<h2>This website can help you get approved for Medicaid</h2>

<p>Get started here by requesting a link from your caseworker.</p>

<%= link_to cbv_flow_entry_path do %>
<button class="usa-button usa-button--outline" type="button">Start Flow Manually</button>
<% end %>
9 changes: 9 additions & 0 deletions bin/update
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env bash
set -euo pipefail

yarn install
bundle install

bin/rails db:migrate

echo "Done. Run bin/dev to run the local development server."
16 changes: 12 additions & 4 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,21 @@
end
scope "(:locale)", locale: /#{I18n.available_locales.join("|")}/ do
# Your application routes go here
root "pages#welcome"
root "pages#home"

# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
get "providers/search" => "providers#search"
get "providers/confirm" => "providers#confirm"

resources :providers
# Defines the root path route ("/")
# root "articles#index"

scope '/cbv', as: :cbv_flow do
get '/entry' => 'cbv_flows#entry'
get '/employer_search' => 'cbv_flows#employer_search'
get '/argyle_link' => 'cbv_flows#argyle_link'
get '/summary' => 'cbv_flows#summary'

# Utility route to clear your session; useful during development
get '/reset' => 'cbv_flows#reset'
end
end
end
10 changes: 10 additions & 0 deletions db/migrate/20240426180929_create_cbv_flows.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateCbvFlows < ActiveRecord::Migration[7.0]
def change
create_table :cbv_flows do |t|
t.string :case_number
t.string :argyle_user_id

t.timestamps
end
end
end
9 changes: 8 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified doc/compliance/rendered/apps/data.logical.pdf
Binary file not shown.
97 changes: 97 additions & 0 deletions spec/controllers/cbv_flows_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
require "rails_helper"

RSpec.describe CbvFlowsController do
def stub_environment_variable(variable, value, &block)
previous_value = ENV[variable]
ENV[variable] = value
block.call
ENV[variable] = previous_value
end

around do |ex|
stub_environment_variable("ARGYLE_API_TOKEN", "foobar", &ex)
end

describe "#entry" do
render_views

it "renders properly" do
get :entry

expect(response).to be_successful
end

it "sets a CbvFlow object in the session" do
expect { get :entry }
.to change { session[:cbv_flow_id] }
.from(nil)
.to(be_an(Integer))
end
end

describe "#employer_search" do
let(:cbv_flow) { CbvFlow.create(case_number: "ABC1234") }
let(:argyle_mock_response) do
{
id: "abc-def-ghi",
user_token: "foobar"
}
end

before do
session[:cbv_flow_id] = cbv_flow.id

allow(Net::HTTP).to receive(:post)
.with(URI(CbvFlowsController::USER_TOKEN_ENDPOINT), anything, anything)
.and_return(instance_double(Net::HTTPOK, code: "200", body: JSON.generate(argyle_mock_response)))
end

context "when rendering views" do
render_views

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

context "when the user does not have an Argyle token" do
it "requests a new token from Argyle" do
get :employer_search
expect(Net::HTTP).to have_received(:post).once
end

it "saves the token in the CbvFlow model" do
expect { get :employer_search }
.to change { cbv_flow.reload.argyle_user_id }
.from(nil)
.to(argyle_mock_response[:id])
end
end

context "when the user already has an Argyle token in their session" do
before do
session[:argyle_user_token] = argyle_mock_response[:user_token]
end

it "does not request a new User Token from Argyle" do
get :employer_search
expect(Net::HTTP).not_to have_received(:post)
end
end
end

describe "#summary" do
render_views

let(:cbv_flow) { CbvFlow.create(case_number: "ABC1234", argyle_user_id: "abc-def-ghi") }
before do
session[:cbv_flow_id] = cbv_flow.id
end

it "renders properly" do
get :summary
expect(response).to be_successful
end
end
end
5 changes: 5 additions & 0 deletions spec/models/cbv_flow_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe CbvFlow, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
require "rails_helper"

RSpec.describe "pages/welcome.html.erb", type: :view do
RSpec.describe "pages/home.html.erb", type: :view do
it "displays the gov banner" do
render template: "pages/welcome", layout: "layouts/application"
render template: "pages/home", layout: "layouts/application"
expect(rendered).to match "An official website of the United States government"
end
end

0 comments on commit 57f0683

Please sign in to comment.