Skip to content

Commit

Permalink
fixed apis for new schema and added seeds
Browse files Browse the repository at this point in the history
  • Loading branch information
surabhisuman committed May 27, 2023
1 parent 983b447 commit 789a285
Show file tree
Hide file tree
Showing 36 changed files with 583 additions and 390 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@

# Ignore master key for decrypting credentials and more.
/config/master.key
.idea
.idea/
328 changes: 36 additions & 292 deletions .idea/health-helper.iml

Large diffs are not rendered by default.

75 changes: 61 additions & 14 deletions .idea/workspace.xml

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

2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ gem "puma", "~> 5.0"

gem "ruby-openai"

gem "pry"

# Build JSON APIs with ease [https://github.com/rails/jbuilder]
# gem "jbuilder"

Expand Down
5 changes: 5 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ GEM
bootsnap (1.16.0)
msgpack (~> 1.2)
builder (3.2.4)
coderay (1.1.3)
concurrent-ruby (1.2.2)
crass (1.0.6)
date (3.3.3)
Expand Down Expand Up @@ -115,6 +116,9 @@ GEM
nio4r (2.5.9)
nokogiri (1.15.2-arm64-darwin)
racc (~> 1.4)
pry (0.14.2)
coderay (~> 1.1)
method_source (~> 1.0)
puma (5.6.5)
nio4r (~> 2.0)
racc (1.6.2)
Expand Down Expand Up @@ -171,6 +175,7 @@ PLATFORMS
DEPENDENCIES
bootsnap
debug
pry
puma (~> 5.0)
rails (~> 7.0.5)
ruby-openai
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/consents_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ def set_consent

# Only allow a list of trusted parameters through.
def consent_params
params.fetch(:consent, {})
params.fetch(:consent)
end
end
20 changes: 16 additions & 4 deletions app/controllers/health_care_provider_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
require_relative '../helpers/consultation_helper'
require_relative '../helpers/central_entity_helper'
class HealthCareProviderController < ApplicationController
extend ConsultationHelper
extend CentralEntityHelper

# checks eligibility for current medication/to be claim
def check_eligibility
Expand All @@ -17,9 +21,10 @@ def send_pre_auth_request
amount = params[:amount].to_i
claim_type = params["claim_type"].to_s
requester_id = params["requester_id"]
health_id = params[:health_id].to_s
health_id = params["health_id"].to_s
customer = Person.find_by_health_id(health_id)
resp = InsuranceHelper.process_pre_auth(amount, claim_type, requester_id, customer.id)
eligibility = CentralEntityHelper.get_eligibility(amount, claim_type, customer.id).with_indifferent_access
resp = InsuranceHelper.process_pre_auth(requester_id, customer.id, eligibility)
render json: resp
rescue StandardError => e
puts(e.message)
Expand All @@ -30,8 +35,15 @@ def update_docs_and_send_claim_request
claim_id = params[:claim_id]
amount = params[:amount]
claim_type = params[:claim_type]
CentralEntityHelper.add_data_to_health_record(params[:prescriptions], params[:invoices], nil, params[:person_id])
resp = InsuranceHelper.send_claim_request(claim_id, amount, claim_type)
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])
#todo: debug low priority why above line wasn't working
consultation = Consultation.find_by(id: params[:consultation_id])
CentralEntityHelper.add_data_to_health_record(consultation, customer.id)
render json: resp
rescue StandardError => e
puts(e.message)
Expand Down
8 changes: 7 additions & 1 deletion app/controllers/notifications_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ class NotificationsController < ApplicationController

# GET /notifications
def index
@notifications = Notification.all
# fetch all notifications, delete all notification in current scope, send notification over http
# wait for next request to come in. one more thing, FE pe state nahi kar payneg manage. no code tool very limited. yes. ok
if params[:person_id].present?
@notifications = Notification.where(person_id: params[:person_id])
else
@notifications = Notification.all
end

