2023-09-16 17:09:38 +03:00
|
|
|
from datetime import datetime, timedelta
|
2023-09-07 14:22:06 +03:00
|
|
|
|
2024-01-26 05:56:54 +03:00
|
|
|
from logger import get_logger
|
2023-11-28 16:27:39 +03:00
|
|
|
from modules.notification.dto.outputs import DeleteNotificationResponse
|
|
|
|
from modules.notification.entity.notification import Notification
|
|
|
|
from modules.notification.repository.notifications_interface import (
|
|
|
|
NotificationInterface,
|
|
|
|
)
|
2023-09-07 14:22:06 +03:00
|
|
|
|
2024-01-26 05:56:54 +03:00
|
|
|
logger = get_logger(__name__)
|
|
|
|
|
2023-09-07 14:22:06 +03:00
|
|
|
|
2023-11-28 16:27:39 +03:00
|
|
|
class Notifications(NotificationInterface):
|
2023-09-07 14:22:06 +03:00
|
|
|
def __init__(self, supabase_client):
|
|
|
|
self.db = supabase_client
|
|
|
|
|
2023-11-28 16:27:39 +03:00
|
|
|
def add_notification(self, notification):
|
2023-09-07 14:22:06 +03:00
|
|
|
"""
|
|
|
|
Add a notification
|
|
|
|
"""
|
|
|
|
response = (
|
|
|
|
self.db.from_("notifications").insert(notification.dict()).execute()
|
|
|
|
).data
|
|
|
|
return Notification(**response[0])
|
|
|
|
|
|
|
|
def update_notification_by_id(
|
2023-11-28 16:27:39 +03:00
|
|
|
self,
|
|
|
|
notification_id,
|
|
|
|
notification,
|
|
|
|
):
|
2024-01-29 08:35:10 +03:00
|
|
|
if notification_id is None:
|
|
|
|
logger.info("Notification id is required")
|
|
|
|
return None
|
|
|
|
|
2023-09-07 14:22:06 +03:00
|
|
|
"""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 == []:
|
2024-01-26 05:56:54 +03:00
|
|
|
logger.info(f"Notification with id {notification_id} not found")
|
|
|
|
return None
|
2023-09-07 14:22:06 +03:00
|
|
|
|
|
|
|
return Notification(**response[0])
|
|
|
|
|
2023-11-28 16:27:39 +03:00
|
|
|
def remove_notification_by_id(self, notification_id):
|
2023-09-07 14:22:06 +03:00
|
|
|
"""
|
|
|
|
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 == []:
|
2024-01-26 05:56:54 +03:00
|
|
|
logger.info(f"Notification with id {notification_id} not found")
|
|
|
|
return None
|
2023-09-07 14:22:06 +03:00
|
|
|
|
|
|
|
return DeleteNotificationResponse(
|
|
|
|
status="deleted", notification_id=notification_id
|
|
|
|
)
|
|
|
|
|
2023-11-28 16:27:39 +03:00
|
|
|
def remove_notifications_by_chat_id(self, chat_id):
|
2023-09-07 18:23:31 +03:00
|
|
|
"""
|
|
|
|
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-11-28 16:27:39 +03:00
|
|
|
def get_notifications_by_chat_id(self, chat_id):
|
2023-09-07 14:22:06 +03:00
|
|
|
"""
|
|
|
|
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]
|