Skip to content

Commit

Permalink
API & Client - process appeal on user warning (wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
SamR1 committed Sep 1, 2024
1 parent 2efc814 commit 2e18cfa
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 17 deletions.
22 changes: 14 additions & 8 deletions fittrackee/administration/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,11 +349,17 @@ def receive_after_flush(session: Session, context: Connection) -> None:
User.id != new_appeal.user_id,
User.is_active == True, # noqa
).all():
notification = Notification(
from_user_id=new_appeal.user_id,
to_user_id=admin.id,
created_at=new_appeal.created_at,
event_type='suspension_appeal',
event_object_id=new_appeal.id,
)
session.add(notification)
admin_action = AdminAction.query.filter_by().first()
if admin_action:
notification = Notification(
from_user_id=new_appeal.user_id,
to_user_id=admin.id,
created_at=new_appeal.created_at,
event_type=(
'user_warning_appeal'
if admin_action.action_type == 'user_warning'
else 'suspension_appeal'
),
event_object_id=new_appeal.id,
)
session.add(notification)
4 changes: 1 addition & 3 deletions fittrackee/reports/reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,7 @@ def create_admin_action(
)


@reports_blueprint.route(
'/suspensions/appeals/<string:appeal_id>', methods=["PATCH"]
)
@reports_blueprint.route('/appeals/<string:appeal_id>', methods=["PATCH"])
@require_auth(scopes=['users:write'], as_admin=True)
def process_appeal(
auth_user: User, appeal_id: str
Expand Down
2 changes: 1 addition & 1 deletion fittrackee/tests/reports/test_reports_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3293,7 +3293,7 @@ def test_it_does_not_send_email_when_email_sending_id_disabled(
class TestProcessAdminActionAppeal(
CommentMixin, WorkoutMixin, UserModerationMixin, ApiTestCaseMixin
):
route = '/api/suspensions/appeals/{appeal_id}'
route = '/api/appeals/{appeal_id}'

def test_it_returns_error_if_user_is_not_authenticated(
self, app: Flask, user_1: User
Expand Down
76 changes: 76 additions & 0 deletions fittrackee/tests/users/test_users_notifications_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1529,3 +1529,79 @@ def test_it_serializes_user_warning_notification_on_comment_report(
assert serialized_notification["type"] == "user_warning"
assert "report" not in serialized_notification
assert "workout" not in serialized_notification


class TestNotificationForUserWarningAppeal(
NotificationTestCase, UserModerationMixin
):
def test_it_does_not_create_notification_when_admin_is_inactive(
self, app: Flask, user_1_admin: User, user_2: User, user_3: User
) -> None:
report = self.create_report(reporter=user_2, reported_object=user_3)
admin_action = self.create_admin_action(
user_1_admin,
user_2,
action_type="user_warning",
report_id=report.id,
)
self.create_action_appeal(admin_action.id, user_2, with_commit=False)
user_1_admin.is_active = False
db.session.commit()

notification = Notification.query.filter_by(
from_user_id=user_2.id,
to_user_id=user_1_admin.id,
event_type='user_warning_appeal',
).first()
assert notification is None

def test_it_creates_notification_on_appeal(
self, app: Flask, user_1_admin: User, user_2: User, user_3: User
) -> None:
report = self.create_report(reporter=user_2, reported_object=user_3)
admin_action = self.create_admin_action(
user_1_admin,
user_2,
action_type="user_warning",
report_id=report.id,
)
appeal = self.create_action_appeal(admin_action.id, user_2)

notification = Notification.query.filter_by(
from_user_id=user_2.id,
to_user_id=user_1_admin.id,
event_type='user_warning_appeal',
).first()
assert notification.created_at == appeal.created_at
assert notification.marked_as_read is False
assert notification.event_object_id == appeal.id

def test_it_serializes_user_warning_appeal_notification(
self, app: Flask, user_1_admin: User, user_2: User, user_3: User
) -> None:
report = self.create_report(reporter=user_2, reported_object=user_3)
admin_action = self.create_admin_action(
user_1_admin,
user_2,
action_type="user_warning",
report_id=report.id,
)
self.create_action_appeal(admin_action.id, user_2)
notification = Notification.query.filter_by(
from_user_id=user_2.id,
to_user_id=user_1_admin.id,
event_type='user_warning_appeal',
).first()

serialized_notification = notification.serialize()

assert serialized_notification["created_at"] == notification.created_at
assert serialized_notification["from"] == user_2.serialize(
current_user=user_1_admin
)
assert serialized_notification["id"] == notification.id
assert serialized_notification["marked_as_read"] is False
assert serialized_notification["report"] == report.serialize(
user_1_admin
)
assert serialized_notification["type"] == "user_warning_appeal"
9 changes: 7 additions & 2 deletions fittrackee/users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
'report',
'suspension_appeal',
'user_warning',
'user_warning_appeal',
'workout_comment',
'workout_like',
'workout_suspension',
Expand Down Expand Up @@ -964,11 +965,15 @@ def serialize(self) -> Dict:
user=to_user
)

if self.event_type in ["report", "suspension_appeal"]:
if self.event_type in [
"report",
"suspension_appeal",
"user_warning_appeal",
]:
from fittrackee.administration.models import AdminActionAppeal
from fittrackee.reports.models import Report

if self.event_type == "suspension_appeal":
if self.event_type in ["suspension_appeal", "user_warning_appeal"]:
appeal = AdminActionAppeal.query.filter_by(
id=self.event_object_id
).first()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,9 @@
/>
<ReportNotification
v-else-if="
['report', 'suspension_appeal'].includes(notification.type) &&
notification.report
['report', 'suspension_appeal', 'user_warning_appeal'].includes(
notification.type
) && notification.report
"
:report="notification.report"
/>
Expand Down Expand Up @@ -176,6 +177,8 @@
return 'notifications.APPEALED_SUSPENSION'
case 'user_warning':
return 'notifications.YOU_RECEIVED_A_WARNING'
case 'user_warning_appeal':
return 'notifications.APPEALED_USER_WARNING'
case 'workout_comment':
return 'notifications.COMMENTED_YOUR_WORKOUT'
case 'workout_like':
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
@@ -1,6 +1,7 @@
{
"ALL": "All",
"APPEALED_SUSPENSION": "appealed the suspension",
"APPEALED_USER_WARNING": "appealed the warning",
"COMMENTED_YOUR_WORKOUT": "commented your workout",
"LIKED_YOUR_COMMENT": "liked your comment",
"LIKED_YOUR_WORKOUT": "liked your workout",
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
@@ -1,6 +1,7 @@
{
"ALL": "Toutes",
"APPEALED_SUSPENSION": "a fait appel de la suspension",
"APPEALED_USER_WARNING": "a fait appel de l'avertissement",
"COMMENTED_YOUR_WORKOUT": "a commenté votre séance",
"LIKED_YOUR_COMMENT": "a aimé votre commentaire",
"LIKED_YOUR_WORKOUT": "a aimé votre séance",
Expand Down
2 changes: 1 addition & 1 deletion fittrackee_client/src/store/modules/reports/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export const actions: ActionTree<IReportsState, IRootState> & IReportsActions =
context.commit(ROOT_STORE.MUTATIONS.EMPTY_ERROR_MESSAGES)
const { appealId, reportId, ...data } = payload
authApi
.patch(`suspensions/appeals/${appealId}`, data)
.patch(`appeals/${appealId}`, data)
.then((res) => {
if (res.data.status === 'success') {
context.dispatch(REPORTS_STORE.ACTIONS.GET_REPORT, {
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 @@ -13,6 +13,7 @@ export type TNotificationType =
| 'report'
| 'suspension_appeal'
| 'user_warning'
| 'user_warning_appeal'
| 'workout_comment'
| 'workout_suspension'
| 'workout_unsuspension'
Expand Down

0 comments on commit 2e18cfa

Please sign in to comment.