Skip to content

Commit

Permalink
Send diabetes treatment payload to CPHC (#5341)
Browse files Browse the repository at this point in the history
**Story card:**
[sc-11383](https://app.shortcut.com/simpledotorg/story/11383/data-migration-for-pune-and-pimpri-chinchwad)

## Because

It was identified earlier that since we have not sent diabetes treatment
payloads to CPHC, the patients have not been marked as under treatment
in their system. We are fixing this with this current migration.

## This addresses

Adds a request to the diabetes treatment api of CPHC
  • Loading branch information
govindkrjoshi authored Dec 19, 2023
1 parent ab74f49 commit a57beef
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 7 deletions.
65 changes: 65 additions & 0 deletions app/services/one_off/cphc_enrollment/diabetes_treatment_payload.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
class OneOff::CphcEnrollment::DiabetesTreatmentPayload
attr_reader :blood_sugar, :patient, :prescription_drugs, :appointment, :encounter_id

def initialize(blood_sugar, prescription_drugs, appointment, encounter_id)
@blood_sugar = blood_sugar
@patient = blood_sugar.patient
@prescription_drugs = prescription_drugs
@appointment = appointment
@encounter_id = encounter_id
end

def payload
medical_history = patient.medical_history
treatment_date = blood_sugar.recorded_at.strftime("%d-%m-%Y")
blood_sugar_payload = OneOff::CphcEnrollment::BloodSugarPayload.new(blood_sugar)

payload = {
encounterId: encounter_id,
treat: {
reviewIn: {
isProtocolOverriden: false,
facility: "DH",
treatDate: treatment_date,
reviewDate: treatment_date,
conDn: true,
remarks: nil,
removeCurrMedication: true,
treatDateWithTimeStamp: blood_sugar.recorded_at.to_i,
diabetesDiagnosisModel: {
date: treatment_date,
selectedDiagnosis: medical_history.diabetes ? "CONFIRMED" : "NAD"
}
}
}
}

payload[:treat][:reviewIn].merge!(blood_sugar_payload.blood_sugar_type_payload)

if appointment.present?
payload[:treat][:docfolUp] = {
followUpDueDate: appointment.scheduled_date.strftime("%d-%m-%Y"),
followUpDuration: "#{appointment.follow_up_days};0;0",
followUpReason: "Clinical Examination"
}
end

if prescription_drugs.present?
payload[:treat][:reviewIn][:selMeds] = prescription_drugs.map do |prescription_drug|
{
dose: nil,
freq: "Once a day",
name: prescription_drug.name,
quan: appointment.present? ? appointment.follow_up_days : 30,
dur: appointment.present? ? appointment.follow_up_days : 30
}
end
end

payload
end

def headers
{"Host" => ENV.fetch("CPHC_DIABETES_HOST")}
end
end
73 changes: 66 additions & 7 deletions app/services/one_off/cphc_enrollment/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ def add_blood_pressure(blood_pressure)

def add_blood_sugar(blood_sugar)
log = CphcMigrationAuditLog.find_by(cphc_migratable: blood_sugar)

if log.present?
logger.error "Blood pressure already migrated to CPHC", blood_sugar
return
Expand All @@ -205,6 +206,25 @@ def add_blood_sugar(blood_sugar)

measurement_id = JSON.parse(response.body.to_s)["encounterId"]

encounter_date = blood_sugar.device_created_at.to_date
range = encounter_date.beginning_of_day..encounter_date.end_of_day

prescription_drugs =
patient
.prescription_drugs
.left_outer_joins(:cphc_migration_audit_log)
.where(cphc_migration_audit_log: {id: nil})
.where(device_created_at: range)

appointment =
patient
.appointments
.left_outer_joins(:cphc_migration_audit_log)
.where(cphc_migration_audit_log: {id: nil})
.where(device_created_at: range)
.order(device_created_at: :desc)
.first

CphcMigrationAuditLog.create(
cphc_migratable: blood_sugar,
facility_id: facility.id,
Expand All @@ -215,10 +235,44 @@ def add_blood_sugar(blood_sugar)
cphc_location: cphc_location
}
)
unless log.present?
make_post_request(
blood_sugar,
diagnosis_path(:diabetes, @diabetes_examination_id),
OneOff::CphcEnrollment::DiabetesDiagnosisPayload.new(blood_sugar, measurement_id)
)
end

make_post_request(
blood_sugar,
diagnosis_path(:diabetes, @diabetes_examination_id),
OneOff::CphcEnrollment::DiabetesDiagnosisPayload.new(blood_sugar, measurement_id)
treatment_path(:diabetes, diabetes_examination_id),
OneOff::CphcEnrollment::DiabetesTreatmentPayload.new(
blood_sugar,
prescription_drugs,
appointment,
measurement_id
)
)
prescription_drugs.each do |prescription_drug|
CphcMigrationAuditLog.create(
cphc_migratable: prescription_drug,
facility_id: facility.id,
metadata: {
patient_id: patient.id,
individual_id: @individual_id,
cphc_location: cphc_location
}
)
end

CphcMigrationAuditLog.create(
cphc_migratable: appointment,
facility_id: facility.id,
metadata: {
patient_id: patient.id,
individual_id: @individual_id,
cphc_location: cphc_location
}
)
end

Expand Down Expand Up @@ -287,7 +341,12 @@ def diagnosis_path(diagnosis, screening_id)

def treatment_path(diagnosis, screening_id)
diagnosis_id = CONFIG[diagnosis][:diagnosis_id]
"#{CPHC_BASE_PATH}/condition/#{diagnosis_id}/individual/#{individual_id}/program/1/examination/#{screening_id}/treatment"
case diagnosis
when :hypertension
"#{CPHC_BASE_PATH}/condition/#{diagnosis_id}/individual/#{individual_id}/program/1/examination/#{screening_id}/treatment"
when :diabetes
"#{CPHC_BASE_PATH}/condition/#{diagnosis_id}/individual/#{individual_id}/examination/#{screening_id}/facility/#{user[:facility_type_id]}/treatment"
end
end

def logger
Expand All @@ -307,10 +366,10 @@ def cphc_location

query =
case cphc_facility.cphc_facility_type
when "SUBCENTER"
{cphc_subcenter_id: cphc_facility.cphc_facility_id}
when "PHC"
{cphc_phc_id: cphc_facility.cphc_facility_id}
when "SUBCENTER"
{cphc_subcenter_id: cphc_facility.cphc_facility_id}
when "PHC"
{cphc_phc_id: cphc_facility.cphc_facility_id}
end

potential_match = CphcFacilityMapping
Expand Down

0 comments on commit a57beef

Please sign in to comment.