Skip to content

Commit

Permalink
Feat: Adding Created_by and updated_by to conf tables
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashutosh619-sudo committed Dec 26, 2024
1 parent af802ea commit 8e31fea
Show file tree
Hide file tree
Showing 14 changed files with 124 additions and 14 deletions.
23 changes: 23 additions & 0 deletions apps/fyle/migrations/0037_auto_20241226_0929.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 3.2.14 on 2024-12-26 09:29

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('fyle', '0036_expense_masked_corporate_card_number'),
]

operations = [
migrations.AddField(
model_name='expensegroupsettings',
name='created_by',
field=models.CharField(blank=True, help_text='Email of the user who created this record', max_length=255, null=True),
),
migrations.AddField(
model_name='expensegroupsettings',
name='updated_by',
field=models.CharField(blank=True, help_text='Email of the user who last updated this record', max_length=255, null=True),
),
]
8 changes: 5 additions & 3 deletions apps/fyle/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from django.db.models import Count, Q, JSONField

from fyle_accounting_mappings.models import ExpenseAttribute
from fyle_accounting_mappings.mixins import AutoAddCreateUpdateInfoMixin

from apps.workspaces.models import Configuration, Workspace

Expand Down Expand Up @@ -217,7 +218,7 @@ def get_default_split_expense_grouping():
return 'MULTIPLE_LINE_ITEM'


class ExpenseGroupSettings(models.Model):
class ExpenseGroupSettings(AutoAddCreateUpdateInfoMixin, models.Model):
"""
ExpenseGroupCustomizationSettings
"""
Expand Down Expand Up @@ -257,7 +258,7 @@ class Meta:
db_table = 'expense_group_settings'

@staticmethod
def update_expense_group_settings(expense_group_settings: Dict, workspace_id: int):
def update_expense_group_settings(expense_group_settings: Dict, workspace_id: int, user):
settings = ExpenseGroupSettings.objects.get(workspace_id=workspace_id)
current_reimbursable_settings = list(settings.reimbursable_expense_group_fields)
current_ccc_settings = list(settings.corporate_credit_card_expense_group_fields)
Expand Down Expand Up @@ -327,7 +328,8 @@ def update_expense_group_settings(expense_group_settings: Dict, workspace_id: in
'reimbursable_export_date_type': expense_group_settings['reimbursable_export_date_type'],
'ccc_export_date_type': expense_group_settings['ccc_export_date_type'],
'split_expense_grouping': expense_group_settings['split_expense_grouping']
}
},
user=user
)


Expand Down
2 changes: 1 addition & 1 deletion apps/fyle/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def get(self, request, *args, **kwargs):

def post(self, request, *args, **kwargs):
expense_group_settings, _ = ExpenseGroupSettings.update_expense_group_settings(
request.data, self.kwargs['workspace_id'])
request.data, self.kwargs['workspace_id'], user=request.user)
return Response(
data=self.serializer_class(expense_group_settings).data,
status=status.HTTP_200_OK
Expand Down
23 changes: 23 additions & 0 deletions apps/mappings/migrations/0016_auto_20241226_0929.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 3.2.14 on 2024-12-26 09:29

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('mappings', '0015_generalmapping_is_tax_balancing_enabled'),
]

operations = [
migrations.AddField(
model_name='generalmapping',
name='created_by',
field=models.CharField(blank=True, help_text='Email of the user who created this record', max_length=255, null=True),
),
migrations.AddField(
model_name='generalmapping',
name='updated_by',
field=models.CharField(blank=True, help_text='Email of the user who last updated this record', max_length=255, null=True),
),
]
4 changes: 3 additions & 1 deletion apps/mappings/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.db import models

from apps.workspaces.models import Workspace
from fyle_accounting_mappings.mixins import AutoAddCreateUpdateInfoMixin


class SubsidiaryMapping(models.Model):
Expand All @@ -22,7 +23,7 @@ class Meta:
db_table = 'subsidiary_mappings'


class GeneralMapping(models.Model):
class GeneralMapping(AutoAddCreateUpdateInfoMixin, models.Model):
"""
General Mapping
"""
Expand Down Expand Up @@ -74,5 +75,6 @@ class GeneralMapping(models.Model):
created_at = models.DateTimeField(auto_now_add=True, help_text='Created at datetime')
updated_at = models.DateTimeField(auto_now=True, help_text='Updated at datetime')


class Meta:
db_table = 'general_mappings'
12 changes: 8 additions & 4 deletions apps/workspaces/apis/export_settings/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Meta:
'is_simplify_report_closure_enabled',
'name_in_journal_entry',
'employee_field_mapping',
'auto_map_employees'
'auto_map_employees',
]

read_only_fields = ['is_simplify_report_closure_enabled']
Expand Down Expand Up @@ -120,6 +120,8 @@ def get_workspace_id(self, instance):
return instance.id

def update(self, instance: Workspace, validated_data):
request = self.context.get('request')
user = request.user if request and hasattr(request, 'user') else None

configurations = validated_data.pop('configuration')
expense_group_settings = validated_data.pop('expense_group_settings')
Expand All @@ -134,7 +136,8 @@ def update(self, instance: Workspace, validated_data):
'employee_field_mapping': configurations['employee_field_mapping'],
'name_in_journal_entry': configurations['name_in_journal_entry'],
'auto_map_employees': configurations['auto_map_employees'],
}
},
user=user
)

