From 7829ace12a7d74e180f7de7c0db8ca9f8bf0c685 Mon Sep 17 00:00:00 2001 From: Hrishabh Tiwari <74908943+Hrishabh17@users.noreply.github.com> Date: Wed, 8 May 2024 10:39:40 +0530 Subject: [PATCH] Handle change of import_settings (#357) --- .../apis/import_settings/serializers.py | 1 + .../apis/import_settings/triggers.py | 27 ++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/apps/workspaces/apis/import_settings/serializers.py b/apps/workspaces/apis/import_settings/serializers.py index 9ee8ffcb..de55c291 100644 --- a/apps/workspaces/apis/import_settings/serializers.py +++ b/apps/workspaces/apis/import_settings/serializers.py @@ -154,6 +154,7 @@ def update(self, instance, validated): trigger.post_save_workspace_general_settings( workspace_general_settings_instance ) + trigger.pre_save_mapping_settings() if workspace_general_settings["import_tax_codes"]: mapping_settings.append( diff --git a/apps/workspaces/apis/import_settings/triggers.py b/apps/workspaces/apis/import_settings/triggers.py index 30c19636..04ff5704 100644 --- a/apps/workspaces/apis/import_settings/triggers.py +++ b/apps/workspaces/apis/import_settings/triggers.py @@ -1,7 +1,7 @@ from typing import Dict, List from django.db.models import Q -from fyle_accounting_mappings.models import MappingSetting +from fyle_accounting_mappings.models import MappingSetting, ExpenseAttribute from apps.workspaces.models import WorkspaceGeneralSettings from apps.mappings.schedules import new_schedule_or_delete_fyle_import_tasks @@ -40,6 +40,31 @@ def post_save_workspace_general_settings( mapping_settings=self.__mapping_settings, ) + def __unset_auto_mapped_flag(self, current_mapping_settings: List[MappingSetting], new_mappings_settings: List[Dict]): + """ + Set the auto_mapped flag to false for the expense_attributes for the attributes + whose mapping is changed. + """ + changed_source_fields = [] + + for new_setting in new_mappings_settings: + destination_field = new_setting['destination_field'] + source_field = new_setting['source_field'] + current_setting = current_mapping_settings.filter(destination_field=destination_field).first() + if current_setting and current_setting.source_field != source_field: + changed_source_fields.append(source_field) + + ExpenseAttribute.objects.filter(workspace_id=self.__workspace_id, attribute_type__in=changed_source_fields).update(auto_mapped=False) + + def pre_save_mapping_settings(self): + """ + Post save action for mapping settings + """ + mapping_settings = self.__mapping_settings + + current_mapping_settings = MappingSetting.objects.filter(workspace_id=self.__workspace_id).all() + self.__unset_auto_mapped_flag(current_mapping_settings, mapping_settings) + def post_save_mapping_settings( self, workspace_general_settings_instance: WorkspaceGeneralSettings ):