Skip to content

Commit

Permalink
Resource Cost_Center: refactored imports
Browse files Browse the repository at this point in the history
  • Loading branch information
Hrishabh17 committed Feb 29, 2024
1 parent 03c81af commit 097c343
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 119 deletions.
5 changes: 3 additions & 2 deletions apps/mappings/queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ def construct_tasks_and_chain_import_fields_to_fyle(workspace_id: int):

# For now adding only for PROJECT
ALLOWED_SOURCE_FIELDS = [
FyleAttributeEnum.PROJECT
FyleAttributeEnum.PROJECT,
FyleAttributeEnum.COST_CENTER,
]

if mapping_settings:
Expand All @@ -141,7 +142,7 @@ def construct_tasks_and_chain_import_fields_to_fyle(workspace_id: int):
'source_field': mapping_setting.source_field,
'destination_field': mapping_setting.destination_field,
'is_custom': mapping_setting.is_custom,
'destination_sync_methods': [SYNC_METHODS[mapping_setting.destination_field.upper()]],
'destination_sync_methods': [SYNC_METHODS.get(mapping_setting.destination_field.upper(), 'tracking_categories')],
'is_auto_sync_enabled': is_auto_sync_allowed(workspace_general_settings, mapping_setting)
}
)
Expand Down
16 changes: 8 additions & 8 deletions apps/mappings/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
from fyle_accounting_mappings.models import Mapping, MappingSetting

