add sending medical_card_id to person while creating

This commit is contained in:
dderbentsov
2023-10-09 00:29:35 +03:00
parent e07d1a1d02
commit 719e81edd0

View File

@@ -22,9 +22,12 @@ class MedicalCardService(BaseService[models.MedicalCard, MedicalCardCreateIn, An
LOCAL_PERSONAL_INFO_URL = 'http://localhost:8001'
self.PERSONAL_INFO_URL = os.environ.get('ASTRA-PERSONAL-INFORMATION') or LOCAL_PERSONAL_INFO_URL
def get_persons(self, searchstring: str) -> List[PersonOut]:
def search_persons(self, searchstring: str) -> List[PersonOut]:
try:
response = httpx.get(f'{self.PERSONAL_INFO_URL}/persons/?full_name={searchstring}')
response = httpx.post(
f'{self.PERSONAL_INFO_URL}/persons/search',
json={"full_name": searchstring}
)
response.raise_for_status()
except httpx.RequestError as exc:
print(f"Ошибка вызова {exc.request.url!r}.")
@@ -52,7 +55,8 @@ class MedicalCardService(BaseService[models.MedicalCard, MedicalCardCreateIn, An
if searchstring is None or searchstring == '':
raise HTTPException(status_code=409, detail='searchstring is required')
persons = self.get_persons(searchstring)
persons = self.search_persons(searchstring)
person_ids = [person.id for person in persons]
if len(person_ids) < 1:
@@ -99,3 +103,21 @@ class MedicalCardService(BaseService[models.MedicalCard, MedicalCardCreateIn, An
self.db_session.commit()
return mc_obj
def create(self, obj: MedicalCardCreateIn):
medical_card = super(MedicalCardService, self).create(obj)
person_id = obj.person_id
print('====' * 20)
print(person_id)
print(medical_card.id)
try:
response = httpx.patch(f'{self.PERSONAL_INFO_URL}/persons/{person_id}', json={"medical_card_id": str(medical_card.id)})
response.raise_for_status()
except httpx.RequestError as exc:
print(f"Ошибка вызова {exc.request.url!r}.")
raise HTTPException(status_code=500, detail=f"Ошибка вызова {exc.request.url!r}.")
except httpx.HTTPStatusError as exc:
print(f"Ошибка {exc.response.status_code} при запросе {exc.request.url!r}.")
raise HTTPException(status_code=exc.response.status_code, detail=f"Ошибка вызова {exc.request.url!r}.")
return medical_card