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

Refactor imports for project and cost_center #314

Merged
merged 8 commits into from
Feb 29, 2024
5 changes: 3 additions & 2 deletions apps/mappings/queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,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 @@ -143,7 +144,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
15 changes: 8 additions & 7 deletions apps/mappings/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from apps.fyle.enums import FyleAttributeEnum
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 @@ -39,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 create_fyle_expense_custom_fields_payload(
xero_attributes: List[DestinationAttribute],
workspace_id: int,
Expand Down
7 changes: 4 additions & 3 deletions apps/workspaces/apis/import_settings/triggers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

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 @@ -67,7 +66,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 +103,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
20 changes: 10 additions & 10 deletions apps/xero/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,31 +52,31 @@ 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),
mapping_setting.is_custom,
q_options={
'cluster': 'import'
}
)

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