From ac4d0fcb3f4d3257976110983806673447bc5f46 Mon Sep 17 00:00:00 2001 From: Bulat Kurbanov Date: Mon, 22 May 2023 00:07:31 +0200 Subject: [PATCH] Add donation notifications viewset --- src/app/views/donate_notifications.py | 41 +++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 src/app/views/donate_notifications.py diff --git a/src/app/views/donate_notifications.py b/src/app/views/donate_notifications.py new file mode 100644 index 0000000..4df8db3 --- /dev/null +++ b/src/app/views/donate_notifications.py @@ -0,0 +1,41 @@ +from datetime import datetime + +from fastapi import APIRouter, Depends + +from app.depends import check_token +from app.models import ChatDonateNotification + + +NOTIFICATION_DELTA_DAYS = 30 + + +donation_notifications_router = APIRouter( + prefix="/donate_notifications", + tags=["donate_notifications"], + dependencies=[Depends(check_token)], +) + + +@donation_notifications_router.get("/{chat_id}/is_need_send") +async def is_need_send(chat_id: int) -> bool: + # add redis cache + notification = await ChatDonateNotification.objects.get_or_none(chat_id=chat_id) + + if notification is None: + return True + + delta = datetime.now() - notification.sended + return delta.days >= NOTIFICATION_DELTA_DAYS + + +@donation_notifications_router.post("/{chat_id}") +async def mark_sended(chat_id: int): + notification, created = await ChatDonateNotification.objects.get_or_create( + _default={"sended": datetime.now()}, chat_id=chat_id + ) + + if created: + return + + notification.sended = datetime.now() + await notification.save()