Skip to content

Commit

Permalink
Moved the email notification logic to a seperate schedule (#183)
Browse files Browse the repository at this point in the history
* Moved the email notification logic to a seperate schedule

* Moved the email notification logic to a seperate schedule and bug fix related to sync_schedule

* fixed tests settings bug

* Typo fix

* Added sql script and modified django query
  • Loading branch information
Hrishabh17 authored Feb 21, 2024
1 parent 6ec811c commit 6f459dc
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 2 deletions.
1 change: 1 addition & 0 deletions admin_settings/tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
'rest_framework',
'fyle_rest_auth',
'django_filters',
'django_q',

# User Created Apps
'apps.users',
Expand Down
9 changes: 9 additions & 0 deletions apps/bamboohr/signals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django.db.models.signals import post_save
from django.dispatch import receiver
from apps.bamboohr.models import BambooHrConfiguration
from apps.bamboohr.tasks import schedule_sync_employees


@receiver(post_save, sender=BambooHrConfiguration)
def run_post_save_configurations(sender, instance: BambooHrConfiguration, *args, **kwargs):
schedule_sync_employees(org_id=instance.org.id)
29 changes: 29 additions & 0 deletions apps/bamboohr/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ def update_employee(org_id: int, payload: dict):
bamboohr_importer = BambooHrEmployeeImport(org_id=org_id)
bamboohr_importer.sync_with_webhook(employee=employee)


def send_employee_email_missing_failure_notification(org_id: int):
"""
Send failure email to employees who don't have email associated with them
"""
bamboo_hr_importer = BambooHrEmployeeImport(org_id=org_id)
bamboo_hr_importer.send_employee_email_missing_failure_notification()


def schedule_sync_employees(org_id):
"""
Create schedule to sync employees every 6 hours
Expand All @@ -45,6 +54,9 @@ def schedule_sync_employees(org_id):
}
)

schedule_failure_emails_for_employees(org_id)


def delete_sync_employee_schedule(org_id):
"""
Delete schedule when bamboohr is disconnected
Expand All @@ -56,3 +68,20 @@ def delete_sync_employee_schedule(org_id):

if schedule:
schedule.delete()


def schedule_failure_emails_for_employees(org_id):
"""
Schedule failure emails for employees who don't have email associated with them
Runs once every week
"""

Schedule.objects.update_or_create(
func = 'apps.bamboohr.tasks.send_employee_email_missing_failure_notification',
args = '{}'.format(org_id),
defaults={
'schedule_type': Schedule.MINUTES,
'minutes': 7 * 24 * 60,
'next_run': datetime.now()
}
)
1 change: 0 additions & 1 deletion apps/bamboohr/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ class SyncEmployeesView(generics.UpdateAPIView):
"""

def post(self, request, *args, **kwargs):

async_task('apps.bamboohr.tasks.import_employees', kwargs['org_id'])

return Response(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.1.14 on 2024-02-19 10:14

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('fyle_hrms_mappings', '0004_mapping'),
]

operations = [
migrations.AddField(
model_name='destinationattribute',
name='is_failure_email_sent',
field=models.BooleanField(default=False, help_text='Indicates whether the failure email is sent'),
),
]
3 changes: 2 additions & 1 deletion apps/fyle_hrms_mappings/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class DestinationAttribute(models.Model):
destination_id = models.CharField(max_length=255, help_text='Destination ID')
auto_created = models.BooleanField(default=False,
help_text='Indicates whether the field is auto created by the integration')
is_failure_email_sent = models.BooleanField(default=False, help_text='Indicates whether the failure email is sent')

class Meta:
db_table = 'destination_attributes'
Expand Down Expand Up @@ -80,7 +81,7 @@ def bulk_create_or_update_destination_attributes(
'id': existing_attribute['id'],
'value': existing_attribute['value'],
'detail': existing_attribute['detail'],
'active' : existing_attribute['active']
'active': existing_attribute['active']
}

attributes_to_be_created = []
Expand Down
23 changes: 23 additions & 0 deletions fyle_employee_imports/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from apps.bamboohr.email import send_failure_notification_email
from django.conf import settings
from django.db.models import Q

class FyleEmployeeImport():

Expand Down Expand Up @@ -169,3 +170,25 @@ def sync_employees(self, is_incremental_sync: bool):

self.import_departments(hrms_employees)
self.fyle_employee_import(hrms_employees)

def send_employee_email_missing_failure_notification(self):
hrms_employees = DestinationAttribute.objects.filter(
Q(detail__email=None),
attribute_type='EMPLOYEE',
org_id=self.org_id,
is_failure_email_sent=False
).values('detail__full_name', 'destination_id').order_by('value', 'id')

employee_to_be_notified = [
{'name': employee['detail__full_name'], 'id': employee['destination_id']}
for employee in hrms_employees
]

if len(employee_to_be_notified) > 0:
send_failure_notification_email(
employees=employee_to_be_notified,
number_of_employees=len(employee_to_be_notified),
admin_email=self.get_admin_email()
)

hrms_employees.update(is_failure_email_sent=True)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
rollback;
begin;

update destination_attributes
set is_failure_email_sent = true
where detail->>'email' is null;

0 comments on commit 6f459dc

Please sign in to comment.