Skip to content

Commit

Permalink
Merge branch 'master' into SeanC/APPEALS-16531
Browse files Browse the repository at this point in the history
  • Loading branch information
craigrva authored May 17, 2023
2 parents 84063e3 + 4416759 commit 2350b45
Show file tree
Hide file tree
Showing 5 changed files with 211 additions and 5 deletions.
69 changes: 69 additions & 0 deletions app/controllers/api/v1/va_notify_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# frozen_string_literal: true

class Api::V1::VaNotifyController < Api::ApplicationController
# Purpose: POST request to VA Notify API to update status for a Notification entry
#
# Params: Params content can be found at https://vajira.max.gov/browse/APPEALS-21021
#
# Response: Update corresponding Notification status
def notifications_update
if required_params[:notification_type] == "email"
email_update
elsif required_params[:notification_type] == "sms"
sms_update
end
end

private

# Purpose: Log error in Rails logger and gives 500 error
#
# Params: Notification type string, either "email" or "SMS"
#
# Response: json error message with uuid and 500 error
def log_error(notification_type)
uuid = SecureRandom.uuid
error_msg = "An #{notification_type} notification with id #{required_params[:id]} could not be found. " \
"Error ID: #{uuid}"
Rails.logger.error(error_msg)
render json: { message: error_msg }, status: :internal_server_error
end

# Purpose: Finds and updates notification if type is email
#
# Params: Params content can be found at https://vajira.max.gov/browse/APPEALS-21021
#
# Response: Update corresponding email Notification status
def email_update
# find notification through external id
notif = Notification.find_by(email_notification_external_id: required_params[:id])
# log external id if notification doesn't exist
return log_error(required_params[:notification_type]) unless notif

# update notification if it exists
notif.update!(email_notification_status: required_params[:status])
render json: { message: "Email notification successfully updated: ID " + required_params[:id] }
end

# Purpose: Finds and updates notification if type is SMS
#
# Params: Params content can be found at https://vajira.max.gov/browse/APPEALS-21021
#
# Response: Update corresponding SMS Notification status
def sms_update
# find notification through external id
notif = Notification.find_by(sms_notification_external_id: required_params[:id])
# log external id if notification doesn't exist
return log_error(required_params[:notification_type]) unless notif

# update notification if it exists
notif.update!(sms_notification_status: params[:status])
render json: { message: "SMS notification successfully updated: ID " + required_params[:id] }
end

def required_params
id_param, notification_type_param, status_param = params.require([:id, :notification_type, :status])

{ id: id_param, notification_type: notification_type_param, status: status_param }
end
end
2 changes: 1 addition & 1 deletion config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
# Raises error for missing translations
# config.action_view.raise_on_missing_translations = true

ENV["VA_DOT_GOV_API_URL"] = "https://staging-api.va.gov/"
ENV["VA_DOT_GOV_API_URL"] = "https://sandbox-api.va.gov/"

# For testing uncertification methods
ENV["TEST_USER_ID"] = "TEST_USER_ID"
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
resources :appeals, only: :index
resources :jobs, only: :create
post 'mpi', to: 'mpi#veteran_updates'
post 'va_notify_update', to: 'va_notify#notifications_update'
end
namespace :v2 do
resources :appeals, only: :index
Expand Down
7 changes: 3 additions & 4 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
t.boolean "appeal_docketed", default: false, null: false, comment: "When true, appeal has been docketed"
t.bigint "appeal_id", null: false, comment: "AMA or Legacy Appeal ID"
t.string "appeal_type", null: false, comment: "Appeal Type (Appeal or LegacyAppeal)"
t.datetime "created_at", null: false
t.datetime "created_at", null: false, comment: "Date and Time the record was inserted into the table"
t.bigint "created_by_id", null: false, comment: "User id of the user that inserted the record"
t.boolean "decision_mailed", default: false, null: false, comment: "When true, appeal has decision mail request complete"
t.boolean "hearing_postponed", default: false, null: false, comment: "When true, appeal has hearing postponed and no hearings scheduled"
Expand All @@ -100,7 +100,7 @@
t.boolean "privacy_act_complete", default: false, null: false, comment: "When true, appeal has a privacy act request completed"
t.boolean "privacy_act_pending", default: false, null: false, comment: "When true, appeal has a privacy act request still open"
t.boolean "scheduled_in_error", default: false, null: false, comment: "When true, hearing was scheduled in error and none scheduled"
t.datetime "updated_at"
t.datetime "updated_at", comment: "Date and time the record was last updated"
t.bigint "updated_by_id", comment: "User id of the last user that updated the record"
t.boolean "vso_ihp_complete", default: false, null: false, comment: "When true, appeal has a VSO IHP request completed"
t.boolean "vso_ihp_pending", default: false, null: false, comment: "When true, appeal has a VSO IHP request pending"
Expand Down Expand Up @@ -1275,7 +1275,7 @@
t.string "recipient_email", comment: "Participant's Email Address"
t.string "recipient_phone_number", comment: "Participants Phone Number"
t.text "sms_notification_content", comment: "Full SMS Text Content of Notification"
t.string "sms_notification_external_id", comment: "VA Notify Notification Id for the sms notification send through their API "
t.string "sms_notification_external_id"
t.string "sms_notification_status", comment: "Status of SMS/Text Notification"
t.datetime "updated_at", comment: "TImestamp of when Notification was Updated"
t.index ["appeals_id", "appeals_type"], name: "index_appeals_notifications_on_appeals_id_and_appeals_type"
Expand Down Expand Up @@ -1577,7 +1577,6 @@
t.boolean "national_cemetery_administration", default: false
t.boolean "no_special_issues", default: false, comment: "Affirmative no special issues, added belatedly"
t.boolean "nonrating_issue", default: false
t.boolean "pact_act", default: false, comment: "The Sergeant First Class (SFC) Heath Robinson Honoring our Promise to Address Comprehensive Toxics (PACT) Act"
t.boolean "pension_united_states", default: false
t.boolean "private_attorney_or_agent", default: false
t.boolean "radiation", default: false
Expand Down
137 changes: 137 additions & 0 deletions spec/controllers/api/v1/va_notify_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
# frozen_string_literal: true

