Ушел от try except и вынес rollback на уровень сессии

This commit is contained in:
Raykov-MS 2026-05-21 11:20:44 +03:00
parent 411773b6e9
commit e129f562dc
2 changed files with 28 additions and 40 deletions

View File

@ -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()

View File

@ -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 = (