Skip to content

Commit

Permalink
[Automated] Merged master into target k8s
Browse files Browse the repository at this point in the history
  • Loading branch information
va-vsp-bot authored Apr 10, 2024
2 parents 9455ac5 + 2efa918 commit 7dec3c6
Show file tree
Hide file tree
Showing 6 changed files with 1,378 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# frozen_string_literal: true

module AccreditedRepresentativePortal
module Services
# The FetchPoaRequests service is responsible for retrieving Power of Attorney (POA) request records
# based on provided Power of Attorney codes. This class currently reads from a JSON file as a temporary
# source of data. In the future, this service will be updated to fetch POA requests directly from the
# Lighthouse API once the appropriate endpoint is ready.
#
# This service is a part of the interim solution to support development and testing of the Accredited
# Representative portal. The use of a static JSON file allows for the simulation of interacting with
# an API and facilitates the frontend development process.
#
# Example usage:
# fetcher = AccreditedRepresentativePortal::Services::FetchPoaRequests.new(['A1Q', '091'])
# result = fetcher.call
# puts result # => { 'records': [...], 'meta': { 'totalRecords': '...' } }
#
# TODO: This class is slated for update to use the Lighthouse API once the appropriate endpoint
# is available. For more information on the transition plan, refer to:
# https://app.zenhub.com/workspaces/accredited-representative-facing-team-65453a97a9cc36069a2ad1d6/issues/gh/department-of-veterans-affairs/va.gov-team/80195
class FetchPoaRequests
# Initializes the FetchPoaRequests service with the given POA codes.
# @param poa_codes [Array<String>] an array of POA codes to filter the POA requests.
def initialize(poa_codes)
@poa_codes = poa_codes
end

# Fetches POA request records filtered by the initialized POA codes.
# Currently reads from a static JSON file as a data source.
# @return [Hash] A hash containing the filtered records and metadata.
def call
file_path = Rails.root.join('modules', 'accredited_representative_portal', 'spec', 'fixtures',
'poa_records.json')
file_data = File.read(file_path)
all_records_json = JSON.parse(file_data)
all_records = all_records_json['records']

filtered_records = all_records.select do |record|
@poa_codes.include?(record['attributes']['poaCode'])
end

{ 'records' => filtered_records, 'meta' => { 'totalRecords' => filtered_records.count.to_s } }
rescue => e
Rails.logger.error "Failed to fetch POA requests: #{e.message}"
{ 'data' => [], 'meta' => { 'totalRecords' => '0' } }
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,77 @@ module AccreditedRepresentativePortal
module V0
class PowerOfAttorneyRequestsController < ApplicationController
def accept
# TODO: The ID will be either a veteran_id or a poa_id
# id = params[:id]
# NOTE: the below is a placeholder for the acceptance logic
render json: { message: 'Accepted' }, status: :ok
id = params[:proc_id]
result = update_poa_request(id, 'Accepted')

if result[:success]
render json: { message: 'Accepted' }, status: :ok
else
render json: { error: result[:error] }, status: :unprocessable_entity
end
end

def decline
# TODO: The ID will be either a veteran_id or a poa_id
# id = params[:id]
# NOTE: the below is a placeholder for the deny logic
render json: { message: 'Declined' }, status: :ok
id = params[:proc_id]
result = update_poa_request(id, 'Declined')

if result[:success]
render json: { message: 'Declined' }, status: :ok
else
render json: { error: result[:error] }, status: :unprocessable_entity
end
end

def index
poa_codes = permitted_params[:poa_codes]&.split(',') || []

return render json: { error: 'POA codes are required' }, status: :bad_request if poa_codes.blank?

poa_requests = AccreditedRepresentativePortal::Services::FetchPoaRequests.new(poa_codes).call

render json: { records: poa_requests['records'], records_count: poa_requests['meta']['totalRecords'].to_i },
status: :ok
end

private

def permitted_params
params.permit(:poa_codes)
end

# TODO: This class is slated for update to use the Lighthouse API once the appropriate endpoint
# is available. For more information on the transition plan, refer to:
# https://app.zenhub.com/workspaces/accredited-representative-facing-team-65453a97a9cc36069a2ad1d6/issues/gh/department-of-veterans-affairs/va.gov-team/80195
def update_poa_request(proc_id, action)
# TODO: Update the below to use the RepresentativeUser's profile data
# representative = {
# first_name: 'John',
# last_name: 'Doe'
# }

# Simulating the interaction with an external service to update POA.
# In real implementation, this method will make an actual API call.
# service_response = ClaimsApi::ManageRepresentativeService.new.update_poa_request(
# representative:,
# proc_id:
# )

if %w[Accepted Declined].include?(action)
{
success: true,
response: {
proc_id:,
action:,
status: 'updated',
dateRequestActioned: Time.current.iso8601,
secondaryStatus: action == 'Accepted' ? 'obsolete' : 'cancelled'
}
}
else
{ success: false, error: 'Invalid action' }
end
rescue => e
{ success: false, error: e.message }
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion modules/accredited_representative_portal/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

AccreditedRepresentativePortal::Engine.routes.draw do
namespace :v0, defaults: { format: :json } do
resources :power_of_attorney_requests, only: [] do
resources :power_of_attorney_requests, only: [:index] do
member do
post :accept
post :decline
Expand Down
Loading

0 comments on commit 7dec3c6

Please sign in to comment.