Skip to content

Commit

Permalink
refactor data serailisation
Browse files Browse the repository at this point in the history
  • Loading branch information
surabhisuman committed May 28, 2023
1 parent 93217d3 commit 5998d3a
Show file tree
Hide file tree
Showing 13 changed files with 128 additions and 63 deletions.
73 changes: 36 additions & 37 deletions .idea/workspace.xml

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

6 changes: 3 additions & 3 deletions app/controllers/consents_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ def show
end

def approve_consent
@consent = Consent.create(consultation_id: params[:consultation_id], person_id: params[:person_id], registered_on: Time.now)
notification = Notification.find_by(id: params[:notification_id])
notification.update(notification_type: Notification::TYPE["NOTICE"], data: @notification.data + " Approved")
@consent = Consent.create(consultation_id: params[:consultation_id].to_i, person_id: params[:person_id].to_i, registered_on: Time.now)
# notification = Notification.find_by(id: params[:notification_id].to_i)
# notification.update(notification_type: Notification::TYPE["NOTICE"], data: notification.data + "has been Approved by you.")
render json: @consent
end

Expand Down
12 changes: 6 additions & 6 deletions app/controllers/health_care_provider_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def check_eligibility
render json: resp
rescue StandardError => e
puts(e.message)
render json: {msg: "Bad request"}
render json: {msg: "no data available", is_eligible: false}
end

def send_pre_auth_request
Expand All @@ -32,17 +32,17 @@ def send_pre_auth_request
end

def update_docs_and_send_claim_request
claim_id = params[:claim_id]
amount = params[:amount]
claim_type = params[:claim_type]
claim_id = params[:claim_id].to_i
amount = params[:amount].to_i
claim_type = params[:claim_type].to_s
health_id = params["health_id"].to_s
customer = Person.find_by_health_id(health_id)
eligibility = CentralEntityHelper.get_eligibility(amount, claim_type, customer.id)
resp = InsuranceHelper.send_claim_request(claim_id, amount, eligibility)
claim = Claim.find_by(id: claim_id)
ConsultationHelper.add_data_to_consultation(params[:prescriptions], params[:invoices], [claim], params[:consultation_id])
ConsultationHelper.add_data_to_consultation(params[:prescriptions], params[:invoices], [claim], claim.consultation_id)
#todo: debug low priority why above line wasn't working
consultation = Consultation.find_by(id: params[:consultation_id])
consultation = claim.consultation
CentralEntityHelper.add_data_to_health_record(consultation, customer.id)
render json: resp
rescue StandardError => e
Expand Down
14 changes: 13 additions & 1 deletion app/controllers/notifications_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class NotificationsController < ApplicationController
before_action :set_notification, only: %i[ show update destroy ]
before_action :set_notification, only: %i[ show update destroy approve_notification]

# GET /notifications?person_id
def index
Expand Down Expand Up @@ -42,6 +42,18 @@ def destroy
@notification.destroy
end

def approve_notification
notification_id = params[:id]
approval = params[:approval] # boolean (true/false)
consultation = @notification.consultation
Consent.create(consultation_id: consultation.id, person_id: @notification.person_id, registered_on: Time.now)
if approval
@notification.update(notification_type: Notification::TYPE["NOTICE"], data: @notification.data + " Approved")
else
@notification.update(notification_type: Notification::TYPE["NOTICE"], data: @notification.data + " Rejected")
end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_notification
Expand Down
2 changes: 0 additions & 2 deletions app/controllers/people_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ def destroy
@person.destroy
end



