...
Validate preperson_id UUID
In case of error - return 422
Check preperson_id doesn't exist in mpi.persons, but exists in medical_data.patients as patient_id. It means that preperson_id relates to preperson, but not to person.
In case of error - return 422 (Such preperson doesn't exist)
Check preperson status is active in medical_data.patients
In case of error - return 409 (Preperson is not active)
Check if exists another preperson merge requests with status NEW or APPROVED for this preperson
if exists - update status to REJECTED
Check if preperson has at least one episode
if no episodes - return 422 (Preperson has no episodes)
Validate employee
Extract user_id and legal_entity_id from token and find corresponding employee_id
Check employee status is APPROVED
In case of error - return 422 error (Only active employee can create preperson merge request)
If employee has division - validate it has status ACTIVE
In case of error - return 422 error (Only employee of active divisions can create preperson merge request)
...