async def move_service_request_to_appointment(operation, request): """ Перемещение заявки между консилиумами POST /move_service_request_to_appointment """ requirement_args = ['appointment_id', 'service_request_id'] req: dict = request.get('resource', {}) if not is_required_args_present(requirement_args, list(req.keys())): return error_response('Some of requirements fields not present', web) service_request_id: str = req.get('service_request_id') appointment_id: str = req.get('appointment_id') new_appointment: Resource = Appointment(sdk, appointment_id) service_request: Resource = ServiceRequest(sdk, service_request_id) try: """ Нажожу консилиум по привязаной к нему заявке, создаю экземпляр клясса с id найденого консилиума и удаляю привязку к заявке """ old_appointment_: AbstractResource = (await Appointment.search(sdk, {'based_on': service_request_id}))[0] old_appointment: Resource = Appointment(sdk, old_appointment_.get('id')) await old_appointment.remove_fields(['basedOn']) """ Удаляю привязку к слоту старого консилиума """ slot_id: str = await service_request.remove_slot() """Проверяем что это за слот, если overbooked, то удаляем, если нет, то меняем статус на free""" slot: Slot = Slot(sdk, slot_id) slot_: AbstractResource = await slot.get() if slot_.get('overbooked'): await old_appointment.remove_subfields(('slot', [slot_id])) await slot.delete() else: await slot.update(field='status', val='free') """ Приязываю заявку к новому консилиуму """ await new_appointment.add_fields({'basedOn': [{'resourceType': 'ServiceRequest', 'id': service_request_id}]}) """ Достаю слоты из нового консилиума и привязывю свободный к заявке """ new_appointment_: AbstractResource = await new_appointment.get() slot_id, overbooked = await _get_free_slot(new_appointment_.get('slot')) if overbooked: await new_appointment.add_subfields(('slot', [{'id': slot_id, 'resourceType': 'Slot'}])) await service_request.add_subfields(('supportingInfo', [{'id': slot_id, 'resourceType': 'Slot'}])) except BaseException as e: return error_response(str(e), web) return success_response({}, web)
async def test_add_fields_appointment(self, create_appointment, create_service_request): await self._appointment.add_fields({ 'basedOn': [{ 'resourceType': 'ServiceRequest', 'id': self._service_request.get_id() }] }) appointment: AbstractResource = Appointment.search( {'based_on': self._service_request.get_id()})[0] assert appointment.id == self._appointment.get_id()
async def create_service_request(operation, request): """ Создание заявки на консилиум POST /create_service_request """ requirement_args = ['appointment_id', 'patient_id', 'condition_id'] req: dict = request.get('resource', {}) if not is_required_args_present(requirement_args, list(req.keys())): return error_response('Some of requirements fields not present', web) patient_id: str = req.get('patient_id') condition_id: str = req.get('condition_id') appointment_id: str = req.get('appointment_id') appointment: Resource = Appointment(sdk, appointment_id) service_request: Resource = ServiceRequest(sdk) fields: dict = { 'supportingInfo': [ {'id': 'ad3f8465-b454-494c-8351-e85f282f48c4', 'resourceType': 'Composition'}, ], 'authoredOn': datetime.now().strftime('%Y-%m-%dT%H:%M:%S'), 'status': 'draft', 'intent': "proposal", 'subject': {'resourceType': 'Patient', 'id': patient_id}, 'requester': {'resourceType': 'Organization', 'id': '576bb080-cf42-401a-9be9-1fa09c49c372'}, 'reasonReference': [{'resourceType': 'Condition', 'id': condition_id}], } try: appointment_: AbstractResource = await appointment.get() slot_id, overbooked = await _get_free_slot(appointment_['slot']) if overbooked: await appointment.add_subfields(('slot', [{'id': slot_id, 'resourceType': 'Slot'}])) if fields.get('supportingInfo') is not None and isinstance(fields.get('supportingInfo'), list): fields['supportingInfo'].append({'id': slot_id, 'resourceType': 'Slot'}) service_request_id: str = await service_request.create(fields) await appointment.add_fields({'basedOn': [{'resourceType': 'ServiceRequest', 'id': service_request_id}]}) except BaseException as e: logger.error(str(e)) return error_response(str(e), web) return success_response({'id': service_request_id}, web)
async def test_remove_fields_appointment(self, create_appointment, create_service_request): await self._appointment.remove_fields(['basedOn']) with pytest.raises(BaseException): assert Appointment.search( {'based_on': self._service_request.get_id()})
async def test_create_appointment(self): appointment: Appointment = Appointment(self._sdk) pk: str = await appointment.create(self._appointment_fields) assert isinstance(pk, str) and len(pk) > 0
class TestApiClasses: """""" _appointment_id: str = '' _service_request_id: str = '' _organisation_id: str = '' _appointment_fields: dict = {} _service_request_fields: dict = {} _sdk: SDK = None _appointment: Appointment = None _service_request: ServiceRequest = None _settings: Settings = None def setup_class(self): self._settings = Settings(**{}) with open('resources.json', 'r') as resource: resources: dict = json.loads(resource.read()) self._sdk = SDK(self._settings, resources=resources, seeds={}) self._appointment_fields = { 'identifier': [{ 'use': 'official', 'value': 'Some name', 'assigner': { 'resourceType': 'Organization', 'id': self._organisation_id, 'type': 'Organization' } }], 'description': 'Some place', 'start': '2020-12-12T12:12:12', 'end': '2020-12-12T12:12:13', 'participant': [ { "actor": { 'id': 'dfaa8925-f32e-4687-8fd0-272844cff544', 'resourceType': 'Patient' }, "required": "required", "status": "accepted" }, ], 'status': 'pending', 'created': datetime.now().strftime('%Y-%m-%dT%H:%M:%S'), 'comment': 'Some comment', 'slot': [{ 'id': 'ad3f8066-b464-474c-8151-e85f202f18c4', 'resourceType': 'Slot' }], 'supportingInformation': [{ 'id': 'ad3f8064-b484-474c-8851-e853202f18c5', 'resourceType': 'DiagnosticReport' }, { 'id': 'ad3f8066-b464-474c-8151-e85f202f18c4', 'resourceType': 'Slot' }] } self._service_request_fields = { 'supportingInfo': [{ 'id': 'ad3f8465-b454-494c-8351-e85f282f48c4', 'resourceType': 'Composition' }, { 'id': 'cd3f8266-d464-474c-7151-s85f502f18m2', 'resourceType': 'Slot' }], 'authoredOn': datetime.now().strftime('%Y-%m-%dT%H:%M:%S'), 'status': 'draft', 'intent': "proposal", 'subject': { 'resourceType': 'Patient', 'id': 'dfaa8925-f32e-4687-8fd0-272844cff544' }, 'requester': { 'resourceType': 'Organization', 'id': '576bb080-cf42-401a-9be9-1fa09c49c372' }, 'reasonReference': [{ 'resourceType': 'Condition', 'id': '0adceaf4-36a7-44cb-b938-b84a3da0fb1f' }], } def teardown_class(self): del self._organisation_id del self._service_request_id del self._service_request_fields del self._service_request del self._appointment_id del self._appointment_fields del self._appointment del self._sdk @pytest.fixture(scope='class', autouse=True) async def init_sdk(self): await create_app(self._settings, self._sdk, debug=True) @pytest.fixture() async def create_appointment(self): if self._appointment is None: self._appointment = Appointment(self._sdk) await self._appointment.create(self._appointment_fields) @pytest.fixture() async def create_service_request(self): if self._service_request is None: self._service_request = ServiceRequest(self._sdk) await self._service_request.create(self._service_request_fields) @pytest.mark.asyncio async def test_create_appointment(self): appointment: Appointment = Appointment(self._sdk) pk: str = await appointment.create(self._appointment_fields) assert isinstance(pk, str) and len(pk) > 0 @pytest.mark.asyncio async def test_add_fields_appointment(self, create_appointment, create_service_request): await self._appointment.add_fields({ 'basedOn': [{ 'resourceType': 'ServiceRequest', 'id': self._service_request.get_id() }] }) appointment: AbstractResource = Appointment.search( {'based_on': self._service_request.get_id()})[0] assert appointment.id == self._appointment.get_id() @pytest.mark.asyncio async def test_remove_fields_appointment(self, create_appointment, create_service_request): await self._appointment.remove_fields(['basedOn']) with pytest.raises(BaseException): assert Appointment.search( {'based_on': self._service_request.get_id()}) @pytest.mark.asyncio async def test_get_appointment(self, create_appointment): appointment = await self._appointment.get() assert appointment.id == self._appointment.get_id() @pytest.mark.asyncio async def test_delete_appointment(self, create_appointment): await self._appointment.delete() with pytest.raises(BaseException): assert self._appointment.get()
async def create_appointment(self): if self._appointment is None: self._appointment = Appointment(self._sdk) await self._appointment.create(self._appointment_fields)
async def create_appointment(operation, request: dict): """ Создание консилиума POST /create_appointment """ requirement_args: list = ['name', 'begin_date', 'organisation_id', 'place'] req: dict = request.get('resource', {}) if not is_required_args_present(requirement_args, list(req.keys())): return error_response('Some of requirements fields not present', web) appointment: Resource = Appointment(sdk) slot: Resource = Slot(sdk) name: str = req.get('name') begin_date: str = req.get('begin_date').replace(' ', 'T') end_date: str = req.get('end_date').replace(' ', 'T') organisation_id: str = req.get('organisation_id') place: str = req.get('place') fields: dict = { 'identifier': [{'use': 'official', 'value': name, 'assigner': {'resourceType': 'Organization', 'id': organisation_id, 'type': 'Organization'} }], 'description': place, 'start': begin_date, 'end': end_date, 'participant': [ {"actor": {'id': 'dfaa8925-f32e-4687-8fd0-272844cff544', 'resourceType': 'Patient'}, "required": "required", "status": "accepted" }, ], 'status': 'pending', 'created': datetime.now().strftime('%Y-%m-%dT%H:%M:%S'), 'comment': 'Some comment', } slot_fields: dict = { 'status': 'free', 'overbooked': False, 'comment': 'Some comment', 'schedule': { 'id': 'ad3f8065-b454-474c-8151-e85f202f18c5', 'resourceType': 'Schedule' }, } try: slots: List[dict] = [] for _ in range(9): slot_fields['end'] = (datetime.now() + timedelta(days=7)).strftime('%Y-%m-%dT%H:%M:%S') slot_fields['start'] = datetime.now().strftime('%Y-%m-%dT%H:%M:%S') slot_id: str = await slot.create(slot_fields) slots.append({'id': slot_id, 'resourceType': 'Slot'}) fields['slot'] = slots appointment_id: str = await appointment.create(fields) return success_response({'id': appointment_id}, web) except BaseException as e: logger.error(str(e)) return error_response(str(e), web)