happy-client
Version:
A JavaScript client to interact with Happy backend.
1,216 lines (1,167 loc) • 45 kB
JavaScript
'use strict';
const axios = require('axios');
const normalizeUrl = require('./clientutils').normalizeUrl;
class HappyClient {
/**
* Constructor. Takes an authorization API and a FHIR API
*/
constructor(happy_backend_url, happy_fhir_url) {
if (!happy_fhir_url || !happy_backend_url) {
throw new Error("Parameter is empty. Please check your input.");
}
happy_backend_url = happy_backend_url.trim();
happy_fhir_url = happy_fhir_url.trim();
if (happy_backend_url.endsWith("/")) {
happy_backend_url = happy_backend_url.substring(0, happy_backend_url.length - 1);
}
if (happy_fhir_url.endsWith("/")) {
happy_fhir_url = happy_fhir_url.substring(0, happy_fhir_url.length - 1);
}
this.happy_backend_url = happy_backend_url;
this.happy_fhir_url = happy_fhir_url;
this.happy_media_url = "https://media.happy.long-nguyen.de";
}
/**
* Avatar url
* @param userName avatar url
* @returns {string} avatar url
*/
avatarUrl(userName) {
return normalizeUrl(this.happy_media_url, "/avatar", userName);
}
/**
* Register a new practitioner. The data like address city, address country and address state
* will be stored on the Fhir database. Only user name and password will be persisted on the authorization server.
*
* @param addressCity city name
* @param addressCountry country name
* @param addressPostalCode postal code
* @param addressState state name
* @param addressText address in street and number
* @param birthDate birth date, for example 2019-10-07T18:03:52.168Z
* @param deceasedBoolean is the user dead?
* @param family family name
* @param gender gender: male, female, unknown
* @param given first name
* @param telecomValue telephone
* @param emailAddress email address of the user
* @param insuranceNumber insurance number of the user
* @param pensionNumber pension number of user. Not to confuse with insurance number
* @param password password
* @param userName
* @returns {Promise<Response>}
*/
registerPractitioner(addressCity,
addressCountry,
addressPostalCode,
addressState,
addressText,
birthDate,
deceasedBoolean,
family,
gender,
given,
telecomValue,
emailAddress,
insuranceNumber,
pensionNumber,
userName,
password
) {
if (!addressCity
|| !addressCountry
|| !addressPostalCode
|| !addressState
|| !birthDate
|| !family
|| !gender
|| !given
|| !telecomValue
|| !emailAddress
|| !userName
|| !insuranceNumber
|| !pensionNumber
|| !password) {
throw new Error("Empty parameter detected.");
}
return axios({
method: 'POST',
url: normalizeUrl(this.happy_backend_url, "/user/register/practitioner"),
headers: {
"Accept": "application/json",
"Content-Type": "application/json"
},
data: {
"addressCity": addressCity,
"addressCountry": addressCountry,
"addressPostalCode": addressPostalCode,
"addressState": addressState,
"addressText": addressText,
"birthDate": birthDate,
"deceasedBoolean": deceasedBoolean,
"family": family,
"gender": gender,
"given": given,
"password": password,
"telecomValue": telecomValue,
"emailAddress": emailAddress,
"insuranceNumber": insuranceNumber,
"pensionNumber": pensionNumber,
"userName": userName,
}
});
}
/**
* Register a new patient. The data like address city, address country and address state
* will be stored on the Fhir database. Only user name and password will be persisted on the authorization server.
*
* @param addressCity city name
* @param addressCountry country name
* @param addressPostalCode postal code
* @param addressState state name
* @param addressText address in street and number
* @param birthDate birth date, for example 2019-10-07T18:03:52.168Z
* @param deceasedBoolean is the user dead?
* @param family family name
* @param gender gender: male, female, unknown
* @param given first name
* @param telecomValue telephone
* @param emailAddress email address of the user
* @param insuranceNumber insurance number of the user
* @param pensionNumber pension number of user. Not to confuse with insurance number
* @param password password
* @param userName
* @returns {Promise<Response>}
*/
registerPatient(addressCity,
addressCountry,
addressPostalCode,
addressState,
addressText,
birthDate,
deceasedBoolean,
family,
gender,
given,
telecomValue,
emailAddress,
insuranceNumber,
pensionNumber,
userName,
password) {
if (!addressCity
|| !addressCountry
|| !addressPostalCode
|| !addressState
|| !birthDate
|| !family
|| !gender
|| !given
|| !telecomValue
|| !userName
|| !password
|| !insuranceNumber
|| !pensionNumber
|| !emailAddress) {
throw new Error("Empty parameter detected.");
}
return axios({
url: normalizeUrl(this.happy_backend_url, "/user/register/patient"),
method: 'POST',
headers:
{
"Accept": "application/json",
"Content-Type": "application/json"
}
,
data: {
"addressCity": addressCity,
"addressCountry": addressCountry,
"addressPostalCode": addressPostalCode,
"addressState": addressState,
"addressText": addressText,
"birthDate": birthDate,
"deceasedBoolean": deceasedBoolean,
"family": family,
"gender": gender,
"given": given,
"password": password,
"insuranceNumber": insuranceNumber,
"pensionNumber": pensionNumber,
"telecomValue": telecomValue,
"emailAddress": emailAddress,
"userName": userName,
}
});
}
/**
* Log user in and obtain authorization token. THe token will be in the response.headers['authorization']
*
* @param userName user name
* @param password password
* @returns {Promise<Response>}
*/
login(userName, password) {
if (!userName || !password) {
throw new Error("Empty parameter detected.");
}
return axios({
url: normalizeUrl(this.happy_backend_url, "/user/login"),
method: 'POST',
headers: {
"Accept": "application/json",
"Content-Type": "application/json"
},
data: {
"password": password,
"userName": userName
}
});
}
/**
* Log an user on server side out. The token will be blacklisted and can not be used any more.
*
* @param authorizationToken Current using authorization token
* @returns {Promise<Response>}
*/
logout(authorizationToken) {
if (!authorizationToken) {
throw new Error("Empty parameter detected.");
}
return axios({
url: normalizeUrl(this.happy_backend_url, "/user/logout"),
method: 'POST',
headers: {
"Accept": "application/json",
"Content-Type": "application/json",
"Authorization": authorizationToken
}
});
}
/**
* Create a Fhir resource. The resource body is a JSON object, which should conform with the Fhir's standard.
*
* @param resourceBody payload
* @param resourceType resource type
* @param authorizationToken authorization token
* @returns {Promise<Response>}
*/
create(resourceBody, resourceType, authorizationToken) {
if (!resourceBody || !resourceType || !authorizationToken) {
throw new Error("Empty parameter detected.");
}
return axios({
url: normalizeUrl(this.happy_fhir_url, resourceType),
method: 'POST',
headers: {
"Accept": "application/json",
"Content-Type": "application/json",
"Authorization": authorizationToken
},
data: resourceBody
});
}
/**
* Update an existing Fhir resource. The resource body is a JSON object, which should conform with the Fhir's standard.
* The resource should already exist on server and can only be edited by owner.
*
* @param resourceBody
* @param resourceType
* @param resourceId
* @param authorizationToken
* @returns {AxiosPromise}
*/
update(resourceBody, resourceType, resourceId, authorizationToken) {
if (!resourceBody || !resourceType || !resourceId || !authorizationToken) {
throw new Error("Empty parameter detected");
}
if (!resourceBody['id']) {
resourceBody['id'] = resourceId;
}
if (!resourceBody['resourceType']) {
resourceBody['resourceType'] = resourceType;
}
return axios({
url: normalizeUrl(this.happy_fhir_url, resourceType, resourceId),
method: 'PUT',
headers: {
"Accept": "application/json",
"Content-Type": "application/json",
"Authorization": authorizationToken
},
data: resourceBody
});
}
/**
* Update an existing Fhir resource. The resource body is a JSON object, which should conform with the Fhir's standard.
* The resource should already exist on server and can only be edited by owner.
*
* @param resourceBody
* @param resourceType
* @param resourceId
* @param authorizationToken
* @returns {AxiosPromise}
*/
delete(resourceType, resourceId, authorizationToken) {
if (!resourceType || !resourceId || !authorizationToken) {
throw new Error("Empty parameter detected");
}
return axios({
url: normalizeUrl(this.happy_fhir_url, resourceType, resourceId),
method: 'DELETE',
headers: {
"Accept": "application/json",
"Content-Type": "application/json",
"Authorization": authorizationToken
}
});
}
/**
* Forgot password. Enter a valid user name and his associating email will be sent with a link to reset his password.
*/
forgotPassword(username) {
if (!username) {
throw new Error("Empty parameter detected!");
}
return axios({
url: normalizeUrl(this.happy_backend_url, "user", "forgetpassword", username),
method: "GET",
headers: {
"Accept": "application/json",
"Content-Type": "application/json",
}
});
}
/**
* Change passwod. After received a link to reset password. The user can use this method to request a new password in database. The resetPasswordToken will
* be sent in the email.
*/
changePassword(newPassword, resetPasswordToken) {
if (!newPassword || !resetPasswordToken) {
throw new Error("Empty parameter detected!");
}
return axios({
url: normalizeUrl(this.happy_backend_url, "user", "resetpassword"),
method: "POST",
headers: {
"Accept": "application/json",
"Content-Type": "application/json",
},
data: {
"newPassword": newPassword,
"resetPasswordToken": resetPasswordToken
}
});
}
/**
* Reset current password. If the user just wants to reset his password. We can use this method.
*/
resetCurrentPassword(newPassword, oldPassword, authorizationToken) {
if (!oldPassword || !newPassword || !authorizationToken) {
throw new Error("Empty parameter detected!");
}
return axios({
url: normalizeUrl(this.happy_backend_url, "user", "resetcurrentpassword"),
method: "POST",
headers: {
"Accept": "application/json",
"Content-Type": "application/json",
"Authorization": authorizationToken
},
data: {
"oldPassword": oldPassword,
"newPassword": newPassword
}
});
}
/**
* Get the latest instance of a resource. The latest instance is the instance that was created most recently
*/
getLatestResource(resourceType, targetedUserName, authorizationToken) {
if (!resourceType || !targetedUserName || !authorizationToken) {
throw new Error("Empty parameter detected!");
}
return axios({
url: normalizeUrl(this.happy_fhir_url, resourceType, "$latest?userName=" + targetedUserName),
method: "GET",
headers: {
"Accept": "application/json",
"Content-Type": "application/json",
"Authorization": authorizationToken
}
});
}
/**
* Reset current password. If the user just wants to reset his password. We can use this method
*/
createResourceOnBehalf(resourceBody, resourceType, targetedUserName, authorizationToken) {
if (!resourceBody || !resourceType || !targetedUserName || !authorizationToken) {
throw new Error("Empty parameter detected!");
}
return axios({
url: normalizeUrl(this.happy_fhir_url, resourceType, "$onbehalf?userName=" + targetedUserName),
method: "POST",
headers: {
"Accept": "application/json",
"Content-Type": "application/json",
"Authorization": authorizationToken
},
data: resourceBody
});
}
/**
* Retrieve every practitioner a patient has granted accesses on. This method
* will directly return Fhir resource therefore more further requests are required.
*/
whocanseeme(patientId, authorizationToken) {
if (!patientId || !authorizationToken) {
throw new Error("Empty parameter detected.")
}
return axios({
method: 'GET',
url: normalizeUrl(this.happy_fhir_url, "Practitioner", patientId, "$whocanseeme"),
headers: {
"Accept": "application/json",
"Content-Type": "application/json",
"Authorization": authorizationToken
}
});
}
/**
* Retrieve everything a Patient has.
*/
readPatientEverything(patientId, authorizationToken) {
if (!patientId || !authorizationToken) {
throw new Error("Empty parameter detected.")
}
return axios({
method: 'GET',
url: normalizeUrl(this.happy_fhir_url, "Patient", patientId, "$everything"),
headers: {
"Accept": "application/json",
"Content-Type": "application/json",
"Authorization": authorizationToken
}
});
}
/**
* Retrieve every patient a practitioner has.
*/
retrieveAllPatientsForPractitioner(practitionerId, authorizationToken) {
if (!practitionerId || !authorizationToken) {
throw new Error("Empty parameter detected.")
}
return axios({
method: 'GET',
url: normalizeUrl(this.happy_fhir_url, "Practitioner", practitionerId, "$list"),
headers: {
"Accept": "application/json",
"Content-Type": "application/json",
"Authorization": authorizationToken
}
});
}
/**
* Read a Fhir resource. This method will return resource's data for every token, which has
* access to the resource (Owner's access, granted access).
*
* @param resourceType resource type
* @param resourceId resource id
* @param authorizationToken authorization token
* @returns {Promise<Response>}
*/
read(resourceType, resourceId, authorizationToken) {
if (!resourceType || !resourceId || !authorizationToken) {
throw new Error("Empty parameter detected.")
}
return axios({
method: 'GET',
url: normalizeUrl(this.happy_fhir_url, resourceType, resourceId),
headers: {
"Accept": "application/json",
"Content-Type": "application/json",
"Authorization": authorizationToken
}
});
}
/**
* Read every resource of an user's of a cetain type
*
* @param resourceType resource type
* @param resourceId resource id
* @param authorizationToken authorization token
* @returns {Promise<Response>}
*/
readEager(resourceType, authorizationToken) {
if (!resourceType || !authorizationToken) {
throw new Error("Empty parameter detected.")
}
return axios({
method: 'GET',
url: normalizeUrl(this.happy_fhir_url, resourceType, "$eager"),
headers: {
"Accept": "application/json",
"Content-Type": "application/json",
"Authorization": authorizationToken
}
});
}
/**
* Retrieve every resource's reference an user has. This could be for example a Patient resource
* for an user who is a patient. This could also be for example a Practitioner resource for an user
* who is a practitioner.
*
* The returned result will be an array of reference, which are from different resource types.
*
* @param userName
* @param authorizationToken
* @returns {Promise<Response>}
*/
getResourceReference(userName, authorizationToken) {
if (!userName || !authorizationToken) {
throw new Error("Empty parameter detected");
}
return axios({
url: normalizeUrl(this.happy_backend_url, "/resource/", userName),
method: 'GET',
headers: {
"Accept": "application/json",
"Content-Type": "application/json",
"Authorization": authorizationToken
}
});
}
/**
* Retrieve every resource's reference an user owns, filtered by resource type. This could be for example a Patient resource
* for an user who is a patient. This could also be for example a Practitioner resource for an user
* who is a practitioner.
*
* The returned result will be an array of references which are of only one resource type.
* @param userName user name
* @param resourceType resource type
* @param authorizationToken authorization token
* @returns {Promise<Response>}
*/
getResourceReferenceByResourceType(userName, resourceType, authorizationToken) {
if (!userName || !resourceType || !authorizationToken) {
throw new Error("Empty parameter detected");
}
return axios({
url: normalizeUrl(this.happy_backend_url, "/resource/", userName, resourceType),
method: 'GET',
headers: {
"Accept": "application/json",
"Content-Type": "application/json",
"Authorization": authorizationToken
}
});
}
/**
* Retrieve every access an user get granted access on but does not own. For example a practitioner could use this
* method to retrieve references of every Patient he has access to.
*
* @param userName user name
* @param authorizationToken authorization token
* @returns {Promise<Response>}
*/
getAccess(userName, authorizationToken) {
if (!userName || !authorizationToken) {
throw new Error("Empty parameter detected.");
}
return axios({
url: normalizeUrl(this.happy_backend_url, "/access/", userName),
method: 'GET',
headers: {
"Accept": "application/json",
"Content-Type": "application/json",
"Authorization": authorizationToken
}
});
}
/**
* Grant access of an resource on another user. This method will be used if a patient for example wants to share
* access of his Appointment with a practitioner.
*
* @param ownerUserName user name of the resource's owner
* @param targetedUserName user name of the other use
* @param forever indicates if the access should last forever
* @param start if the access is not forever, this will be the begin of the access
* @param end if the access is not forever, this will be the end of the access
* @param authorizationToken authorization token
* @returns {Promise<Response>}
*/
grantAccess(ownerUserName, targetedUserName, forever, start, end, authorizationToken) {
if (!ownerUserName || !targetedUserName || !authorizationToken) {
throw new Error("Empty parameter detected!");
}
return axios({
url: normalizeUrl(this.happy_backend_url, "/access/grant/", ownerUserName),
method: 'POST',
headers: {
"Accept": "application/json",
"Content-Type": "application/json",
"Authorization": authorizationToken
},
data: {
"end": end,
"forever": forever,
"start": start,
"ownerUserName": ownerUserName,
"targetedUserName": targetedUserName
}
});
}
/**
* Get every access an user has granted on another user. If a patient wants to check to whom he has granted access
* on his own resources, the developer could use this method to implement the feature.
*
* @param userName owner of the granted resources
* @param authorizationToken authorization token
* @returns {Promise<Response>}
*/
getGrantedAccess(userName, authorizationToken) {
if (!userName || !authorizationToken) {
throw new Error("Empty parameter detected");
}
return axios({
url: normalizeUrl(this.happy_backend_url, "/access/granted/", userName),
method: 'GET',
headers: {
"Accept": "application/json",
"Content-Type": "application/json",
"Authorization": authorizationToken
}
});
}
/**
* Delete access an user has granted on another user. Only owner of the auth token can process this method.
*
* @param targetUsername owner of token
* @param authorizationToken bearer token
* @returns {AxiosPromise}
*/
deleteGrantedAccess(targetUsername, authorizationToken) {
if (!targetUsername || !authorizationToken) {
throw new Error("Empty parameter detected");
}
return axios({
url: normalizeUrl(this.happy_backend_url, "/access/delete/", targetUsername),
method: 'DELETE',
headers: {
"Accept": "application/json",
"Content-Type": "application/json",
"Authorization": authorizationToken
}
});
}
/**
* Template for a Medication, returns valid FHIR JSON
*
* @param status active | inactive | entered-in-error
* @param code human readable code
* @param amount
* @return Medication
*/
medicationTemplate(status, code, amount) {
if (!status || !code || !amount) {
throw new Error('Empty Parameter detected');
}
return {
"resourceType": "Medication",
"status": status, // active | inactive | entered-in-error
"code": {
"text": code
},
"ingredient": [
{
"itemCodeableConcept": {
"coding": [
{
"display": code
}
]
},
"strength": {
"numerator": {
"value": amount,
"system": "http://unitsofmeasure.org",
"code": ""
},
"denominator": {
"value": amount,
"system": "http://unitsofmeasure.org",
"code": ""
}
}
}]
}
}
/**
* Template for a Medication Dispense, returns valid FHIR JSON
*
* @param status preparation | in-progress | cancelled | on-hold | completed | entered-in-error | stopped | declined | unknown
* @param medicationReference reference to an existing Medication
* @param patientReference Reference to who this Dispense is for
* @param practitionerReference Reference to who performed the dispense
* @param whenPreparedDateTime
* @param note any information about the dispense as string
* @param dosageInstruction text about how medication is to be used
* @param title title of the medication dispense. This corresponds the attribute type of medication dispense
* @param quantity
* @return Medication
*/
medicationDispenseTemplate(patientReference, practitionerReference, status,
medicationReference,
whenPreparedDateTime,
note,
dosageInstruction, title, quantity) {
if (!status
|| !medicationReference
|| !patientReference
|| !practitionerReference
|| !whenPreparedDateTime
|| !note
|| !dosageInstruction
|| !title
|| !quantity) {
throw new Error('Empty Parameter detected');
}
return {
"resourceType": "MedicationDispense",
"status": status, //preparation | in-progress | cancelled | on-hold | completed | entered-in-error | stopped | declined | unknown
"medicationReference": {"reference": medicationReference},
"subject": {"reference": patientReference},
"performer": [{
"actor": {
"reference": practitionerReference
}
}],
"whenPrepared": whenPreparedDateTime,
"note": [{"text": note}],
"dosageInstruction": [
{
"text": dosageInstruction
}
],
"type": {
"coding": [
{
"system": "https://happy.long-nguyen.de/code",
"code": "medication-dispense-title",
"display": title
}
]
},
"quantity": {
"value": quantity
}
}
}
/**
* Template for Medication, returns valid FHIR JSON
*
* @param statusAppointment proposed | pending | booked | arrived | fulfilled | cancelled | noshow | entered-in-error | checked-in | waitlist
* @param description Description of the appoinment
* @param start start of the appointment.
* @param patientInstruction what the patients should bring with
* @param patientReference reference of the patient
* @param practitionerReference reference of the practitioner
* @param title title of the appointment (This will correspond the attribute comment of appointment)
* @returns Appointment
*/
appointmentTemplate(patientReference, practitionerReference, statusAppointment, description, start, patientInstruction, title) {
if (!statusAppointment
|| !description
|| !start
|| !patientInstruction
|| !patientReference
|| !practitionerReference) {
throw Error('Empty Parameter detected. Please check your input');
}
return {
"resourceType": "Appointment",
"status": statusAppointment, // proposed | pending | booked | arrived | fulfilled | cancelled | noshow | entered-in-error | checked-in | waitlist
"description": description,
"start": start,
"minutesDuration": 15,
"patientInstruction": patientInstruction,
"participant": [
{
"actor": {"reference": patientReference},
},
{
"actor": {"reference": practitionerReference},
}
],
"reasonCode": {
"coding": [
{
"system": "https://happy.long-nguyen.de/code",
"code": "appointment-title",
"display": title
}
]
},
}
}
/**
* Template for DiagnosticReport, returns valid FHIR JSON
*
* @param status registered | partial | preliminary | final
* @param patientReference id of patient
* @param performerReference id of practitioner
* @param conclusion humand readable string
* @param title title of the diagnostic report. This corresponds the attribute code of the diagnostic report
* @returns DiagnosticReport fhir diagnostic report json
*/
diagnosticReportTemplate(patientReference, performerReference, status, conclusion, title) {
if (!status
|| !patientReference
|| !performerReference
|| !conclusion
|| !title) {
throw Error('Empty Parameter detected. Please check your input');
}
return {
"resourceType": "DiagnosticReport",
"status": status,
"subject": {"reference": patientReference},
"performer": [{"reference": performerReference}],
"conclusion": conclusion,
"code": {
"coding": [
{
"system": "https://happy.long-nguyen.de/code",
"code": "diagnostic-report-title",
"display": title
}
]
}
}
}
/**
* Template for Encounter
*
* @param practitionerReference id of practitioner
* @param patientReference id of patient
* @param status planned | arrived | triaged | in-progress | onleave | finished | cancelled
* @param startDateTime 1970-01-01
* @param endDateTime 1970-01-01
* @param description human readable description. This corresponds the attribute type
* @param title title of the encounter. This corresponds the attribute type
* @returns {{identifier: *, period: {start: *, end: *}, subject: {reference: *}, reasonCode: {text: *}[], resourceType: string, status: *}}
*/
encounterTemplate(patientReference, practitionerReference, status, startDateTime, endDateTime, description, title) {
if (!practitionerReference
|| !patientReference
|| !status
|| !startDateTime
|| !endDateTime
|| !description
|| !title) {
throw Error('Empty Parameter detected. Please check your input');
}
return {
"resourceType": "Encounter",
"participant": [{
"type": [{
"coding": [{
"system": "http://hl7.org/fhir/ValueSet/encounter-participant-type",
"code": "PPRF",
"display": "primary performer",
}],
"text": "primary performer"
}],
"individual": {
"reference": practitionerReference,
}
}],
"status": status,
"subject": {
"reference": patientReference,
},
"period": {
"start": startDateTime,
"end": endDateTime
},
"type": {
"coding": [
{
"system": "https://happy.long-nguyen.de/code",
"code": "encounter-description",
"display": description
},
{
"system": "https://happy.long-nguyen.de/code",
"code": "encounter-title",
"display": title
},
]
},
}
}
/**
* Template procedure
*
* @param status preparation | in-progress | not-done | on-hold | stopped | completed | entered-in-error | unknown
* @param patientReference id of patient
* @param performedDateTime date of performance
* @param title title of the performance
* @param description human readable string
* @param practitionerReference id of practitioner
* @returns {{note: {text: *}[], code: {coding: {display: *}[]}, performer: {actor: {reference: *}}, subject: {reference: *}, performedDateTime: *, resourceType: string, status: *}}
*/
procedureTemplate(patientReference, practitionerReference, status, performedDateTime, title, description) {
if (!status || !patientReference || !performedDateTime || !practitionerReference || !title) {
throw Error('Empty Parameter detected. Please check your input');
}
return {
"resourceType": "Procedure",
"status": status,
"code": {
"coding": [
{
"system": "https://happy.long-nguyen.de/code",
"code": "procedure-title",
"display": title
}
]
},
"subject": {
"reference": patientReference
},
"performer": {
"actor": {
"reference": practitionerReference
}
},
"performedDateTime": performedDateTime,
"note": [
{
"text": description
}
]
}
}
/**
* Template for document composition. Can be used as personal note.
*
* @param status preliminary | final | amended | entered-in-error
* @param authorReference id of the author, can be patient or practitioner
* @param timestamp unix timestamp
* @param title human readable title
* @param description description title
* @param color Color of the note. WIll be stored in attribute type
* @returns {{date: *, author: {reference: *}, section: {title: *}, title: *, resourceType: string, status: *}}
*/
compositionTemplate(authorReference, status, timestamp, title, description, color) {
if (!status || !authorReference || !timestamp || !title || !description || !color) {
throw Error('Empty Parameter detected. Please check your input');
}
return {
"resourceType": "Composition",
"status": status, // current | superseded | entered-in-error
"author": {
"reference": authorReference,
},
"subject": {
"reference": authorReference
},
"date": timestamp,
"title": title,
"section": {
"title": description,
},
"type": {
"coding": [
{
"system": "https://www.w3schools.com/colors/colors_rgb.asp",
"code": "note-color",
"display": color
}
]
}
}
}
/**
* Condition (Fhir's name) or can be interpreted as a Diagnosis
*
* @param patientReference reference of the patient
* @param practitionerReference reference of the practitioner
* @param recordedDate recorded date
* @param note content of the diagnosis
* @param title title of the condition. Corresponds the attribute code of condition
* @returns {{note: *, recorder: {reference: *}, subject: {reference: *}, recordedDate: *, resourceType: string}}
*/
conditionTemplate(patientReference, practitionerReference, recordedDate, note, title) {
if (!patientReference || !practitionerReference || !recordedDate || !note || !title) {
throw Error('Empty Parameter detected. Please check your input');
}
return {
"resourceType": "Condition",
"subject": {
"reference": patientReference
},
"recorder": {
"reference": practitionerReference
},
"recordedDate": recordedDate,
"note": {
"text": note
},
"code": {
"coding": [
{
"system": "https://happy.long-nguyen.de/code",
"code": "condition-title",
"display": title
}
]
}
}
}
/**
* Medication request
*
* @param patientReference reference to the patient
* @param practitionerReference reference to the pracitioner
* @param medicationReference
* @param note instruction of doctor. Corresponds doseageInstruction
* @param title medication request title. Will be in note
* @param startDate start of the date.
* @param endDate end date. Will be in
* @returns {{note: {text: *}, performer: {reference: *}, dosageInstruction: {timing: {event: *}, text: *}, subject: {reference: *}, resourceType: string}}
*/
medicationRequestTemplate(patientReference, practitionerReference, medicationReference, note, title, startDate, endDate) {
if (!patientReference || !practitionerReference || !note || !title || !endDate || !startDate) {
throw Error('Empty Parameter detected. Please check your input');
}
return {
"resourceType": "MedicationRequest",
"subject": {
"reference": patientReference
},
"performer": {
"reference": practitionerReference
},
"medicationReference": {
"reference": medicationReference
},
"note": {
"text": title
},
"dosageInstruction": {
"text": note,
},
"dispenseRequest": {
"validityPeriod": {
"start": startDate,
"end": endDate
}
}
}
}
/**
* Create a consent
*
* @param patientReference patient
* @param optIn boolean, does patient want to donate every thing?
* @param optOut boolean, does patient not want to donate everything?
* @param optOthersWillDecide boolean, should another person decide this?
* @param optParts boolean, does patient want to donate specific parts?
* @param toDonate string of everything patients want to donate. Only needed if optParts is true
* @param notDonate string of every thing patients want to not donate. Only needed if optParts is true
* @param presentative address and name of the person who will presents the person
* @returns {{policyRule: {coding: *[]}, resourceType: string}}
*/
consentTemplate(patientReference, optIn, optOut, optOthersWillDecide, optParts, toDonate, notDonate, presentative, specialInstruction) {
if (!patientReference) {
throw Error('Empty Parameter detected. Please check your input');
}
return {
"resourceType": "Consent",
"policyRule": {
"coding": [
{
"system": "https://happy.long-nguyen.de/code",
"code": "consent-opt-in",
"display": String(optIn)
},
{
"system": "https://happy.long-nguyen.de/code",
"code": "consent-opt-out",
"display": String(optOut)
},
{
"system": "https://happy.long-nguyen.de/code",
"code": "consent-parts",
"display": String(optParts)
},
{
"system": "https://happy.long-nguyen.de/code",
"code": "consent-others-will-decide",
"display": String(optOthersWillDecide)
},
{
"system": "https://happy.long-nguyen.de/code",
"code": "consent-to-donate",
"display": toDonate
},
{
"system": "https://happy.long-nguyen.de/code",
"code": "consent-not-donate",
"display": notDonate
},
{
"system": "https://happy.long-nguyen.de/code",
"code": "consent-presentative",
"display": presentative
},
{
"system": "https://happy.long-nguyen.de/code",
"code": "special-instruction",
"display": specialInstruction
}
]
}
}
}
/**
* Create a task
*
* @param ownerReference reference of owner, can be patient or practitioner
* @param title title of the Task
* @param repetitions how often the task shall be executed
* @param hoursBefore how many hours before there shall be a reminder
* @param note description of the task
* @param start start of the task
* @returns {{owner: {reference: *}, description: *, executionPeriod: {start: *, end: *}, resourceType: string}}
*/
taskTemplate(ownerReference, title, repetitions, hoursBefore, note, start) {
if (!ownerReference || !note || !start || !title || !repetitions || !hoursBefore) {
throw Error('Empty Parameter detected. Please check your input');
}
return {
"resourceType": "Task",
"owner": {
"reference": ownerReference
},
"note": [
{
"text": note
}
],
"description": title,
"executionPeriod": {
"start": start
},
"restriction": {
"repetitions": repetitions
},
"code": {
"text": hoursBefore
}
};
}
}
module.exports = HappyClient;