describe Api::V1::VaNotifyController, type: :controller do
before do
Seeds::NotificationEvents.new.seed!
end
let(:api_key) { ApiKey.create!(consumer_name: "API Consumer").key_string }
let!(:appeal) { create(:appeal) }
let!(:notification_email) do
create(
:notification,
appeals_id: appeal.uuid,
appeals_type: "Appeal",
event_date: "2023-02-27 13:11:51.91467",
event_type: "Quarterly Notification",
notification_type: "Email",
notified_at: "2023-02-28 14:11:51.91467",
email_notification_external_id: "3fa85f64-5717-4562-b3fc-2c963f66afa6",
email_notification_status: "No Claimant Found"
)
end
let!(:notification_sms) do
create(
:notification,
appeals_id: appeal.uuid,
appeals_type: "Appeal",
event_date: "2023-02-27 13:11:51.91467",
event_type: "Quarterly Notification",
notification_type: "Email",
notified_at: "2023-02-28 14:11:51.91467",
sms_notification_external_id: "3fa85f64-5717-4562-b3fc-2c963f66afa6",
sms_notification_status: "Preferences Declined"
)
end
let(:default_payload) do
{
id: "3fa85f64-5717-4562-b3fc-2c963f66afa6",
body: "string",
completed_at: "2023-04-17T12:38:48.699Z",
created_at: "2023-04-17T12:38:48.699Z",
created_by_name: "string",
email_address: "user@example.com",
line_1: "string",
line_2: "string",
line_3: "string",
line_4: "string",
line_5: "string",
line_6: "string",
phone_number: "+16502532222",
postage: "string",
postcode: "string",
reference: "string",
scheduled_for: "2023-04-17T12:38:48.699Z",
sent_at: "2023-04-17T12:38:48.699Z",
sent_by: "string",
status: "created",
subject: "string",
notification_type: ""
}
end

context "email notification status is changed" do
let(:payload_email) do
default_payload.deep_dup.tap do |payload|
payload[:notification_type] = "email"
end
end

it "updates status of notification" do
request.headers["Authorization"] = "Bearer #{api_key}"
post :notifications_update, params: payload_email
notification_email.reload
expect(notification_email.email_notification_status).to eq("created")
end
end

context "sms notification status is changed" do
let(:payload_sms) do
default_payload.deep_dup.tap do |payload|
payload[:notification_type] = "sms"
end
end

it "updates status of notification" do
request.headers["Authorization"] = "Bearer #{api_key}"
post :notifications_update, params: payload_sms
notification_sms.reload
expect(notification_sms.sms_notification_status).to eq("created")
end
end

context "notification does not exist" do
let(:payload_fake) do
{
id: "fake",
body: "string",
completed_at: "2023-04-17T12:38:48.699Z",
created_at: "2023-04-17T12:38:48.699Z",
created_by_name: "string",
email_address: "user@example.com",
line_1: "string",
line_2: "string",
line_3: "string",
line_4: "string",
line_5: "string",
line_6: "string",
phone_number: "+16502532222",
postage: "string",
postcode: "string",
recipient_identifiers: [
{
id_type: "VAPROFILEID",
id_value: "string"
}
],
reference: "string",
scheduled_for: "2023-04-17T12:38:48.699Z",
sent_at: "2023-04-17T12:38:48.699Z",
sent_by: "string",
status: "created",
subject: "string",
template: {
id: "3fa85f64-5717-4562-b3fc-2c963f66afa6",
uri: "string",
version: 0
},
notification_type: "sms"
}
end

it "updates status of notification" do
request.headers["Authorization"] = "Bearer #{api_key}"
post :notifications_update, params: payload_fake
expect(response.status).to eq(500)
end
end
end

0 comments on commit 2350b45

Please sign in to comment.