fix!: removed .commit() from repository level
This commit is contained in:
@@ -1,19 +1,19 @@
|
||||
from sqlalchemy import func, or_, select, text
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from db.models.service_notifications import ServiceNotification
|
||||
from db.session import UnitOfWork
|
||||
from schemas.enums import NotificationStatus
|
||||
|
||||
|
||||
async def get_notification_by_id(session: AsyncSession, n_id: int) -> ServiceNotification | None:
|
||||
async def get_notification_by_id(uow: UnitOfWork, n_id: int) -> ServiceNotification | None:
|
||||
stmt = select(ServiceNotification).where(ServiceNotification.id == n_id)
|
||||
r = await session.execute(stmt)
|
||||
r = await uow.session.execute(stmt)
|
||||
|
||||
return r.scalar_one_or_none()
|
||||
|
||||
|
||||
async def get_pending_notifications(
|
||||
session: AsyncSession, batch_size: int = 50
|
||||
uow: UnitOfWork, batch_size: int = 50
|
||||
) -> list[ServiceNotification]:
|
||||
stmt = (
|
||||
select(ServiceNotification)
|
||||
@@ -33,26 +33,24 @@ async def get_pending_notifications(
|
||||
.with_for_update(skip_locked=True)
|
||||
.limit(batch_size)
|
||||
)
|
||||
r = await session.execute(stmt)
|
||||
r = await uow.session.execute(stmt)
|
||||
|
||||
return list(r.scalars().all())
|
||||
|
||||
|
||||
async def ack_notification(session: AsyncSession, n_id: int) -> ServiceNotification | None:
|
||||
notification = await get_notification_by_id(session, n_id)
|
||||
async def ack_notification(uow: UnitOfWork, n_id: int) -> ServiceNotification | None:
|
||||
notification = await get_notification_by_id(uow, n_id)
|
||||
if not notification:
|
||||
return
|
||||
|
||||
notification.sent_at = func.now()
|
||||
notification.status = NotificationStatus.SENT
|
||||
|
||||
await session.commit()
|
||||
|
||||
return notification
|
||||
|
||||
|
||||
async def mark_notification_as_dispatched(session: AsyncSession, n_id: int):
|
||||
notification = await get_notification_by_id(session, n_id)
|
||||
async def mark_notification_as_dispatched(uow: UnitOfWork, n_id: int):
|
||||
notification = await get_notification_by_id(uow, n_id)
|
||||
if not notification:
|
||||
return
|
||||
|
||||
@@ -60,5 +58,4 @@ async def mark_notification_as_dispatched(session: AsyncSession, n_id: int):
|
||||
notification.status = NotificationStatus.DISPATCHED
|
||||
notification.attempts += 1
|
||||
|
||||
await session.commit()
|
||||
return notification
|
||||
|
||||
Reference in New Issue
Block a user