Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 22 additions & 11 deletions src/PrefetchTemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export class PrefetchTemplate {
);
const PATIENT_PREFETCH = new PrefetchTemplate('{{context.patientId}}');

const PHARMACY_PREFETCH = new PrefetchTemplate('HealthcareService/{{context.pharmacyId}}');

const ALL_REQUESTS_PREFETCH = new PrefetchTemplate(
'MedicationRequest?subject={{context.patientId}}&_include=MedicationRequest:medication'
);
Expand All @@ -19,6 +21,7 @@ export class PrefetchTemplate {
prefetchMap.set('request', REQUEST_PREFETCH);
prefetchMap.set('practitioner', PRACTITIONER_PREFETCH);
prefetchMap.set('patient', PATIENT_PREFETCH);
prefetchMap.set('pharmacy', PHARMACY_PREFETCH);
prefetchMap.set('medicationRequests', ALL_REQUESTS_PREFETCH);
// prefetchMap.set("ServiceRequest", SERVICE_REQUEST_BUNDLE);
// prefetchMap.set("Encounter", ENCOUNTER_BUNDLE);
Expand All @@ -44,35 +47,43 @@ export class PrefetchTemplate {
paramElementMap.set('context.draftOrders.context.appointments.Appointment.id', ['id']);
paramElementMap.set('context.draftOrders.context.encounterId', ['id']);
paramElementMap.set('context.patientId', ['subject', 'reference']);
paramElementMap.set('context.pharmacyId', ['id']);

return paramElementMap;
}

static generateQueries(requestBundle, patientReference, userReference, ...prefetchKeys) {
var resolvedQueries = new Map();
for (var i = 0; i < prefetchKeys.length; i++) {
var prefetchKey = prefetchKeys[i];
var query = prefetchMap.get(prefetchKey).getQuery();
static generateQueries(requestBundle, patientReference, userReference, pharmacyId, ...prefetchKeys) {
var resolvedQueries = new Map();
for (var i = 0; i < prefetchKeys.length; i++) {
var prefetchKey = prefetchKeys[i];
var query = prefetchMap.get(prefetchKey).getQuery();
// Regex source: https://regexland.com/all-between-specified-characters/
var parametersToFill = query.match(/(?<={{).*?(?=}})/gs);
var resolvedQuery = query.slice();
var parametersToFill = query.match(/(?<={{).*?(?=}})/gs);
var resolvedQuery = query.slice();
for (var j = 0; j < parametersToFill.length; j++) {
var unresolvedParameter = parametersToFill[j];
var resolvedParameter;
if (requestBundle) {
resolvedParameter = PrefetchTemplate.resolveParameter(unresolvedParameter, requestBundle);
if (unresolvedParameter === 'context.pharmacyId') {
resolvedParameter = pharmacyId;
} else {
resolvedParameter = PrefetchTemplate.resolveParameter(unresolvedParameter, requestBundle);
}
} else {
if (unresolvedParameter === 'context.patientId') {
resolvedParameter = patientReference;
} else if (unresolvedParameter === 'context.userId') {
resolvedParameter = userReference;
} else if (unresolvedParameter === 'context.pharmacyId') {
resolvedParameter = pharmacyId;
}
}
resolvedQuery = resolvedQuery.replace('{{' + unresolvedParameter + '}}', resolvedParameter);
}
resolvedQueries.set(prefetchKey, resolvedQuery);
}
return resolvedQueries;
resolvedQueries.set(prefetchKey, resolvedQuery);
}
return resolvedQueries;
}

// Source: https://www.tutorialspoint.com/accessing-nested-javascript-objects-with-string-key
static getProp(object, path) {
Expand Down
22 changes: 18 additions & 4 deletions src/components/SMARTBox/PatientBox.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -167,17 +167,27 @@ const PatientBox = props => {
const fetchResources = queries => {
let requests = [];
callback('prefetchCompleted', false);
console.log('🔍 PHARMACY DEBUG - All queries to fetch:', queries); // ADD THIS

queries.forEach((query, queryKey) => {
console.log(`🔍 PHARMACY DEBUG - Processing ${queryKey}: ${query}`); // ADD THIS
const urlQuery = '/' + query;
requests.push(
client
.request(urlQuery)
.then(response => {
console.log(response);
console.log(`🔍 PHARMACY DEBUG - SUCCESS for ${queryKey}:`, response); // ADD THIS
return response;
})
.catch(error => {
console.log(`🔍 PHARMACY DEBUG - FAILED for ${queryKey}:`, error); // ADD THIS
return null; // Return null so Promise.all doesn't fail completely
})
.then(resource => {
callbackMap('prefetchedResources', queryKey, resource);
if (resource) {
console.log(`🔍 PHARMACY DEBUG - Calling callbackMap for ${queryKey}`); // ADD THIS
callbackMap('prefetchedResources', queryKey, resource);
}
})
);
});
Expand All @@ -202,9 +212,11 @@ const PatientBox = props => {
request,
patientReference,
userReference,
'pharm0111',
'request',
'patient',
'practitioner'
'practitioner',
'pharmacy'
);
fetchResources(queries);

Expand All @@ -218,9 +230,11 @@ const PatientBox = props => {
request,
patientReference,
userReference,
'pharm0111',
'patient',
'practitioner',
'medicationRequests'
'medicationRequests',
'pharmacy'
);
fetchResources(queries);
}
Expand Down
4 changes: 3 additions & 1 deletion src/containers/RequestBuilder.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const RequestBuilder = props => {
const displayRequestBox = !!globalState.patient?.id;

useEffect(() => {
console.log('Prefetched Resources updated:');
console.log(state.prefetchedResources);
}, [state.prefetchedResources]);

Expand Down Expand Up @@ -181,7 +182,8 @@ const RequestBuilder = props => {
prefetch,
globalState.sendPrefetch,
hook,
hookConfig
hookConfig,
'pharm0111'
);

let baseUrl = globalState.baseUrl;
Expand Down
7 changes: 5 additions & 2 deletions src/util/buildRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ export default function buildRequest(
prefetch,
includePrefetch,
hook,
hookConfig
hookConfig,
pharmacyId = 'pharm0111'

) {
// Use the provided user if there is no request for this hook
let userId = 'Practitioner/' + user;
Expand All @@ -31,7 +33,8 @@ export default function buildRequest(
context: {
userId: userId,
patientId: patient.id,
encounterId: 'enc89284'
encounterId: 'enc89284',
pharmacyId: pharmacyId
}
};

Expand Down