|
55 | 55 | let patient: any = resourcesToReview.filter((r) => r.resourceType === "Patient")[0];
|
56 | 56 | // TODO: Get summary DocumentReferences from resourcesToReview
|
57 | 57 | let summaryDocRefs: any[] = resourcesToReview.filter((r) => r.resourceType === "DocumentReference" && !(r.type?.coding[0]?.code === "34108-1"));
|
| 58 | + if (summaryDocRefs.length === 0) { |
| 59 | + console.error('No sessions found for user'); |
| 60 | + } |
58 | 61 | // Compare sessionIDs in most recent DocRef with sessionID in most recent SHL
|
59 | 62 | // let mostRecentDocRef = summaryDocRefs.sort((a, b) => b.date - a.date)[0];
|
60 |
| - let mostRecentDocRef = summaryDocRefs[summaryDocRefs.length-1]; |
| 63 | + let mostRecentDocRef = summaryDocRefs[0]; |
61 | 64 | // TODO: Get shl DocumentReferences from resourcesToReview
|
62 |
| - let shlDocRefs: any[] = resourcesToReview.filter((r) => r.resourceType === "DocumentReference" && r.type?.coding[0]?.code === "34108-1").reverse(); |
| 65 | + let shlDocRefs: any[] = resourcesToReview.filter((r) => r.resourceType === "DocumentReference" && r.type?.coding[0]?.code === "34108-1"); |
63 | 66 |
|
64 | 67 | patientId = sofClient.getPatientID();
|
65 | 68 | sessionId = mostRecentDocRef.id;
|
|
109 | 112 | $shlStore.encryptionKey = shlData.encryptionKey;
|
110 | 113 | $shlStore.managementToken = shlData.managementToken;
|
111 | 114 | $shlStore.label = shlData.label;
|
112 |
| - |
| 115 | + $shlStore.files = []; |
113 | 116 | found = true;
|
114 | 117 | break;
|
115 | 118 | }
|
116 | 119 | }
|
117 | 120 |
|
118 | 121 | if (found) {
|
119 | 122 | console.log("Successfully retrieved SHL and Resources");
|
| 123 | + if (mostRecentDocRef.id != $shlStore.sessionId) { |
| 124 | + console.log(`Most recent SHL ${$shlStore.id} doesn't match session ${$shlStore.sessionId}, updating SHL`); |
| 125 | + let deleted = await shlClient.deleteAllFiles($shlStore); |
| 126 | + let ips = createIpsPayload(patient, mostRecentDocRef); |
| 127 | + let shc = await packageShc(ips); |
| 128 | + $shlStore = await addFiles($shlStore, [shc]); |
| 129 | + let reportDate = new Date(mostRecentDocRef.date) |
| 130 | + .toLocaleDateString('en-US', { |
| 131 | + day: 'numeric', |
| 132 | + month: 'long', |
| 133 | + year: 'numeric' |
| 134 | + }); |
| 135 | + let reportLabel = `Let's Talk Tech Choices Report (${reportDate})`; |
| 136 | + let result = await sofClient.postShl($shlStore, mostRecentDocRef, reportLabel); |
| 137 | + } |
120 | 138 | // The current SHL is most recent, so use it
|
121 | 139 | shlReadyDispatch('shl-ready', true);
|
122 | 140 | } else if (mostRecentDocRef) {
|
123 | 141 | console.log(`Couldn't find FHIR record for SHL ${$shlStore.id} and session ${$shlStore.sessionId}, creating new SHL`);
|
| 142 | + shlClient.deleteShl($shlStore); |
124 | 143 | newShl(patient, mostRecentDocRef);
|
125 | 144 | } else {
|
126 | 145 | throw Error("No summary found for patient")
|
|
0 commit comments