|
186 | 186 | });
|
187 | 187 | }
|
188 | 188 |
|
| 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 | +
|
189 | 213 | async function prepareIps() {
|
190 | 214 | fetchError = '';
|
191 | 215 | processing = true;
|
|
203 | 227 | if (resources.length === 0) {
|
204 | 228 | console.warn("No advance directives found for patient "+patient.id);
|
205 | 229 | }
|
| 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 | +
|
206 | 249 | resources.unshift(patient);
|
207 | 250 |
|
208 | 251 | result = {
|
|
0 commit comments