Skip to content

Commit

Permalink
API & Client - create notification on user registration
Browse files Browse the repository at this point in the history
  • Loading branch information
SamR1 committed Nov 11, 2024
1 parent 70de5cd commit 68d98f5
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 1 deletion.
39 changes: 39 additions & 0 deletions fittrackee/tests/users/test_auth_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from fittrackee.tests.comments.mixins import CommentMixin
from fittrackee.users.models import (
BlacklistedToken,
Notification,
User,
UserDataExport,
UserSportPreference,
Expand Down Expand Up @@ -484,6 +485,44 @@ def test_it_does_not_call_account_confirmation_email_if_user_already_exists( #

account_confirmation_email_mock.send.assert_not_called()

def test_it_creates_notifications_for_admins_on_registration(
self,
app: Flask,
user_1_admin: User,
user_2_admin: User,
user_3: User,
account_confirmation_email_mock: Mock,
) -> None:
email = self.random_email()
client = app.test_client()

client.post(
'/api/auth/register',
data=json.dumps(
dict(
username=self.random_string(),
email=email,
password=self.random_string(),
accepted_policy=True,
)
),
content_type='application/json',
)

new_user = User.query.filter_by(email=email).first()
notification = Notification.query.filter_by(
event_type='account_creation', event_object_id=new_user.id
).all()
assert len(notification) == 2
for notification in notification:
assert notification.created_at == new_user.created_at
assert notification.from_user_id == new_user.id
assert notification.event_object_id == new_user.id
assert notification.to_user_id in [
user_1_admin.id,
user_2_admin.id,
]


class TestUserLogin(ApiTestCaseMixin):
def test_it_returns_error_if_payload_is_empty(self, app: Flask) -> None:
Expand Down
13 changes: 13 additions & 0 deletions fittrackee/users/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
from .models import (
BlacklistedToken,
BlockedUser,
Notification,
User,
UserDataExport,
UserSportPreference,
Expand Down Expand Up @@ -184,6 +185,18 @@ def register_user() -> Union[Tuple[Dict, int], HttpResponse]:
if new_user:
new_user.language = language
new_user.accepted_policy_date = datetime.datetime.utcnow()
for admin in User.query.filter(
User.admin == True, # noqa
User.is_active == True, # noqa
).all():
notification = Notification(
from_user_id=new_user.id,
to_user_id=admin.id,
created_at=new_user.created_at,
event_type='account_creation',
event_object_id=new_user.id,
)
db.session.add(notification)
db.session.commit()
send_account_confirmation_email(new_user)

Expand Down
1 change: 1 addition & 0 deletions fittrackee/users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
)

NOTIFICATION_TYPES = [
'account_creation',
'comment_like',
'comment_reply',
'comment_suspension',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,14 @@
)
}
function displayRelationshipCard(notificationType: TNotificationType) {
return ['follow', 'follow_request'].includes(notificationType)
return ['follow', 'follow_request', 'account_creation'].includes(
notificationType
)
}
function getUserAction(notificationType: TNotificationType): string {
switch (notificationType) {
case 'account_creation':
return 'notifications.SIGN_UP'
case 'comment_like':
return 'notifications.LIKED_YOUR_COMMENT'
case 'comment_reply':
Expand Down
1 change: 1 addition & 0 deletions fittrackee_client/src/locales/en/notifications.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"REPORTED_USER_COMMENT": "reported this comment:",
"REPORTED_USER_WORKOUT": "reported this workout:",
"SEND_FOLLOW_REQUEST_TO_YOU": "send a follow request to you",
"SIGN_UP": "signed up",
"STATUS": "Status",
"TYPES": {
"LABEL": "Type",
Expand Down
1 change: 1 addition & 0 deletions fittrackee_client/src/locales/fr/notifications.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"REPORTED_USER_COMMENT": "a signalé ce commentaire :",
"REPORTED_USER_WORKOUT": "a signalé cette séance :",
"SEND_FOLLOW_REQUEST_TO_YOU": "souhaite s'abonner",
"SIGN_UP": "s'est inscrit.e",
"STATUS": "Status",
"TYPES": {
"LABEL": "Type",
Expand Down
1 change: 1 addition & 0 deletions fittrackee_client/src/types/notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { IUserReportAction, IUserProfile } from '@/types/user'
import type { IComment, IWorkout } from '@/types/workouts'

export type TNotificationType =
| 'account_creation'
| 'comment_like'
| 'comment_reply'
| 'comment_suspension'
Expand Down

0 comments on commit 68d98f5

Please sign in to comment.