Skip to content

Commit

Permalink
fix: Deleting Patient deletes resources which reference this patient …
Browse files Browse the repository at this point in the history
…as well
  • Loading branch information
SimonKonar committed Mar 25, 2024
1 parent 8788388 commit 0976572
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
27 changes: 21 additions & 6 deletions service/blaze_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,26 +218,41 @@ def get_number_of_resources(self, resource_type: str) -> int:
logger.error("Cannot connect to blaze!")
return 0

def delete_fhir_resource(self, resource_type: str, identifier: str) -> int:
def delete_fhir_resource(self, resource_type: str, param_value: str, search_param: str = "identifier") -> int:
"""
Deletes all FHIR resources from the Blaze server of a specific type having a specific identifier.
:param identifier: Identifier belonging to the resource.
It is not the FHIR resource ID!
:param search_param: Parameter by which the resources are filtered. Default value is identifier
:param param_value: actual value of the search parameter.
(When using identifier, Identifier belonging to the resource. It is not the FHIR resource ID!)
:param resource_type: Type of FHIR resource to delete.
:return: Status code of the http request.
"""
response = requests.get(url=self._blaze_url + f"/{resource_type.capitalize()}"
f"?identifier={identifier}",
f"?{search_param}={param_value}",
auth=self._credentials,
verify=False)
list_of_full_urls = glom(response.json(), "**.fullUrl")
list_of_full_urls: list[str] = glom(response.json(), "**.fullUrl")
if response.status_code == 404 or len(list_of_full_urls) == 0:
return 404
for url in list_of_full_urls:
logger.info("Deleting " + url)
if resource_type.capitalize() == "Patient":
patient_reference = url[url.find("Patient/"):]
logger.info(f"In order to delete Patient successfully, "
f"all resources which reference this patient needs to be deleted as well")
self.delete_fhir_resource("Condition", patient_reference, "reference")
self.delete_fhir_resource("Specimen", patient_reference, "reference")
deleted_resource = requests.get(url=url, auth=self._credentials).json()
logger.debug(f"{deleted_resource}")
return requests.delete(url=url, auth=self._credentials).status_code
delete_response = requests.delete(url=url, auth=self._credentials)
# logger.debug(f"delete resource json response: {delete_response.json()}")
if delete_response.status_code != 204:
reason_index = delete_response.text.find("diagnostics")
if reason_index != -1:
logger.debug(f"could not delete. Reason: {delete_response.text[reason_index:]}")
logger.debug(f"response status: {delete_response.status_code}")
# return requests.delete(url=url, auth=self._credentials).status_code
return delete_response.status_code

def is_resource_present_in_blaze(self, resource_type: str, identifier: str) -> bool:
"""
Expand Down
4 changes: 2 additions & 2 deletions test/integration/test_blaze.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ def run_around_tests(self):
sample_collection_repository=SampleCollectionRepoStub())
yield # run test
try:
for donor in SampleDonorRepoStub().get_all():
self.blaze_service.delete_fhir_resource("Patient", donor.identifier)
for sample in SampleRepoStub().get_all():
self.blaze_service.delete_fhir_resource("Specimen", sample.identifier)
for donor in SampleDonorRepoStub().get_all():
self.blaze_service.delete_fhir_resource("Patient", donor.identifier)
for organization in SampleCollectionRepoStub().get_all():
self.blaze_service.delete_fhir_resource("Organization", organization.identifier)
except requests.exceptions.ConnectionError:
Expand Down

0 comments on commit 0976572

Please sign in to comment.