Skip to content

Commit

Permalink
chore: added claim status update and data update to central DB
Browse files Browse the repository at this point in the history
  • Loading branch information
surabhisuman committed May 27, 2023
1 parent 1e72dd1 commit cd83793
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 19 deletions.
20 changes: 15 additions & 5 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions .irb_history
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
IRB
IRB.conf
ls
2 changes: 1 addition & 1 deletion app/controllers/health_care_provider_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def update_docs_and_send_claim_request
claim_id = params[:claim_id]
amount = params[:claims_amount]
claim_type = params[:claim_type]
CentralEntityHelper.add_data_to_health_record(params[:prescriptions], params[:invoices], params[:person_id])
CentralEntityHelper.add_data_to_health_record(params[:prescriptions], params[:invoices], null, params[:person_id])
resp = InsuranceHelper.send_claim_request(claim_id, amount, claim_type)
render json: resp
rescue StandardError => e
Expand Down
14 changes: 10 additions & 4 deletions app/helpers/central_entity_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,19 @@ def get_eligibility(claim_amount, claim_type, customer_id)
# particular claim_type is under covers
end

def add_data_to_health_record(new_prescriptions, new_invoices, customer_id)
def add_data_to_health_record(new_prescriptions, new_invoices, new_claims, customer_id)
health_report = HealthReport.find_by_person_id(customer_id)
invoices = health_report.invoices
invoices.add(new_invoices)
prescriptions = health_report.prescriptions
prescriptions.add(new_prescriptions)
health_report.update(invoices: invoices, prescriptions: prescriptions)
new_invoices.each do |ninv|
invoices.add(Invoice.create(amount: ninv[:amount], health_report: health_report))
end
new_prescriptions.each do |npr|
prescriptions.add(Prescription.create(medicines: npr[:medicines], lab_tests: npr[:lab_tests], health_report: health_report))
end
claims = health_report.claims
claims.add(new_claims)
health_report.update(invoices: invoices, prescriptions: prescriptions, claims: claims)
end

end
Expand Down
27 changes: 18 additions & 9 deletions app/helpers/insurance_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def process_pre_auth(amount, claim_type, requester_id, customer_id)
eligibility = CentralEntityHelper.get_eligibility(amount, claim_type, customer_id).with_indifferent_access
eligible_policy_id = eligibility[:eligible_policy_id]
claim = Claim.create(status: "pre-auth-approved", person_id: customer_id, insurance_policy_id: eligible_policy_id)
return { "success": true, "claim_id": claim.id, msg: "Pre auth claim registered successfully" }
return { "success": true, "claim_id": claim.id, "customer_id": customer_id, msg: "Pre auth claim registered successfully" }
else
NotificationHelper.send_notification(requester_id, customer_id, "Pre Auth Request")
# send notification
Expand All @@ -37,15 +37,24 @@ def send_claim_request(claim_id, amount, claim_type)
end
eligibility = CentralEntityHelper.get_eligibility(amount, claim_type, claim.person_id)
if eligibility[:is_eligible]
claim.update(status: "processing")
#todo: call fraud api
else
new_status = "rejected"
ClaimStatusHistory.create(transition_from: claim.status, transition_to: new_status, claim_id: claim.id)
claim.update(status: new_status)
# todo: create notif & notify customer
updateClaimStatus("processing", claim)
is_fraud = false # todo: replace with gpt call
unless is_fraud
updateClaimStatus("approved", claim)
CentralEntityHelper.add_data_to_health_record([], [], [claim], claim.person_id)
# NotificationHelper.send_notification(claim.insurance_policy.)
return
end
end
#todo: update claim and max coverage to central DB too
self.updateClaimStatus("rejected", claim)
# todo: create notif & notify customer
end

private

def updateClaimStatus(new_status, claim)
ClaimStatusHistory.create(transition_from: claim.status, transition_to: new_status, claim_id: claim.id)
claim.update(status: new_status)
end
end
end
1 change: 1 addition & 0 deletions app/models/health_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ class HealthReport < ApplicationRecord
has_many :invoices
has_many :insurance_policies
has_many :prescriptions
has_many :claims
belongs_to :person
end
72 changes: 72 additions & 0 deletions app/services/fraud_detection_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
class FraudDetectionService
# TODO: add env variable
@@client = OpenAI::Client.new(access_token: ENV.fetch("OPEN_AI_TOKEN"))

MODEL = "gpt-3.5-turbo"

PREFIX = "
Below is a Patient Insurance Claim History, Patient Medical History, Doctor-Patient Transcript, Doctor's Prescription it starts with <PatientMedicalHistory> and ends with </PatientMedicalHistory>
"

INSTRUCTION = "
Based on the Patient Insurance Claim History, Patient Medical History, Doctor-Patient Transcript, Doctor's Prescription provided below, generate a fraud detection response in JSON format:
{
\"approved\": <Yes/No>,
\"confidence\": (0,1),
\"explanation\": <string>
}
"

TEXT = "
<PatientMedicalHistory>
%{medical_history}
</PatientMedicalHistory>
"

def self.detect(medical_history)
result = {
success: false,
approved: nil,
confidence: nil,
summary: nil,
error_message: ""
}

prompt = (PREFIX + "\n" + TEXT + "\n" + INSTRUCTION) % {medical_history: medical_history}

response = @@client.chat(
parameters: {
model: MODEL,
messages: [{ role: "user", content: prompt}],
temperature: 0.7
})

if response.dig("error", "message")
result[:error_message] = response.dig("error", "message")
return result
end

response_text = response.dig("choices", 0, "message", "content")
begin
parsed_response = JSON.parse(response_text)
rescue StandardError => e
puts("ChatGPT Error:", e.message)
parsed_response = {}
end

result[:approved] = parsed_response["approved"]
result[:confidence] = parsed_response["confidence"]
result[:summary] = parsed_response["explanation"]

result[:success] = !(result[:approved].nil? || result[:confidence].nil? || result[:summary].nil?)
return result

# binding.pry
# split_response = response_text.split("\n")
# result[:approved] = split_response.second if split_response.first.downcase == "approved:" && split_response.second.to_s.downcase.in?(["yes", "no"])
# result[:confidence] = split_response.fourth.to_f if split_response.third.downcase == "confidence:" && split_response.fourth.to_s.to_f.to_s == split_response.fourth
# result[:summary] = split_response.last if split_response.fifth.downcase == "explanation:" && split_response.last.to_s == split_response.last
# result[:success] = !(result[:approved].nil? || result[:confidence].nil? || result[:summary].nil?)
# return result
end
end
1 change: 1 addition & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ class Application < Rails::Application
# Middleware like session, flash, cookies can be added back manually.
# Skip views, helpers and assets when generating a new resource.
config.api_only = true
config.autoload_paths << "#{Rails.root}/app/services"
end
end

0 comments on commit cd83793

Please sign in to comment.