Getting active and inactive nurse and device data - TREP-LABS/realdrip-backend GitHub Wiki
Active/Inactive nurses and devices
API Spec: Link
Let's represent the nurse and device entity with X. From the API spec, there is no way to fetch active(or in-active) X data, you can only get a single or all X data. This is not a mistake, the API spec was deliberately designed that way. If we really think about it, the only reason why we say X is active is because it is partaking in an infusion process, for example, the only reason a nurse is active is because he/she is partaking in an infusion treatment, the same applies to devices and any other potential component of an infusion treatment that we would want to classify active or inactive. Consequently, we decided to make only the infusion entity hold the active(or inactive) status. So how do we get the active/inactive X data? Mapping to the rescue.
We can get all active(or inactive) infusions via the GET /infusion
endpoint with the appropriate query parameter, using the result from this query, we can easily map out the active nurses and devices, this is a simplistic implementation of the logic in Javascript:
const activeNurses = [];
const activeDevices = [];
// NOTE: api.fetch is an hypothetical method that synchronously makes an HTTP request to fetch data
const activeInfusions = api.fetch('/api/infusion?status=active&populate=nurse,device');
activeInfusions.forEach(infusion => {
activeNurses.push(infusion.nurseId);
activeDevices.push(infusion.deviceId);
});
Take note of the populate
query parameter in the infusion URL(/api/infusion?status=active&populate=nurse,device
) up there. By default, when you fetch infusions, this a sample of what you get back in the response:
...
"data": [
{
"_id": "5dba11be8612fd116880e468",
// ...other fields
"deviceId": "5db23403347ab06cc7bfd8a2",
"nurseId": "5da839d4372bc83d70e16747",
}
]
...
The value of deviceId
and nurseId
in the response are just reference ids to the documents containing the actual device and nurse information respectively. This response is enough if you don't need extra details about the nurse or device, however, if you intend to map out the nurse or device information from the infusion data, you need to use the populate query parameter. The value of the populate query parameter is a comma separated list of reference fields you want populated. For example, cosuming this endpoint: GET /api/infusion?status=active&populate=nurse,device
would return all the active infusions with their nurse and device references populated:
...
"data": [
{
"_id": "5dba11be8612fd116880e468",
// ...other fields
"deviceId": {
"_id": "5db23403347ab06cc7bfd8a2",
"hospitalId": "5db2c457f381140022f815f1",
"wardId": "5db23e1878171c0022827b4b",
"label": ""
},
"nurseId": {
"_id": "5df3d2070766e900229359b3",
"name": "Sample",
"email": "[email protected]",
"phoneNo": "090887787",
"defaultPass": true,
"hospitalId": "5dbad36074df3900221a55f1",
"wardId": "5dbad42174df3900221a55f2"
},
}
]
...
Mapping out the active(or inactive) nurses and devices should be straight forward from here.
Note: Inactive infusions are infusion with the status of either
ended
orterminated