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

42 lines
1.1 KiB
Python
Raw Normal View History

from typing import List, Optional
import strawberry # noqa
2023-04-10 20:23:31 +08:00
from api.gql.noticeboard.types import NoticeType
from apps.noticeboard import models
from core.uid_gen import FelicityID
2021-12-06 00:37:33 +08:00
@strawberry.type
class NoticeQuery:
@strawberry.field
2023-03-19 23:21:32 +08:00
async def notice_by_uid(self, info, uid: FelicityID) -> Optional[NoticeType]:
2021-12-06 00:37:33 +08:00
return await models.Notice.get(uid=uid)
@strawberry.field
2023-03-19 23:25:50 +08:00
async def notices_by_creator(
self, info, uid: FelicityID
) -> Optional[List[NoticeType]]:
2021-12-06 00:37:33 +08:00
return await models.Notice.get_all(created_by_uid=uid)
@strawberry.field
async def notice_filter(
2023-03-19 23:25:50 +08:00
self,
info,
2023-04-25 22:42:39 +08:00
group_uid: FelicityID | None,
department_uid: FelicityID | None,
) -> List[NoticeType]:
2021-12-06 00:37:33 +08:00
filters = {}
if group_uid:
filters["groups__uid__in"] = [group_uid]
2021-12-06 00:37:33 +08:00
if department_uid:
filters["departments__uid__in"] = [department_uid]
2021-12-06 00:37:33 +08:00
notice_stmt = models.Notice.smart_query(
filters=filters, sort_attrs=["-created_at"]
2021-12-06 00:37:33 +08:00
)
notices = (await models.Notice.session.execute(notice_stmt)).scalars().all()
return list(notices)