Skip to content

Commit

Permalink
email support on import fail
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashutosh619-sudo committed Jan 5, 2024
1 parent a3cf9b0 commit 8f0d5b5
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
41 changes: 41 additions & 0 deletions apps/bamboohr/email.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import csv
from io import StringIO

import sendgrid
from sendgrid.helpers.mail import Mail, Attachment
from python_http_client.exceptions import HTTPError
import base64

from admin_settings import settings

def send_failure_notification_email(employees, number_of_employees ,admin_email):

with open('apps/bamboohr/templates/mail_template.html', 'r') as file:
email_template = file.read()

email_content_html = email_template.format_map({'number_of_employees':number_of_employees})

csv_file = StringIO()
csv_writer = csv.DictWriter(csv_file, fieldnames=['id', 'name'])
csv_writer.writeheader()
csv_writer.writerows(employees)

csv_content_base64 = base64.b64encode(csv_file.getvalue().encode()).decode()

message = Mail(
from_email=settings.SENDGRID_EMAIL,
to_emails=admin_email,
subject='Error Importing Employees from Bamboo HR to Fyle',
html_content=email_content_html
)

attachment = Attachment()
attachment.file_content = csv_content_base64
attachment.file_name = 'employee_data.csv'
attachment.file_type = 'text/csv'
attachment.disposition = 'attachment'
message.attachment = attachment

sg = sendgrid.SendGridAPIClient(api_key=settings.SENDGRID_API_KEY)
sg.send(message)

16 changes: 14 additions & 2 deletions fyle_employee_imports/base.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
from typing import Dict, List
from datetime import datetime

from apps.bamboohr.models import BambooHr
from apps.bamboohr.models import BambooHr, BambooHrConfiguration
from apps.fyle_hrms_mappings.models import DestinationAttribute, ExpenseAttribute
from apps.orgs.models import Org
from apps.users.helpers import PlatformConnector
from fyle_rest_auth.models import AuthToken

from apps.bamboohr.email import send_failure_notification_email
from django.conf import settings

class FyleEmployeeImport():

def __init__(self, org_id: int, user):
self.org_id = org_id
self.user = user
self.bamboohr = BambooHr.objects.get(org_id__in=self.org_id)
self.bamboohr = BambooHr.objects.get(org_id__in=[self.org_id])
self.bamboohr_configuration = BambooHrConfiguration.objects.get(org_id__in=[self.org_id])
refresh_token = AuthToken.objects.get(user__user_id=self.user).refresh_token
cluster_domain = Org.objects.get(user__user_id=self.user).cluster_domain
self.platform_connection = PlatformConnector(refresh_token, cluster_domain)
Expand Down Expand Up @@ -78,6 +82,8 @@ def get_employee_and_approver_payload(self, hrms_employees):
employee_emails: List[str] = []
approver_emails: List[str] = []
employee_approver_payload: List[Dict] = []
unimported_employees: List = []
no_unimported_employees: int = 0

for employee in hrms_employees:
if employee.detail['email']:
Expand All @@ -97,6 +103,12 @@ def get_employee_and_approver_payload(self, hrms_employees):
'approver_emails': employee.detail['approver_emails']
})
approver_emails.extend(employee.detail['approver_emails'])
else:
no_unimported_employees += 1
unimported_employees.append({'name': employee.detail['full_name'], 'id':employee.destination_id})

admin_email = self.bamboohr_configuration.additional_email_options['admin_email']
send_failure_notification_email(employees=unimported_employees, number_of_employees=no_unimported_employees, admin_email=admin_email)

existing_approver_emails = ExpenseAttribute.objects.filter(
org_id=self.org_id, attribute_type='EMPLOYEE', value__in=approver_emails
Expand Down
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,6 @@ polling==0.3.2
# Requests
requests==2.25.0
sentry-sdk==1.19.1

#sendgrid
sendgrid==6.11.0

0 comments on commit 8f0d5b5

Please sign in to comment.