generated from openmrs/openmrs-esm-template-app
-
Notifications
You must be signed in to change notification settings - Fork 33
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
(feat) : O3-3686 Add ability to close active visit on medication dispense #115
Open
donaldkibet
wants to merge
2
commits into
main
Choose a base branch
from
O3-3686
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,203 @@ | ||
import { getVisitTypes, showSnackbar, updateVisit } from '@openmrs/esm-framework'; | ||
import { saveMedicationDispense } from '../medication-dispense/medication-dispense.resource'; | ||
import { updateMedicationRequestFulfillerStatus } from '../medication-request/medication-request.resource'; | ||
import { type DispenseFormHandlerParams, MedicationDispenseStatus } from '../types'; | ||
import { computeNewFulfillerStatusAfterDispenseEvent, getFulfillerStatus, getUuidFromReference } from '../utils'; | ||
import { createStockDispenseRequestPayload, sendStockDispenseRequest } from './stock-dispense/stock.resource'; | ||
|
||
class Handler { | ||
nextHandler: Handler | null; | ||
|
||
constructor() { | ||
this.nextHandler = null; | ||
} | ||
|
||
setNext(handler) { | ||
this.nextHandler = handler; | ||
return handler; | ||
} | ||
|
||
handle(request: DispenseFormHandlerParams) { | ||
if (this.nextHandler) { | ||
return this.nextHandler.handle(request); | ||
} | ||
return Promise.resolve(request); | ||
} | ||
} | ||
|
||
class MedicationDispenseHandler extends Handler { | ||
handle(request) { | ||
const { medicationDispensePayload, medicationRequestBundle, config, abortController } = request; | ||
return saveMedicationDispense(medicationDispensePayload, MedicationDispenseStatus.completed, abortController) | ||
.then((response) => { | ||
if (response.ok) { | ||
const newFulfillerStatus = computeNewFulfillerStatusAfterDispenseEvent( | ||
medicationDispensePayload, | ||
medicationRequestBundle, | ||
config.dispenseBehavior.restrictTotalQuantityDispensed, | ||
); | ||
if (getFulfillerStatus(medicationRequestBundle.request) !== newFulfillerStatus) { | ||
return updateMedicationRequestFulfillerStatus( | ||
getUuidFromReference(medicationDispensePayload.authorizingPrescription[0].reference), | ||
newFulfillerStatus, | ||
); | ||
} | ||
} | ||
return response; | ||
}) | ||
.then((response) => { | ||
request.response = response; | ||
return super.handle(request); | ||
}); | ||
} | ||
} | ||
|
||
class StockDispenseHandler extends Handler { | ||
async handle(request) { | ||
const { | ||
response, | ||
config, | ||
inventoryItem, | ||
patientUuid, | ||
encounterUuid, | ||
medicationDispensePayload, | ||
abortController, | ||
t, | ||
} = request; | ||
const { status } = response; | ||
|
||
if (config.enableStockDispense && (status === 201 || status === 200)) { | ||
try { | ||
const stockDispenseRequestPayload = createStockDispenseRequestPayload( | ||
inventoryItem, | ||
patientUuid, | ||
encounterUuid, | ||
medicationDispensePayload, | ||
); | ||
await sendStockDispenseRequest(stockDispenseRequestPayload, abortController); | ||
showSnackbar({ | ||
isLowContrast: true, | ||
title: t('stockDispensed', 'Stock dispensed'), | ||
kind: 'success', | ||
subtitle: t('stockDispensedSuccessfully', 'Stock dispensed successfully and batch level updated.'), | ||
}); | ||
} catch (error) { | ||
showSnackbar({ | ||
title: t('stockDispensedError', 'Stock dispensed error'), | ||
kind: 'error', | ||
isLowContrast: true, | ||
timeoutInMs: 5000, | ||
subtitle: error?.message, | ||
}); | ||
} | ||
} | ||
|
||
return super.handle(request); | ||
} | ||
} | ||
|
||
class EndCurrentVisitHandler extends Handler { | ||
async handle(request) { | ||
const { currentVisit, abortController, closeVisitOnDispense, response, t, config } = request; | ||
|
||
try { | ||
const visitTypes = await getVisitTypes().toPromise(); | ||
const shouldCloseVisit = | ||
shouldEndVisitOnDispense(currentVisit, visitTypes, response.status, config) && closeVisitOnDispense; | ||
|
||
if (shouldCloseVisit) { | ||
const updateResponse = await updateVisit( | ||
currentVisit.uuid, | ||
{ | ||
stopDatetime: new Date(), | ||
location: currentVisit.location.uuid, | ||
startDatetime: undefined, | ||
visitType: currentVisit?.visitType.uuid, | ||
}, | ||
abortController, | ||
).toPromise(); | ||
|
||
showSnackbar({ | ||
title: t('visitClose', 'Visit closed'), | ||
kind: 'success', | ||
subtitle: t('visitClosedSuccessfully', 'Visit closed successfully.'), | ||
}); | ||
request.response = updateResponse; | ||
} | ||
} catch (error) { | ||
showSnackbar({ title: 'Close visit error', kind: 'error', subtitle: error?.message }); | ||
} | ||
|
||
return super.handle(request); | ||
} | ||
} | ||
|
||
class FinalResponseHandler extends Handler { | ||
handle(request) { | ||
const { response, closeOverlay, revalidate, encounterUuid, setIsSubmitting, mode, t } = request; | ||
const { status } = response; | ||
|
||
if (status === 201 || status === 200) { | ||
closeOverlay(); | ||
revalidate(encounterUuid); | ||
showSnackbar({ | ||
isLowContrast: true, | ||
kind: 'success', | ||
subtitle: t('medicationListUpdated', 'Medication dispense list has been updated.'), | ||
title: t( | ||
mode === 'enter' ? 'medicationDispensed' : 'medicationDispenseUpdated', | ||
mode === 'enter' ? 'Medication successfully dispensed.' : 'Dispense record successfully updated.', | ||
), | ||
timeoutInMs: 5000, | ||
}); | ||
} else { | ||
showSnackbar({ | ||
title: t( | ||
mode === 'enter' ? 'medicationDispenseError' : 'medicationDispenseUpdatedError', | ||
mode === 'enter' ? 'Error dispensing medication.' : 'Error updating dispense record', | ||
), | ||
kind: 'error', | ||
isLowContrast: true, | ||
subtitle: response.error?.message, | ||
}); | ||
setIsSubmitting(false); | ||
} | ||
|
||
return super.handle(request); | ||
} | ||
} | ||
|
||
const setupChain = () => { | ||
const medicationDispenseHandler = new MedicationDispenseHandler(); | ||
const stockDispenseHandler = new StockDispenseHandler(); | ||
const closeActiveVisitHandler = new EndCurrentVisitHandler(); | ||
const finalResponseHandler = new FinalResponseHandler(); | ||
|
||
medicationDispenseHandler | ||
.setNext(stockDispenseHandler) | ||
.setNext(closeActiveVisitHandler) | ||
.setNext(finalResponseHandler); | ||
|
||
return medicationDispenseHandler; | ||
}; | ||
|
||
export const executeMedicationDispenseChain = (params) => { | ||
const chain = setupChain(); | ||
return chain.handle(params); | ||
}; | ||
|
||
/** | ||
* Determines whether the current visit should be closed based on the provided parameters. | ||
* | ||
* @param {object} currentVisit - The current visit object. | ||
* @param {Array} visitTypes - An array of allowed visit types. | ||
* @param {number} status - The status code of the request. | ||
* @param {object} config - The configuration object. | ||
* @returns {boolean} - Returns true if the current visit should be closed, false otherwise. | ||
*/ | ||
function shouldEndVisitOnDispense(currentVisit, visitTypes, status, config): boolean { | ||
if (!currentVisit || !visitTypes) return false; | ||
|
||
const hasAllowedVisitType = visitTypes.some((vt) => vt.uuid === currentVisit.visitType.uuid); | ||
return hasAllowedVisitType && config.closeVisitOnDispense.enabled && (status === 200 || status === 201); | ||
} |
Oops, something went wrong.
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.
Minor note, you might want to clarify that this renders a checkbox to close the visit, it doesn't automatically close the visit (if I'm understanding correctly)
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.
+1