from fastapi import APIRouter, Depends, HTTPException from core.deps import get_service_identity from db.session import UnitOfWork, get_uow 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), uow: UnitOfWork = Depends(get_uow), ): notifications = await get_pending_notifications(uow, 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(uow, notification.id) await uow.commit() return NotificationResponse(users=response_users, issued_by=ctx.service) @router.post("/ack") async def acknowledge( req: NotificationAcknowledgeRequest, ctx: ServiceIdentity = Depends(get_service_identity), uow: UnitOfWork = Depends(get_uow), ): try: r = await ack_notification(uow, req.notification_id) except Exception as e: raise HTTPException(500, detail=str(e)) from None if r: await uow.commit() return "OK" raise HTTPException(500, detail="No such notification found.")