This method is designed to process previously created Medication dispense
Key points
Only authenticated and authorized users of a legal entity who has created Medication dispense can process Medication dispense
Medication dispense can be processed only from ‘NEW' status.
The process should be signed with DS. So, all the Medication dispense data should be submitted.
Invoke Get Medication dispense details by Pharmacy User to obtain previously created Medication dispense. In order to process dispense payment_id (optional) and payment_amount (optional, if medical program has funding sourse != NHS) should be added.
Посилання на ресурс, наприклад: /api/persons/create
Scope
medication_dispense:process
Scope для доступу
Components
ePrescription
Зазначається перелік бізнес компонентів, які використовують цей метод, наприклад: ePrescription
Microservices
API paragraph not found
Перелік мікросервісів, які використовує метод API, наприклад: Auth, ABAC
Protocol type
REST
Тип протоколу, який використовується запитом, наприклад: SOAP | REST
Request type
PATCH
Тип запиту API, наприклад: GET, POST, PATCH…
Sync/Async
Sync
Метод є синхронним чи асинхронним?
Public/Private/Internal
Public
Logic
Save signed content to media storage
Update Medication dispense data in OPS DB:
set status = ‘PROCESSED’
updated_by = user_id
updated_at = now()
Update related Medication request data in OPS DB:
set status = ‘COMPLETED’
updated_by = user_id
updated_at = now()
Write changes to event manager (Medication dispense and Medication request)
Change Care plan activity (If related medication request has reference on Care plan, then corresponding care plan activity should be changed in async way after Medication dispense processed. Update activity events invokes using ops.medication_dispense_process table):
Set activity status to in_progress if previous activity status was scheduled
Check the MD identifier in outcome_reference attribute of the activity:
if not found:
Add reference on the medication dispense resource to the outcome_reference attribute .
If quantity in the activity was set then calculate and set remaining quantity:
select all MD in status PROCESSED related to MR which based on care plan and current activity
calculate dispensed quantity in the current activity as sum of medication_qty in the filtered MD list
calculate remaining quantity by subtracting dispensed quantity from activity quantity
update remaining_quantity value in the activity with calculated value
If found, then skip updating activity (it means that activity has been already updated by event from current MD processing).
Preconditions
If skip_medication_dispense_sign=false, MD should be created
in case of error - return 400 (“document must be signed by 1 signer but contains 0 signatures”)
Validate that DS is valid and not expired
Validate that DS belongs to the user
Check that DRFO from DS and party.tax_id matches
in case of error - return 422 (“Does not match the signer drfo“)
Check that Last Name from DS and party.last_name matches
in case of error - return 422 (“Does not match the signer last name“).
Validate Medication dispense
Get Medication dispense identifier from the URL
Check Medication dispense exists in DB (OPS)
Return 404 ('not_found') in case of error
Check Medication dispense belongs to legal entity
Return 404 ('not_found') in case of error
Validate User
Extract user_id from token
Return 404 ('not_found') in case employee didn’t create medication dispense
Validate Care plan and Activity
It is needed to check if medication request with a program allowed to be created for specified care plan and activity.
If based_on submitted, check field has array with two values of Reference type: one is valid Care plan resource, another - Activity resource.
Verify Care plan:
It should belong to the same person as set in MRR
in case of error return 422 with msg ("Care plan not found")
It should be in active status
Verify submitted Activity:
It belongs to the Care plan.
in case of error return 422 with msg ("Activity not found")
It has activity.detail.kind=medication_request; activity.detail.product_reference=medication_id.
in case of error return 422 with msg ("Invalid activity kind")
It has scheduled, in_progress status
in case of error return 422 with msg ("Invalid activity status")
If it has quantity then calculate remaining quantity:
select all MRR in status NEW which based on current activity
select all MR in statuses ACTIVE, COMPLETED based on current activity
calculate reserved at the moment medication quantity as sum of medication_qty in the filtered MRR and MR list, including medication_qty from current MRR
calculate remaining quantity by subtracting reserved quantity from activity quantity
Check remaining quantity is greater then or equal to zero
in case of error return 409 "The total amount of the prescribed medication quantity exceeds quantity in care plan activity"
check that medical_program_id equal to $.activity[].program
in case of error return 422 with msg ("Medical program from activity should be equal to medical program from request")
Validate started_at/ended_at of Medication request Request:
if care plan activity has detail.scheduled_timing.repeat.bounds_period - validate started_at/ended_at within bounds_period
if care plan activity has detail.scheduled_period - validate started_at/ended_at within scheduled_period
else - validate started_at/ended_at within care_plan.period
in the case of started_at/ended_at is not within care_plan.period return 422 with msg ("Invalid care plan period")
Validate signed content
Check that signed content is equal to online generated dispense with ids in DB
Do not compare:
payment_amount field
payment_id field
medication_request/legal_entity object
medication_request/division object
medication_request/employee object
medication_request/person/id field
medication_request/rejected_at field
medication_request/rejected_by field
in case error return 422 ("Signed content does not match to previously created dispense")
Validate transition
Get status of Medication dispense by $.id in OPS DB. Check that Medication dispense is in status ‘NEW’
in case of error - return 409 ("Can't update medication dispense status from {status} to PROCESSED")
Write changes to event manager (Medication dispense and Medication request)
Change Care plan activity (If related medication request has reference on Care plan, then corresponding care plan activity should be changed after Medication dispense processed):
Set activity status to in_progress if previous activity status was scheduled
Check the MD identifier in outcome_reference attribute of the activity:
if not found:
Add reference on the medication dispense resource to the outcome_reference attribute .
If quantity in the activity was set - validate $.activity.details.remaining_quantity_type:
if $.activity.details.remaining_quantity_type =for_request than:
select all MRR in status NEW which based on current activity
select all MR in statuses ACTIVE based on current activity
select all MD (medication dispenses) in status PROCESSED related to the Medication requests which are in status COMPLETE, REJECTED, EXPIRED
calculate:
reserved_qty as sum of medication_qty in the filtered MRR and MR list
dispensed_qty as sum of medication_qty in the filtered MD list
calculate and update remaining quantity by subtracting reserved_quantity and dispensed_qty from activity quantity
if $.activity.details.remaining_quantity_type = for_use than:
select all MD in status PROCESSED related to MR which based on care plan and current activity
calculate dispensed quantity in the current activity as sum of medication_qty in the filtered MD list
calculate remaining quantity by subtracting dispensed quantity from activity quantity
update remaining_quantity value in the activity with calculated value
If found, then skip updating activity (it means that activity has been already updated by event from current MD processing).
HTTP status codes
HTTP status code
Message
What caused the error
HTTP status code
Message
What caused the error
200
Response
400
Document must be signed by 1 signer but contains 0 signatures
401
Invalid access token
403
Your scope does not allow to access this resource. Missing allowances: medication_dispense:process
409
Can't update medication dispense status from {status} to PROCESSED.
Medication request is not active.
Medication request is blocked.
Care plan is not active.
Care plan activity should be scheduled or in_progress.
422
Does not match the signer drfo.
Does not match the signer last name
Signed content does not match to previously created dispense.