Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete notifications older than 90 days #1328

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions care/facility/tasks/notification/delete_older_notifications.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from datetime import timedelta

from celery.decorators import periodic_task
from celery.schedules import crontab
from django.utils import timezone

from care.facility.models.notification import Notification


@periodic_task(
run_every=crontab(minute="0", hour="0")
) # Run the task daily at midnight
def delete_old_notifications():
ninety_days_ago = timezone.now() - timedelta(days=90)
Notification.objects.filter(created_date__lte=ninety_days_ago).delete()
28 changes: 28 additions & 0 deletions care/facility/tests/test_delete_older_notifications_task.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from datetime import timedelta

from django.test import TestCase
from django.utils import timezone
from freezegun import freeze_time

from care.facility.models.notification import Notification
from care.facility.tasks.notification.delete_older_notifications import (
delete_old_notifications,
)


class DeleteOldNotificationsTest(TestCase):
def test_delete_old_notifications(self):
# notifications created 90 days ago
with freeze_time(timezone.now() - timedelta(days=90)):
notification1 = Notification.objects.create()
notification2 = Notification.objects.create()

# notification created now
notification3 = Notification.objects.create()

delete_old_notifications()

# Assert
self.assertFalse(Notification.objects.filter(pk=notification1.pk).exists())
self.assertFalse(Notification.objects.filter(pk=notification2.pk).exists())
self.assertTrue(Notification.objects.filter(pk=notification3.pk).exists())