ЕСОЗ - публічна документація
(RC-1 MRIN) Process Medication Dispense
- 1 Purpose
- 2 Specification
- 3 WS logic
- 3.1 Authorize user
- 3.2 Validate request
- 3.2.1 Digital signature
- 3.2.2 Validate user
- 3.2.3 Validate client
- 3.2.4 Validate FK
- 3.2.5 Validate related Medication request
- 3.2.6 Validate context
- 3.2.7 Validate fields
- 3.2.8 Validate signed medication dispense
- 3.2.9 Validate status (transition)
- 3.2.10 Validate payment_amount
- 3.3 Save signed content
- 3.4 Change dispense status
- 3.5 Change medication request status
- 3.6 Change Care plan activity
- 3.7 Add status to event manager
- 3.7.1 For medication dispense
- 3.7.2 for medication request
Purpose
This method is designed to process previously created Medication Dispense
Specification
WS logic
Authorize user
Verify the validity of access token
Return 401 in case validation fails
Check user scopes in order to perform this action (scope = 'medication_dispense:process')
Return 403 in case invalid scope(s)
Validate request
Digital signature
Decode content that is encrypted in an electronic digital signature.
Use Digital signature WS. Method checks digital signature and returns result.
See service specification
Validate user
get user_id from token
Check if drfo in DS = (SELECT tax_id FROM party_users JOIN parties ON party_users.party_id = parties.id WHERE user_id='$.user_id')
and surname in DS = (SELECT last_name FROM party_users JOIN parties ON party_users.party_id = parties.id WHERE user_id='$.user_id')
in case error return 422 "DS does not match to user"
Validate client
Extract legal_entity_id (client_id) from token
Check edrpou if exists or drfo in DS = (SELECT edrpou from legal_entities where id='$.client_id')
in case error return 422 "DS edrpou does not match to legal_entity"
Validate FK
1. Validate legal_entity_id - legal_entity_id exists
In case error - return 422 error:
{:error, [{%{
description: "Legal entity not found",
params: [],
rule: :invalid
}, "$.legal_entity_id"}]}
2. Validate id - dispense.id exists
In case error - return 404 error
Validate related Medication request
Check if related medication request has reference on the Care plan - based_on attribute filled. If it has, validate following:
Care plan is not in final status.
In case of error - return 409 error (Care plan is not active)
Corresponding Care plan activity is not in final status.
In case of error - return 409 error (Care plan activity should be scheduled or in_progress)
Validate context
Only users of legal entity who has created Medication Dispense and NHS Admin can process Medication Dispense
medication_dispense.legal_entity_id = client_id (context) or client_id (context) (type) in TOKENS_TYPE_ADMIN
In case error - return 403 error (type: forbidden)
Validate fields
Validate medication dispense with json schema, in case error return 422
Validate signed medication dispense
Check that signed content is equal to online generated dispense with ids in DB
Do not compare next fields
payment_amount
payment_id
in case error return 422 "Signed content does not match to previously created dispense"
Validate status (transition)
Dispense should be active in order to process it
is_active = true
status = 'NEW'
In case error - return 409 error (type: 'invalid_transition', message: 'Can't update medication dispense status from {status} to PROCESSED')
Validate payment_amount
Payment_amount should be >=0
In case of error - return 422 error (message: 'Payment amount should be greater or equal to 0')
Save signed content
Save singed content to the bucket `dispenses`
Change dispense status
Update dispense status to PROCESSED
Parameter | Source | Description |
---|---|---|
payment_id | Request: $.payment_id or NULL | If payment_id exists in request - update record with payment_id |
payment_amount | Request: $.payment_amount or NULL | If payment_amount exists in request - update record with payment_amount |
status | Const: PROCESSED | |
updated_at | Timestamp: now() | Get current date-time |
updated_by | Token: user_id | Extract user from token |
Change medication request status
In case medication dispense is divided in several attempts, the medication request status remains ACTIVE until the full medication quantity is dispensed or moves to EXPIRED if the patient does not dispense the all of the medication quantity from corresponding medication request.
Update medication request status to COMPLETED in case all of the medication quantity is dispensed
Parameter | Source | Description |
---|---|---|
status | Const: COMPLETED | |
updated_at | Timestamp: now() | Get current date-time |
updated_by | Token: user_id | Extract user from token |
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.
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).
Add status to event manager
For medication dispense
After status was changed (status = PROCESSED) - add new status to event_manager
field | value |
---|---|
|
|
| MedicationDispense |
| $.id |
| $.status |
| $.update_at |
| $. |
for medication request
Medication request doesn't move to COMPLETED status in case not of all medication quantity is dispensed.
After status was changed (status = COMPLETED or EXPIRED) - add new status to event_manager:
field | value |
---|---|
|
|
| MedicationRequest |
| $.id |
| $.status |
| $.update_at |
| $. |
ЕСОЗ - публічна документація