diff --git a/server/src/scimodom/services/file.py b/server/src/scimodom/services/file.py index 6cb81415..af5203fd 100644 --- a/server/src/scimodom/services/file.py +++ b/server/src/scimodom/services/file.py @@ -221,6 +221,15 @@ def create_project_metadata_file(self, smid: str) -> TextIO: metadata_file = Path(self._get_project_metadata_dir(), f"{smid}.json") return open(metadata_file, "w", opener=write_opener) + def delete_project_metadata_file(self, smid: str) -> None: + """Remove a metadata file. + + :param smid: Sci-ModoM ID (SMID) + :type smid: str + """ + path = Path(self._get_project_metadata_dir(), f"{smid}.json") + path.unlink() + def create_project_request_file(self, request_uuid) -> TextIO: """Open a metadata (request) file for writing. diff --git a/server/src/scimodom/services/project.py b/server/src/scimodom/services/project.py index 9cc7970a..fc1f3853 100644 --- a/server/src/scimodom/services/project.py +++ b/server/src/scimodom/services/project.py @@ -146,6 +146,7 @@ def delete_project(self, project: Project) -> None: if len(contact.projects) == 1: self._session.delete(contact) self._session.delete(project) + self._file_service.delete_project_metadata_file(project.id) self._session.commit() except Exception: self._session.rollback() diff --git a/server/tests/unit/services/test_project.py b/server/tests/unit/services/test_project.py index ca65b311..eed7f671 100644 --- a/server/tests/unit/services/test_project.py +++ b/server/tests/unit/services/test_project.py @@ -30,6 +30,7 @@ class MockFileService: def __init__(self): self.files_by_name: dict[str, MockStringIO | MockBytesIO] = {} self.deleted_requests: list[str] = [] + self.deleted_projects: list[str] = [] def create_project_metadata_file(self, smid: str) -> TextIO: metadata_file = Path( @@ -51,6 +52,10 @@ def delete_project_request_file(self, request_uuid) -> None: name = self._get_project_request_file_path(request_uuid).as_posix() self.deleted_requests.append(name) + def delete_project_metadata_file(self, smid) -> None: + name = Path(self._get_project_metadata_dir(), f"{smid}.json").as_posix() + self.deleted_projects.append(name) + def _get_project_metadata_dir(self) -> Path: return Path("/data", "metadata") @@ -379,3 +384,4 @@ def test_delete_project(Session, project, file_service): session.scalar(select(func.count()).select_from(UserProjectAssociation)) == 0 ) + assert file_service.deleted_projects == ["/data/metadata/12345678.json"]