Skip to content

Commit

Permalink
Revert "API view added for refreshing employees from BambooHR (#120)"
Browse files Browse the repository at this point in the history
This reverts commit 82a01d6.
  • Loading branch information
Ashutosh619-sudo committed Jan 5, 2024
1 parent a17a0be commit 96b4105
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 131 deletions.
18 changes: 0 additions & 18 deletions apps/bamboohr/migrations/0007_bamboohr_webhook_id.py

This file was deleted.

1 change: 0 additions & 1 deletion apps/bamboohr/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class BambooHr(models.Model):
updated_at = models.DateTimeField(auto_now=True, help_text='Updated at datetime')
employee_exported_at = models.DateTimeField(auto_now_add=True, help_text='Employee exported to Fyle at datetime')
is_credentials_expired = models.BooleanField(default=False, help_text='BambooHr Credential Status')
webhook_id = models.IntegerField(null=True, help_text='ID of the webhook created by BambooHr')

class Meta:
db_table = 'bamboohr'
Expand Down
30 changes: 2 additions & 28 deletions apps/bamboohr/signals.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,13 @@

from django.db.models.signals import pre_save, post_save, pre_delete
from django.db.models.signals import pre_save
from django.dispatch import receiver
from admin_settings.settings import API_URL

from workato import Workato

from bamboosdk.bamboohrsdk import BambooHrSDK
from apps.bamboohr.models import BambooHrConfiguration, BambooHr
from apps.bamboohr.models import BambooHrConfiguration
from apps.orgs.models import Org

@receiver(pre_save, sender=BambooHrConfiguration)
def run_pre_save_configuration_triggers(sender, instance: BambooHrConfiguration, **kwargs):
connector = Workato()
org = Org.objects.get(id=instance.org_id)
connector.recipes.post(org.managed_user_id, instance.recipe_id, None, 'stop')


@receiver(post_save, sender=BambooHr)
def run_post_save_bamboohr_triggers(sender, instance: BambooHr, **kwargs):

bamboohrsdk = BambooHrSDK(api_token=instance.api_token, sub_domain=instance.sub_domain)

webhook_payload = {
'postFields': {
'firstName': 'firstName',
'lastName': 'lastName',
'department': 'department',
'workEmail': 'workEmail',
'status': 'status'
},
'name': instance.org.name,
'monitorFields': ['firstName', 'lastName', 'department', 'workEmail', 'status'],
'url': API_URL + f'/orgs/{instance.org.id}/bamboohr/webhook_callback/',
'format': 'json'
}

response = bamboohrsdk.webhook.post(payload=webhook_payload)
BambooHr.objects.filter(id=instance.id).update(webhook_id=int(response['id']))
40 changes: 0 additions & 40 deletions apps/bamboohr/tasks.py

This file was deleted.

3 changes: 1 addition & 2 deletions apps/bamboohr/urls.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
from django.urls import path

from .views import PostFolder, PostPackage, BambooHrConnection, BambooHrView, BambooHrConfigurationView, \
DisconnectView, SyncEmployeesView, HealthCheck, WebhookCallbackAPIView
DisconnectView, SyncEmployeesView, HealthCheck

app_name = 'bamboohr'

urlpatterns = [
path('webhook_callback/', WebhookCallbackAPIView.as_view(), name='webhook-callback'),
path('health_check/', HealthCheck.as_view(), name='health-check'),
path('', BambooHrView.as_view(), name='bamboohr'),
path('packages/', PostPackage.as_view(), name='package'),
Expand Down
58 changes: 31 additions & 27 deletions apps/bamboohr/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from apps.bamboohr.actions import disconnect_bamboohr, sync_employees
from apps.names import BAMBOO_HR

from django_q.tasks import async_task
from rest_framework.views import APIView

logger = logging.getLogger(__name__)
logger.level = logging.INFO
Expand Down Expand Up @@ -48,24 +48,6 @@ def get(self, request, *args, **kwargs):
status=status.HTTP_404_NOT_FOUND
)


class WebhookCallbackAPIView(generics.CreateAPIView):

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

org_id = kwargs['org_id']
user = self.request.user
payload = request.data

async_task('apps.bamboohr.tasks.update_employee', org_id, user, payload)

return Response(
{
'status': 'success'
},
status=status.HTTP_201_CREATED
)

class BambooHrView(generics.ListAPIView):
serializer_class = BambooHrSerializer

Expand Down Expand Up @@ -213,11 +195,17 @@ class DisconnectView(generics.CreateAPIView):

