diff --git a/api/src/db/session.py b/api/src/db/session.py index eb04b72..83da0bc 100644 --- a/api/src/db/session.py +++ b/api/src/db/session.py @@ -15,6 +15,9 @@ async def get_db() -> AsyncGenerator: try: yield db await db.commit() + except Exception: + await db.rollback() + raise finally: await db.close() diff --git a/api/src/repository/user_repository.py b/api/src/repository/user_repository.py index 73b07f4..07ddd94 100644 --- a/api/src/repository/user_repository.py +++ b/api/src/repository/user_repository.py @@ -105,50 +105,35 @@ class UserRepository: query = select(UserOrg.org_unit_id).where(UserOrg.user_id == user_id) return (await self.db.execute(query)).scalars().all() - async def set_many_ssp(self, user_id: int, ssp_ids: list[int]) -> bool: - try: - for ssp_id in ssp_ids: - await self.db.execute( - text( - "SELECT v3.grant_user_org_access(:user_id, :org_unit_id)" - ), - {"user_id": user_id, "org_unit_id": ssp_id}, - ) - await self.db.commit() - return True - except Exception: - await self.db.rollback() - return False - - async def unset_many_ssp(self, user_id: int, ssp_ids: list[int]) -> bool: - try: - for ssp_id in ssp_ids: - await self.db.execute( - text( - "SELECT v3.revoke_user_org_access(:user_id, :org_unit_id)" - ), - {"user_id": user_id, "org_unit_id": ssp_id}, - ) - await self.db.commit() - return True - except Exception: - await self.db.rollback() - return False - - async def clear_many_ssp(self, user_id: int) -> bool: - try: - org_unit_ids = await self.get_many_ssp_ids(user_id) + async def set_many_ssp(self, user_id: int, ssp_ids: list[int]) -> None: + for ssp_id in ssp_ids: await self.db.execute( text( - "SELECT v3.revoke_many_user_org_access(:user_id, :org_unit_ids)" + "SELECT v3.grant_user_org_access(:user_id, :org_unit_id)" ), - {"user_id": user_id, "org_unit_ids": org_unit_ids}, + {"user_id": user_id, "org_unit_id": ssp_id}, ) - await self.db.commit() - return True - except Exception: - await self.db.rollback() - return False + await self.db.commit() + + async def unset_many_ssp(self, user_id: int, ssp_ids: list[int]) -> None: + for ssp_id in ssp_ids: + await self.db.execute( + text( + "SELECT v3.revoke_user_org_access(:user_id, :org_unit_id)" + ), + {"user_id": user_id, "org_unit_id": ssp_id}, + ) + await self.db.commit() + + async def clear_many_ssp(self, user_id: int) -> None: + org_unit_ids = await self.get_many_ssp_ids(user_id) + await self.db.execute( + text( + "SELECT v3.revoke_many_user_org_access(:user_id, :org_unit_ids)" + ), + {"user_id": user_id, "org_unit_ids": org_unit_ids}, + ) + await self.db.commit() async def create(self, user_data: dict) -> AppUser: hashed_password = (