diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 00000000..dd752d7f --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "fyle_integrations_imports"] + path = fyle_integrations_imports + url = https://github.com/fylein/fyle_integrations_imports.git diff --git a/apps/mappings/exceptions.py b/apps/mappings/exceptions.py index dfb7e59c..01512144 100644 --- a/apps/mappings/exceptions.py +++ b/apps/mappings/exceptions.py @@ -6,6 +6,7 @@ from qbosdk.exceptions import WrongParamsError as QBOWrongParamsError from apps.workspaces.models import QBOCredential +from fyle_integrations_imports.models import ImportLog logger = logging.getLogger(__name__) logger.level = logging.INFO @@ -50,3 +51,55 @@ def new_fn(workspace_id: int, *args): return new_fn return decorator + + +def handle_import_exceptions_v2(func): + def new_fn(expense_attribute_instance, *args): + import_log: ImportLog = args[0] + workspace_id = import_log.workspace_id + attribute_type = import_log.attribute_type + error = { + 'task': 'Import {0} to Fyle and Auto Create Mappings'.format(attribute_type), + 'workspace_id': workspace_id, + 'message': None, + 'response': None + } + try: + return func(expense_attribute_instance, *args) + except WrongParamsError as exception: + error['message'] = exception.message + error['response'] = exception.response + error['alert'] = True + import_log.status = 'FAILED' + + except InvalidTokenError: + error['message'] = 'Invalid Token for fyle' + error['alert'] = False + import_log.status = 'FAILED' + + except InternalServerError: + error['message'] = 'Internal server error while importing to Fyle' + error['alert'] = True + import_log.status = 'FAILED' + + except (QBOWrongParamsError, QBOInvalidTokenError, QBOCredential.DoesNotExist) as exception: + error['message'] = 'Invalid Token or QBO credentials does not exist workspace_id - {0}'.format(workspace_id) + error['alert'] = False + import_log.status = 'FAILED' + + except Exception: + response = traceback.format_exc() + error['message'] = 'Something went wrong' + error['response'] = response + error['alert'] = False + import_log.status = 'FATAL' + + if error['alert']: + logger.error(error) + else: + logger.info(error) + + import_log.error_log = error + import_log.save() + + return new_fn diff --git a/apps/mappings/helpers.py b/apps/mappings/helpers.py index 1643a8e3..b8040ab2 100644 --- a/apps/mappings/helpers.py +++ b/apps/mappings/helpers.py @@ -24,7 +24,7 @@ def get_auto_sync_permission(mapping_setting: MappingSetting): :return: bool """ is_auto_sync_status_allowed = False - if (mapping_setting.destination_field == 'PROJECT' and mapping_setting.source_field == 'CUSTOMER') or mapping_setting.source_field == 'CATEGORY': + if (mapping_setting.destination_field == 'CUSTOMER' and mapping_setting.source_field == 'PROJECT') or mapping_setting.source_field == 'CATEGORY': is_auto_sync_status_allowed = True return is_auto_sync_status_allowed diff --git a/apps/mappings/queues.py b/apps/mappings/queues.py index eb14ff75..de5a30b0 100644 --- a/apps/mappings/queues.py +++ b/apps/mappings/queues.py @@ -7,6 +7,8 @@ from apps.mappings.models import GeneralMapping from apps.workspaces.models import WorkspaceGeneralSettings, QBOCredential from apps.mappings.helpers import get_auto_sync_permission +from fyle_integrations_imports.queues import chain_import_fields_to_fyle +from fyle_integrations_imports.dataclasses import TaskSetting SYNC_METHODS = { 'ACCOUNT': 'accounts', @@ -107,26 +109,25 @@ def construct_tasks_and_chain_import_fields_to_fyle(workspace_id): mapping_settings = MappingSetting.objects.filter(workspace_id=workspace_id, import_to_fyle=True) credentials = QBOCredential.objects.get(workspace_id=workspace_id) - task_settings = { + task_settings: TaskSetting = { 'import_tax': None, 'import_vendors_as_merchants': None, 'import_categories': None, 'mapping_settings': [], - 'credentails': credentials, + 'credentials': credentials, 'sdk_connection_string': 'apps.quickbooks_online.utils.QBOConnector', } # For now we are only adding PROJECTS support that is why we are hardcoding it - for mapping_setting in mapping_settings: - if mapping_setting.source_field in ['PROJECT']: - task_settings['mapping_settings'].append({ - 'source_field': mapping_setting.source_field, - 'destination_field': mapping_setting.destination_field, - 'destination_sync_method': SYNC_METHODS[mapping_setting.destination_field], - 'is_auto_sync_enabled': get_auto_sync_permission(mapping_setting), - 'is_custom': False, - }) - - # Make a call to the SDK - # chain_import_fields_to_fyle(workspace_id, task_settings) - pass + if mapping_settings: + for mapping_setting in mapping_settings: + if mapping_setting.source_field in ['PROJECT']: + task_settings['mapping_settings'].append({ + 'source_field': mapping_setting.source_field, + 'destination_field': mapping_setting.destination_field, + 'destination_sync_method': SYNC_METHODS[mapping_setting.destination_field], + 'is_auto_sync_enabled': get_auto_sync_permission(mapping_setting), + 'is_custom': False, + }) + + chain_import_fields_to_fyle(workspace_id, task_settings) diff --git a/apps/workspaces/apis/map_employees/triggers.py b/apps/workspaces/apis/map_employees/triggers.py index 882041d5..c658b60f 100644 --- a/apps/workspaces/apis/map_employees/triggers.py +++ b/apps/workspaces/apis/map_employees/triggers.py @@ -1,4 +1,4 @@ -from apps.mappings.queue import schedule_auto_map_employees +from apps.mappings.queues import schedule_auto_map_employees from apps.workspaces.models import WorkspaceGeneralSettings diff --git a/fyle_integrations_imports b/fyle_integrations_imports new file mode 160000 index 00000000..5be2be9f --- /dev/null +++ b/fyle_integrations_imports @@ -0,0 +1 @@ +Subproject commit 5be2be9f504f0bfbaebb47b2b37cf97bafcaf96d diff --git a/fyle_qbo_api/settings.py b/fyle_qbo_api/settings.py index 970e9477..05d7b262 100644 --- a/fyle_qbo_api/settings.py +++ b/fyle_qbo_api/settings.py @@ -44,6 +44,7 @@ 'corsheaders', 'fyle_rest_auth', 'fyle_accounting_mappings', + 'fyle_integrations_imports', 'django_q', 'django_filters', # User Created Apps diff --git a/sql/functions/delete-workspace.sql b/sql/functions/delete-workspace.sql index 175206f6..87c40fbb 100644 --- a/sql/functions/delete-workspace.sql +++ b/sql/functions/delete-workspace.sql @@ -25,6 +25,12 @@ BEGIN GET DIAGNOSTICS rcount = ROW_COUNT; RAISE NOTICE 'Deleted % errors', rcount; + DELETE + FROM import_logs il + WHERE il.workspace_id = _workspace_id; + GET DIAGNOSTICS rcount = ROW_COUNT; + RAISE NOTICE 'Deleted % import_logs', rcount; + DELETE FROM bill_lineitems bl WHERE bl.bill_id IN (