Skip to content

Commit

Permalink
feat: reset timestamp of import log on change of mapping setting (#720)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hrishabh17 authored Jan 11, 2025
1 parent 52c28a3 commit 853738d
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 0 deletions.
33 changes: 33 additions & 0 deletions apps/workspaces/apis/import_settings/triggers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datetime import datetime, timezone
from typing import Dict, List

from django_q.tasks import async_task
Expand All @@ -7,6 +8,7 @@
from apps.fyle.models import ExpenseGroupSettings
from apps.workspaces.models import WorkspaceGeneralSettings
from apps.mappings.schedules import schedule_or_delete_fyle_import_tasks as new_schedule_or_delete_fyle_import_tasks
from fyle_integrations_imports.models import ImportLog


class ImportSettingsTrigger:
Expand Down Expand Up @@ -106,6 +108,32 @@ def __unset_auto_mapped_flag(self, current_mapping_settings: List[MappingSetting

ExpenseAttribute.objects.filter(workspace_id=self.__workspace_id, attribute_type__in=changed_source_fields).update(auto_mapped=False)

def __reset_import_log_timestamp(
self,
current_mapping_settings: List[MappingSetting],
new_mappings_settings: List[Dict],
workspace_id: int,
):
"""
Reset Import logs when mapping settings are deleted or the source_field is changed.
"""
changed_source_fields = set()

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(source_field=source_field).first()
if current_setting and current_setting.destination_field != destination_field:
changed_source_fields.add(source_field)

current_source_fields = set(mapping_setting.source_field for mapping_setting in current_mapping_settings)
new_source_fields = set(mapping_setting['source_field'] for mapping_setting in new_mappings_settings)
deleted_source_fields = current_source_fields.difference(new_source_fields | {'CORPORATE_CARD', 'CATEGORY'})

reset_source_fields = changed_source_fields.union(deleted_source_fields)

ImportLog.objects.filter(workspace_id=workspace_id, attribute_type__in=reset_source_fields).update(last_successful_run_at=None, updated_at=datetime.now(timezone.utc))

def pre_save_mapping_settings(self):
"""
Post save action for mapping settings
Expand All @@ -117,6 +145,11 @@ def pre_save_mapping_settings(self):

self.__remove_old_department_source_field(current_mappings_settings=current_mapping_settings, new_mappings_settings=mapping_settings)
self.__unset_auto_mapped_flag(current_mapping_settings=current_mapping_settings, new_mappings_settings=mapping_settings)
self.__reset_import_log_timestamp(
current_mapping_settings=current_mapping_settings,
new_mappings_settings=mapping_settings,
workspace_id=self.__workspace_id
)

def post_save_mapping_settings(self, workspace_general_settings_instance: WorkspaceGeneralSettings):
"""
Expand Down
102 changes: 102 additions & 0 deletions tests/test_workspaces/test_apis/test_import_settings/test_triggers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
from datetime import datetime, timezone

from fyle_accounting_mappings.models import MappingSetting

from apps.workspaces.apis.import_settings.triggers import ImportSettingsTrigger
from apps.workspaces.models import WorkspaceGeneralSettings
from fyle_integrations_imports.models import ImportLog


def test__reset_import_log_timestamp_case_1(db):
"""
Case: When the mapping settings is deleted
"""
workspace_id = 1
general_settings = WorkspaceGeneralSettings.objects.filter(workspace_id=workspace_id).first()
import_settings_trigger = ImportSettingsTrigger(
workspace_general_settings=general_settings,
mapping_settings=[],
workspace_id=workspace_id
)

current_mapping_settings = MappingSetting.objects.create(
source_field='PROJECT',
destination_field='CUSTOMER',
workspace_id=workspace_id
)

ImportLog.objects.create(
attribute_type='PROJECT',
status='COMPLETE',
workspace_id=workspace_id,
last_successful_run_at=datetime.now(timezone.utc)
)

import_settings_trigger._ImportSettingsTrigger__reset_import_log_timestamp(
current_mapping_settings=[current_mapping_settings],
new_mappings_settings=[],
workspace_id=workspace_id
)

assert ImportLog.objects.filter(workspace_id=workspace_id, attribute_type='PROJECT').first().last_successful_run_at is None


def test__reset_import_log_timestamp_case_2(db):
"""
Case: When the mapping settings is changed
"""
workspace_id = 1
general_settings = WorkspaceGeneralSettings.objects.filter(workspace_id=workspace_id).first()
import_settings_trigger = ImportSettingsTrigger(
workspace_general_settings=general_settings,
mapping_settings=[],
workspace_id=workspace_id
)

current_mapping_settings_dict = [{
'source_field': 'PROJECT',
'destination_field': 'CUSTOMER'
},{
'source_field': 'COST_CENTER',
'destination_field': 'DEPARTMENT'
}]

new_mapping_settings = [{
'source_field': 'COST_CENTER',
'destination_field': 'CUSTOMER'
},{
'source_field': 'PROJECT',
'destination_field': 'DEPARTMENT'
}]

for mapping_setting_dict in current_mapping_settings_dict:
MappingSetting.objects.create(
source_field=mapping_setting_dict['source_field'],
destination_field=mapping_setting_dict['destination_field'],
workspace_id=workspace_id
)

current_mapping_settings = MappingSetting.objects.filter(workspace_id=workspace_id)

ImportLog.objects.create(
attribute_type='PROJECT',
status='COMPLETE',
workspace_id=workspace_id,
last_successful_run_at=datetime.now(timezone.utc)
)

ImportLog.objects.create(
attribute_type='COST_CENTER',
status='COMPLETE',
workspace_id=workspace_id,
last_successful_run_at=datetime.now(timezone.utc)
)

import_settings_trigger._ImportSettingsTrigger__reset_import_log_timestamp(
current_mapping_settings=current_mapping_settings,
new_mappings_settings=new_mapping_settings,
workspace_id=workspace_id
)

assert ImportLog.objects.filter(workspace_id=workspace_id, attribute_type='PROJECT').first().last_successful_run_at is None
assert ImportLog.objects.filter(workspace_id=workspace_id, attribute_type='COST_CENTER').first().last_successful_run_at is None

0 comments on commit 853738d

Please sign in to comment.