Skip to content

Commit

Permalink
Updated the service to save participant_outcome_state
Browse files Browse the repository at this point in the history
  • Loading branch information
javier-npq committed Jun 23, 2023
1 parent 222015c commit 97a28c3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 41 deletions.
44 changes: 30 additions & 14 deletions app/lib/services/ecf/ecf_lead_provider_approval_status_reciever.rb
Original file line number Diff line number Diff line change
@@ -1,28 +1,44 @@
module Services
module Ecf
class EcfLeadProviderApprovalStatusReciever
class EcfLeadProviderApprovalStatusReceiver
def call
uri = URI.parse("#{ENV['ECF_APP_BASE_URL']}/api/v3/npq-applications/send_lead_provider_approval_status_to_npq")
uri = URI.parse("#{ENV['ECF_APP_BASE_URL']}/api/v1/npq_synchronisation/send_lead_provider_approval_status_to_npq")
request = Net::HTTP::Get.new(uri)
request["Authorization"] = "Bearer #{ENV['ECF_APP_BEARER_TOKEN']}"

response = Net::HTTP.start(uri.hostname, uri.port) do |http|
http.request(request)
end

handle_response(response)
rescue StandardError => e
Rails.logger.error "An error occurred during lead provider approval status retrieval: #{e.message}"
end

private

def handle_response(response)
if response.is_a?(Net::HTTPSuccess)
response_data = JSON.parse(response.body)
filtered_applications = Application.where.not(ecf_id: nil)
response_data["data"].map do |status_data|
retrieved_id = status_data["attributes"]["id"]
retrieved_status = status_data["attributes"]["lead_provider_approval_status"]
application = filtered_applications.find_by(ecf_id: retrieved_id)
application.update!(lead_provider_approval_status: retrieved_status) if application.present?
end
update_applications(JSON.parse(response.body)["data"])
else
response_message = response.message
raise "Failed to retrieve lead provider approval status: #{response_message}"
raise "Failed to retrieve lead provider approval status: #{response.message}"
end
end

def update_applications(status_data)
filtered_applications = Application.where.not(ecf_id: nil)

status_data.each do |data|
retrieved_id = data["attributes"]["id"]
retrieved_status = data["attributes"]["lead_provider_approval_status"]
retrieved_state = data["attributes"]["participant_outcome_state"]

application = filtered_applications.find_by(ecf_id: retrieved_id)
application&.update!(
lead_provider_approval_status: retrieved_status,
participant_outcome_state: retrieved_state,
)
end
rescue StandardError => e
Rails.logger.error "An error occurred during lead provider approval status retrieval: #{e.message}"
end
end
end
Expand Down
27 changes: 0 additions & 27 deletions app/models/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,31 +37,4 @@ def new_headteacher?
def school
School.find_by(urn: school_urn)
end
<<<<<<< HEAD

def receive_lead_provider_approval_status_from_ecf
uri = URI.parse("#{ENV['ECF_APP_BASE_URL']}/api/v3/npq-applications/send_lead_provider_approval_status_to_npq")
request = Net::HTTP::Get.new(uri)
request["Authorization"] = "Bearer #{ENV['ECF_APP_BEARER_TOKEN']}"
response = Net::HTTP.start(uri.hostname, uri.port) do |http|
http.request(request)
end
if response.is_a?(Net::HTTPSuccess)
response_data = JSON.parse(response.body)
filtered_applications = Application.where.not(ecf_id: nil)
response_data["data"].map do |status_data|
retrieved_id = status_data["attributes"]["id"]
retrieved_status = status_data["attributes"]["lead_provider_approval_status"]
application = filtered_applications.find_by(ecf_id: retrieved_id)
application.update!(lead_provider_approval_status: retrieved_status) if application.present?
end
else
response_message = response.message
raise "Failed to retrieve lead provider approval status: #{response_message}"
end
rescue StandardError => e
Rails.logger.error "An error occurred during lead provider approval status retrieval: #{e.message}"
end
=======
>>>>>>> 35f455f8 (Created scheduled background job)
end

0 comments on commit 97a28c3

Please sign in to comment.