Skip to content

Commit

Permalink
Removed dead code, add few test cases, added script for adding new sc…
Browse files Browse the repository at this point in the history
…hedule for different workspaces
  • Loading branch information
Hrishabh17 committed Mar 7, 2024
1 parent b7c3ac3 commit af2b90f
Show file tree
Hide file tree
Showing 18 changed files with 8,048 additions and 620 deletions.
10 changes: 6 additions & 4 deletions apps/mappings/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
WrongParamsError,
RetryException
)
from xerosdk.exceptions import InvalidGrant
from xerosdk.exceptions import InvalidTokenError as XeroInvalidTokenError
from xerosdk.exceptions import UnsuccessfulAuthentication
from xerosdk.exceptions import WrongParamsError as XeroWrongParamsError
from xerosdk.exceptions import (
InvalidGrant,
InvalidTokenError as XeroInvalidTokenError,
UnsuccessfulAuthentication,
WrongParamsError as XeroWrongParamsError
)

from apps.workspaces.models import XeroCredentials
from fyle_integrations_imports.models import ImportLog
Expand Down
40 changes: 0 additions & 40 deletions apps/mappings/helpers.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,7 @@
from datetime import datetime

from django_q.models import Schedule
from fyle_accounting_mappings.models import MappingSetting

from apps.fyle.enums import FyleAttributeEnum
from apps.workspaces.models import WorkspaceGeneralSettings


def schedule_or_delete_fyle_import_tasks(configuration: WorkspaceGeneralSettings):
"""
:param configuration: WorkspaceGeneralSettings Instance
:return: None
"""
project_mapping = MappingSetting.objects.filter(
source_field=FyleAttributeEnum.PROJECT, workspace_id=configuration.workspace_id
).first()
if (
configuration.import_categories
or (project_mapping and project_mapping.import_to_fyle)
or configuration.import_suppliers_as_merchants
):
start_datetime = datetime.now()
Schedule.objects.update_or_create(
func="apps.mappings.tasks.auto_import_and_map_fyle_fields",
cluster='import',
args="{}".format(configuration.workspace_id),
defaults={
"schedule_type": Schedule.MINUTES,
"minutes": 24 * 60,
"next_run": start_datetime,
},
)
elif (
not configuration.import_categories
and not (project_mapping and project_mapping.import_to_fyle)
and not configuration.import_suppliers_as_merchants
):
Schedule.objects.filter(
func="apps.mappings.tasks.auto_import_and_map_fyle_fields",
args="{}".format(configuration.workspace_id),
).delete()


def is_auto_sync_allowed(workspace_general_settings: WorkspaceGeneralSettings, mapping_setting: MappingSetting = None):
"""
Get the auto sync permission
Expand Down
72 changes: 1 addition & 71 deletions apps/mappings/queue.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime, timedelta
from datetime import datetime

from django_q.models import Schedule
from fyle_accounting_mappings.models import MappingSetting
Expand Down Expand Up @@ -32,76 +32,6 @@ def schedule_auto_map_employees(employee_mapping_preference: str, workspace_id:
schedule.delete()


def schedule_cost_centers_creation(import_to_fyle, workspace_id: int):
if import_to_fyle:
schedule, _ = Schedule.objects.update_or_create(
func="apps.mappings.tasks.auto_create_cost_center_mappings",
cluster='import',
args="{}".format(workspace_id),
defaults={
"schedule_type": Schedule.MINUTES,
"minutes": 24 * 60,
"next_run": datetime.now(),
},
)
else:
schedule: Schedule = Schedule.objects.filter(
func="apps.mappings.tasks.auto_create_cost_center_mappings",
args="{}".format(workspace_id),
).first()

if schedule:
schedule.delete()


def schedule_tax_groups_creation(import_tax_codes, workspace_id):
if import_tax_codes:
schedule, _ = Schedule.objects.update_or_create(
func="apps.mappings.tasks.auto_create_tax_codes_mappings",
cluster='import',
args="{}".format(workspace_id),
defaults={
"schedule_type": Schedule.MINUTES,
"minutes": 24 * 60,
"next_run": datetime.now(),
},
)
else:
schedule: Schedule = Schedule.objects.filter(
func="apps.mappings.tasks.auto_create_tax_codes_mappings",
args="{}".format(workspace_id),
).first()

if schedule:
schedule.delete()


def schedule_fyle_attributes_creation(workspace_id: int):
mapping_settings = MappingSetting.objects.filter(
is_custom=True, import_to_fyle=True, workspace_id=workspace_id
).all()

if mapping_settings:
schedule, _ = Schedule.objects.get_or_create(
func="apps.mappings.tasks.async_auto_create_custom_field_mappings",
cluster='import',
args="{0}".format(workspace_id),
defaults={
"schedule_type": Schedule.MINUTES,
"minutes": 24 * 60,
"next_run": datetime.now() + timedelta(hours=24),
},
)
else:
schedule: Schedule = Schedule.objects.filter(
func="apps.mappings.tasks.async_auto_create_custom_field_mappings",
args=workspace_id,
).first()

if schedule:
schedule.delete()


def construct_tasks_and_chain_import_fields_to_fyle(workspace_id: int):
"""
Construct tasks and chain import fields to fyle
Expand Down
11 changes: 8 additions & 3 deletions apps/workspaces/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@

