ЕСОЗ - публічна документація

(RC-1 MRIN) Process Medication Dispense

Purpose

This method is designed to process previously created Medication Dispense

Status Charts (reimbursement)

Specification

Apiary

Sequence diagram

WS logic

Authorize user

  1. Verify the validity of access token

    1. Return 401 in case validation fails

  2. Check user scopes in order to perform this action (scope = 'medication_dispense:process')

    1. 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:

  1. Care plan is not in final status.

    1. In case of error - return 409 error (Care plan is not active)

  2. Corresponding Care plan activity is not in final status.

    1. In case of error - return 409 error (Care plan activity should be scheduled or in_progress)

Validate context

  1. Only users of legal entity who has created Medication Dispense and NHS Admin can process Medication Dispense

    1. 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

  1. 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)

  1. Dispense should be active in order to process it

    1. is_active = true

    2. 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

  1. 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

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

See: Status Charts (reimbursement)

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

Parameter

Source

Description

status

Const: COMPLETED

See: Status Charts (reimbursement)

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.

  1. Set activity status to in_progress if previous activity status was scheduled 

  2. Check the MD identifier in outcome_reference attribute of the activity:

    1. if not found:

      1. Add reference on the medication dispense resource to the outcome_reference attribute .

      2. If quantity in the activity was set then calculate and set remaining quantity:

        1. select all MD in status PROCESSED related to MR which based on care plan and current activity

        2. calculate dispensed quantity in the current activity as sum of medication_qty in the filtered MD list

        3. calculate remaining quantity by subtracting dispensed quantity from activity quantity

        4. update remaining_quantity value in the activity with calculated value

    2. 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

field

value

event_type

StatusChangeEvent

entity_type

MedicationDispense

entity_id

$.id

properties.status.new_value

$.status

event_time

$.update_at

changed_by

$.changed_by



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

field

value

event_type

StatusChangeEvent

entity_type

MedicationRequest

entity_id

$.id

properties.status.new_value

$.status

event_time

$.update_at

changed_by

$.changed_by



ЕСОЗ - публічна документація