diff --git a/formatted_data.xlsx b/formatted_data.xlsx index 257e248..6afb61b 100644 Binary files a/formatted_data.xlsx and b/formatted_data.xlsx differ diff --git a/module_notification/tasks.py b/module_notification/tasks.py index 181b89d..eaf3bd7 100644 --- a/module_notification/tasks.py +++ b/module_notification/tasks.py @@ -8,9 +8,9 @@ from module_activity.models import MealRecord, Bowel, MealSymptomRecord, Medicat def notification_for_meal(): - notification_message = "You haven’t filled your meal details yet." + notification_message = "Have you eaten yet? It's been a while since you logged a meal." current_date = datetime.now() - fifteen_days_ago = current_date - timedelta(days=20) + fifteen_days_ago = current_date - timedelta(days=15) users = IAmPrincipal.objects.filter( last_login__gte=fifteen_days_ago, @@ -60,9 +60,9 @@ def notification_for_meal(): pass def notification_for_medication(): - notification_message = "You haven’t filled your medication details yet. " + notification_message = "Have you taken your medication today? Remember to log your medication to stay on track with your treatment!" current_date = datetime.now() - fifteen_days_ago = current_date - timedelta(days=20) + fifteen_days_ago = current_date - timedelta(days=15) users = IAmPrincipal.objects.filter( last_login__gte=fifteen_days_ago, @@ -111,6 +111,58 @@ def notification_for_medication(): print(f"Notification error in medication {str(e)}") pass +def notification_for_symptom(): + notification_message = "How are you feeling? Don't forget to input your symptoms." + current_date = datetime.now() + fifteen_days_ago = current_date - timedelta(days=15) + + users = IAmPrincipal.objects.filter( + last_login__gte=fifteen_days_ago, + principal_type=IAmPrincipalType.get_principal_user(), + ).values_list("id", flat=True) + + symptom_obj = MealSymptomRecord.objects.filter(date=current_date).values_list( + "principal", flat=True + ) + + # Remove IDs of users who have recorded symptoms for the current day + users_without_medications = set(users) - set(symptom_obj) + print(f"user id {set(users)}") + print( + f"users_without_medication {users_without_medications}" + ) + + player_ids = list( + IAmPrincipal.objects.filter( + id__in=users_without_medications + ).values_list("player_id", flat=True) + ) + # removing none from list + player_ids = list(itertools.filterfalse(lambda x: x is None, player_ids)) + + notifications_to_create = [] + if users_without_medications: + for user_id in users_without_medications: + message = notification_message + notifications_to_create.append( + InAppNotification(user_id=user_id, message=message) + ) + + # Bulk create notifications + if notifications_to_create: + InAppNotification.objects.bulk_create(notifications_to_create) + + try: + notification = OneSignalService() + response = notification.send_notification( + headings="Medication", + contents=notification_message, + include_player_ids=player_ids, + ) + except Exception as e: + print(f"Notification error in medication {str(e)}") + pass + def testing_cron(): print("cron is working ") diff --git a/module_project/settings/base.py b/module_project/settings/base.py index d877059..ef5b9e8 100644 --- a/module_project/settings/base.py +++ b/module_project/settings/base.py @@ -287,6 +287,7 @@ SIMPLE_JWT = { CRONJOBS = [ ('0 18 * * *', 'module_notification.tasks.notification_for_meal', '>> {}'.format(str(BASE_DIR / 'logs/cron.log'))), ('0 20 * * *', 'module_notification.tasks.notification_for_medication', '>> {}'.format(str(BASE_DIR / 'logs/cron.log'))), + ('0 22 * * *', 'module_notification.tasks.notification_for_symptom', '>> {}'.format(str(BASE_DIR / 'logs/cron.log'))), # ('* * * * *', 'module_notification.tasks.testing_cron', '>> {}'.format(str(BASE_DIR / 'logs/cron.log'))), ]