Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ad section injection #22

Merged
merged 3 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
21 changes: 21 additions & 0 deletions src/lib/AddFile.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@
section: undefined,
resources: undefined
};
let adData: {section: any|undefined; resources: any[]|undefined} = {
section: undefined,
resources: undefined
};
let resourcesToInject: Record<string, {section: any|undefined; resources: {[key: string]: ResourceHelper}}> = {};
let patientName = "My";
let patient: any | undefined;
Expand Down Expand Up @@ -107,6 +111,22 @@
}
}

$: {
if (adData.resources || adData.section) {
let adInjection: {section: any|undefined; resources: {[key: string]: ResourceHelper}} = {
section: adData.section,
resources: {}
}
adData.resources?.forEach((r) => {
let rh = new ResourceHelper(r.resource);
adInjection.resources[rh.tempId] = rh;
});
resourcesToInject["Advance Directives"] = adInjection;
} else {
delete resourcesToInject["Advance Directives"];
}
}

onMount(() => {
if (sessionStorage.getItem('URL')) {
let url = sessionStorage.getItem('URL') ?? '/create';
Expand Down Expand Up @@ -350,6 +370,7 @@
<TabPane class="ad-tab" tabId="ad" style="padding-top:10px">
<span class="ad-tab" slot="tab">Advance Directive Search</span>
<FetchAD
bind:adSection={adData.section} bind:adSectionResources={adData.resources}
on:update-resources={ async ({ detail }) => { handleNewResources(detail) } }>
</FetchAD>
</TabPane>
Expand Down
92 changes: 65 additions & 27 deletions src/lib/FetchAD.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
import type { ResourceRetrieveEvent } from './types';
import { createEventDispatcher } from 'svelte';

export let adSection: any | undefined;
export let adSectionResources: any[] | undefined;

const resourceDispatch = createEventDispatcher<{ 'update-resources': ResourceRetrieveEvent }>();

let selectedUrl = "https://qa-rr-fhir.maxmddirect.com";
Expand Down Expand Up @@ -49,6 +52,41 @@
resources: undefined
};

let adSectionTemplate = {
title: "Advance Directives",
code: {
coding: [
{
system: "http://loinc.org",
code: "42348-3",
display: "Advance Directives"
}
]
},
entry: []
};

function updateAdSection(resources: any[]) {
if (adSection === undefined) {
adSection = JSON.parse(JSON.stringify(adSectionTemplate));
}
if (adSectionResources) {
adSectionResources = adSectionResources.concat(resources);
} else {
adSectionResources = resources.map((r, index) => {
r.id = `advance-directive-document-${index+1}`;
let entry = {resource: r, fullUrl: `urn:uuid:${r.id}`};
return entry;
});
}
adSection.entry = adSectionResources.map((r) => {
return {
reference: r.fullUrl
}
});
console.log(adSectionResources);
}

let summaryUrlValidated: URL | undefined = undefined;
$: {
setSummaryUrlValidated(selectedUrl);
Expand Down Expand Up @@ -114,6 +152,7 @@

function buildPatientSearchQuery() {
let query = "?";
query += 'active=true&';
query += dob ? `birthdate=${dob}&` : '';
query += first ? `given=${first}&` : '';
query += last ? `family=${last}&` : '';
Expand All @@ -124,7 +163,7 @@
query += city ? `address-city=${city}&` : '';
query += state ? `address-state=${state}&` : '';
query += zip ? `address-postalcode=${zip}&` : '';
query += 'active=true';
query = query.substring(0, query.length - 1);
return query;
}

Expand Down Expand Up @@ -186,29 +225,29 @@
});
}

async function injectPdfIntoDocRef(url, attachment) {
try {
const response = await fetch(url);
if (response.ok) {
const blob = await response.blob();
const reader = new FileReader();
reader.onloadend = () => {
console.log(reader.result); // Log the full data URL for debugging
attachment.data = reader.result.split(',')[1]; // Base64 encoded data
};
reader.readAsDataURL(blob);
/**
const arrayBuffer = await response.arrayBuffer();
const base64String = btoa(String.fromCharCode(...new Uint8Array(arrayBuffer)));
attachment.data = base64String;
*/
} else {
console.error(`Failed to fetch PDF from ${url}`);
async function injectPdfIntoDocRef(url, attachment) {
try {
const response = await fetch(url);
if (response.ok) {
const blob = await response.blob();
const reader = new FileReader();
reader.onloadend = () => {
console.log(reader.result); // Log the full data URL for debugging
attachment.data = reader.result?.split(',')[1]; // Base64 encoded data
};
reader.readAsDataURL(blob);
/**
const arrayBuffer = await response.arrayBuffer();
const base64String = btoa(String.fromCharCode(...new Uint8Array(arrayBuffer)));
attachment.data = base64String;
*/
} else {
console.error(`Failed to fetch PDF from ${url}`);
}
} catch (error) {
console.error(`Error fetching PDF from ${url}:`, error);
}
} catch (error) {
console.error(`Error fetching PDF from ${url}:`, error);
}
}

async function prepareIps() {
fetchError = '';
Expand Down Expand Up @@ -245,14 +284,13 @@ async function injectPdfIntoDocRef(url, attachment) {
}
}
});

resources.unshift(patient);

updateAdSection(resources);
// resources.unshift(patient);
result = {
resources: resources,
resources: [patient], // resources,
source: hostname
};
console.log(resources);
console.log([patient, ...resources]);
resourceDispatch('update-resources', result);
} catch (e) {
processing = false;
Expand Down
9 changes: 5 additions & 4 deletions src/lib/ResourceSelector.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@
export let injectedResources: Record<string, {section: any|undefined; resources: { [key: string]: ResourceHelper }}>;

const components: Record<string, any> = {
"DocumentReference": AdvanceDirective,
"Consent": AdvanceDirective,
"AllergyIntolerance": AllergyIntolerance,
"Condition": Condition,
"Consent": AdvanceDirective,
"DiagnosticReport": DiagnosticReport,
"DocumentReference": AdvanceDirective,
"Immunization": Immunization,
"Location": Location,
"Medication": Medication,
Expand All @@ -53,7 +53,8 @@
"Practitioner": Practitioner,
"Problem": Problem,
"Procedure": Procedure,
"Occupational Data for Health": OccupationalDataForHealth
"Occupational Data for Health": OccupationalDataForHealth,
"Advance Directives": AdvanceDirective
};

const ipsDispatch = createEventDispatcher<{ 'ips-retrieved': IPSRetrieveEvent }>();
Expand Down Expand Up @@ -305,7 +306,7 @@
if (injectedResources[section].resources[rkeys[i]].include) {
let entry = {
resource: injectedResources[section].resources[rkeys[i]].resource,
fullUrl: injectedResources[section].resources[rkeys[i]].resource.id
fullUrl: `urn:uuid:${injectedResources[section].resources[rkeys[i]].resource.id}`
}
content.entry.push(entry);
injectedResources[section].section.entry.push({
Expand Down
4 changes: 4 additions & 0 deletions src/routes/create/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@
goto(`/view/${shl.id}`);
} else {
const newShl = await newShlFromShc(detail);
newShl.files.map(f => {
f.contentEncrypted = f.contentEncrypted.slice(0, 200);
return f;
});
$shlStore = [...$shlStore, newShl];
goto(`/view/${newShl.id}`);
}
Expand Down