diff --git a/app/services/one_off/cphc_enrollment/diabetes_treatment_payload.rb b/app/services/one_off/cphc_enrollment/diabetes_treatment_payload.rb new file mode 100644 index 0000000000..6c3b18fa9e --- /dev/null +++ b/app/services/one_off/cphc_enrollment/diabetes_treatment_payload.rb @@ -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 diff --git a/app/services/one_off/cphc_enrollment/service.rb b/app/services/one_off/cphc_enrollment/service.rb index c7ca1b345e..95e6531eb2 100644 --- a/app/services/one_off/cphc_enrollment/service.rb +++ b/app/services/one_off/cphc_enrollment/service.rb @@ -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 @@ -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, @@ -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 @@ -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 @@ -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