felicity-lims/felicity/api/gql/notification/query.py

45 lines
1.2 KiB
Python
Raw Normal View History

from typing import List, Optional
import strawberry # noqa
2023-03-19 23:21:32 +08:00
from felicity.api.gql.notification.types import NotificationType
2022-11-06 20:09:44 +08:00
from felicity.apps.notification import models
2023-03-19 23:25:50 +08:00
from felicity.core.uid_gen import FelicityID
@strawberry.type
2022-01-13 05:06:57 +08:00
class StreamNotificationQuery:
@strawberry.field
async def notification_filter(
self,
info,
2023-03-19 23:21:32 +08:00
group_uid: Optional[FelicityID],
department_uid: Optional[FelicityID],
user_uid: Optional[FelicityID],
) -> List[NotificationType]:
filters = {}
if group_uid:
filters["groups__uid__in"] = [group_uid]
if department_uid:
filters["departments__uid__in"] = [department_uid]
if user_uid:
filters["users__uid__in"] = [user_uid]
notif_stmt = models.Notification.smart_query(
filters=filters, sort_attrs=["-created_at"]
)
notifications = (
(await models.Notification.session.execute(notif_stmt)).scalars().all()
)
return list(notifications)
@strawberry.field
2023-03-19 23:25:50 +08:00
async def notification_by_uid(
self, info, uid: FelicityID
) -> Optional[NotificationType]:
return await models.Notification.get(uid=uid)