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

Feat: Add created_by, updated_by to conf tables #710

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions apps/fyle/migrations/0041_auto_20241226_1155.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 11:55

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('fyle', '0040_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),
),
]
6 changes: 4 additions & 2 deletions apps/fyle/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from django.db import models
from django.db.models import Count, JSONField
from fyle_accounting_mappings.models import ExpenseAttribute
from fyle_accounting_mappings.mixins import AutoAddCreateUpdateInfoMixin

from apps.workspaces.models import Workspace, WorkspaceGeneralSettings
from apps.workspaces.utils import round_amount
Expand Down Expand Up @@ -202,7 +203,7 @@ def get_default_expense_state():
return 'PAYMENT_PROCESSING'


class ExpenseGroupSettings(models.Model):
class ExpenseGroupSettings(AutoAddCreateUpdateInfoMixin, models.Model):
"""
ExpenseGroupCustomizationSettings
"""
Expand All @@ -226,7 +227,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 @@ -302,6 +303,7 @@ def update_expense_group_settings(expense_group_settings: Dict, workspace_id: in
'import_card_credits': import_card_credits,
'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 @@ -83,7 +83,7 @@ def get(self, request, *args, **kwargs):
return Response(data=self.serializer_class(expense_group_settings).data, status=status.HTTP_200_OK)

def post(self, request, *args, **kwargs):
expense_group_settings, _ = ExpenseGroupSettings.update_expense_group_settings(request.data, self.kwargs['workspace_id'])
expense_group_settings, _ = ExpenseGroupSettings.update_expense_group_settings(request.data, self.kwargs['workspace_id'], 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/0011_auto_20241226_1155.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 11:55

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('mappings', '0010_auto_20220323_0914'),
]

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),
),
]
3 changes: 2 additions & 1 deletion apps/mappings/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
from django.db import models

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


class GeneralMapping(models.Model):
class GeneralMapping(AutoAddCreateUpdateInfoMixin, models.Model):
"""
General Mappings
"""
Expand Down
6 changes: 5 additions & 1 deletion apps/workspaces/apis/advanced_configurations/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ def get_workspace_id(self, instance):
return instance.id

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

workspace_general_settings = validated.pop('workspace_general_settings')
general_mappings = validated.pop('general_mappings')
workspace_schedules = validated.pop('workspace_schedules')
Expand All @@ -81,9 +84,10 @@ def update(self, instance, validated):
'change_accounting_period': workspace_general_settings.get('change_accounting_period'),
'memo_structure': workspace_general_settings.get('memo_structure'),
},
user=user
)

GeneralMapping.objects.update_or_create(workspace=instance, defaults={'bill_payment_account_name': general_mappings.get('bill_payment_account').get('name'), 'bill_payment_account_id': general_mappings.get('bill_payment_account').get('id')})
GeneralMapping.objects.update_or_create(workspace=instance, defaults={'bill_payment_account_name': general_mappings.get('bill_payment_account').get('name'), 'bill_payment_account_id': general_mappings.get('bill_payment_account').get('id')}, user=user)

schedule_sync(
workspace_id=instance.id,
Expand Down
9 changes: 9 additions & 0 deletions apps/workspaces/apis/advanced_configurations/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,12 @@ class AdvancedConfigurationsView(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
9 changes: 7 additions & 2 deletions apps/workspaces/apis/export_settings/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ def get_workspace_id(self, instance):
return instance.id

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

workspace_general_settings = validated.pop('workspace_general_settings')
expense_group_settings = validated.pop('expense_group_settings')
general_mappings = validated.pop('general_mappings')
Expand Down Expand Up @@ -121,14 +124,15 @@ def update(self, instance, validated):
'map_fyle_cards_qbo_account': enable_cards_mapping,
'name_in_journal_entry': workspace_general_settings.get('name_in_journal_entry'),
},
user=user
)

