From 87433ad14b9502684e44a1823882ffc366878eb2 Mon Sep 17 00:00:00 2001 From: Derek Dyer <4297274+digitaldrk@users.noreply.github.com> Date: Thu, 28 Nov 2024 08:08:32 -0500 Subject: [PATCH] 97958 Debt team - connect digital disupte endpoint to model --- .../v0/digital_disputes_controller.rb | 9 +++- .../models/debts_api/v0/digital_dispute.rb | 4 ++ .../debt_api/v0/digital_dispute_spec.rb | 15 ++++++ .../debts_api/v0/digital_disputes_spec.rb | 46 +++++++++++++++++++ 4 files changed, 72 insertions(+), 2 deletions(-) diff --git a/modules/debts_api/app/controllers/debts_api/v0/digital_disputes_controller.rb b/modules/debts_api/app/controllers/debts_api/v0/digital_disputes_controller.rb index 1309122154e..caa64b1d9b0 100644 --- a/modules/debts_api/app/controllers/debts_api/v0/digital_disputes_controller.rb +++ b/modules/debts_api/app/controllers/debts_api/v0/digital_disputes_controller.rb @@ -6,8 +6,13 @@ class DigitalDisputesController < ApplicationController service_tag 'financial-report' def create - # Just returning data back for now while we wait on our integration partner - render json: digital_disputes_params + digital_dispute = V0::DigitalDispute.new(digital_disputes_params, current_user) + if digital_dispute.valid? + # Just returning data back for now while we wait on our integration partner + render json: digital_dispute.sanitized_json + else + render json: { errors: digital_dispute.errors }, status: :unprocessable_entity + end end private diff --git a/modules/debts_api/app/models/debts_api/v0/digital_dispute.rb b/modules/debts_api/app/models/debts_api/v0/digital_dispute.rb index bef453a8357..67fe4737c8b 100644 --- a/modules/debts_api/app/models/debts_api/v0/digital_dispute.rb +++ b/modules/debts_api/app/models/debts_api/v0/digital_dispute.rb @@ -18,6 +18,10 @@ def initialize(attributes, user) self.attributes = attributes.to_h.merge(user_icn: user.icn) end + def sanitized_json + as_json(except: [:user_icn]) + end + private def validate_contact_information diff --git a/modules/debts_api/spec/models/debt_api/v0/digital_dispute_spec.rb b/modules/debts_api/spec/models/debt_api/v0/digital_dispute_spec.rb index 3c6bbbf6109..07af6e666ac 100644 --- a/modules/debts_api/spec/models/debt_api/v0/digital_dispute_spec.rb +++ b/modules/debts_api/spec/models/debt_api/v0/digital_dispute_spec.rb @@ -97,4 +97,19 @@ end end end + + describe '#sanitized_json' do + let(:user) { build(:user, :loa3) } + let(:raw_params) do + get_fixture_absolute('modules/debts_api/spec/fixtures/digital_disputes/standard_submission') + end + + let(:params) { ActionController::Parameters.new(raw_params) } + + it 'returns a sanitized json' do + digital_dispute = described_class.new(params.permit!.to_h, user) + + expect(digital_dispute.sanitized_json).to eq(raw_params) + end + end end diff --git a/modules/debts_api/spec/requests/debts_api/v0/digital_disputes_spec.rb b/modules/debts_api/spec/requests/debts_api/v0/digital_disputes_spec.rb index 38a4c1d8619..38c51fc7a2f 100644 --- a/modules/debts_api/spec/requests/debts_api/v0/digital_disputes_spec.rb +++ b/modules/debts_api/spec/requests/debts_api/v0/digital_disputes_spec.rb @@ -22,6 +22,52 @@ ) expect(response).to have_http_status(:ok) + expect(JSON.parse(response.body)).to eq(params) + end + + context 'when invalid contact_information' do + let(:params) do + get_fixture_absolute('modules/debts_api/spec/fixtures/digital_disputes/standard_submission') + end + + it 'returns an error when there is no contact information provided' do + params.delete('contact_information') + + post( + '/debts_api/v0/digital_disputes', + params: params, + as: :json + ) + + expect(response).to have_http_status(:unprocessable_entity) + expect(JSON.parse(response.body)).to eq( + 'errors' => { + 'contact_information' => [ + 'is missing required information: email, phone_number, address_line1, city' + ] + } + ) + end + + it 'returns an error when invalid email is submitted' do + params = get_fixture_absolute('modules/debts_api/spec/fixtures/digital_disputes/standard_submission') + params['contact_information']['email'] = 'invalid_email' + + post( + '/debts_api/v0/digital_disputes', + params: params, + as: :json + ) + + expect(response).to have_http_status(:unprocessable_entity) + expect(JSON.parse(response.body)).to eq( + 'errors' => { + 'contact_information' => [ + 'must include a valid email address' + ] + } + ) + end end end end