Skip to content

Commit 4c36ced

Browse files
authored
Merge pull request #20 from uwcirg/filter-out-signature-docrefs
Advance Directive - If resource.category doesn't exist, ignore the DR…
2 parents 56fb69f + 5d5a598 commit 4c36ced

File tree

2 files changed

+88
-4
lines changed

2 files changed

+88
-4
lines changed

src/lib/FetchAD.svelte

+43
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,30 @@
186186
});
187187
}
188188
189+
async function injectPdfIntoDocRef(url, attachment) {
190+
try {
191+
const response = await fetch(url);
192+
if (response.ok) {
193+
const blob = await response.blob();
194+
const reader = new FileReader();
195+
reader.onloadend = () => {
196+
console.log(reader.result); // Log the full data URL for debugging
197+
attachment.data = reader.result.split(',')[1]; // Base64 encoded data
198+
};
199+
reader.readAsDataURL(blob);
200+
/**
201+
const arrayBuffer = await response.arrayBuffer();
202+
const base64String = btoa(String.fromCharCode(...new Uint8Array(arrayBuffer)));
203+
attachment.data = base64String;
204+
*/
205+
} else {
206+
console.error(`Failed to fetch PDF from ${url}`);
207+
}
208+
} catch (error) {
209+
console.error(`Error fetching PDF from ${url}:`, error);
210+
}
211+
}
212+
189213
async function prepareIps() {
190214
fetchError = '';
191215
processing = true;
@@ -203,6 +227,25 @@
203227
if (resources.length === 0) {
204228
console.warn("No advance directives found for patient "+patient.id);
205229
}
230+
231+
// If resource.category doesn't exist, ignore the DR - DR's w/out that are simply signature DR's.
232+
// Lambda function to check if resource.category exists
233+
const nonSignatureDR = dr => dr.category !== undefined;
234+
// Filter out resources that don't have a category
235+
resources = resources.filter(nonSignatureDR);
236+
237+
// if one of the DR's `content` elements has attachment.contentType = 'application/pdf', download if possible, put base64 of pdf in DR.content.attachment.data
238+
const hasPdfContent = dr => dr.content && dr.content.some(content => content.attachment && content.attachment.contentType === 'application/pdf');
239+
240+
resources.forEach(async dr => {
241+
if (hasPdfContent(dr)) {
242+
const pdfContent = dr.content.find(content => content.attachment && content.attachment.contentType === 'application/pdf');
243+
if (pdfContent && pdfContent.attachment && pdfContent.attachment.url) {
244+
await injectPdfIntoDocRef (pdfContent.attachment.url, pdfContent.attachment);
245+
}
246+
}
247+
});
248+
206249
resources.unshift(patient);
207250
208251
result = {

src/lib/resource-templates/AdvanceDirective.svelte

+45-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
export let resource; // Define a prop to pass the data to the component
33
</script>
44

5+
<!--
56
Type: {resource.resourceType}
67
<br />
78
Text:
@@ -11,15 +12,55 @@ Text:
1112
<i>No text provided in resource</i>
1213
{/if}
1314
<br />
15+
-->
16+
1417
Category:
1518
{#if resource.category && resource.category[0] && resource.category[0].coding && resource.category[0].coding[0]}
16-
{resource.category[0].coding[0].display}
19+
{resource.category[0].coding[0].display} (LOINC {resource.category[0].coding[0].code})
20+
{/if}
21+
<br />
22+
Type:
23+
<!-- 42348-3 is for "Advance Directive"; per cthon, it's presence here is redundant w/ category above. -->
24+
{#if resource.type && resource.type.coding && resource.type.coding[0] && resource.type.coding[0].code != '42348-3'}
25+
{resource.type.coding[0].display} (LOINC {resource.type.coding[0].code})
26+
{/if}
27+
{#if resource.type && resource.type.coding && resource.type.coding[1] && resource.type.coding[1].code != '42348-3'}
28+
{resource.type.coding[1].display} (LOINC {resource.type.coding[1].code}).
1729
{/if}
1830
<br />
19-
Intent:
20-
{#if resource.provision && resource.provision.code && resource.provision.code[0] && resource.provision.code[0].coding && resource.provision.code[0].coding[0]}
21-
{resource.provision.code[0].coding[0].display}
31+
Description:
32+
{#if resource.description}
33+
{resource.description}
34+
{/if}
35+
<br />
36+
setId:
37+
{#if resource.identifier && resource.identifier[0] && resource.identifier[0].system && resource.identifier[0].system == 'https://mydirectives.com/standards/terminology/namingSystem/setId'}
38+
{resource.identifier[0].value}
39+
{/if}
40+
<br />
41+
Date:
42+
{#if resource.date}
43+
{resource.date}
44+
{/if}
45+
<br />
46+
Status:
47+
{#if resource.status}
48+
{resource.status}
49+
{/if}
50+
<br />
51+
docStatus:
52+
{#if resource.docStatus}
53+
{resource.docStatus}
2254
{/if}
2355
{#if resource.description && resource.description.text}
56+
<br />
2457
{resource.description.text}
2558
{/if}
59+
<br/>
60+
{#if resource.content}
61+
{#each resource.content as content}
62+
{#if content.attachment && content.attachment.data}
63+
PDF present: <a href={"data:application/pdf;base64," + content.attachment.data} target="_blank" rel="noopener noreferrer">View</a>
64+
{/if}
65+
{/each}
66+
{/if}

0 commit comments

Comments
 (0)