| 1 |
8440155 |
mahdim10 |
1y ago |
import asyncio |
| 2 |
8440155 |
mahdim10 |
1y ago |
from datetime import datetime, timedelta |
| 3 |
8440155 |
mahdim10 |
1y ago |
|
| 4 |
8440155 |
mahdim10 |
1y ago |
from app.config import NOTIFY_DAYS_LEFT |
| 5 |
8440155 |
mahdim10 |
1y ago |
from app.db import GetDB |
| 6 |
8440155 |
mahdim10 |
1y ago |
from app.db.models import User |
| 7 |
8440155 |
mahdim10 |
1y ago |
from app.models.notification import UserNotification |
| 8 |
8440155 |
mahdim10 |
1y ago |
from app.models.user import UserResponse |
| 9 |
8440155 |
mahdim10 |
1y ago |
from app.notification.notifiers import notify |
| 10 |
8440155 |
mahdim10 |
1y ago |
|
| 11 |
8440155 |
mahdim10 |
1y ago |
|
| 12 |
8440155 |
mahdim10 |
1y ago |
async def expire_days_reached(): |
| 13 |
8440155 |
mahdim10 |
1y ago |
""" |
| 14 |
8440155 |
mahdim10 |
1y ago |
Identifies users whose expiration days have reached a predefined notification period and sends notifications. |
| 15 |
8440155 |
mahdim10 |
1y ago |
|
| 16 |
8440155 |
mahdim10 |
1y ago |
This function checks each active user with an expiration date set and determines if they are within a |
| 17 |
8440155 |
mahdim10 |
1y ago |
specific notification window (`NOTIFY_DAYS_LEFT` days before expiration). If the condition is met, |
| 18 |
8440155 |
mahdim10 |
1y ago |
it sends a notification. |
| 19 |
8440155 |
mahdim10 |
1y ago |
|
| 20 |
8440155 |
mahdim10 |
1y ago |
How it works: |
| 21 |
8440155 |
mahdim10 |
1y ago |
- The current UTC time is obtained. |
| 22 |
8440155 |
mahdim10 |
1y ago |
- For each user in the database who is activated and currently active, check if their expiration date is set. |
| 23 |
8440155 |
mahdim10 |
1y ago |
- If the user's expiration date is within the `NOTIFY_DAYS_LEFT` threshold (plus a 30-second margin), |
| 24 |
8440155 |
mahdim10 |
1y ago |
log the event and trigger a notification. |
| 25 |
8440155 |
mahdim10 |
1y ago |
""" |
| 26 |
8440155 |
mahdim10 |
1y ago |
|
| 27 |
8440155 |
mahdim10 |
1y ago |
now = datetime.utcnow() |
| 28 |
8440155 |
mahdim10 |
1y ago |
cleft = timedelta(days=NOTIFY_DAYS_LEFT) + now |
| 29 |
8440155 |
mahdim10 |
1y ago |
cright = timedelta(days=NOTIFY_DAYS_LEFT, seconds=30) + now |
| 30 |
8440155 |
mahdim10 |
1y ago |
with GetDB() as db: |
| 31 |
8440155 |
mahdim10 |
1y ago |
users = db.query(User).filter( |
| 32 |
8440155 |
mahdim10 |
1y ago |
User.is_active == True, |
| 33 |
8440155 |
mahdim10 |
1y ago |
User.activated == True, |
| 34 |
8440155 |
mahdim10 |
1y ago |
User.expire_date.isnot(None), |
| 35 |
8440155 |
mahdim10 |
1y ago |
User.expire_date.between(cleft, cright), |
| 36 |
8440155 |
mahdim10 |
1y ago |
) |
| 37 |
8440155 |
mahdim10 |
1y ago |
for user in users: |
| 38 |
8440155 |
mahdim10 |
1y ago |
asyncio.ensure_future( |
| 39 |
8440155 |
mahdim10 |
1y ago |
notify( |
| 40 |
8440155 |
mahdim10 |
1y ago |
action=UserNotification.Action.reached_days_left, |
| 41 |
8440155 |
mahdim10 |
1y ago |
user=UserResponse.model_validate(user), |
| 42 |
8440155 |
mahdim10 |
1y ago |
) |
| 43 |
8440155 |
mahdim10 |
1y ago |
) |