exports_trigger = ExportSettingsTrigger(
Expand All @@ -156,7 +159,7 @@ def update(self, instance: Workspace, validated_data):
if not expense_group_settings['ccc_export_date_type']:
expense_group_settings['ccc_export_date_type'] = 'current_date'

ExpenseGroupSettings.update_expense_group_settings(expense_group_settings, workspace_id=workspace_id)
ExpenseGroupSettings.update_expense_group_settings(expense_group_settings, workspace_id=workspace_id, user=user)

GeneralMapping.objects.update_or_create(
workspace=instance,
Expand All @@ -169,7 +172,8 @@ def update(self, instance: Workspace, validated_data):
'accounts_payable_name': general_mappings['accounts_payable']['name'],
'default_ccc_vendor_id': general_mappings['default_ccc_vendor']['id'],
'default_ccc_vendor_name': general_mappings['default_ccc_vendor']['name']
}
},
user=user
)

if instance.onboarding_state == 'EXPORT_SETTINGS':
Expand Down
9 changes: 9 additions & 0 deletions apps/workspaces/apis/export_settings/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,12 @@ class ExportSettingsView(generics.RetrieveUpdateAPIView):

def get_object(self):
return Workspace.objects.filter(id=self.kwargs['workspace_id']).first()

def get_serializer_context(self):
"""
Override to include the request in the serializer context.
This allows serializers to access the current user.
"""
context = super().get_serializer_context()
context['request'] = self.request
return context
5 changes: 4 additions & 1 deletion apps/workspaces/apis/import_settings/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ def get_workspace_id(self, instance):
return instance.id

def update(self, instance, validated_data):
request = self.context.get('request')
user = request.user if request and hasattr(request, 'user') else None

configurations = validated_data.pop('configuration')
general_mappings = validated_data.pop('general_mappings')
Expand All @@ -123,9 +125,10 @@ def update(self, instance, validated_data):
'import_vendors_as_merchants': configurations.get('import_vendors_as_merchants'),
'import_netsuite_employees': configurations.get('import_netsuite_employees')
},
user=user
)

GeneralMapping.objects.update_or_create(workspace=instance, defaults={'default_tax_code_name': general_mappings.get('default_tax_code').get('name'), 'default_tax_code_id': general_mappings.get('default_tax_code').get('id')})
GeneralMapping.objects.update_or_create(workspace=instance, defaults={'default_tax_code_name': general_mappings.get('default_tax_code').get('name'), 'default_tax_code_id': general_mappings.get('default_tax_code').get('id')}, user=user)

trigger: ImportSettingsTrigger = ImportSettingsTrigger(configurations=configurations, mapping_settings=mapping_settings, workspace_id=instance.id)

Expand Down
9 changes: 9 additions & 0 deletions apps/workspaces/apis/import_settings/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,12 @@ class ImportSettingsView(generics.RetrieveUpdateAPIView):

def get_object(self):
return Workspace.objects.filter(id=self.kwargs['workspace_id']).first()

def get_serializer_context(self):
"""
Override to include the request in the serializer context.
This allows serializers to access the current user.
"""
context = super().get_serializer_context()
context['request'] = self.request
return context
5 changes: 4 additions & 1 deletion apps/workspaces/apis/map_employees/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ def get_workspace_id(self, instance):
return instance.id

def update(self, instance, validated_data):
request = self.context.get('request')
user = request.user if request and hasattr(request, 'user') else None

workspace_id = instance.id
configuration = validated_data.pop('configuration')
Expand All @@ -34,7 +36,8 @@ def update(self, instance, validated_data):
configuration_instance.save()

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']}
workspace_id=workspace_id, defaults={'employee_field_mapping': configuration['employee_field_mapping'], 'auto_map_employees': configuration['auto_map_employees']},
user=user
)

MapEmployeesTriggers.run_configurations_triggers(configuration=configuration_instance)
Expand Down
9 changes: 9 additions & 0 deletions apps/workspaces/apis/map_employees/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,12 @@ class MapEmployeesView(generics.RetrieveUpdateAPIView):

def get_object(self):
return Workspace.objects.filter(id=self.kwargs['workspace_id']).first()

def get_serializer_context(self):
"""
Override to include the request in the serializer context.
This allows serializers to access the current user.
"""
context = super().get_serializer_context()
context['request'] = self.request
return context
23 changes: 23 additions & 0 deletions apps/workspaces/migrations/0043_auto_20241224_1102.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 3.2.14 on 2024-12-24 11:02

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('workspaces', '0042_auto_20241219_1808'),
]

operations = [
migrations.AddField(
model_name='configuration',
name='created_by',
field=models.CharField(blank=True, help_text='Email of the user who created this record', max_length=255, null=True),
),
migrations.AddField(
model_name='configuration',
name='updated_by',
field=models.CharField(blank=True, help_text='Email of the user who last updated this record', max_length=255, null=True),
),
]
4 changes: 2 additions & 2 deletions apps/workspaces/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from django_q.models import Schedule
from django.db.models import JSONField

from fyle_accounting_mappings.mixins import AutoAddCreateUpdateInfoMixin

User = get_user_model()

Expand Down Expand Up @@ -135,7 +135,7 @@ def get_default_memo_fields():
return ['employee_email', 'category', 'merchant', 'spent_on', 'report_number', 'purpose']


class Configuration(models.Model):
class Configuration(AutoAddCreateUpdateInfoMixin, models.Model):
"""
Workspace General Settings
"""
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ django-sendgrid-v5==1.2.0
enum34==1.1.10
future==0.18.2
fyle==0.37.0
fyle-accounting-mappings==1.35.0
fyle-accounting-mappings==1.36.1
fyle-integrations-platform-connector==1.39.3
fyle-rest-auth==1.7.2
gunicorn==20.1.0
Expand Down

0 comments on commit 8e31fea

Please sign in to comment.