[V1.0] Working application, added notifications.

Ready to upload to store.
This commit is contained in:
c-d-p
2025-04-27 00:39:52 +02:00
parent 04d9136b96
commit 62d6b8bdfd
86 changed files with 2250 additions and 240 deletions

View File

@@ -1,10 +1,12 @@
# modules/admin/api.py
from typing import Annotated
from fastapi import APIRouter, Depends
from typing import Annotated, Optional
from fastapi import APIRouter, Depends, HTTPException, status
from pydantic import BaseModel
from sqlalchemy.orm import Session
from core.database import get_db
from modules.auth.dependencies import admin_only
from modules.auth.models import User
from modules.notifications.service import send_push_notification
from .tasks import cleardb
router = APIRouter(prefix="/admin", tags=["admin"], dependencies=[Depends(admin_only)])
@@ -14,6 +16,13 @@ class ClearDbRequest(BaseModel):
hard: bool
class SendNotificationRequest(BaseModel):
username: str
title: str
body: str
data: Optional[dict] = None
@router.get("/")
def read_admin():
return {"message": "Admin route"}
@@ -29,3 +38,43 @@ def clear_db(payload: ClearDbRequest, db: Annotated[Session, Depends(get_db)]):
hard = payload.hard
cleardb.delay(hard)
return {"message": "Clearing database in the background", "hard": hard}
@router.post("/send-notification", status_code=status.HTTP_200_OK)
async def send_user_notification(
payload: SendNotificationRequest,
db: Annotated[Session, Depends(get_db)],
):
"""
Admin endpoint to send a push notification to a specific user by username.
"""
target_user = db.query(User).filter(User.username == payload.username).first()
if not target_user:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"User with username '{payload.username}' not found.",
)
if not target_user.expo_push_token:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=f"User '{payload.username}' does not have a registered push token.",
)
success = await send_push_notification(
push_token=target_user.expo_push_token,
title=payload.title,
body=payload.body,
data=payload.data,
)
if not success:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Failed to send push notification via Expo service.",
)
return {
"message": f"Push notification sent successfully to user '{payload.username}'"
}