export_trigger = ExportSettingsTrigger(workspace_general_settings, instance.id)

export_trigger.post_save_workspace_general_settings()

if enable_cards_mapping:
MappingSetting.objects.update_or_create(destination_field='CREDIT_CARD_ACCOUNT', workspace_id=instance.id, defaults={'source_field': 'CORPORATE_CARD', 'import_to_fyle': False, 'is_custom': False})
MappingSetting.objects.update_or_create(destination_field='CREDIT_CARD_ACCOUNT', workspace_id=instance.id, defaults={'source_field': 'CORPORATE_CARD', 'import_to_fyle': False, 'is_custom': False}, user=user)

if not expense_group_settings['reimbursable_expense_group_fields']:
expense_group_settings['reimbursable_expense_group_fields'] = ['employee_email', 'report_id', 'fund_source']
Expand All @@ -151,7 +155,7 @@ def update(self, instance, validated):
):
expense_group_settings['import_card_credits'] = True

ExpenseGroupSettings.update_expense_group_settings(expense_group_settings, instance.id)
ExpenseGroupSettings.update_expense_group_settings(expense_group_settings, instance.id, user)

GeneralMapping.objects.update_or_create(
workspace=instance,
Expand All @@ -169,6 +173,7 @@ def update(self, instance, validated):
'default_ccc_vendor_name': general_mappings.get('default_ccc_vendor').get('name'),
'default_ccc_vendor_id': general_mappings.get('default_ccc_vendor').get('id'),
},
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 @@ -9,3 +9,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
7 changes: 6 additions & 1 deletion apps/workspaces/apis/import_settings/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ def get_workspace_id(self, instance):
return instance.id

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

workspace_general_settings = validated.pop('workspace_general_settings')
general_mappings = validated.pop('general_mappings')
mapping_settings = validated.pop('mapping_settings')
Expand All @@ -104,9 +107,10 @@ def update(self, instance, validated):
'import_vendors_as_merchants': workspace_general_settings.get('import_vendors_as_merchants'),
'import_code_fields': workspace_general_settings.get('import_code_fields'),
},
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(workspace_general_settings=workspace_general_settings, mapping_settings=mapping_settings, workspace_id=instance.id)

Expand All @@ -129,6 +133,7 @@ def update(self, instance, validated):
'is_custom': setting['is_custom'] if 'is_custom' in setting else False,
'source_placeholder': setting['source_placeholder'] if 'source_placeholder' in setting else None,
},
user=user
)

trigger.post_save_mapping_settings(workspace_general_settings_instance)
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 @@ -11,6 +11,15 @@ 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


class ImportCodeFieldView(generics.GenericAPIView):
"""
Expand Down
6 changes: 5 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,9 @@ 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
workspace_general_settings = validated_data.pop('workspace_general_settings')

Expand All @@ -33,7 +36,8 @@ def update(self, instance, validated_data):
defaults={
'employee_field_mapping': workspace_general_settings['employee_field_mapping'],
'auto_map_employees': workspace_general_settings['auto_map_employees']
}
},
user=user
)

MapEmployeesTriggers.run_workspace_general_settings_triggers(workspace_general_settings_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/0050_auto_20241226_1155.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 11:55

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('workspaces', '0049_auto_20241223_1039'),
]

operations = [
migrations.AddField(
model_name='workspacegeneralsettings',
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='workspacegeneralsettings',
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/workspaces/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from django.db.models import JSONField
from django_q.models import Schedule

from fyle_accounting_mappings.mixins import AutoAddCreateUpdateInfoMixin

User = get_user_model()


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


class WorkspaceGeneralSettings(models.Model):
class WorkspaceGeneralSettings(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.2
fyle-accounting-mappings==1.35.0
fyle-accounting-mappings==1.36.2
fyle-integrations-platform-connector==1.39.3
fyle-rest-auth==1.7.4
flake8==4.0.1
Expand Down
Loading
Loading