diff --git a/resolvers/schema.py b/resolvers/schema.py index 211b3f8..92ab26f 100644 --- a/resolvers/schema.py +++ b/resolvers/schema.py @@ -2,6 +2,7 @@ from typing import List, Any from sqlalchemy import and_ from sqlalchemy.orm import aliased +from sqlalchemy.exc import SQLAlchemyError from orm.author import Author from orm.notification import Notification as NotificationMessage, NotificationSeen from services.auth import login_required @@ -41,33 +42,38 @@ class Query: async def load_notifications(self, info, limit: int = 50, offset: int = 0) -> NotificationsResult: user_id = info.context["user_id"] with local_session() as session: - author = session.query(Author).filter(Author.user == user_id).first() - NotificationSeenAlias = aliased(NotificationSeen) + try: + author = session.query(Author).filter(Author.user == user_id).first() + NotificationSeenAlias = aliased(NotificationSeen) - if author: - nnn = session.query( - NotificationMessage, - NotificationSeenAlias.viewer.label("seen") - ).outerjoin( - NotificationSeen, - and_(NotificationSeen.viewer == author.id, NotificationSeen.notification == NotificationMessage.id), - ).limit(limit).offset(offset).all() - notifications = [] - for n in nnn: - ntf = Notification() - ntf.id = n.id - ntf.payload = n.payload - ntf.entity = n.entity - ntf.action = n.action - ntf.created_at = n.created_at - ntf.seen = n.seen - notifications.append(ntf) - nr = NotificationsResult( - notifications = notifications, - unread = sum(1 for n in notifications if author.id in n.seen), - total = session.query(NotificationMessage).count() - ) - return nr + if author: + nnn = session.query( + NotificationMessage, + NotificationSeenAlias.viewer.label("seen") + ).outerjoin( + NotificationSeen, + and_(NotificationSeen.viewer == author.id, NotificationSeen.notification == NotificationMessage.id), + ).group_by(NotificationSeen.notification).limit(limit).offset(offset).all() + notifications = [] + for n, seen in nnn: + ntf = Notification( + id=n.id, + payload=n.payload, + entity=n.entity, + action=n.action, + created_at=n.created_at, + seen=seen, + ) + if ntf: + notifications.append(ntf) + nr = NotificationsResult( + notifications = notifications, + unread = sum(1 for n in notifications if author.id in n.seen), + total = session.query(NotificationMessage).count() + ) + return nr + except Exception as ex: + print(f"[resolvers.schema] {ex}") return NotificationsResult( notifications=[], total = 0, @@ -87,10 +93,10 @@ class Mutation: ns = NotificationSeen({"notification": notification_id, "viewer": author.id}) session.add(ns) session.commit() - except Exception as e: + except SQLAlchemyError as e: session.rollback() print(f"[mark_notification_as_read] error: {str(e)}") - nsr = NotificationSeenResult(error = "cant mark as read") + nsr = NotificationSeenResult(error="cant mark as read") return nsr return NotificationSeenResult() @@ -104,11 +110,10 @@ class Mutation: author = session.query(Author).filter(Author.user == user_id).first() if author: _nslist = session.query(NotificationSeen).filter(NotificationSeen.viewer == author.id).all() - except Exception as e: + except SQLAlchemyError as e: session.rollback() print(f"[mark_all_notifications_as_read] error: {str(e)}") - nsr = NotificationSeenResult() - nsr.error = "cant mark as read" + nsr = NotificationSeenResult(error="cant mark as read") return nsr return NotificationSeenResult()