from apps.fyle.enums import FyleAttributeEnum
from apps.fyle.models import ExpenseGroupSettings
from apps.mappings.queue import schedule_auto_map_employees, schedule_tax_groups_creation
from apps.mappings.queue import schedule_auto_map_employees
from apps.workspaces.models import Workspace, WorkspaceGeneralSettings
from apps.xero.queue import schedule_payment_creation, schedule_reimbursements_sync, schedule_xero_objects_status_sync
from fyle_xero_api.utils import assert_valid
from apps.mappings.schedules import new_schedule_or_delete_fyle_import_tasks


def generate_token(authorization_code: str, redirect_uri: str = None) -> str:
Expand Down Expand Up @@ -221,8 +222,12 @@ def create_or_update_general_settings(general_settings_payload: Dict, workspace_
general_settings_payload["auto_map_employees"], workspace_id
)

schedule_tax_groups_creation(
import_tax_codes=general_settings.import_tax_codes, workspace_id=workspace_id
new_schedule_or_delete_fyle_import_tasks(
workspace_general_settings_instance=workspace_general_settings,
mapping_settings=MappingSetting.objects.filter(
workspace_id=workspace_id,
source_field=FyleAttributeEnum.TAX_GROUP
).first(),
)

return general_settings
Expand Down
2 changes: 1 addition & 1 deletion fyle_integrations_imports
40 changes: 40 additions & 0 deletions scripts/python/add_new_import_schedule.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from django.db import transaction
from django_q.models import Schedule


existing_import_schedules = (
Schedule.objects.filter(
func__in=[
'apps.mappings.tasks.auto_import_and_map_fyle_fields',
'apps.mappings.tasks.auto_create_tax_codes_mappings',
'apps.mappings.tasks.async_auto_create_custom_field_mappings',
'apps.mappings.tasks.auto_create_cost_center_mappings'
]
).values('args').distinct()
)

try:
with transaction.atomic():
for schedule in existing_import_schedules:
workspace_id = schedule['args']

is_new_schedule_created = Schedule.objects.filter(
func='apps.mappings.queue.construct_tasks_and_chain_import_fields_to_fyle',
args=workspace_id
).exists()

if not is_new_schedule_created:
old_schedule = Schedule.objects.filter(
args=workspace_id
).first()

Schedule.objects.create(
func='apps.mappings.queue.construct_tasks_and_chain_import_fields_to_fyle',
args=workspace_id,
schedule_type=Schedule.MINUTES,
minutes=24 * 60,
next_run=old_schedule.next_run
)

except Exception as e:
print('Error:', e)
71 changes: 71 additions & 0 deletions tests/test_fyle_integrations_imports/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import pytest

from apps.workspaces.models import Workspace, XeroCredentials, FyleCredential
from fyle_accounting_mappings.models import (
ExpenseAttribute,
DestinationAttribute,
Mapping
)
from apps.mappings.models import TenantMapping


@pytest.fixture
def create_temp_workspace(db):
workspace_id = 3
Workspace.objects.create(
id=workspace_id,
name="Fyle for Hrishabh Testing",
fyle_org_id="Testing123",
xero_short_code="xero123",
last_synced_at=None,
source_synced_at=None,
destination_synced_at=None,
xero_accounts_last_synced_at=None
)


@pytest.fixture
def add_xero_credentials(db, create_temp_workspace):
workspace_id = 3
XeroCredentials.objects.create(
workspace_id=workspace_id,
refresh_token="refresh_token",
is_expired=False,
)


@pytest.fixture()
def add_fyle_credentials(db):
workspace_id = 3

FyleCredential.objects.create(
refresh_token="refresh_token",
workspace_id=workspace_id,
cluster_domain="https://staging.fyle.tech",
)


@pytest.fixture()
def add_tenant_mapping(db, create_temp_workspace):
workspace = Workspace.objects.filter(id = 3).first()

tenant_mapping = TenantMapping(
tenant_name="sample", tenant_id="wertyui", workspace=workspace
)
tenant_mapping.save()


@pytest.fixture
def create_project_mapping(db, create_temp_workspace):
workspace_id = 3
expense_attribute = ExpenseAttribute.objects.create(attribute_type='PROJECT', display_name='project', value='Concrete', source_id='src123', workspace_id=workspace_id, active=True)
destination_attribute = DestinationAttribute.objects.create(attribute_type='CUSTOMER', display_name='customer', value='Concrete', destination_id='dest123', workspace_id=workspace_id, active=True)
Mapping.objects.create(source_type='PROJECT', destination_type='CUSTOMER', destination_id=destination_attribute.id, source_id=expense_attribute.id, workspace_id=workspace_id)


@pytest.fixture
def create_category_mapping(db, create_temp_workspace):
workspace_id = 3
expense_attribute = ExpenseAttribute.objects.create(attribute_type='CATEGORY', display_name='category', value='Concrete', source_id='src123', workspace_id=workspace_id, active=True)
destination_attribute = DestinationAttribute.objects.create(attribute_type='ACCOUNT', display_name='account', value='Concrete', destination_id='dest123', workspace_id=workspace_id, active=True)
Mapping.objects.create(source_type='CATEGORY', destination_type='ACCOUNT', destination_id=destination_attribute.id, source_id=expense_attribute.id, workspace_id=workspace_id)
Loading

0 comments on commit af2b90f

Please sign in to comment.