render json: @notifications
end
Expand Down
20 changes: 4 additions & 16 deletions app/helpers/central_entity_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,11 @@ 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, new_claims, customer_id)
def add_data_to_health_record(consultation, customer_id)
health_report = HealthReport.find_by_person_id(customer_id)
invoices = health_report.invoices.to_a
prescriptions = health_report.prescriptions.to_a
new_invoices.each do |ninv|
invoices << Invoice.create(amount: ninv[:amount], health_report: health_report)
end
new_prescriptions.each do |npr|
prescriptions << Prescription.create(medicines: npr[:medicines], lab_tests: npr[:lab_tests], health_report: health_report)
end
claims = health_report.claims.to_a
if new_claims
new_claims.each do |nc|
claims << nc
end
end
health_report.update(invoices: invoices, prescriptions: prescriptions, claims: claims)
existing_consultations = health_report.consultations || []
existing_consultations << consultation
health_report.update(consultations: existing_consultations)
end

end
Expand Down
28 changes: 28 additions & 0 deletions app/helpers/consultation_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module ConsultationHelper
class << self

def add_data_to_consultation(new_prescriptions, new_invoices, new_claims, consultation_id)
consultation = Consultation.find_by_id(consultation_id)
invoices = consultation.invoices.to_a
prescriptions = consultation.prescriptions.to_a
new_invoices.each do |ninv|
invoices << Invoice.create(amount: ninv[:amount], consultation: consultation)
end
new_prescriptions.each do |npr|
prescriptions << Prescription.create(medicines: npr[:medicines], lab_tests: npr[:lab_tests], consultation: consultation)
end
claims = consultation.claims.to_a
if new_claims
new_claims.each do |nc|
claims << nc
end
end
consultation.update(invoices: invoices, prescriptions: prescriptions, claims: claims)
return consultation
end

def create_or_update_consultation(health_report) # update todo
Consultation.create(health_report: health_report)
end
end
end
25 changes: 14 additions & 11 deletions app/helpers/insurance_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,22 @@ class << self
# if consent doesn't exist - register customer consent
# if approved, store customer health data [later can be made accessible via api only for a limited time]
# and register claim in database with pre-auth status
def process_pre_auth(amount, claim_type, requester_id, customer_id)
consent = Consent.find_by(person_id: customer_id, requested_by: requester_id)
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
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)
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, "customer_id": customer_id, msg: "Pre auth claim registered successfully" }
claim = Claim.create(status: "pre-auth-approved", person_id: customer_id, insurance_policy_id: eligible_policy_id, consultation_id: consultation.id)
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")
NotificationHelper.send_notification(requester_id, customer_id, "Pre Auth Request", consultation.id)
# send notification
return { "success": false, "msg": "Consent pending fom customer" }
end
Expand All @@ -29,13 +34,12 @@ def process_pre_auth(amount, claim_type, requester_id, customer_id)
# if fraud engine validates
# mark claim as success
# send notif to customer
def send_claim_request(claim_id, amount, claim_type)
def send_claim_request(claim_id, amount, eligibility)
#todo: add claim type to claim model
claim = Claim.find_by(id: claim_id)
if !claim || claim.status != 'pre-auth-approved'
return {"success": false, msg: "customer not authorised, invalid claim status"}
end
eligibility = CentralEntityHelper.get_eligibility(amount, claim_type, claim.person_id)
if eligibility[:is_eligible]
updateClaimStatus("processing", claim)
insured_policy = InsurancePolicy.find_by_id(eligibility[:eligible_policy_id])
Expand All @@ -44,13 +48,12 @@ def send_claim_request(claim_id, amount, claim_type)
updateClaimStatus("approved", claim)
updated_coverage = insured_policy.coverage - amount
insured_policy.update(coverage: updated_coverage)
CentralEntityHelper.add_data_to_health_record([], [], [claim], claim.person_id)
NotificationHelper.send_notification(insured_policy.insurer, claim.person_id, "Claim of Rs." + (amount.to_s) +" approved")
NotificationHelper.send_notification(insured_policy.insurer, claim.person_id, "Claim of Rs." + (amount.to_s) +" approved", claim.consultation_id)
return {"success": true, "msg": "Claim processed successfully"}
end
end
updateClaimStatus("rejected", claim)
NotificationHelper.send_notification(insured_policy.insurer, claim.person_id, "Claim of Rs." + amount +" rejected")
NotificationHelper.send_notification(insured_policy.insurer, claim.person_id, "Claim of Rs." + amount +" rejected", claim.consultation_id)
end

private
Expand Down
Loading

0 comments on commit 789a285

Please sign in to comment.