2023-09-16 17:09:38 +03:00
|
|
|
from datetime import datetime, timedelta
|
2023-09-07 14:22:06 +03:00
|
|
|
from typing import Optional
|
|
|
|
from uuid import UUID
|
|
|
|
|
|
|
|
from fastapi import HTTPException
|
|
|
|
from models.databases.repository import Repository
|
|
|
|
from models.notifications import Notification, NotificationsStatusEnum
|
|
|
|
from pydantic import BaseModel
|
|
|
|
|
|
|
|
|
|
|
|
class CreateNotificationProperties(BaseModel):
|
|
|
|
"""Properties that can be received on notification creation"""
|
|
|
|
|
|
|
|
chat_id: Optional[UUID] = None
|
|
|
|
message: Optional[str] = None
|
|
|
|
action: str
|
|
|
|
status: NotificationsStatusEnum = NotificationsStatusEnum.Pending
|
|
|
|
|
|
|
|
def dict(self, *args, **kwargs):
|
|
|
|
notification_dict = super().dict(*args, **kwargs)
|
|
|
|
if notification_dict.get("chat_id"):
|
|
|
|
notification_dict["chat_id"] = str(notification_dict.get("chat_id"))
|
|
|
|
return notification_dict
|
|
|
|
|
|
|
|
|
|
|
|
class DeleteNotificationResponse(BaseModel):
|
|
|
|
"""Response when deleting a prompt"""
|
|
|
|
|
|
|
|
status: str = "delete"
|
|
|
|
notification_id: UUID
|
|
|
|
|
|
|
|
|
|
|
|
class NotificationUpdatableProperties(BaseModel):
|
|
|
|
"""Properties that can be received on notification update"""
|
|
|
|
|
|
|
|
message: Optional[str]
|
|
|
|
status: Optional[NotificationsStatusEnum] = NotificationsStatusEnum.Done
|
|
|
|
|
|
|
|
|
|
|
|
class Notifications(Repository):
|
|
|
|
def __init__(self, supabase_client):
|
|
|
|
self.db = supabase_client
|
|
|
|
|
|
|
|
def add_notification(
|
|
|
|
self, notification: CreateNotificationProperties
|
|
|
|
) -> Notification:
|
|
|
|
"""
|
|
|
|
Add a notification
|
|
|
|
"""
|
|
|
|
response = (
|
|
|
|
self.db.from_("notifications").insert(notification.dict()).execute()
|
|
|
|
).data
|
|
|
|
return Notification(**response[0])
|
|
|
|
|
|
|
|
def update_notification_by_id(
|
|
|
|
self, notification_id: UUID, notification: NotificationUpdatableProperties
|
|
|
|
) -> Notification:
|
|
|
|
"""Update a notification by id"""
|
|
|
|
response = (
|
|
|
|
self.db.from_("notifications")
|
|
|
|
.update(notification.dict(exclude_unset=True))
|
|
|
|
.filter("id", "eq", notification_id)
|
|
|
|
.execute()
|
|
|
|
).data
|
|
|
|
|
|
|
|
if response == []:
|
|
|
|
raise HTTPException(404, "Notification not found")
|
|
|
|
|
|
|
|
return Notification(**response[0])
|
|
|
|
|
|
|
|
def remove_notification_by_id(
|
|
|
|
self, notification_id: UUID
|
|
|
|
) -> DeleteNotificationResponse:
|
|
|
|
"""
|
|
|
|
Remove a notification by id
|
|
|
|
Args:
|
|
|
|
notification_id (UUID): The id of the notification
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
str: Status message
|
|
|
|
"""
|
|
|
|
response = (
|
|
|
|
self.db.from_("notifications")
|
|
|
|
.delete()
|
|
|
|
.filter("id", "eq", notification_id)
|
|
|
|
.execute()
|
|
|
|
.data
|
|
|
|
)
|
|
|
|
|
|
|
|
if response == []:
|
|
|
|
raise HTTPException(404, "Notification not found")
|
|
|
|
|
|
|
|
return DeleteNotificationResponse(
|
|
|
|
status="deleted", notification_id=notification_id
|
|
|
|
)
|
|
|
|
|
2023-09-07 18:23:31 +03:00
|
|
|
def remove_notifications_by_chat_id(self, chat_id: UUID) -> None:
|
|
|
|
"""
|
|
|
|
Remove all notifications for a chat
|
|
|
|
Args:
|
|
|
|
chat_id (UUID): The id of the chat
|
|
|
|
"""
|
|
|
|
(
|
|
|
|
self.db.from_("notifications")
|
|
|
|
.delete()
|
|
|
|
.filter("chat_id", "eq", chat_id)
|
|
|
|
.execute()
|
|
|
|
).data
|
|
|
|
|
2023-09-07 14:22:06 +03:00
|
|
|
def get_notifications_by_chat_id(self, chat_id: UUID) -> list[Notification]:
|
|
|
|
"""
|
|
|
|
Get all notifications for a chat
|
|
|
|
Args:
|
|
|
|
chat_id (UUID): The id of the chat
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
list[Notification]: The notifications
|
|
|
|
"""
|
2023-09-17 23:36:42 +03:00
|
|
|
five_minutes_ago = (datetime.now() - timedelta(minutes=5)).strftime(
|
|
|
|
"%Y-%m-%d %H:%M:%S.%f"
|
|
|
|
)
|
2023-09-16 17:09:38 +03:00
|
|
|
|
2023-09-07 18:23:31 +03:00
|
|
|
notifications = (
|
2023-09-07 14:22:06 +03:00
|
|
|
self.db.from_("notifications")
|
|
|
|
.select("*")
|
|
|
|
.filter("chat_id", "eq", chat_id)
|
2023-09-17 23:36:42 +03:00
|
|
|
.filter("datetime", "gt", five_minutes_ago)
|
2023-09-07 14:22:06 +03:00
|
|
|
.execute()
|
|
|
|
).data
|
2023-09-07 18:23:31 +03:00
|
|
|
|
|
|
|
return [Notification(**notification) for notification in notifications]
|