diff --git a/modules/accredited_representative_portal/app/controllers/accredited_representative_portal/v0/power_of_attorney_requests_controller.rb b/modules/accredited_representative_portal/app/controllers/accredited_representative_portal/v0/power_of_attorney_requests_controller.rb index 867b3db8d32..98f59734be0 100644 --- a/modules/accredited_representative_portal/app/controllers/accredited_representative_portal/v0/power_of_attorney_requests_controller.rb +++ b/modules/accredited_representative_portal/app/controllers/accredited_representative_portal/v0/power_of_attorney_requests_controller.rb @@ -3,54 +3,21 @@ module AccreditedRepresentativePortal module V0 class PowerOfAttorneyRequestsController < ApplicationController - POA_REQUEST_ITEM_MOCK_DATA = { - status: 'Pending', - declinedReason: nil, - powerOfAttorneyCode: '091', - submittedAt: '2024-04-30T11:03:17Z', - acceptedOrDeclinedAt: nil, - isAddressChangingAuthorized: false, - isTreatmentDisclosureAuthorized: true, - veteran: { - firstName: 'Jon', - middleName: nil, - lastName: 'Smith', - participantId: '6666666666666' - }, - representative: { - email: 'j2@example.com', - firstName: 'Jane', - lastName: 'Doe' - }, - claimant: { - firstName: 'Sam', - lastName: 'Smith', - participantId: '777777777777777', - relationshipToVeteran: 'Child' - }, - claimantAddress: { - city: 'Hartford', - state: 'CT', - zip: '06107', - country: 'GU', - militaryPostOffice: nil, - militaryPostalCode: nil - } - }.freeze - - POA_REQUEST_LIST_MOCK_DATA = [ - POA_REQUEST_ITEM_MOCK_DATA, - POA_REQUEST_ITEM_MOCK_DATA, - POA_REQUEST_ITEM_MOCK_DATA - ].freeze - def index - render json: POA_REQUEST_LIST_MOCK_DATA + requests = PowerOfAttorneyRequest.includes(:resolution) + render json: PowerOfAttorneyRequestSerializer.new(requests).serializable_hash, status: :ok end def show - render json: POA_REQUEST_ITEM_MOCK_DATA + request = PowerOfAttorneyRequest.includes(:resolution).find(params[:id]) + render json: PowerOfAttorneyRequestSerializer.new(request).serializable_hash, status: :ok + rescue ActiveRecord::RecordNotFound + render json: { error: 'Record not found' }, status: :not_found end + + private + + def authenticate; end end end end diff --git a/modules/accredited_representative_portal/app/serializers/accredited_representative_portal/power_of_attorney_request_serializer.rb b/modules/accredited_representative_portal/app/serializers/accredited_representative_portal/power_of_attorney_request_serializer.rb index b35acb5f643..89961c9989e 100644 --- a/modules/accredited_representative_portal/app/serializers/accredited_representative_portal/power_of_attorney_request_serializer.rb +++ b/modules/accredited_representative_portal/app/serializers/accredited_representative_portal/power_of_attorney_request_serializer.rb @@ -4,7 +4,11 @@ module AccreditedRepresentativePortal class PowerOfAttorneyRequestSerializer include JSONAPI::Serializer - attributes :id, :claimant_id, :created_at + attributes :id, :claimant_id + + attribute :created_at do |object| + object.created_at.strftime('%Y-%m-%dT%H:%M:%S.%LZ') + end attribute :resolution do |object| next nil unless object.resolution @@ -12,7 +16,7 @@ class PowerOfAttorneyRequestSerializer { id: object.resolution.id, type: object.resolution.resolving_type&.demodulize&.underscore, - created_at: object.resolution.created_at.iso8601, + created_at: object.resolution.created_at.strftime('%Y-%m-%dT%H:%M:%S.%LZ'), reason: object.resolution&.reason, creator_id: object.resolution.resolving.try(:creator_id) }.compact diff --git a/modules/accredited_representative_portal/spec/requests/accredited_representative_portal/v0/power_of_attorney_requests_spec.rb b/modules/accredited_representative_portal/spec/requests/accredited_representative_portal/v0/power_of_attorney_requests_spec.rb index c1349f2db60..d58082927c0 100644 --- a/modules/accredited_representative_portal/spec/requests/accredited_representative_portal/v0/power_of_attorney_requests_spec.rb +++ b/modules/accredited_representative_portal/spec/requests/accredited_representative_portal/v0/power_of_attorney_requests_spec.rb @@ -4,28 +4,8 @@ RSpec.describe AccreditedRepresentativePortal::V0::PowerOfAttorneyRequestsController, type: :request do let(:test_user) { create(:representative_user) } - let(:poa_request_details_id) { '123' } - let(:poa_request_details_mock_data) do - { - 'status' => 'Pending', - 'declinedReason' => nil, - 'powerOfAttorneyCode' => '091', - 'submittedAt' => '2024-04-30T11:03:17Z', - 'acceptedOrDeclinedAt' => nil, - 'isAddressChangingAuthorized' => false, - 'isTreatmentDisclosureAuthorized' => true, - 'veteran' => { 'firstName' => 'Jon', 'middleName' => nil, 'lastName' => 'Smith', - 'participantId' => '6666666666666' }, - 'representative' => { 'email' => 'j2@example.com', 'firstName' => 'Jane', 'lastName' => 'Doe' }, - 'claimant' => { 'firstName' => 'Sam', 'lastName' => 'Smith', 'participantId' => '777777777777777', - 'relationshipToVeteran' => 'Child' }, - 'claimantAddress' => { 'city' => 'Hartford', 'state' => 'CT', 'zip' => '06107', 'country' => 'GU', - 'militaryPostOffice' => nil, 'militaryPostalCode' => nil } - } - end - let(:poa_request_list_mock_data) do - [poa_request_details_mock_data, poa_request_details_mock_data, poa_request_details_mock_data] - end + let(:poa_request) { create(:power_of_attorney_request) } + let(:poa_requests) { create_list(:power_of_attorney_request, 3) } before do Flipper.enable(:accredited_representative_portal_pilot) @@ -33,18 +13,49 @@ end describe 'GET /accredited_representative_portal/v0/power_of_attorney_requests' do - it 'returns the list of a power of attorney request' do + it 'returns the list of power of attorney requests' do + poa_requests + get('/accredited_representative_portal/v0/power_of_attorney_requests') + expect(response).to have_http_status(:ok) - expect(JSON.parse(response.body)).to eq(poa_request_list_mock_data) + parsed_response = JSON.parse(response.body) + + expected_response = AccreditedRepresentativePortal::PowerOfAttorneyRequestSerializer + .new(poa_requests) + .serializable_hash + + expect(deep_stringify(parsed_response)).to eq(deep_stringify(expected_response)) end end describe 'GET /accredited_representative_portal/v0/power_of_attorney_requests/:id' do - it 'returns the details of a power of attorney request' do - get("/accredited_representative_portal/v0/power_of_attorney_requests/#{poa_request_details_id}") + it 'returns the details of a specific power of attorney request' do + get("/accredited_representative_portal/v0/power_of_attorney_requests/#{poa_request.id}") + expect(response).to have_http_status(:ok) - expect(JSON.parse(response.body)).to include(poa_request_details_mock_data) + parsed_response = JSON.parse(response.body) + + expected_response = AccreditedRepresentativePortal::PowerOfAttorneyRequestSerializer + .new(poa_request) + .serializable_hash + + expect(deep_stringify(parsed_response)).to eq(deep_stringify(expected_response)) + end + end + + def deep_stringify(value) + case value + when Hash + value.each_with_object({}) do |(k, v), result| + result[k.to_s] = deep_stringify(v) # Stringify keys and recursively process values + end + when Array + value.map { |v| deep_stringify(v) } # Recursively process arrays + when Symbol + value.to_s # Convert symbols to strings + else + value # Leave other primitives (e.g., strings, numbers, nil) as is end end end diff --git a/modules/accredited_representative_portal/spec/serializers/accredited_representative_portal/power_of_attorney_request_serializer_spec.rb b/modules/accredited_representative_portal/spec/serializers/accredited_representative_portal/power_of_attorney_request_serializer_spec.rb index 38da05c1556..14011bf6ec6 100644 --- a/modules/accredited_representative_portal/spec/serializers/accredited_representative_portal/power_of_attorney_request_serializer_spec.rb +++ b/modules/accredited_representative_portal/spec/serializers/accredited_representative_portal/power_of_attorney_request_serializer_spec.rb @@ -1,19 +1,21 @@ +# frozen_string_literal: true + # spec/serializers/power_of_attorney_request_serializer_spec.rb require 'rails_helper' RSpec.describe AccreditedRepresentativePortal::PowerOfAttorneyRequestSerializer do - let(:request) { create(:power_of_attorney_request, created_at: '2024-12-17T00:30:55Z') } - subject { described_class.new(request).serializable_hash[:data][:attributes] } + let(:request) { create(:power_of_attorney_request, created_at: '2024-12-17T00:30:55Z') } + context 'when resolution is nil' do it 'returns the request without resolution' do expect(subject).to eq({ - id: request.id, - claimant_id: request.claimant_id, - created_at: '2024-12-17T00:30:55Z', - resolution: nil - }) + id: request.id, + claimant_id: request.claimant_id, + created_at: '2024-12-17T00:30:55.000Z', + resolution: nil + }) end end @@ -26,11 +28,11 @@ it 'includes resolution details with type expiration and reason' do expect(subject[:resolution]).to eq({ - id: resolution.id, - type: 'power_of_attorney_request_expiration', - created_at: resolution.created_at.iso8601, - reason: 'Test reason for resolution' - }) + id: resolution.id, + type: 'power_of_attorney_request_expiration', + created_at: resolution.created_at.iso8601(3), + reason: 'Test reason for resolution' + }) end end @@ -43,12 +45,12 @@ it 'includes resolution details with type decision and creator_id' do expect(subject[:resolution]).to eq({ - id: resolution.id, - type: 'power_of_attorney_request_decision', - created_at: resolution.created_at.iso8601, - reason: 'Test reason for resolution', - creator_id: resolution.resolving.creator_id - }) + id: resolution.id, + type: 'power_of_attorney_request_decision', + created_at: resolution.created_at.iso8601(3), + reason: 'Test reason for resolution', + creator_id: resolution.resolving.creator_id + }) end end @@ -62,12 +64,12 @@ it 'includes resolution details with type decision, reason, and creator_id' do expect(subject[:resolution]).to eq({ - id: resolution.id, - type: 'power_of_attorney_request_decision', - created_at: resolution.created_at.iso8601, - reason: "Didn't authorize treatment record disclosure", - creator_id: resolution.resolving.creator_id - }) + id: resolution.id, + type: 'power_of_attorney_request_decision', + created_at: resolution.created_at.iso8601(3), + reason: "Didn't authorize treatment record disclosure", + creator_id: resolution.resolving.creator_id + }) end end end