-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update and Create Employee on Webhook trigger from bamboohr #124
Changes from 7 commits
abeb483
6b7e151
416c596
11f8bff
f2c9958
35a5887
536e60e
0112fdf
42c0b2f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Generated by Django 3.1.14 on 2024-01-02 16:10 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('bamboohr', '0006_bamboohr_is_credentials_expired'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='bamboohr', | ||
name='webhook_id', | ||
field=models.IntegerField(help_text='ID of the webhook created by BambooHr', null=True), | ||
), | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,46 @@ | ||
|
||
from django.db.models.signals import pre_save | ||
from django.db.models.signals import pre_save, post_save, pre_delete | ||
from django.dispatch import receiver | ||
from admin_settings.settings import API_URL | ||
|
||
from workato import Workato | ||
from apps.bamboohr.models import BambooHrConfiguration | ||
|
||
from bamboosdk.bamboohrsdk import BambooHrSDK | ||
from apps.bamboohr.models import BambooHrConfiguration, BambooHr | ||
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): | ||
|
||
# Disconnect the signal to avoid triggering it again | ||
post_save.disconnect(run_post_save_bamboohr_triggers, sender=BambooHr) | ||
|
||
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) | ||
instance.webhook_id = int(response['id']) | ||
instance.save() | ||
|
||
# Reconnect the signal after saving | ||
post_save.connect(run_post_save_bamboohr_triggers, sender=BambooHr) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
from django.urls import path | ||
|
||
from .views import PostFolder, PostPackage, BambooHrConnection, BambooHrView, BambooHrConfigurationView, \ | ||
DisconnectView, SyncEmployeesView, HealthCheck | ||
DisconnectView, SyncEmployeesView, HealthCheck, WebhookAPIView | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. WebhookCallbackAPIView |
||
|
||
app_name = 'bamboohr' | ||
|
||
urlpatterns = [ | ||
path('webhook_callback/', WebhookAPIView.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'), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,6 +48,24 @@ def get(self, request, *args, **kwargs): | |
status=status.HTTP_404_NOT_FOUND | ||
) | ||
|
||
|
||
class WebhookAPIView(generics.CreateAPIView): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. WebhookCallbackAPIView, match class name with url |
||
|
||
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 | ||
|
||
|
@@ -195,17 +213,12 @@ 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() | ||
|
||
connection = disconnect_bamboohr(kwargs['org_id'], configuration, bamboohr) | ||
|
||
# in case of an error response | ||
if isinstance(connection, Response): | ||
return connection | ||
|
||
webhook_id = request.data['webhook_id'] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. read it from table since we have access for it, need not depend on client |
||
bambamboohrsdk = BambooHrSDK(api_token=bamboohr.api_token, sub_domain=bamboohr.sub_domain) | ||
bambamboohrsdk.webhook.delete(id=webhook_id) | ||
return Response( | ||
data=connection, | ||
data='Succesfully disconnected Bamboohr', | ||
status=status.HTTP_200_OK | ||
) | ||
except BambooHr.DoesNotExist: | ||
|
@@ -215,11 +228,6 @@ 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): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do all these only when webhook_id is null