31 lines
1.5 KiB
Python
Executable File
31 lines
1.5 KiB
Python
Executable File
# /opt/docker/dev/service_finder/backend/app/services/notification_service.py
|
|
from datetime import datetime, timedelta, timezone
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
from sqlalchemy import select
|
|
from fastapi import BackgroundTasks
|
|
from app.models.identity import User
|
|
from app.models.asset import Asset
|
|
from app.core.email import send_expiry_notification # Feltételezett core funkció
|
|
|
|
class NotificationService:
|
|
@staticmethod
|
|
async def check_expiring_documents(db: AsyncSession, background_tasks: BackgroundTasks):
|
|
"""
|
|
Példa: Műszaki vizsga lejárata 30 napon belül.
|
|
A logikát az új Asset és Identity modellekhez igazítottuk.
|
|
"""
|
|
threshold = datetime.now(timezone.utc).date() + timedelta(days=30)
|
|
|
|
# JAVÍTVA: Asset join identity.User-el az új struktúra szerint
|
|
stmt = select(Asset, User).join(User, Asset.owner_org_id == User.scope_id).where(
|
|
Asset.status == "active"
|
|
)
|
|
|
|
result = await db.execute(stmt)
|
|
for asset, user in result.all():
|
|
# A lejárati adatot a dúsított factory_data-ból vesszük
|
|
expiry = asset.factory_data.get("mot_expiry_date") if asset.factory_data else None
|
|
if expiry:
|
|
expiry_dt = datetime.strptime(expiry, "%Y-%m-%d").date()
|
|
if expiry_dt <= threshold:
|
|
send_expiry_notification(background_tasks, user.email, f"Műszaki vizsga lejár: {asset.license_plate}") |