-
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 dorner/APPEALS-25338
- Loading branch information
Showing
104 changed files
with
5,918 additions
and
326 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
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
1,281 changes: 1,281 additions & 0 deletions
1,281
app/controllers/api/docs/pacman/idt-pacman-spec.yml
Large diffs are not rendered by default.
Oops, something went wrong.
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,82 @@ | ||
# frozen_string_literal: true | ||
|
||
# shared code for building mail packages to submit to Package Manager external service | ||
|
||
module MailPackageConcern | ||
extend ActiveSupport::Concern | ||
|
||
private | ||
|
||
def recipient_info | ||
params[:recipient_info] | ||
end | ||
|
||
def copies | ||
# Default value of 1 for copies | ||
return 1 if params[:copies].blank? | ||
|
||
params[:copies] | ||
end | ||
|
||
def mail_package | ||
return nil if recipient_info.blank? | ||
|
||
{ distributions: json_mail_requests, copies: copies, created_by_id: user.id } | ||
end | ||
|
||
# Purpose: - Creates and validates a MailRequest object for each recipient | ||
# - Calls #call method on each MailRequest to save corresponding VbmsDistirbution and | ||
# VbmsDistributionDestination to the db | ||
# - Stores the distribution IDs (to be returned to the IDT user as an immediate means of tracking | ||
# each distribution) | ||
# | ||
def build_mail_package | ||
return if recipient_info.blank? | ||
|
||
throw_error_if_copies_out_of_range | ||
mail_requests.map do |request| | ||
request.call | ||
distribution_ids << request.vbms_distribution_id | ||
end | ||
end | ||
|
||
def mail_requests | ||
@mail_requests ||= create_mail_requests_and_track_errors | ||
end | ||
|
||
def json_mail_requests | ||
mail_requests.map(&:to_json) | ||
end | ||
|
||
def create_mail_requests_and_track_errors | ||
requests = recipient_info.map.with_index do |recipient, idx| | ||
MailRequest.new(recipient).tap do |request| | ||
if request.invalid? | ||
recipient_errors["distribution #{idx + 1}"] = request.errors.full_messages.join(", ") | ||
end | ||
end | ||
end | ||
throw_error_if_recipient_info_invalid | ||
requests | ||
end | ||
|
||
def throw_error_if_copies_out_of_range | ||
unless (1..500).cover?(copies) | ||
fail Caseflow::Error::MissingRecipientInfo, "Copies must be between 1 and 500 (inclusive)".to_json | ||
end | ||
end | ||
|
||
def throw_error_if_recipient_info_invalid | ||
return unless recipient_errors.any? | ||
|
||
fail Caseflow::Error::MissingRecipientInfo, recipient_errors.to_json | ||
end | ||
|
||
def recipient_errors | ||
@recipient_errors ||= {} | ||
end | ||
|
||
def distribution_ids | ||
@distribution_ids ||= [] | ||
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,74 @@ | ||
# frozen_string_literal: true | ||
|
||
class Idt::Api::V2::DistributionsController < Idt::Api::V1::BaseController | ||
protect_from_forgery with: :exception | ||
before_action :verify_access | ||
|
||
def distribution | ||
distribution_id = params[:distribution_id] | ||
# Checks if the distribution id is blank and if it exists with the database | ||
if distribution_id.blank? || !valid_id?(distribution_id) | ||
return render_error(400, "Distribution Does Not Exist Or Id is blank", distribution_id) | ||
end | ||
|
||
distribution_uuid = distribution_uuid_from_id(distribution_id) | ||
|
||
return pending_establishment(distribution_id) unless distribution_uuid | ||
|
||
begin | ||
# Retrieves the distribution package from the PacMan API | ||
distribution_response = PacmanService.get_distribution_request(distribution_uuid) | ||
|
||
response_code = distribution_response.code | ||
|
||
fail StandardError if response_code != 200 | ||
# Handles errors when making any requests both from Pacman and the DB | ||
rescue StandardError | ||
return render_error(response_code, "Internal Server Error", distribution_id) | ||
end | ||
|
||
render json: format_response(distribution_response) | ||
end | ||
|
||
private | ||
|
||
def pending_establishment(distribution_id) | ||
render json: { id: distribution_id, status: "PENDING_ESTABLISHMENT" }, status: :ok | ||
end | ||
|
||
def format_response(response) | ||
response_body = response.raw_body | ||
|
||
begin | ||
parsed_response = JSON.parse(response_body) | ||
|
||
# Convert keys from camelCase to snake_case | ||
parsed_response.deep_transform_keys do |key| | ||
key.to_s.underscore.gsub(/e(\d)/, 'e_\1') | ||
end | ||
rescue JSON::ParseError => error | ||
log_error(error + " Distribution ID: #{params[:distribution_id]}") | ||
|
||
response_body | ||
end | ||
end | ||
|
||
# Checks if the distribution exists in the database before sending request to Pacman | ||
def valid_id?(distribution_id) | ||
VbmsDistribution.exists?(id: distribution_id) | ||
end | ||
|
||
def distribution_uuid_from_id(pk_id) | ||
VbmsDistribution.find(pk_id).uuid | ||
end | ||
|
||
# Renders errors and logs and tracks the here within Raven | ||
# :reek:FeatureEnvy | ||
def render_error(status, message, distribution_id) | ||
error_uuid = SecureRandom.uuid | ||
error_message = "[IDT] Http Status Code: #{status}, #{message}, (Distribution ID: #{distribution_id})" | ||
Rails.logger.error(error_message.to_s + "Error ID: " + error_uuid) | ||
Raven.capture_exception(error_message, extra: { error_uuid: error_uuid }) | ||
render json: { message: error_message + " #{error_uuid}" }, status: status | ||
end | ||
end |
Oops, something went wrong.