Skip to content
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

V2 map employees api #430

Merged
merged 15 commits into from
Oct 20, 2023
10 changes: 9 additions & 1 deletion apps/mappings/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,17 @@
from apps.workspaces.models import Configuration
from apps.workspaces.tasks import delete_cards_mapping_settings

from .models import GeneralMapping
from .models import GeneralMapping, SubsidiaryMapping
from .tasks import schedule_auto_map_ccc_employees


@receiver(post_save, sender=SubsidiaryMapping)
def run_post_subsidiary_mappings(sender, instance: SubsidiaryMapping, **kwargs):

workspace = instance.workspace
workspace.onboarding_state = 'MAP_EMPLOYEES'
workspace.save()

@receiver(post_save, sender=MappingSetting)
def run_post_mapping_settings_triggers(sender, instance: MappingSetting, **kwargs):
"""
Expand Down
Empty file.
55 changes: 55 additions & 0 deletions apps/workspaces/apis/map_employees/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from apps.workspaces.apis.map_employees.triggers import MapEmplyeesTriggers
from apps.workspaces.models import Configuration, Workspace
from rest_framework import serializers


class ConfigurationSerializer(serializers.ModelSerializer):
class Meta:
model = Configuration
fields = ['employee_field_mapping', 'auto_map_employees']


class MapEmployeesSerializer(serializers.ModelSerializer):

configuration = ConfigurationSerializer()
workspace_id = serializers.SerializerMethodField()

class Meta:
model = Workspace
fields = ['configuration', 'workspace_id']
read_only_fields = ['workspace_id']

def get_workspace_id(self, instance):
return instance.id

def update(self, instance, validated_data):

workspace_id = instance.id
configuration = validated_data.pop('configuration')

configuration_instance = Configuration.objects.filter(workspace_id=workspace_id).first()

if configuration and (configuration_instance.employee_field_mapping != configuration['employee_field_mapping']):
configuration_instance.reimbursable_expenses_object = None
configuration_instance.save()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this block required?


configuration_instance, _ = Configuration.objects.update_or_create(
workspace_id=workspace_id, defaults={'employee_field_mapping': configuration['employee_field_mapping'], 'auto_map_employees': configuration['auto_map_employees']}
)

MapEmplyeesTriggers.run_workspace_general_settings_triggers(configuration=configuration_instance)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

run_configurations_triggers


if instance.onboarding_state == 'MAP_EMPLOYEES':
instance.onboarding_state = 'EXPORT_SETTINGS'
instance.save()

return instance

def validate(self,data):
if not data.get('configuration').get('employee_field_mapping'):
raise serializers.ValidationError('employee_field_mapping field is required')

if data.get('configuration').get('auto_map_employees') and data.get('configuration').get('auto_map_employees') not in ['EMAIL', 'NAME', 'EMPLOYEE_CODE']:
raise serializers.ValidationError('auto_map_employees can have only EMAIL / NAME / EMPLOYEE_CODE')

return data
10 changes: 10 additions & 0 deletions apps/workspaces/apis/map_employees/triggers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from apps.mappings.tasks import schedule_auto_map_employees
from apps.workspaces.models import Configuration


class MapEmplyeesTriggers:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MapEmployeesTriggers


@staticmethod
def run_workspace_general_settings_triggers(configuration: Configuration):

schedule_auto_map_employees(configuration.auto_map_employees, configuration.workspace.id)
11 changes: 11 additions & 0 deletions apps/workspaces/apis/map_employees/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from rest_framework import generics
from apps.workspaces.apis.map_employees.serializers import MapEmployeesSerializer
from apps.workspaces.models import Workspace


class MapEmployeesView(generics.RetrieveUpdateAPIView):

serializer_class = MapEmployeesSerializer

def get_object(self):
return Workspace.objects.filter(id=self.kwargs['workspace_id']).first()
8 changes: 8 additions & 0 deletions apps/workspaces/apis/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.urls import path

from apps.workspaces.apis.map_employees.views import MapEmployeesView


urlpatterns = [
path('<int:workspace_id>/map_employees/', MapEmployeesView.as_view()),
]
18 changes: 18 additions & 0 deletions apps/workspaces/migrations/0035_auto_20231019_1025.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.1.14 on 2023-10-19 10:25

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('workspaces', '0034_auto_20231012_0750'),
]

operations = [
migrations.AlterField(
model_name='configuration',
name='reimbursable_expenses_object',
field=models.CharField(choices=[('EXPENSE REPORT', 'EXPENSE REPORT'), ('JOURNAL ENTRY', 'JOURNAL ENTRY'), ('BILL', 'BILL')], help_text='Reimbursable Expenses type', max_length=50, null=True),
),
]
2 changes: 1 addition & 1 deletion apps/workspaces/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class Configuration(models.Model):
max_length=50, choices=EMPLOYEE_FIELD_MAPPING_CHOICES, help_text='Employee field mapping', null=True
)
reimbursable_expenses_object = models.CharField(
max_length=50, choices=REIMBURSABLE_EXPENSES_OBJECT_CHOICES, help_text='Reimbursable Expenses type'
max_length=50, choices=REIMBURSABLE_EXPENSES_OBJECT_CHOICES, help_text='Reimbursable Expenses type', null=True
)
corporate_credit_card_expenses_object = models.CharField(
max_length=50, choices=COPORATE_CARD_EXPENSES_OBJECT_CHOICES,
Expand Down
1 change: 1 addition & 0 deletions apps/workspaces/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ def post(self, request, **kwargs):
workspace=workspace
)
workspace.ns_account_id = ns_account_id
workspace.onboarding_state = 'SUBSIDIARY'
workspace.save()

else:
Expand Down
3 changes: 2 additions & 1 deletion fyle_netsuite_api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@
path('admin/', admin.site.urls),
path('api/auth/', include('fyle_rest_auth.urls')),
path('api/workspaces/', include('apps.workspaces.urls')),
path('api/user/', include('apps.users.urls'))
path('api/user/', include('apps.users.urls')),
path('api/v2/workspaces/', include('apps.workspaces.apis.urls')),
]
Loading