UNPKG

happy-client

Version:

A JavaScript client to interact with Happy backend.

239 lines (215 loc) 9.12 kB
const HappyClient = require('./client'); const uuidv5 = require('uuid/v5'); const uuidv1 = require('uuid/v1'); const dotenv = require('dotenv'); dotenv.config(); 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); const faker = require('faker'); faker.locale = "de"; function uuid() { return uuidv5("Unit test", uuidv1()).replace("-", "") + uuidv5("Unit test", uuidv1()).replace("-", "") + uuidv5("Unit test", uuidv1()).replace("-", ""); } async function stress_test(patient_numbers, practitioner_numbers, resource_numbers, reading_requests) { const patient_infos = []; const practitioner_infos = []; for (let i = 0; i < patient_numbers; i++) { const userName = uuid(); const password = uuid(); await client.registerPatient(uuid(), // City uuid(), // Country uuid(), // Postal code uuid(), // State uuid(), // Text "1970-01-01", false, uuid(), // Family "unknown", uuid(), // Given uuid(), // Telecom uuid(), // Email uuid(), // Insurance uuid(), // Pension userName, password).then(async function (response) { patient_infos.push({ "userName": userName, "password": password, "fhirReference": response.data.fhirReference, "fhirResourceId": response.data.fhirResourceId }); console.log("Created new test patient at: " + response.data.fhirReference); }); } for (let i = 0; i < practitioner_numbers; i++) { const userName = uuid(); const password = uuid(); await client.registerPractitioner(uuid(), // City uuid(), // Country uuid(), // Postal code uuid(), // State uuid(), // Text "1970-01-01", false, uuid(), // Family "unknown", uuid(), // Given uuid(), // Telecom uuid(), // Email uuid(), // Insurance uuid(), // Pension userName, password).then(async function (response) { practitioner_infos.push({ "userName": userName, "password": password, "fhirReference": response.data.fhirReference, "fhirResourceId": response.data.fhirResourceId }); console.log("Created new test practitioner at: " + response.data.fhirReference); }); } for (let index in patient_infos) { let user_info = patient_infos[index]; const userName = user_info.userName; const password = user_info.password; await client.login(userName, password).then(async function (response) { const auth = response.headers.authorization; user_info.auth = auth; console.log("User logged in and obtained authorization token: " + auth.substring(0, 10)); }); } for (let i = 0; i < patient_numbers; i++) { const patient = patient_infos[i]; for (let j = 0; j < practitioner_numbers; j++) { const practitioner = practitioner_infos[j]; await client .grantAccess(patient.userName, practitioner.userName, true, "1970-01-01", "1980-01-01", patient.auth) .then(async function (response) { console.log("Granted access on patient to practitioner: " + response.data); }); } } for (let k = 0; k < resource_numbers; k++) { for (let index in patient_infos) { let user_info = patient_infos[index]; const userName = user_info.userName; const password = user_info.password; await client.login(userName, password).then(async function (response) { const auth = response.headers.authorization; user_info.auth = auth; console.log("User logged in and obtained authorization token: " + auth.substring(0, 10)); }); } for (let index in patient_infos) { const patient = patient_infos[index]; const practitioner = practitioner_infos[0]; let medication_ref = null; const medication = client.medicationTemplate( "active", faker.lorem.sentences(1)); await client.create(medication, "Medication", patient.auth) .then(function (response) { console.log("Created appointment: " + JSON.stringify(response.data)); medication_ref = response.data.resourceType + "/" + response.data.id; }).catch(function (error) { console.log("Error"); }); console.log(practitioner.auth); const medicationDispense = client.medicationDispenseTemplate(patient.fhirReference, practitioner.fhirReference, "preparation", medication_ref, faker.date.past(), faker.lorem.sentences(3), faker.lorem.sentences(3)); await client.create(medicationDispense, "MedicationDispense", patient.auth) .then(function (response) { console.log("Created medication dispense: " + JSON.stringify(response.data)); }).catch(function (error) { console.log("Error"); }); const appointment = client.appointmentTemplate( patient.fhirReference, practitioner.fhirReference, "proposed", faker.lorem.sentences(1), faker.date.past(), faker.lorem.sentences(3)); await client.create(appointment, "Appointment", patient.auth) .then(function (response) { console.log("Created appointment: " + JSON.stringify(response.data)); }).catch(function (error) { console.log("Error"); }); const diagnosticReport = client.diagnosticReportTemplate( patient.fhirReference, practitioner.fhirReference, "final", faker.lorem.sentences(3)); await client.create(diagnosticReport, "DiagnosticReport", patient.auth) .then(function (response) { console.log("Created diagnostic report: " + JSON.stringify(response.data)); }).catch(function (error) { console.log("Error"); }); const encounter = client.encounterTemplate(patient.fhirReference, practitioner.fhirReference, "planned", faker.date.past(), faker.date.future(), faker.lorem.sentences(3)); await client.create(encounter, "Encounter", patient.auth).then(function (response) { console.log("Created encounter: " + JSON.stringify(response.data)); }).catch(function (error) { console.log("Error"); }); const procedure = client.procedureTemplate(patient.fhirReference, practitioner.fhirReference, "preparation", faker.date.past(), faker.lorem.sentences(1), faker.lorem.sentences(3)); await client.create(procedure, "Procedure", patient.auth).then(function (response) { console.log("Created procedure: " + JSON.stringify(response.data)); }).catch(function (error) { console.log("Error"); }); const composition = client.compositionTemplate(patient.fhirReference, "superseded", faker.date.past(), faker.lorem.sentences(1), faker.lorem.sentences(3)); await client.create(composition, "Composition", patient.auth).then(function (response) { console.log("Created composition: " + JSON.stringify(response.data)); }).catch(function (error) { console.log("Error"); }); } for (let j = 0; j < reading_requests; j++) { for (let index in patient_infos) { const user_info = patient_infos[index]; await client.readPatientEverything(user_info.fhirResourceId, user_info.auth).then(function (response) { console.log(response.data); }); } } } } stress_test(1, 1, 1, 0); module.exports = stress_test;