From 8766401b216ee25c454448d0650293b2ce278e26 Mon Sep 17 00:00:00 2001 From: Tyler Coleman Date: Fri, 8 Nov 2024 16:37:12 -0800 Subject: [PATCH] add declined notify job --- .../claims_api/va_notify_declined_job.rb | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 modules/claims_api/app/sidekiq/claims_api/va_notify_declined_job.rb diff --git a/modules/claims_api/app/sidekiq/claims_api/va_notify_declined_job.rb b/modules/claims_api/app/sidekiq/claims_api/va_notify_declined_job.rb new file mode 100644 index 00000000000..5be60050731 --- /dev/null +++ b/modules/claims_api/app/sidekiq/claims_api/va_notify_declined_job.rb @@ -0,0 +1,88 @@ +# frozen_string_literal: true + +module ClaimsApi + class VANotifyDeclinedJob < ClaimsApi::ServiceBase + LOG_TAG = 'va_notify_declined_job' + + def perform(poa_id, rep) + return if Rails.env.test? + + poa = ClaimsApi::PowerOfAttorney.find(poa_id) + + unless poa + raise ::ClaimsApi::Common::Exceptions::Lighthouse::ResourceNotFound.new( + detail: "Could not find Power of Attorney with id: #{poa_id}" + ) + end + + begin + res = send_declined_notification(poa, rep) + + ClaimsApi::VANotifyFollowUpJob.perform_async(res.id) if res.present? + rescue => e + msg = "VA Notify email notification failed to send for #{poa_id} with error #{e}" + slack_client = SlackNotify::Client.new(webhook_url: Settings.claims_api.slack.webhook_url, + channel: '#api-benefits-claims-alerts', + username: 'Failed ClaimsApi::VANotifyDeclinedJob') + + slack_client.notify(msg) + ClaimsApi::Logger.log(LOG_TAG, detail: msg) + + raise e + end + end + + private + + def send_declined_notification(poa, rep) + form_data = poa.form_data + + if form_data['serviceOrganization'] + poa_code = form_data['serviceOrganization']['poaCode'] + org = ::Veteran::Service::Organization.find_by(poa: poa_code) + + return send_organization_notification(poa, org) + end + + send_representative_notification(poa, rep) + end + + # TODO: Discover if we need org + def send_organization_notification(poa, _org) + content = { + recipient_identifier: recipient_identifier(poa), + personalisation: { + first_name: poa.auth_headers['va_eauth_firstName'] || '', + form_type: 'TODO' # TODO: Add form type, i.e., VSO or Individual + }, + template_id: Settings.claims_api.vanotify.organization_declined_template_id + } + + vanotify_service.send_email(content) + end + + # TODO: Discover if we need rep + def send_representative_notification(poa, _rep) + content = { + recipient_identifier: recipient_identifier(poa), + personalisation: { + first_name: poa.auth_headers['va_eauth_firstName'] || '', + representative_type: poa.form_data.dig('representative', 'type') || '', + representative_type_abbreviated: 'TODO', # TODO: Add representative type abbreviated + form_type: 'TODO' # TODO: Add form type, i.e., VSO or Individual + }, + template_id: Settings.claims_api.vanotify.representative_declined_template_id + } + + vanotify_service.send_email(content) + end + + def recipient_identifier(poa) + poa.auth_headers[ClaimsApi::V2::Veterans::PowerOfAttorney::BaseController::VA_NOTIFY_KEY] + end + + def vanotify_service + VaNotify::Service.new(Settings.claims_api.vanotify.services.lighthouse.api_key) + end + end +end