Skip to content

Commit

Permalink
Delete notifications older than 90 days (#1328)
Browse files Browse the repository at this point in the history
* used prefetch_related method to optimize the query performed to fetch a many to many relationship

* Revert "add direnv support (#1304)"

This reverts commit f613661.

* Revert "Revert "add direnv support (#1304)""

This reverts commit 7d410fd.

* Revert "used prefetch_related method to optimize the query performed to fetch a many to many relationship"

This reverts commit 8946d5f.

* used prefetch where we are querying the skills using UserAssigned Serializer

* Revert "used prefetch where we are querying the skills using UserAssigned Serializer"

This reverts commit 7000a38.

* made neccessary changes for prefetching the skills

* Revert "made neccessary changes for prefetching the skills"

This reverts commit f59ff6b.

* delete older notifications

* added tests
  • Loading branch information
yaswanthsaivendra authored Jun 8, 2023
1 parent d0c4096 commit c282517
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
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())

0 comments on commit c282517

Please sign in to comment.