def post(self, request, *args, **kwargs):
try:
configuration = BambooHrConfiguration.objects.get(org__id=kwargs['org_id'])
bamboohr = BambooHr.objects.filter(org__id=kwargs['org_id']).first()
bambamboohrsdk = BambooHrSDK(api_token=bamboohr.api_token, sub_domain=bamboohr.sub_domain)
response = bambamboohrsdk.webhook.delete(id=bamboohr.webhook_id)

connection = disconnect_bamboohr(kwargs['org_id'], configuration, bamboohr)

# in case of an error response
if isinstance(connection, Response):
return connection

return Response(
data=response,
data=connection,
status=status.HTTP_200_OK
)
except BambooHr.DoesNotExist:
Expand All @@ -227,6 +215,11 @@ def post(self, request, *args, **kwargs):
},
status = status.HTTP_404_NOT_FOUND
)
except BambooHrConfiguration.DoesNotExist:
return Response(
data={'message': 'BambooHr Configuration does not exist for this Workspace'},
status=status.HTTP_404_NOT_FOUND
)


class SyncEmployeesView(generics.UpdateAPIView):
Expand All @@ -237,9 +230,20 @@ class SyncEmployeesView(generics.UpdateAPIView):

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

async_task('apps.bamboohr.tasks.refresh_employees', kwargs['org_id'], self.request.user)
try:
config = BambooHrConfiguration.objects.get(org__id=kwargs['org_id'])
sync_recipe = sync_employees(kwargs['org_id'], config)

return Response(
data = {'message': 'success'},
status=status.HTTP_201_CREATED
)
# in case of an error response
if isinstance(sync_recipe, Response):
return sync_recipe

return Response(
data=sync_recipe,
status=status.HTTP_200_OK
)
except BambooHrConfiguration.DoesNotExist:
return Response(
data={'message': 'BambooHr Configuration does not exist for this Workspace'},
status=status.HTTP_404_NOT_FOUND
)
3 changes: 2 additions & 1 deletion bamboosdk/api/api_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ def _delete_request(self, module_api_path):
url= self.API_BASE_URL.format(self.__sub_domain) + module_api_path
response = requests.delete(url=url, headers=self.headers)
if response.status_code == 200:
return {'message':'Web hook has been deleted'}
result = json.loads(response.text)
return result

if response.status_code == 403:
error_msg = json.loads(response.text)
Expand Down
20 changes: 8 additions & 12 deletions fyle_employee_imports/bamboo_hr.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ class BambooHrEmployeeImport(FyleEmployeeImport):

def __init__(self, org_id: int, user: User):
super().__init__(org_id, user)
self.bamboohr_sdk = BambooHrSDK(api_token=self.bamboohr.api_token, sub_domain=self.bamboohr.sub_domain)
bamboo_hr = BambooHr.objects.get(org_id__in=org_id)
self.bamboohr_sdk = BambooHrSDK(api_token=bamboo_hr.api_token, sub_domain=bamboo_hr.sub_domain)

def sync_hrms_employees(self):
employees = self.bamboohr_sdk.employees.get_all()
Expand All @@ -19,23 +20,18 @@ def sync_hrms_employees(self):
def upsert_employees(self, employees: Dict):
attributes = []
for employee in employees['employees']:
supervisor = [employee.get('supervisorEmail', None)]
active_status = True if employee.get('status', None) == 'Active' else False

display_name = employee.get('displayName', None)
if not display_name:
display_name = employee['firstName'] + ' ' + employee['lastName']

supervisor = [employee['supervisorEmail']]
active_status = True if employee['status'] == 'Active' else False
detail = {
'email': employee.get('workEmail', None),
'department_name': employee.get('department', None),
'full_name': display_name,
'email': employee['workEmail'] if employee['workEmail'] else None,
'department_name': employee['department'] if employee['department'] else None,
'full_name': employee['displayName'] if employee['displayName'] else None,
'approver_emails': supervisor,
}

attributes.append({
'attribute_type': 'EMPLOYEE',
'value': display_name,
'value': employee['displayName'],
'destination_id': employee['id'],
'detail': detail,
'active': active_status
Expand Down
5 changes: 3 additions & 2 deletions fyle_employee_imports/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ 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)
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 @@ -115,6 +115,7 @@ def get_employee_and_approver_payload(self, hrms_employees):

def fyle_employee_import(self, hrms_employees):
fyle_employee_payload, employee_approver_payload = self.get_employee_and_approver_payload(hrms_employees)

if fyle_employee_payload:
self.platform_connection.bulk_post_employees(employees_payload=fyle_employee_payload)

Expand All @@ -140,6 +141,6 @@ def sync_employees(self):
org_id=self.org_id,
updated_at__gte=self.bamboohr.employee_exported_at,
).order_by('value', 'id')

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

0 comments on commit 96b4105

Please sign in to comment.