From e6c89978d2e129a6e6b5e5049c73f03bf8025cad Mon Sep 17 00:00:00 2001 From: Albert Avkhadeev Date: Sat, 23 Nov 2024 18:12:49 +0300 Subject: [PATCH] feat: add endpoint --- backend/src/modules/notifies/repository.py | 2 +- backend/src/modules/notifies/routes.py | 8 ++++++++ backend/src/storages/mongo/notifies.py | 8 +++++++- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/backend/src/modules/notifies/repository.py b/backend/src/modules/notifies/repository.py index 4884e44..e4a250a 100644 --- a/backend/src/modules/notifies/repository.py +++ b/backend/src/modules/notifies/repository.py @@ -15,7 +15,7 @@ async def get_notification_by_user_id(self, user_id: PydanticObjectId) -> list[N return await Notification.find(Notification.user_id == user_id).to_list() async def list_all_valid_notifications(self) -> list[Notification]: - return await Notification.find(Notification.sent is False) + return await Notification.find(Notification.sent is False).to_list() notification_repository = NotificationRepository() diff --git a/backend/src/modules/notifies/routes.py b/backend/src/modules/notifies/routes.py index 0fa70d1..b8fded2 100644 --- a/backend/src/modules/notifies/routes.py +++ b/backend/src/modules/notifies/routes.py @@ -31,6 +31,7 @@ async def create_notification(notification_create: NotificationCreateReq, auth: event_title=event.title, target_date=notification_create["notification_options"]["expiration_time"], user_id=user_id, + event_id=event.id, endpoint=notification_create["notification_options"]["endpoint"], keys=notification_create["notification_options"]["keys"], ) @@ -42,6 +43,7 @@ async def create_notification(notification_create: NotificationCreateReq, auth: sport_title=sport.sport, target_date=notification_create["notification_options"]["expiration_time"], user_id=user_id, + sport_id=sport.id, endpoint=notification_create["notification_options"]["endpoint"], keys=notification_create["notification_options"]["keys"], ) @@ -55,3 +57,9 @@ async def get_notification(notification_id: PydanticObjectId): if not notification: raise HTTPException(status_code=404, detail="Notification not found") return notification + + +@router.post("/my-subscriptions") +async def get_user_subscriptions(auth: USER_AUTH) -> list[Notification]: + user_id = auth.user_id + return await notification_repository.get_notification_by_user_id(user_id) diff --git a/backend/src/storages/mongo/notifies.py b/backend/src/storages/mongo/notifies.py index bd9adc2..67fb2b0 100644 --- a/backend/src/storages/mongo/notifies.py +++ b/backend/src/storages/mongo/notifies.py @@ -1,6 +1,7 @@ import datetime from beanie import PydanticObjectId +from pymongo import IndexModel from src.pydantic_base import BaseSchema from src.storages.mongo.__base__ import CustomDocument @@ -8,7 +9,9 @@ class NotifySchema(BaseSchema): event_title: str | None = None + event_id: PydanticObjectId | None = None sport_title: str | None = None + sport_id: PydanticObjectId | None = None endpoint: str keys: dict target_date: datetime.datetime @@ -17,4 +20,7 @@ class NotifySchema(BaseSchema): class Notification(NotifySchema, CustomDocument): - pass + class Settings: + indexes = [ + IndexModel(["user_id", "event_id", "sport_id"], unique=True), + ]