from apps.fyle.enums import FyleAttributeEnum
# from apps.mappings.helpers import schedule_or_delete_fyle_import_tasks
from apps.mappings.models import TenantMapping
from apps.mappings.queue import schedule_cost_centers_creation, schedule_fyle_attributes_creation
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
Expand Down Expand Up @@ -40,19 +39,20 @@ def run_post_mapping_settings_triggers(sender, instance: MappingSetting, **kwarg
workspace_general_settings = WorkspaceGeneralSettings.objects.filter(
workspace_id=instance.workspace_id
).first()
if instance.source_field == FyleAttributeEnum.PROJECT:

ALLOWED_SOURCE_FIELDS = [
FyleAttributeEnum.PROJECT,
FyleAttributeEnum.COST_CENTER,
]

if instance.source_field in ALLOWED_SOURCE_FIELDS:
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.source_field == FyleAttributeEnum.COST_CENTER:
schedule_cost_centers_creation(
instance.import_to_fyle, int(instance.workspace_id)
)

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

Expand Down
92 changes: 0 additions & 92 deletions apps/mappings/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,98 +287,6 @@ def sync_xero_attributes(xero_attribute_type: str, workspace_id: int):
xero_connection.sync_tracking_categories()


def create_fyle_cost_centers_payload(
xero_attributes: List[DestinationAttribute], existing_fyle_cost_centers: list
):
"""
Create Fyle Cost Centers Payload from xero Objects
:param workspace_id: Workspace integer id
:param xero_attributes: xero Objects
:param fyle_attribute: Fyle Attribute
:return: Fyle Cost Centers Payload
"""
existing_fyle_cost_centers = [
cost_center.lower() for cost_center in existing_fyle_cost_centers
]
fyle_cost_centers_payload = []

for xero_attribute in xero_attributes:
if xero_attribute.value.lower() not in existing_fyle_cost_centers:
fyle_cost_centers_payload.append(
{
"name": xero_attribute.value,
"is_enabled": True
if xero_attribute.active is None
else xero_attribute.active,
"description": "Cost Center - {0}, Id - {1}".format(
xero_attribute.value, xero_attribute.destination_id
),
}
)

logger.info("| Importing Cost Centers to Fyle | Content: {{Fyle Payload count: {}}}".format(len(fyle_cost_centers_payload)))
return fyle_cost_centers_payload


def post_cost_centers_in_batches(
platform: PlatformConnector, workspace_id: int, xero_attribute_type: str
):
existing_cost_center_names = ExpenseAttribute.objects.filter(
attribute_type=FyleAttributeEnum.COST_CENTER, workspace_id=workspace_id
).values_list("value", flat=True)

xero_attribute_count = DestinationAttribute.objects.filter(
attribute_type=xero_attribute_type, workspace_id=workspace_id
).count()

page_size = 200

for offset in range(0, xero_attribute_count, page_size):
limit = offset + page_size
paginated_xero_attributes = DestinationAttribute.objects.filter(
attribute_type=xero_attribute_type, workspace_id=workspace_id
).order_by("value", "id")[offset:limit]

paginated_xero_attributes = remove_duplicates(paginated_xero_attributes)

fyle_payload: List[Dict] = create_fyle_cost_centers_payload(
paginated_xero_attributes, existing_cost_center_names
)

if fyle_payload:
platform.cost_centers.post_bulk(fyle_payload)
platform.cost_centers.sync()

Mapping.bulk_create_mappings(
paginated_xero_attributes, FyleAttributeEnum.COST_CENTER, xero_attribute_type, workspace_id
)


@handle_import_exceptions(task_name="auto create cost center mappings")
def auto_create_cost_center_mappings(workspace_id: int):
"""
Create Cost Center Mappings
"""

fyle_credentials: FyleCredential = FyleCredential.objects.get(
workspace_id=workspace_id
)

platform = PlatformConnector(fyle_credentials)

mapping_setting = MappingSetting.objects.get(
source_field=FyleAttributeEnum.COST_CENTER, import_to_fyle=True, workspace_id=workspace_id
)

platform.cost_centers.sync()

sync_xero_attributes(mapping_setting.destination_field, workspace_id=workspace_id)

post_cost_centers_in_batches(
platform, workspace_id, mapping_setting.destination_field
)


def disable_renamed_projects(workspace_id, destination_field):
page_size = 200
expense_attributes_count = ExpenseAttribute.objects.filter(
Expand Down
9 changes: 4 additions & 5 deletions apps/workspaces/apis/import_settings/triggers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
from django.db.models import Q
from fyle_accounting_mappings.models import MappingSetting

from apps.mappings.helpers import schedule_or_delete_fyle_import_tasks
from apps.mappings.queue import (
schedule_cost_centers_creation,
schedule_fyle_attributes_creation,
schedule_tax_groups_creation,
)
Expand Down Expand Up @@ -47,7 +45,6 @@ def post_save_workspace_general_settings(
destination_field="CUSTOMER",
).delete()

# schedule_or_delete_fyle_import_tasks(workspace_general_settings_instance)
new_schedule_or_delete_fyle_import_tasks(
workspace_general_settings_instance=workspace_general_settings_instance,
mapping_settings=self.__mapping_settings,
Expand All @@ -67,7 +64,10 @@ def pre_save_mapping_settings(self):
cost_center_mapping_available = True

if not cost_center_mapping_available:
schedule_cost_centers_creation(False, self.__workspace_id)
new_schedule_or_delete_fyle_import_tasks(
workspace_general_settings_instance=self.__workspace_general_settings,
mapping_settings=self.__mapping_settings,
)

# Schdule for auto creating custom field mappings
schedule_fyle_attributes_creation(self.__workspace_id)
Expand Down Expand Up @@ -101,7 +101,6 @@ def post_save_mapping_settings(
workspace_id=self.__workspace_id,
).delete()

schedule_or_delete_fyle_import_tasks(workspace_general_settings_instance)
new_schedule_or_delete_fyle_import_tasks(
workspace_general_settings_instance=workspace_general_settings_instance,
mapping_settings=self.__mapping_settings,
Expand Down
21 changes: 9 additions & 12 deletions apps/xero/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,31 +52,28 @@ def refersh_xero_dimension(workspace_id):
)
chain = Chain()

ALLOWED_SOURCE_FIELDS = [
FyleAttributeEnum.PROJECT,
FyleAttributeEnum.COST_CENTER,
]

for mapping_setting in mapping_settings:
if mapping_setting.source_field == FyleAttributeEnum.PROJECT:
# run auto_import_and_map_fyle_fields
if mapping_setting.source_field in ALLOWED_SOURCE_FIELDS:
# run new_schedule_or_delete_fyle_import_tasks
chain.append(
'fyle_integrations_imports.tasks.trigger_import_via_schedule',
workspace_id,
mapping_setting.destination_field,
mapping_setting.source_field,
'apps.xero.utils.XeroConnector',
xero_credentials,
[SYNC_METHODS[mapping_setting.destination_field]],
[SYNC_METHODS.get(mapping_setting.destination_field.upper(), 'tracking_categories')],
is_auto_sync_allowed(workspace_general_settings, mapping_setting),
False,
None,
mapping_setting.is_custom
)
elif mapping_setting.source_field == FyleAttributeEnum.COST_CENTER:
# run auto_create_cost_center_mappings
chain.append(
"apps.mappings.tasks.auto_create_cost_center_mappings",
int(workspace_id),
q_options={
'cluster': 'import'
}
)

elif mapping_setting.is_custom:
# run async_auto_create_custom_field_mappings
chain.append(
Expand Down

0 comments on commit 097c343

Please sign in to comment.