From 6495bdb6f992c72848380f466a11afcbbfd95eee Mon Sep 17 00:00:00 2001 From: Albert Avkhadeev Date: Sun, 24 Nov 2024 02:31:54 +0300 Subject: [PATCH] feat: cronjob --- backend/src/api/lifespan.py | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/backend/src/api/lifespan.py b/backend/src/api/lifespan.py index eea6321..3e94e37 100644 --- a/backend/src/api/lifespan.py +++ b/backend/src/api/lifespan.py @@ -3,6 +3,7 @@ import asyncio import json from contextlib import asynccontextmanager +from datetime import UTC, datetime from beanie import init_beanie from fastapi import FastAPI @@ -52,22 +53,28 @@ async def push_notification(): for notification in notifications: sent_notification_number = len(notification.event_dates) for start_date in notification.event_dates: - days_before = 4 - # if (start_date - datetime.now(UTC)).days == 30: - # days_before = 30 - # elif (start_date - datetime.now(UTC)).days == 7: - # days_before = 7 - # elif (start_date - datetime.now(UTC)).days == 1: - # days_before = 1 - # sent_notification_number -= 1 - # elif (start_date - datetime.now(UTC)).days < 0: - # sent_notification_number -= 1 + days_before = None + if (start_date - datetime.now(UTC)).days == 30: + days_before = 30 + elif (start_date - datetime.now(UTC)).days == 7: + days_before = 7 + elif (start_date - datetime.now(UTC)).days == 1: + days_before = 1 + sent_notification_number -= 1 + elif (start_date - datetime.now(UTC)).days < 0: + sent_notification_number -= 1 if days_before is not None: outMsg: str if notification.sport_id is not None: - outMsg = f'«Через {days_before} дней соревнование по виду спорта "{notification.sport_title}"' + if days_before == 1: + outMsg = f'Через {days_before} день соревнование по виду спорта "{notification.sport_title}"' + else: + outMsg = f'Через {days_before} дней соревнование по виду спорта "{notification.sport_title}"' elif notification.event_id is not None: - outMsg = f'«Через {days_before} дней соревнование "{notification.event_title}" по виду спорта "{notification.sport_title}"' + if days_before == 1: + outMsg = f'Через {days_before} день соревнование "{notification.event_title}" по виду спорта "{notification.sport_title}"' + else: + outMsg = f'Через {days_before} дней соревнование "{notification.event_title}" по виду спорта "{notification.sport_title}"' try: webpush( subscription_info=notification.subscription_info,