Skip to content

Commit

Permalink
feat: added events in account level notification preferences (#36231)
Browse files Browse the repository at this point in the history
  • Loading branch information
AhtishamShahid authored Feb 7, 2025
1 parent eaf6f66 commit 02d2d34
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 27 deletions.
69 changes: 47 additions & 22 deletions openedx/core/djangoapps/notifications/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,32 @@ def notification_event_context(user, course_id, notification):
}


def notification_preferences_viewed_event(request, course_id):
def notification_preferences_viewed_event(request, course_id=None):
"""
Emit an event when a user views their notification preferences.
"""
event_data = {
'user_id': str(request.user.id),
'course_id': None,
'user_forum_roles': [],
'user_course_roles': [],
'type': 'account'
}
if not course_id:
tracker.emit(
NOTIFICATION_PREFERENCES_VIEWED,
event_data
)
return
context = contexts.course_context_from_course_id(course_id)
with tracker.get_tracker().context(NOTIFICATION_PREFERENCES_VIEWED, context):
event_data['course_id']: str(course_id)
event_data['user_forum_roles'] = get_user_forums_roles(request.user, course_id)
event_data['user_course_roles'] = get_user_course_roles(request.user, course_id)
event_data['type'] = 'course'
tracker.emit(
NOTIFICATION_PREFERENCES_VIEWED,
{
'user_id': str(request.user.id),
'course_id': str(course_id),
'user_forum_roles': get_user_forums_roles(request.user, course_id),
'user_course_roles': get_user_course_roles(request.user, course_id),
}
event_data
)


Expand Down Expand Up @@ -125,23 +137,36 @@ def notification_preference_update_event(user, course_id, updated_preference):
"""
Emit an event when a notification preference is updated.
"""
context = contexts.course_context_from_course_id(course_id)
with tracker.get_tracker().context(NOTIFICATION_PREFERENCES_UPDATED, context):
value = updated_preference.get('value', '')
if updated_preference.get('notification_channel', '') == 'email_cadence':
value = updated_preference.get('email_cadence', '')
value = updated_preference.get('value', '')
if updated_preference.get('notification_channel', '') == 'email_cadence':
value = updated_preference.get('email_cadence', '')
event_data = {
'user_id': str(user.id),
'notification_app': updated_preference.get('notification_app', ''),
'notification_type': updated_preference.get('notification_type', ''),
'notification_channel': updated_preference.get('notification_channel', ''),
'value': value,
'course_id': None,
'user_forum_roles': [],
'user_course_roles': [],
'type': 'course',
}
if not isinstance(course_id, list):
context = contexts.course_context_from_course_id(course_id)
with tracker.get_tracker().context(NOTIFICATION_PREFERENCES_UPDATED, context):
event_data['course_id'] = str(course_id)
event_data['user_forum_roles'] = get_user_forums_roles(user, course_id)
event_data['user_course_roles'] = get_user_course_roles(user, course_id)
tracker.emit(
NOTIFICATION_PREFERENCES_UPDATED,
event_data
)
else:
event_data['course_ids'] = course_id
event_data['type'] = 'account'
tracker.emit(
NOTIFICATION_PREFERENCES_UPDATED,
{
'user_id': str(user.id),
'course_id': str(course_id),
'user_forum_roles': get_user_forums_roles(user, course_id),
'user_course_roles': get_user_course_roles(user, course_id),
'notification_app': updated_preference.get('notification_app', ''),
'notification_type': updated_preference.get('notification_type', ''),
'notification_channel': updated_preference.get('notification_channel', ''),
'value': value
}
event_data
)


Expand Down
19 changes: 14 additions & 5 deletions openedx/core/djangoapps/notifications/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
notification_preferences_viewed_event,
notification_read_event,
notification_tray_opened_event,
notifications_app_all_read_event
notifications_app_all_read_event,
)
from .models import CourseNotificationPreference, Notification
from .serializers import (
Expand Down Expand Up @@ -528,7 +528,6 @@ def post(self, request):
'course_id': str(preference.course_id),
'error': str(e)
})

response_data = {
'status': 'success' if updated_courses else 'partial_success' if errors else 'error',
'message': 'Notification preferences update completed',
Expand All @@ -542,10 +541,20 @@ def post(self, request):
'total_courses': notification_preferences.count()
}
}

if errors:
response_data['errors'] = errors

event_data = {
'notification_app': app,
'notification_type': notification_type,
'notification_channel': channel,
'value': value,
'email_cadence': value
}
notification_preference_update_event(
request.user,
[course['course_id'] for course in updated_courses],
event_data
)
return Response(
response_data,
status=status.HTTP_200_OK if updated_courses else status.HTTP_400_BAD_REQUEST
Expand Down Expand Up @@ -579,7 +588,7 @@ def get(self, request):
notification_configs = aggregate_notification_configs(
notification_configs
)

notification_preferences_viewed_event(request)
return Response({
'status': 'success',
'message': 'Notification preferences retrieved',
Expand Down

0 comments on commit 02d2d34

Please sign in to comment.