diff --git a/manage_notifications/management/commands/one_week_alert.py b/manage_notifications/management/commands/one_week_alert.py index 9edcb70..5740382 100644 --- a/manage_notifications/management/commands/one_week_alert.py +++ b/manage_notifications/management/commands/one_week_alert.py @@ -11,14 +11,68 @@ from onesignal_sdk.client import Client as OneSignalClient from django.conf import settings +# class Command(BaseCommand): +# help = "Send subscription expiry one week reminders" + +# def handle(self, *args, **options): +# client = OneSignalClient( +# app_id=settings.ONE_SIGNAL_APP_ID, rest_api_key=settings.ONE_SIGNAL_API_KEY +# ) +# eligible_principals = self.eligible_principals() + +# for principal in eligible_principals: +# try: +# player_id = principal.principal.player_id +# except AttributeError: +# continue + +# notification_title = "Subscription Expiry Reminder" +# notification_message = "Your subscription is going to expire in a week." +# notification_category = NotificationCategoryChoices.SUBSCRIPTION + +# notification_payload = { +# "headings": {"en": notification_title}, +# "contents": {"en": notification_message}, +# "include_player_ids": [player_id], +# } +# response = client.send_notification(notification_payload) + +# in_app_notification = InAppNotification( +# principal=principal.principal, +# title=notification_title, +# message=notification_message, +# notification_category=notification_category, +# ) +# in_app_notification.save() + +# self.stdout.write(self.style.SUCCESS("Notifications sent successfully")) + +# def eligible_principals(self): +# one_week_from_now = timezone.now().date() + timedelta(days=7) +# return IAmPrincipalNotificationSettings.objects.filter( +# principal__principal_subscription__end_date=one_week_from_now, +# principal__principal_subscription__status=SubscriptionStatus.ACTIVE, +# principal__principal_subscription__cancelled=False, +# principal__principal_subscription__deleted=False, +# notification_category=NotificationCategoryChoices.SUBSCRIPTION, +# is_enabled=True, +# ).select_related("principal") + + class Command(BaseCommand): - help = "Send subscription expiry one week reminders" + help = "Send subscription expiry reminders" def handle(self, *args, **options): client = OneSignalClient( app_id=settings.ONE_SIGNAL_APP_ID, rest_api_key=settings.ONE_SIGNAL_API_KEY ) - eligible_principals = self.eligible_principals() + # Process each time frame + for days_before in [7, 3, 1]: + self.send_notifications(days_before, client) + + def send_notifications(self, days_before, client): + eligible_principals = self.eligible_principals(days_before) + message = f"Your subscription is going to expire in {days_before} days." for principal in eligible_principals: try: @@ -27,12 +81,9 @@ class Command(BaseCommand): continue notification_title = "Subscription Expiry Reminder" - notification_message = "Your subscription is going to expire in a week." - notification_category = NotificationCategoryChoices.SUBSCRIPTION - notification_payload = { "headings": {"en": notification_title}, - "contents": {"en": notification_message}, + "contents": {"en": message}, "include_player_ids": [player_id], } response = client.send_notification(notification_payload) @@ -40,17 +91,20 @@ class Command(BaseCommand): in_app_notification = InAppNotification( principal=principal.principal, title=notification_title, - message=notification_message, - notification_category=notification_category, + message=message, + notification_category=NotificationCategoryChoices.SUBSCRIPTION, ) in_app_notification.save() + self.stdout.write( + self.style.SUCCESS( + f"Notifications for {days_before} days sent successfully" + ) + ) - self.stdout.write(self.style.SUCCESS("Notifications sent successfully")) - - def eligible_principals(self): - one_week_from_now = timezone.now().date() + timedelta(days=7) + def eligible_principals(self, days_before): + target_date = timezone.now().date() + timedelta(days=days_before) return IAmPrincipalNotificationSettings.objects.filter( - principal__principal_subscription__end_date=one_week_from_now, + principal__principal_subscription__end_date=target_date, principal__principal_subscription__status=SubscriptionStatus.ACTIVE, principal__principal_subscription__cancelled=False, principal__principal_subscription__deleted=False,