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

Resource Tax Group: refactored imports #315

Merged
merged 16 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
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
10 changes: 9 additions & 1 deletion apps/mappings/queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,17 @@ def construct_tasks_and_chain_import_fields_to_fyle(workspace_id: int):
FyleAttributeEnum.COST_CENTER,
]

if workspace_general_settings.import_tax_codes:
task_settings['import_tax'] = {
'destination_field': 'TAX_CODE',
'destination_sync_methods': [SYNC_METHODS['TAX_CODE']],
'is_auto_sync_enabled': is_auto_sync_allowed(workspace_general_settings, None),
Copy link
Contributor

Choose a reason for hiding this comment

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

can sent it False by default since we know this block is only for tax

'is_3d_mapping': False
}

if mapping_settings:
for mapping_setting in mapping_settings:
if mapping_setting.source_field in ALLOWED_SOURCE_FIELDS:
if mapping_setting.source_field in ALLOWED_SOURCE_FIELDS or mapping_setting.is_custom:
task_settings['mapping_settings'].append(
{
'source_field': mapping_setting.source_field,
Expand Down
2 changes: 1 addition & 1 deletion apps/mappings/schedules.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ def new_schedule_or_delete_fyle_import_tasks(
# short-hand notation, it returns True as soon as it encounters import_to_fyle as True
task_to_be_scheduled = any(mapping_setting['import_to_fyle'] for mapping_setting in mapping_settings)

# if task_to_be_scheduled is True or import_customers(as Project) is True
if (
task_to_be_scheduled
or workspace_general_settings_instance.import_customers
or workspace_general_settings_instance.import_tax_codes
):
Schedule.objects.update_or_create(
func='apps.mappings.queue.construct_tasks_and_chain_import_fields_to_fyle',
Expand Down
101 changes: 78 additions & 23 deletions apps/mappings/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,29 @@
Mappings Signal
"""


import logging
from datetime import datetime, timedelta, timezone
from django.db.models.signals import post_save, pre_save
from django.dispatch import receiver
from django_q.tasks import async_task
from fyle_accounting_mappings.models import Mapping, MappingSetting

from apps.fyle.enums import FyleAttributeEnum
from apps.mappings.models import TenantMapping
from apps.mappings.queue import schedule_fyle_attributes_creation
from apps.mappings.tasks import upload_attributes_to_fyle
from apps.tasks.models import Error
from apps.workspaces.models import WorkspaceGeneralSettings
from apps.mappings.schedules import new_schedule_or_delete_fyle_import_tasks
from apps.workspaces.models import XeroCredentials, FyleCredential
from apps.mappings.constants import SYNC_METHODS
from fyle_integrations_imports.models import ImportLog
from apps.xero.utils import XeroConnector
from fyle_integrations_imports.modules.expense_custom_fields import ExpenseCustomField
from fyle_integrations_platform_connector import PlatformConnector
from fyle.platform.exceptions import WrongParamsError
from rest_framework.exceptions import ValidationError

logger = logging.getLogger(__name__)
logger.level = logging.INFO


@receiver(post_save, sender=Mapping)
Expand Down Expand Up @@ -45,17 +55,14 @@ def run_post_mapping_settings_triggers(sender, instance: MappingSetting, **kwarg
FyleAttributeEnum.COST_CENTER,
]

if instance.source_field in ALLOWED_SOURCE_FIELDS:
if instance.source_field in ALLOWED_SOURCE_FIELDS or instance.is_custom:
new_schedule_or_delete_fyle_import_tasks(
workspace_general_settings_instance=workspace_general_settings,
mapping_settings=MappingSetting.objects.filter(
workspace_id=instance.workspace_id
).values()
)

if instance.is_custom:
schedule_fyle_attributes_creation(int(instance.workspace_id))


@receiver(pre_save, sender=MappingSetting)
def run_pre_mapping_settings_triggers(sender, instance: MappingSetting, **kwargs):
Expand All @@ -76,22 +83,70 @@ def run_pre_mapping_settings_triggers(sender, instance: MappingSetting, **kwargs
instance.source_field = instance.source_field.upper().replace(" ", "_")

if instance.source_field not in default_attributes:
upload_attributes_to_fyle(
workspace_id=int(instance.workspace_id),
xero_attribute_type=instance.destination_field,
fyle_attribute_type=instance.source_field,
source_placeholder=instance.source_placeholder,
)

async_task(
"apps.mappings.tasks.auto_create_expense_fields_mappings",
int(instance.workspace_id),
instance.destination_field,
instance.source_field,
q_options={
'cluster': 'import'
}
)
try:
workspace_id = int(instance.workspace_id)
# Checking is import_log exists or not if not create one
import_log, is_created = ImportLog.objects.get_or_create(
workspace_id=workspace_id,
attribute_type=instance.source_field,
defaults={
'status': 'IN_PROGRESS'
}
)

last_successful_run_at = None
if import_log and not is_created:
last_successful_run_at = import_log.last_successful_run_at or None
time_difference = datetime.now() - timedelta(minutes=30)
offset_aware_time_difference = time_difference.replace(tzinfo=timezone.utc)

if (
last_successful_run_at and offset_aware_time_difference
and (offset_aware_time_difference < last_successful_run_at)
):
import_log.last_successful_run_at = offset_aware_time_difference
last_successful_run_at = offset_aware_time_difference
import_log.save()

xero_credentials = XeroCredentials.get_active_xero_credentials(workspace_id=workspace_id)
xero_connection = XeroConnector(credentials_object=xero_credentials, workspace_id=workspace_id)

# Creating the expense_custom_field object with the correct last_successful_run_at value
expense_custom_field = ExpenseCustomField(
workspace_id=workspace_id,
source_field=instance.source_field,
destination_field=instance.destination_field,
sync_after=last_successful_run_at,
sdk_connection=xero_connection,
destination_sync_methods=[SYNC_METHODS.get(instance.destination_field.upper(), 'tracking_categories')]
)

fyle_credentials = FyleCredential.objects.get(workspace_id=workspace_id)
platform = PlatformConnector(fyle_credentials=fyle_credentials)

import_log.status = 'IN_PROGRESS'
import_log.save()

expense_custom_field.construct_payload_and_import_to_fyle(platform=platform, import_log=import_log)
expense_custom_field.sync_expense_attributes(platform=platform)

except WrongParamsError as error:
logger.error(
'Error while creating %s workspace_id - %s in Fyle %s %s',
instance.source_field, instance.workspace_id, error.message, {'error': error.response}
)
if error.response and 'message' in error.response:
raise ValidationError({
'message': error.response['message'],
'field_name': instance.source_field
})

# setting the import_log.last_successful_run_at to -30mins for the post_save_trigger
import_log = ImportLog.objects.filter(workspace_id=workspace_id, attribute_type=instance.source_field).first()
if import_log.last_successful_run_at:
last_successful_run_at = import_log.last_successful_run_at - timedelta(minutes=30)
import_log.last_successful_run_at = last_successful_run_at
import_log.save()


@receiver(post_save, sender=TenantMapping)
Expand Down
Loading
Loading