From 0c165ee7e4fa5a3163d41f2843fd355454ec6418 Mon Sep 17 00:00:00 2001 From: Ronald-pro <36841157+Ronald-pro@users.noreply.github.com> Date: Tue, 30 Apr 2024 09:14:28 +0300 Subject: [PATCH] check on patient details --- routes/processes/nishauri_new.js | 228 ++++++++++++++++--------------- 1 file changed, 118 insertions(+), 110 deletions(-) diff --git a/routes/processes/nishauri_new.js b/routes/processes/nishauri_new.js index cfa753c..18b0897 100644 --- a/routes/processes/nishauri_new.js +++ b/routes/processes/nishauri_new.js @@ -2986,114 +2986,124 @@ async function getVLResults(baseURL, userID, authToken) { } router.get( - "/patient_clinic", - passport.authenticate("jwt", { session: false }), - async (req, res) => { - const user_id = req.query.user_id; - const decodedUserid = base64.decode(user_id); - const authToken = req.header("Authorization").replace("Bearer ", ""); - - try { - const userPrograms = await NUserprograms.findAll({ - where: { - user_id: decodedUserid, - is_active: 1 - } - }); - - const finalJson = { - programs: [] - }; - - const promises = []; - - for (const program of userPrograms) { - const { program_type, program_identifier } = program; - - const programDetails = await NprogramTypes.findOne({ - where: { id: program_type } - }); - - if (!programDetails) { - console.error("Program details not found for program:", program); - continue; - } - - const { name } = programDetails; - - const clientDetails = await Client.findOne({ - where: { id: program_identifier } - }); - - const { mfl_code, clinic_number } = clientDetails; - - let patientFacility = await masterFacility.findOne({ - where: { - code: mfl_code - }, - attributes: ["code", "name"] - }); - - if (!patientFacility) { - console.error("Facility details not found for program:", program); - continue; - } - - const regimenUrl = `${process.env.ART_URL}patient/${clinic_number}/regimen`; - - promises.push(new Promise((resolve, reject) => { - request.get(regimenUrl, (err, res_, body) => { - - let programData; - - try { - programData = JSON.parse(body); - const programItem = programData.message[0] || {}; - finalJson.programs.push({ - name, - facility: patientFacility.name, - patient_observations: programItem - }); - } catch (parseError) { - reject(parseError); - } - - resolve(); - }); - })); - - promises.push(getVLResults( - `${req.protocol}://${req.get("host")}`, - user_id, - authToken - ).then(vlResults => { - const existingProgramIndex = finalJson.programs.findIndex( - p => p.name === name && p.facility === patientFacility.name - ); - - if (existingProgramIndex !== -1) { - if (!finalJson.programs[existingProgramIndex].patient_observations.viral_load) { - finalJson.programs[existingProgramIndex].patient_observations.viral_load = vlResults.result !== undefined - ? vlResults.result.viral_load - : "No VL results available for this patient."; - } - } - }).catch(error => { - console.error("Error fetching VL results:", error); - })); - } - - await Promise.all(promises); - - return res.status(200).json(finalJson); - } catch (error) { - console.error("I want to catch this error!!:", error); - return res.status(500).json({ - success: false, - msg: "Error occurred while fetching patient data" - }); - } - } + "/patient_clinic", + passport.authenticate("jwt", { session: false }), + async (req, res) => { + const user_id = req.query.user_id; + const decodedUserid = base64.decode(user_id); + const authToken = req.header("Authorization").replace("Bearer ", ""); + + const userPrograms = await NUserprograms.findAll({ + where: { + user_id: decodedUserid, + is_active: 1 + } + }); + + const finalJson = { + programs: [] + }; + + const promises = []; + + try { + for (const program of userPrograms) { + const { program_type, program_identifier } = program; + + const programDetails = await NprogramTypes.findOne({ + where: { id: program_type } + }); + + if (!programDetails) { + console.error("Program details not found for program:", program); + continue; + } + + const { name } = programDetails; + + const clientDetails = await Client.findOne({ + where: { id: program_identifier } + }); + + const { mfl_code, clinic_number } = clientDetails; + + let patientFacility = await masterFacility.findOne({ + where: { + code: mfl_code + }, + attributes: ["code", "name"] + }); + + if (!patientFacility) { + console.error("Facility details not found for program:", program); + continue; + } + + const regimenUrl = `${process.env.ART_URL}patient/${clinic_number}/regimen`; + + promises.push( + new Promise((resolve, reject) => { + request.get(regimenUrl, (err, res_, body) => { + let programData; + + try { + programData = JSON.parse(body); + const programItem = programData.message[0] || {}; + finalJson.programs.push({ + name, + facility: patientFacility.name, + patient_observations: programItem + }); + } catch (parseError) { + reject(parseError); + } + + resolve(); + }); + }) + ); + + promises.push( + getVLResults( + `${req.protocol}://${req.get("host")}`, + user_id, + authToken + ) + .then((vlResults) => { + const existingProgramIndex = finalJson.programs.findIndex( + (p) => p.name === name && p.facility === patientFacility.name + ); + + if (existingProgramIndex !== -1) { + if ( + !finalJson.programs[existingProgramIndex].patient_observations + .viral_load + ) { + finalJson.programs[ + existingProgramIndex + ].patient_observations.viral_load = + vlResults.result !== undefined + ? vlResults.result.viral_load + : "No VL results available for this patient."; + } + } + }) + .catch((error) => { + console.error("Error fetching VL results:", error); + }) + ); + } + + await Promise.all(promises); + + return res.status(200).json(finalJson); + } catch (error) { + return res.status(500).json({ + success: false, + msg: "Error occurred while fetching patient data" + }); + } + } ); router.get("/bmi_details", async (req, res) => { @@ -3117,7 +3127,5 @@ router.get("/bmi_details", async (req, res) => { } }); - - module.exports = router; //module.exports = { router, users };