-
Notifications
You must be signed in to change notification settings - Fork 249
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
Basic Support for Offline Registered Patients #527
Merged
FlorianRappl
merged 19 commits into
openmrs:master
from
manuelroemer:feature/offline-registered-patient-chart
Jan 27, 2022
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
b0b1d49
Pre-merge master.
manuelroemer 3c50201
Merge branch 'master' of github.com:openmrs/openmrs-esm-patient-chart…
manuelroemer 7a49b94
Enable patient banner to work with offline registered patient.
manuelroemer f9f6d5a
Fixed new bug where the offline visit conversion failed due to undefi…
manuelroemer e74394e
Improve and fix queued sync items w.r.t. offline registered patients.
manuelroemer 6c0b4c9
Refined fhir.Patient <> PatientCreate interaction.
manuelroemer 3b2c0f0
Consider FHIR errors when fetching an online patient.
manuelroemer 8b8f1ba
Merge branch 'master' of github.com:openmrs/openmrs-esm-patient-chart…
manuelroemer aa2e6c3
Adapted patient FHIR gender mapping and values read from registration…
manuelroemer 10a4475
Use expected visit sync type dependencies.
manuelroemer b0e1961
Adapt FHIR mapping to use correct `id` prop.
manuelroemer cc1fbf3
Merge branch 'master' of github.com:openmrs/openmrs-esm-patient-chart…
manuelroemer 60bc1c0
Updated yarn.lock.
manuelroemer d306c0e
Fixed offline registered patient loading which got broken by newest c…
manuelroemer 8f2ed2b
Use dedicated offline visit.
manuelroemer 9c57ff2
Moved FHIR mapping to patient-registration.
manuelroemer b61ed18
Adapt to updated patient registration typings.
manuelroemer 6bb471c
Merge branch 'master' of github.com:openmrs/openmrs-esm-patient-chart…
manuelroemer 1d93b68
Pin @jsenv/file-size-impact dep.
manuelroemer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import { NewVisitPayload, Visit } from '@openmrs/esm-api'; | ||
import { NewVisitPayload } from '@openmrs/esm-api'; | ||
import { | ||
getStartedVisit, | ||
getSynchronizationItems, | ||
|
@@ -10,16 +10,19 @@ import { | |
VisitStatus, | ||
subscribeConnectivity, | ||
QueueItemDescriptor, | ||
usePatient, | ||
} from '@openmrs/esm-framework'; | ||
import { useEffect } from 'react'; | ||
import { v4 } from 'uuid'; | ||
import useSWR, { SWRResponse } from 'swr'; | ||
|
||
const visitSyncType = 'visit'; | ||
const patientRegistrationSyncType = 'patient-registration'; | ||
|
||
interface OfflineVisit extends NewVisitPayload { | ||
uuid: string; | ||
} | ||
|
||
const visitSyncType = 'visit'; | ||
|
||
export function setupCacheableRoutes() { | ||
messageOmrsServiceWorker({ | ||
type: 'registerDynamicRoute', | ||
|
@@ -33,7 +36,7 @@ export function setupCacheableRoutes() { | |
} | ||
|
||
export function setupOfflineVisitsSync() { | ||
setupOfflineSync<OfflineVisit>(visitSyncType, [], async (visit, options) => { | ||
setupOfflineSync<OfflineVisit>(visitSyncType, [patientRegistrationSyncType], async (visit, options) => { | ||
const visitPayload = { | ||
...visit, | ||
stopDatetime: new Date(), | ||
|
@@ -73,19 +76,30 @@ async function getOfflineVisitForPatient(patientUuid: string) { | |
} | ||
|
||
async function createOfflineVisitForPatient(patientUuid: string, location: string) { | ||
const patientRegistrationSyncItems = await getSynchronizationItems<any>(patientRegistrationSyncType); | ||
const isVisitForOfflineRegisteredPatient = patientRegistrationSyncItems.some( | ||
(item) => item.fhirPatient.id === patientUuid, | ||
); | ||
|
||
const offlineVisit: OfflineVisit = { | ||
uuid: v4(), | ||
patient: patientUuid, | ||
startDatetime: new Date(), | ||
location, | ||
// TODO: This UUID belongs to the "Facility Visit" type. | ||
// This should be replaced with the dedicated offline visit as soon as it exists in the BE. | ||
visitType: '7b0f5697-27e3-40c4-8bae-f4049abfb4ed', | ||
visitType: 'a22733fa-3501-4020-a520-da024eeff088', // "Offline" visit type UUID. | ||
}; | ||
|
||
const descriptor: QueueItemDescriptor = { | ||
id: offlineVisit.uuid, | ||
dependencies: [], | ||
displayName: 'Offline visit', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this be translated? |
||
dependencies: isVisitForOfflineRegisteredPatient | ||
? [ | ||
{ | ||
type: patientRegistrationSyncType, | ||
id: patientUuid, | ||
}, | ||
] | ||
: [], | ||
}; | ||
|
||
await queueSynchronizationItem(visitSyncType, offlineVisit, descriptor); | ||
|
@@ -95,15 +109,67 @@ async function createOfflineVisitForPatient(patientUuid: string, location: strin | |
function offlineVisitToVisit(offlineVisit: OfflineVisit) { | ||
return { | ||
uuid: offlineVisit.uuid, | ||
startDatetime: offlineVisit.startDatetime.toString(), | ||
stopDatetime: offlineVisit.stopDatetime.toString(), | ||
startDatetime: offlineVisit.startDatetime?.toString(), | ||
stopDatetime: offlineVisit.stopDatetime?.toString(), | ||
encounters: [], | ||
visitType: { | ||
uuid: offlineVisit.visitType, | ||
display: 'OFFLINE_VISIT_PLACEHOLDER', | ||
display: 'Offline', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this be translated? |
||
}, | ||
patient: { | ||
uuid: offlineVisit.patient, | ||
}, | ||
}; | ||
} | ||
|
||
export function usePatientOrOfflineRegisteredPatient(patientUuid: string): ReturnType<typeof usePatient> { | ||
const onlinePatientState = usePatient(patientUuid); | ||
const offlinePatientState = useSWR(`offlineRegisteredPatient/${patientUuid}`, async () => { | ||
const offlinePatient = await getOfflineRegisteredPatientAsFhirPatient(patientUuid); | ||
if (!offlinePatient) { | ||
throw new Error(`No offline registered patient could be found. UUID: ${patientUuid}`); | ||
} | ||
|
||
return offlinePatient; | ||
}); | ||
|
||
if (onlinePatientState.isLoading || (!offlinePatientState.data && !offlinePatientState.error)) { | ||
return { | ||
isLoading: true, | ||
patient: null, | ||
patientUuid, | ||
error: null, | ||
}; | ||
} | ||
|
||
if (onlinePatientState.patient && !(onlinePatientState.patient as any).issue) { | ||
return { | ||
isLoading: false, | ||
patient: onlinePatientState.patient, | ||
patientUuid, | ||
error: null, | ||
}; | ||
} | ||
|
||
if (offlinePatientState.data) { | ||
return { | ||
isLoading: false, | ||
patient: offlinePatientState.data, | ||
patientUuid, | ||
error: null, | ||
}; | ||
} | ||
|
||
return { | ||
isLoading: false, | ||
patient: null, | ||
patientUuid, | ||
error: onlinePatientState.error ?? offlinePatientState.error, | ||
}; | ||
} | ||
|
||
async function getOfflineRegisteredPatientAsFhirPatient(patientUuid: string): Promise<fhir.Patient | undefined> { | ||
const patientRegistrationSyncItems = await getSynchronizationItems<any>(patientRegistrationSyncType); | ||
const patientSyncItem = patientRegistrationSyncItems.find((item) => item.fhirPatient.id === patientUuid); | ||
return patientSyncItem.fhirPatient; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this be translated?