feat(notification): add target_mode enum constraint and merge register-notifications script
- Add NotificationTargetMode enum (new_users/exist_users/all_users/user_ids) - Create Alembic migrations: drop duplicate indexes, add target_mode column - Merge register-notifications.sh into dev-migrate.sh sync-notifications subcommand - Shorten notification config path: static/notification/notifications -> static/notifications - Update registration flow to dispatch notifications by target_mode - Add is_first_registration to RegisterBonusResult for first-time user detection - Remove dead code: link_published_notifications_to_user - Update welcome_points.yaml to target new_users only - Add 44 unit tests + 1 integration test, all passing
This commit is contained in:
@@ -9,6 +9,7 @@ from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from models.notification import Notification
|
||||
from models.user_notification import UserNotification
|
||||
from schemas.enums import NotificationTargetMode
|
||||
|
||||
|
||||
class NotificationRepository:
|
||||
@@ -116,13 +117,24 @@ class NotificationRepository:
|
||||
async def commit(self) -> None:
|
||||
await self._session.commit()
|
||||
|
||||
async def link_published_notifications_to_user(self, *, user_id: UUID) -> int:
|
||||
async def link_notifications_for_registered_user(
|
||||
self, *, user_id: UUID, is_first_registration: bool
|
||||
) -> int:
|
||||
target_modes: list[NotificationTargetMode]
|
||||
if is_first_registration:
|
||||
target_modes = [
|
||||
NotificationTargetMode.NEW_USERS,
|
||||
NotificationTargetMode.ALL_USERS,
|
||||
]
|
||||
else:
|
||||
target_modes = [NotificationTargetMode.ALL_USERS]
|
||||
notification_ids = list(
|
||||
(
|
||||
await self._session.execute(
|
||||
select(Notification.id).where(
|
||||
Notification.status == "published",
|
||||
Notification.deleted_at.is_(None),
|
||||
Notification.target_mode.in_(target_modes),
|
||||
)
|
||||
)
|
||||
)
|
||||
@@ -136,8 +148,8 @@ class NotificationRepository:
|
||||
insert(UserNotification)
|
||||
.values(
|
||||
[
|
||||
{"user_id": user_id, "notification_id": notification_id}
|
||||
for notification_id in notification_ids
|
||||
{"user_id": user_id, "notification_id": nid}
|
||||
for nid in notification_ids
|
||||
]
|
||||
)
|
||||
.on_conflict_do_nothing(index_elements=["user_id", "notification_id"])
|
||||
|
||||
Reference in New Issue
Block a user