Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 35 additions & 10 deletions app/core/notification/endpoints_notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,34 @@ async def get_topic_identifier(

@router.post(
"/notification/send",
status_code=201,
status_code=204,
)
async def send_notification(
notification_request: schemas_notification.GroupNotificationRequest,
user: models_users.CoreUser = Depends(is_user_in(GroupType.admin)),
notification_tool: NotificationTool = Depends(get_notification_tool),
):
"""
Send a notification to a group.

**Only admins can use this endpoint**
"""
message = schemas_notification.Message(
title=notification_request.title,
content=notification_request.content,
action_module="",
)
await notification_tool.send_notification_to_group(
group_id=notification_request.group_id,
message=message,
)


@router.post(
"/notification/test/send",
status_code=201,
)
async def send_test_notification(
user: models_users.CoreUser = Depends(is_user_in(GroupType.admin)),
notification_tool: NotificationTool = Depends(get_notification_tool),
):
Expand All @@ -267,10 +292,10 @@ async def send_notification(


@router.post(
"/notification/send/future",
status_code=201,
"/notification/test/send/future",
status_code=204,
)
async def send_future_notification(
async def send_test_future_notification(
user: models_users.CoreUser = Depends(is_user_in(GroupType.admin)),
notification_tool: NotificationTool = Depends(get_notification_tool),
scheduler: Scheduler = Depends(get_scheduler),
Expand All @@ -295,10 +320,10 @@ async def send_future_notification(


@router.post(
"/notification/send/topic",
status_code=201,
"/notification/test/send/topic",
status_code=204,
)
async def send_notification_topic(
async def send_test_notification_topic(
user: models_users.CoreUser = Depends(is_user_in(GroupType.admin)),
notification_tool: NotificationTool = Depends(get_notification_tool),
):
Expand All @@ -319,10 +344,10 @@ async def send_notification_topic(


@router.post(
"/notification/send/topic/future",
status_code=201,
"/notification/test/send/topic/future",
status_code=204,
)
async def send_future_notification_topic(
async def send_test_future_notification_topic(
user: models_users.CoreUser = Depends(is_user_in(GroupType.admin)),
notification_tool: NotificationTool = Depends(get_notification_tool),
scheduler: Scheduler = Depends(get_scheduler),
Expand Down
6 changes: 6 additions & 0 deletions app/core/notification/schemas_notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,9 @@ class Message(BaseModel):
class FirebaseDevice(BaseModel):
user_id: str = Field(description="The Hyperion user id")
firebase_device_token: str = Field("Firebase device token")


class GroupNotificationRequest(BaseModel):
group_id: str
title: str
content: str
23 changes: 23 additions & 0 deletions app/utils/communication/notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from app.core.notification import cruds_notification, models_notification
from app.core.notification.notification_types import CustomTopic
from app.core.notification.schemas_notification import Message
from app.core.users import cruds_users
from app.core.utils.config import Settings
from app.types.scheduler import Scheduler

Expand Down Expand Up @@ -327,6 +328,28 @@ def __init__(
self.db = db
# self.scheduler = scheduler

async def send_notification_to_group(
self,
group_id: str,
message: Message,
scheduler: Scheduler | None = None,
defer_date: datetime | None = None,
job_id: str | None = None,
):
users = await cruds_users.get_users(
included_groups=[group_id],
db=self.db,
)
user_ids = [user.id for user in users]

await self.send_notification_to_users(
user_ids=user_ids,
message=message,
scheduler=scheduler,
defer_date=defer_date,
job_id=job_id,
)

async def send_notification_to_users(
self,
user_ids: list[str],
Expand Down
45 changes: 45 additions & 0 deletions tests/test_notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,48 @@ def test_get_topic_identifier(client: TestClient) -> None:
)
assert response.status_code == 200
assert response.json() == [TOPIC_2]


def test_send_notification_to_group(client: TestClient) -> None:
response = client.post(
"/notification/send",
json={
"title": "Test Notification",
"content": "This is a test notification.",
"group_id": GroupType.admin.value,
},
headers={
"Authorization": f"Bearer {admin_user_token}",
},
)
assert response.status_code == 204


def test_send_test_future_notification(client: TestClient) -> None:
response = client.post(
"/notification/test/send/future",
headers={
"Authorization": f"Bearer {admin_user_token}",
},
)
assert response.status_code == 204


def test_send_test_notification_topic(client: TestClient) -> None:
response = client.post(
"/notification/test/send/topic",
headers={
"Authorization": f"Bearer {admin_user_token}",
},
)
assert response.status_code == 204


def test_send_test_future_notification_topic(client: TestClient) -> None:
response = client.post(
"/notification/test/send/topic/future",
headers={
"Authorization": f"Bearer {admin_user_token}",
},
)
assert response.status_code == 204