Skip to content

Commit

Permalink
Update Create Employee on Webhook trigger
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashutosh619-sudo committed Jan 2, 2024
1 parent 0dc08d7 commit abeb483
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
28 changes: 28 additions & 0 deletions apps/bamboohr/tasks.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,37 @@
from apps.bamboohr.models import BambooHr
from apps.fyle_hrms_mappings.models import DestinationAttribute
from fyle_employee_imports.bamboo_hr import BambooHrEmployeeImport
from apps.users.models import User

def refresh_employees(org_id, user):

bambooHrImporter = BambooHrEmployeeImport(org_id=org_id, user=user)
bambooHrImporter.sync_employees()

def update_employee(org_id: int, user: User, payload: dict):

"""
Update employee in fyle when employee in Bamboohr is added or updated
"""
bamboohr = BambooHr.objects.get(org_id__in=org_id, is_credentials_expired=False)
bamboohr_importer = BambooHrEmployeeImport(org_id=org_id, user=user)

employee_payload = {'employees': []}
payload = payload['employees'][0]
employee = {}
for field in payload['changedFields']:
employee['id'] = payload['id']
employee[field] = payload['fields'][field]['value']

employee_payload['employees'].append(employee)

bamboohr_importer.upsert_employees(employees=employee_payload)

hrms_employees = DestinationAttribute.objects.filter(
attribute_type='EMPLOYEE',
org_id=org_id,
updated_at__gte=bamboohr.employee_exported_at,
).order_by('value', 'id')

bamboohr_importer.import_departments(hrms_employees=hrms_employees)
bamboohr_importer.fyle_employee_import(hrms_employees=hrms_employees)
18 changes: 18 additions & 0 deletions apps/bamboohr/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,24 @@ def get(self, request, *args, **kwargs):
status=status.HTTP_404_NOT_FOUND
)


class WebhookAPIView(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_OK
)

class BambooHrView(generics.ListAPIView):
serializer_class = BambooHrSerializer

Expand Down
2 changes: 1 addition & 1 deletion fyle_employee_imports/bamboo_hr.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def sync_hrms_employees(self):
def upsert_employees(self, employees: Dict):
attributes = []
for employee in employees['employees']:
supervisor = [employee['supervisorEmail']]
supervisor = [employee['supervisorEmail']] if employee['supervisorEmail'] else None
active_status = True if employee['status'] == 'Active' else False
detail = {
'email': employee['workEmail'] if employee['workEmail'] else None,
Expand Down

0 comments on commit abeb483

Please sign in to comment.