Files
malenia-backend/routes/internal/renewal.py
2026-08-24 10:22:14 +07:00

60 lines
1.8 KiB
Python

from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.ext.asyncio import AsyncSession
from core.deps import get_service_identity
from db.session import get_db
from repositories.service_notifications import (
ack_notification,
get_pending_notifications,
mark_notification_as_dispatched,
)
from schemas.dto import ServiceIdentity
from schemas.notifications import (
NotificationAcknowledgeRequest,
NotificationResponse,
UserNotificationData,
)
router = APIRouter(prefix="/renewal")
@router.get("/pending", response_model=NotificationResponse)
async def get_pending(
limit: int = 50,
ctx: ServiceIdentity = Depends(get_service_identity),
session: AsyncSession = Depends(get_db),
):
notifications = await get_pending_notifications(session, limit)
response_users: list[UserNotificationData] = []
for notification in notifications:
response_users.append(
UserNotificationData(
notification_id=notification.id,
username=notification.subscription.user.username,
telegram_id=notification.subscription.user.telegram_id,
expires_at=notification.sub_expires_at,
notification_type=notification.notify_type,
)
)
await mark_notification_as_dispatched(session, notification.id)
return NotificationResponse(users=response_users, issued_by=ctx.service)
@router.post("/ack")
async def acknowledge(
req: NotificationAcknowledgeRequest,
ctx: ServiceIdentity = Depends(get_service_identity),
session: AsyncSession = Depends(get_db),
):
try:
r = await ack_notification(session, req.notification_id)
except Exception as e:
raise HTTPException(500, detail=str(e)) from None
if r:
return "OK"
raise HTTPException(500, detail="No such notification found.")