UNPKG

happy-client

Version:

A JavaScript client to interact with Happy backend.

245 lines (222 loc) 10.6 kB
const HappyClient = require('./client'); const axios = require('axios'); const dotenv = require('dotenv'); const faker = require('faker'); const fs = require("fs"); dotenv.config(); const test_data_url = process.env.TEST_DATA_URL; const happy_fhir_url = process.env.HAPPY_FHIR_URL; const happy_backend_url = process.env.HAPPY_BACKEND_URL; const client = new HappyClient(happy_backend_url, happy_fhir_url); let rawdata = fs.readFileSync('sampled_transcription.json'); let medical_text_array = Object.values(JSON.parse(rawdata)["transcription"]); function random_text(){ return medical_text_array[Math.floor(Math.random()*medical_text_array.length)]; } async function demodata(resoure_count) { const patient_infos = []; const practitioner_infos = []; let global_auth = null; await client.login("longuyen", "123456").then(function (response) { global_auth = response.headers['authorization']; }); await axios({ url: test_data_url, headers: { "authorization": global_auth } }).then(async function (response) { for (let index in response.data) { const user = response.data[index]; await client.login(user.userName, user.password) .then(function (response) { if (response.data.fhirResourceType === 'Patient') { console.log("Patient: " + JSON.stringify(response.data)); patient_infos.push({ "userName": response.data.userName, "auth": response.headers.authorization, "fhirReference": response.data.fhirReference }); } else { console.log("Practitioner: " + JSON.stringify(response.data)); practitioner_infos.push({ "userName": response.data.userName, "auth": response.headers.authorization, "fhirReference": response.data.fhirReference }); } }); } }).catch(function (error) { console.log(error.response.data); }); for (let i = 0; i < resoure_count; i++) { for (let patient_index in patient_infos) { for (let practitioner_index in practitioner_infos) { const patient = patient_infos[patient_index]; const practitioner = practitioner_infos[practitioner_index]; let medication_ref = null; const medication = client.medicationTemplate( "active", faker.lorem.sentences(3), "2"); await client.create(medication, "Medication", practitioner.auth) .then(function (response) { console.log("Created medication: " + JSON.stringify(response.data) + "\n"); medication_ref = response.data.resourceType + "/" + response.data.id; }).catch(function (error) { console.log("Error: " + JSON.stringify(error.response.data)); }); const medicationDispense = client.medicationDispenseTemplate(patient.fhirReference, practitioner.fhirReference, "preparation", medication_ref, faker.date.past(), random_text(), random_text(), faker.lorem.sentences(1), 2); await client.create(medicationDispense, "MedicationDispense", practitioner.auth) .then(function (response) { console.log("Created medication dispense: " + JSON.stringify(response.data) + "\n"); }).catch(function (error) { console.log("Error: " + JSON.stringify(error.response.data)); }); const appointment = client.appointmentTemplate( patient.fhirReference, practitioner.fhirReference, "proposed", faker.lorem.sentences(1), faker.date.past(), random_text(), faker.lorem.sentences(1)); await client.create(appointment, "Appointment", practitioner.auth) .then(function (response) { console.log("Created appointment: " + JSON.stringify(response.data) + "\n"); }).catch(function (error) { console.log("Error: " + JSON.stringify(error.response.data)); }); const diagnosticReport = client.diagnosticReportTemplate( patient.fhirReference, practitioner.fhirReference, "final", random_text(), faker.lorem.sentences(1)); await client.create(diagnosticReport, "DiagnosticReport", practitioner.auth) .then(function (response) { console.log("Created diagnostic report: " + JSON.stringify(response.data) + "\n"); }).catch(function (error) { console.log("Error: " + JSON.stringify(error.response.data)); }); const encounter = client.encounterTemplate(patient.fhirReference, practitioner.fhirReference, "planned", faker.date.past(), faker.date.future(), random_text(), faker.lorem.sentences(1)); await client.create(encounter, "Encounter", practitioner.auth).then(function (response) { console.log("Created encounter: " + JSON.stringify(response.data) + "\n"); }).catch(function (error) { console.log("Error: " + JSON.stringify(error.response.data)); }); const procedure = client.procedureTemplate(patient.fhirReference, practitioner.fhirReference, "preparation", faker.date.past(), faker.lorem.sentences(1), random_text()); await client.create(procedure, "Procedure", practitioner.auth).then(function (response) { console.log("Created procedure: " + JSON.stringify(response.data) + "\n"); }).catch(function (error) { console.log("Error: " + JSON.stringify(error.response.data)); }); const composition = client.compositionTemplate( patient.fhirReference, "final", faker.date.past(), faker.lorem.sentences(1), random_text(), "rgb(132, 92, 205)"); await client.create(composition, "Composition", patient.auth).then(function (response) { console.log("Created composition: " + JSON.stringify(response.data) + "\n"); }).catch(function (error) { console.log("Error: " + JSON.stringify(error.response.data)); }); const condition = client.conditionTemplate(patient.fhirReference, practitioner.fhirReference, faker.date.past(), random_text(), faker.lorem.sentences(1)); await client.create(condition, "Condition", patient.auth).then(function (response) { console.log("Created condition: " + JSON.stringify(response.data) + "\n"); }).catch(function (error) { console.log("Error: " + JSON.stringify(error.response.data)); }); const medicationRequest = client.medicationRequestTemplate(patient.fhirReference, practitioner.fhirReference, medication_ref, random_text(), faker.lorem.sentences(1), faker.date.past(), faker.date.future()); await client.create(medicationRequest, "MedicationRequest", patient.auth).then(function (response) { console.log("Created medication request: " + JSON.stringify(response.data) + "\n"); }).catch(function (error) { console.log("Error: " + JSON.stringify(error.response.data)); }); const consent = client.consentTemplate( patient.fhirReference, true, false, false, false, random_text(), random_text(), random_text(), random_text()); await client.create(consent, "Consent", patient.auth).then(function (response) { console.log("Created consent: " + JSON.stringify(response.data) + "\n"); }).catch(function (error) { console.log("Error: " + JSON.stringify(error.response.data)); }); const task = client.taskTemplate( patient.fhirReference, random_text(), faker.random.number({min:0,max:365}), "1", random_text(), faker.date.future()); await client.create(task, "Task", patient.auth).then(function (response) { console.log("Created task: " + JSON.stringify(response.data) + "\n"); }).catch(function (error) { console.log("Error: " + JSON.stringify(error.response.data)); }); } } } } demodata(1); module.exports = random_text;