-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into SeanC/APPEALS-16531
- Loading branch information
Showing
5 changed files
with
211 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |