This commit is contained in:
45
resolvers/seen.py
Normal file
45
resolvers/seen.py
Normal file
@@ -0,0 +1,45 @@
|
||||
from orm.notification import NotificationSeen
|
||||
from services.db import local_session
|
||||
from resolvers.model import NotificationSeenResult
|
||||
from resolvers.load import get_notifications_grouped
|
||||
import strawberry
|
||||
import logging
|
||||
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@strawberry.type
|
||||
class Mutation:
|
||||
@strawberry.mutation
|
||||
async def mark_notification_as_read(self, info, notification_id: int) -> NotificationSeenResult:
|
||||
author_id = info.context.get("author_id")
|
||||
if author_id:
|
||||
with local_session() as session:
|
||||
try:
|
||||
ns = NotificationSeen(notification=notification_id, viewer=author_id)
|
||||
session.add(ns)
|
||||
session.commit()
|
||||
except SQLAlchemyError as e:
|
||||
session.rollback()
|
||||
logger.error(
|
||||
f"[mark_notification_as_read] Ошибка при обновлении статуса прочтения уведомления: {str(e)}"
|
||||
)
|
||||
return NotificationSeenResult(error="cant mark as read")
|
||||
return NotificationSeenResult(error=None)
|
||||
|
||||
@strawberry.mutation
|
||||
async def mark_all_notifications_as_read(self, info, limit: int = 10, offset: int = 0) -> NotificationSeenResult:
|
||||
# TODO: use latest loaded notification_id as input offset parameter
|
||||
ngroups = {}
|
||||
error = None
|
||||
try:
|
||||
author_id = info.context.get("author_id")
|
||||
if author_id:
|
||||
ngroups = get_notifications_grouped(author_id, limit, offset, mark_as_read=True)
|
||||
except Exception as e:
|
||||
print(e)
|
||||
error = "cant mark as read"
|
||||
return NotificationSeenResult(error=error)
|
Reference in New Issue
Block a user