private
# Use callbacks to share common setup or constraints between actions.
def set_person
Expand Down
4 changes: 2 additions & 2 deletions app/helpers/central_entity_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ def get_eligibility(claim_amount, claim_type, customer_id)
return { is_eligible: false } unless health_report
health_report.insurance_policies.each do |policy|
if policy.covers.include?(claim_type.downcase) && claim_amount <= policy.coverage.to_i
return { eligible_policy_id: policy.id, max_coverage_left: policy.coverage, is_eligible: true }
return { eligible_policy_id: policy.id, max_coverage_left: policy.coverage, is_eligible: true, requester: policy.insurer}
end
end
{ is_eligible: false }
{ is_eligible: false, msg: "claim limit exceeds coverage" }
# get uhid of customer
# get health record
# check insurances and it's active status
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/consultation_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def add_data_to_consultation(new_prescriptions, new_invoices, new_claims, consul
claims << nc
end
end
consultation.update(invoices: invoices, prescriptions: prescriptions, claims: claims)
consultation.update(invoices: invoices, prescriptions: prescriptions, claims: claims, status: 'completed')
return consultation
end

Expand Down
34 changes: 27 additions & 7 deletions app/helpers/insurance_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,37 @@ class << self
def process_pre_auth(requester_id, customer_id, eligibility)
health_report = HealthReport.find_by_person_id(customer_id)
# create new consultation at time of pre-auth
consultation = Consultation.create(health_report: health_report)
#todo: stitch some mechanism to create consent when notification is approved or just create consent from seeds
existing_consultations = health_report.consultations || []
consultation = nil
existing_consultations.each do |ec|
if ec.status == 'pre-auth-approved'
consultation = ec
break
end
end
consultation = Consultation.create(health_report: health_report, status: 'pre-auth-approved') if consultation.nil?
consent = Consent.find_by(person_id: customer_id, consultation_id: consultation.id)
#todo: comment next line
# consent = Consent.create(consultation: consultation, person_id: customer_id, registered_on: Time.now)
# a consent is valid for 2 days
if consent && (Time.now-1.day..Time.now+2.days).cover?(consent.registered_on)
# health_report = HealthReport.find_by_person_id(customer_id)
eligible_policy_id = eligibility[:eligible_policy_id]
claim = Claim.create(status: "pre-auth-approved", person_id: customer_id, insurance_policy_id: eligible_policy_id, consultation_id: consultation.id)
existing_claims = consultation.claims || []
claim = nil
existing_claims.each do |ec|
if ec.status == 'pre-auth-approved'
claim = ec
break
end
end
claim = Claim.create(status: "pre-auth-approved", person_id: customer_id, insurance_policy_id: eligible_policy_id, consultation_id: consultation.id) if claim.nil?
return { "success": true, "claim_id": claim.id, "consultation_id": consultation.id, msg: "Pre auth claim registered successfully" }
else
NotificationHelper.send_notification(requester_id, customer_id, "Pre Auth Request", consultation.id, "CONSENT")
# send notification
return { "success": false, "msg": "Consent pending fom customer" }
claim = Claim.find_by(consultation_id: consultation.id, status: "pre-auth-approved")
return { "success": false, "msg": "Consent pending fom customer", "claim_id": claim&.id, "consultation_id": consultation.id }
end
end

Expand All @@ -40,20 +56,24 @@ def send_claim_request(claim_id, amount, eligibility)
if !claim || claim.status != 'pre-auth-approved'
return {"success": false, msg: "customer not authorised, invalid claim status"}
end
fraud_response = nil
if eligibility[:is_eligible]
updateClaimStatus("processing", claim)
insured_policy = InsurancePolicy.find_by_id(eligibility[:eligible_policy_id])
is_fraud = false # todo: replace with gpt call
unless is_fraud
medical_report = ClaimReportGenerator.new(claim.person_id).generate
fraud_response = FraudDetectionService.detect(medical_report)
if fraud_response[:approved] == "Yes"
updateClaimStatus("approved", claim)
updated_coverage = insured_policy.coverage - amount
insured_policy.update(coverage: updated_coverage)
NotificationHelper.send_notification(insured_policy.insurer, claim.person_id, "Claim of Rs." + (amount.to_s) +" approved", claim.consultation_id, "NOTICE")
return {"success": true, "msg": "Claim processed successfully"}
return {"success": true, "msg": "Claim processed successfully", fraud_detector_response: fraud_response}
end
end
updateClaimStatus("rejected", claim)
NotificationHelper.send_notification(insured_policy.insurer, claim.person_id, "Claim of Rs." + amount +" rejected", claim.consultation_id, "NOTICE")
NotificationHelper.send_notification(insured_policy.insurer, claim.person_id, "Claim of Rs." + (amount.to_s) +" rejected", claim.consultation_id, "NOTICE")
return {"success": false, "msg": "Claim rejected", fraud_detector_response: (fraud_response || {"summary": "Not eligible for claim"})}
end

private
Expand Down
4 changes: 2 additions & 2 deletions app/services/claim_report_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ def insurance_claim_history
@claims.each_with_index do |claim, index|
claim_data = CLAIM_HISTORY_TEMPLATE % {
index: index + 1,
year_of_claim: claim.date_of_service.year,
year_of_claim: claim.date_of_service&.year,
insurance_provider: claim.insurance_policy.insurer,
date_of_claim: claim.date_of_service.strftime("%b %d, %Y"),
date_of_claim: claim.date_of_service&.strftime("%b %d, %Y"),
health_care_provider: claim.provider,
diagnosis: claim.diagnosis,
procedure: claim.procedure,
Expand Down
31 changes: 30 additions & 1 deletion app/services/fraud_detection_service.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
class FraudDetectionService
# TODO: add env variable
@@client = OpenAI::Client.new(access_token: ENV.fetch("OPEN_AI_TOKEN"))
binding.pry

MODEL = "gpt-3.5-turbo"

Expand Down Expand Up @@ -70,4 +69,34 @@ def self.detect(medical_history)
# result[:success] = !(result[:approved].nil? || result[:confidence].nil? || result[:summary].nil?)
# return result
end

def self.mock_response_success(medical_history)
return {
success: true,
approved: "Yes",
confidence: "0.99",
summary: "This is approved as it is mocked",
error_message: ""
}
end

def self.mock_response_error(medical_history)
return {
success: false,
approved: nil,
confidence: nil,
summary: nil,
error_message: "Mocked error"
}
end

def self.mock_response_fail(medical_history)
return {
success: true,
approved: "No",
confidence: "0.99",
summary: "This is rejected as it is mocked",
error_message: ""
}
end
end
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@
#
#
post '/approve_consent', to: "consents#approve_consent"
post '/notification/:id/approve', to: "notifications#approve_notification"
end
5 changes: 5 additions & 0 deletions db/migrate/20230528035801_add_status_to_consultation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddStatusToConsultation < ActiveRecord::Migration[7.0]
def change
add_column :consultations, :status, :string
end
end
Loading

0 comments on commit 5998d3a

Please sign in to comment.