Skip to content

Commit

Permalink
Platform connector | Nonetype Object not subscriptable | Nonetype obj… (
Browse files Browse the repository at this point in the history
#403)

* Platform connector | Nonetype Object not subscriptable | Nonetype object has no attribute srftime errors resolved

* Test cases improved

* Test cases improved
  • Loading branch information
ruuushhh authored Apr 4, 2023
1 parent a274f4e commit 0c953a9
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 10 deletions.
33 changes: 24 additions & 9 deletions apps/mappings/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from qbosdk.exceptions import WrongParamsError as QBOWrongParamsError, InvalidTokenError

from fyle.platform.exceptions import WrongParamsError
from fyle.platform.exceptions import WrongParamsError, InvalidTokenError as FyleInvalidTokenError
from fyle_integrations_platform_connector import PlatformConnector
from fyle_accounting_mappings.models import MappingSetting, Mapping, DestinationAttribute, ExpenseAttribute,\
EmployeeMapping
Expand Down Expand Up @@ -214,6 +214,9 @@ def auto_create_tax_codes_mappings(workspace_id: int):

except QBOCredential.DoesNotExist:
logger.info('QBO credentials not found workspace_id - %s', workspace_id)

except FyleInvalidTokenError:
logger.info('Invalid Token for fyle')

except WrongParamsError as exception:
logger.error(
Expand Down Expand Up @@ -257,6 +260,9 @@ def auto_create_project_mappings(workspace_id: int):
except QBOCredential.DoesNotExist:
logger.info('QBO credentials not found workspace_id - %s', workspace_id)

except FyleInvalidTokenError:
logger.info('Invalid Token for fyle')

except WrongParamsError as exception:
logger.error(
'Error while creating projects workspace_id - %s in Fyle %s %s',
Expand Down Expand Up @@ -389,12 +395,12 @@ def auto_create_category_mappings(workspace_id):

except QBOCredential.DoesNotExist:
logger.info('QBO credentials not found workspace_id - %s', workspace_id)

except FyleInvalidTokenError:
logger.info('Invalid Token for fyle')

except WrongParamsError as exception:
logger.error(
'Error while creating categories workspace_id - %s in Fyle %s %s',
workspace_id, exception.message, {'error': exception.response}
)
logger.error( 'Error while creating categories workspace_id - %s in Fyle %s %s', workspace_id, exception.message, {'error': exception.response} )

except (QBOWrongParamsError, InvalidTokenError):
logger.info('QBO token expired workspace_id - %s', workspace_id)
Expand Down Expand Up @@ -445,9 +451,9 @@ def check_exact_matches(employee_mapping_preference: str, source_attribute: Expe
destination = {}
if employee_mapping_preference == 'EMAIL':
source_value = source_attribute.value
elif employee_mapping_preference == 'NAME':
elif source_attribute.detail and employee_mapping_preference == 'NAME':
source_value = source_attribute.detail['full_name']
elif employee_mapping_preference == 'EMPLOYEE_CODE':
elif source_attribute.detail and employee_mapping_preference == 'EMPLOYEE_CODE':
source_value = source_attribute.detail['employee_code']

# Handling employee_code or full_name null case
Expand Down Expand Up @@ -607,10 +613,10 @@ def async_auto_map_employees(workspace_id: int):
employee_mapping_preference = general_settings.auto_map_employees
destination_type = general_settings.employee_field_mapping

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

try:
fyle_credentials = FyleCredential.objects.get(workspace_id=workspace_id)
platform = PlatformConnector(fyle_credentials)
qbo_credentials = QBOCredential.get_active_qbo_credentials(workspace_id)
qbo_connection = QBOConnector(credentials_object=qbo_credentials, workspace_id=workspace_id)

Expand All @@ -631,6 +637,9 @@ def async_auto_map_employees(workspace_id: int):
logger.info(
'QBO Credentials not found for workspace_id %s', workspace_id
)

except FyleInvalidTokenError:
logger.info('Invalid Token for fyle')

except (QBOWrongParamsError, InvalidTokenError):
logger.info('QBO token expired workspace_id - %s', workspace_id)
Expand Down Expand Up @@ -876,6 +885,9 @@ def auto_create_cost_center_mappings(workspace_id):
except QBOCredential.DoesNotExist:
logger.info('QBO credentials not found workspace_id - %s', workspace_id)

except FyleInvalidTokenError:
logger.info('Invalid Token for fyle')

except WrongParamsError as exception:
logger.error(
'Error while creating cost centers workspace_id - %s in Fyle %s %s',
Expand Down Expand Up @@ -1128,6 +1140,9 @@ def auto_create_vendors_as_merchants(workspace_id):

except QBOCredential.DoesNotExist:
logger.info('QBO credentials not found workspace_id - %s', workspace_id)

except FyleInvalidTokenError:
logger.info('Invalid Token for fyle')

except WrongParamsError as exception:
logger.error(
Expand Down
8 changes: 7 additions & 1 deletion apps/workspaces/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,20 +226,26 @@ def run_email_notification(workspace_id):
for admin_email in ws_schedule.emails_selected:
attribute = ExpenseAttribute.objects.filter(workspace_id=workspace_id, value=admin_email).first()

admin_name = 'Admin'
if attribute:
admin_name = attribute.detail['full_name']
else:
for data in ws_schedule.additional_email_options:
if data['email'] == admin_email:
admin_name = data['name']

if workspace.last_synced_at and workspace.ccc_last_synced_at:
export_time = max(workspace.last_synced_at, workspace.ccc_last_synced_at)
else:
export_time = workspace.last_synced_at or workspace.ccc_last_synced_at

if task_logs and (ws_schedule.error_count is None or len(task_logs) > ws_schedule.error_count):
context = {
'name': admin_name,
'errors': len(task_logs),
'fyle_company': workspace.name,
'qbo_company': qbo.company_name,
'export_time': workspace.last_synced_at.strftime("%d %b %Y | %H:%M"),
'export_time': export_time.strftime("%d %b %Y | %H:%M"),
'year': date.today().year,
'app_url': "{0}/workspaces/main/dashboard".format(settings.FYLE_APP_URL),
'task_logs': mark_safe(expense_html),
Expand Down
17 changes: 17 additions & 0 deletions tests/test_mappings/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from unittest.mock import Mock
from django_q.models import Schedule
from qbosdk.exceptions import WrongParamsError
from fyle.platform.exceptions import InvalidTokenError as FylePlatformError
from fyle_accounting_mappings.models import DestinationAttribute, ExpenseAttribute, CategoryMapping, \
Mapping, MappingSetting, EmployeeMapping
from apps.mappings.tasks import *
Expand Down Expand Up @@ -47,6 +48,10 @@ def test_auto_create_tax_codes_mappings(db, mocker):
mock_call.side_effect = WrongParamsError(msg='invalid params', response='invalid params')
auto_create_tax_codes_mappings(workspace_id=workspace_id)

with mock.patch('fyle_integrations_platform_connector.apis.TaxGroups.sync') as mock_call:
mock_call.side_effect = FyleInvalidTokenError(msg='Invalid Token for fyle', response='Invalid Token for fyle')
auto_create_tax_codes_mappings(workspace_id=workspace_id)

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

Expand Down Expand Up @@ -130,6 +135,9 @@ def test_auto_create_project_mappings(db, mocker):
mock_call.side_effect = Exception
auto_create_project_mappings(workspace_id=workspace_id)

mock_call.side_effect = FyleInvalidTokenError(msg='Invalid Token for fyle', response='Invalid Token for fyle')
auto_create_project_mappings(workspace_id=workspace_id)

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

Expand Down Expand Up @@ -193,6 +201,9 @@ def test_auto_create_category_mappings(db, mocker):
mock_call.side_effect = WrongParamsError(msg='invalid params', response='invalid params')
auto_create_project_mappings(workspace_id=workspace_id)

mock_call.side_effect = FyleInvalidTokenError(msg='Invalid Token for fyle', response='Invalid Token for fyle')
auto_create_project_mappings(workspace_id=workspace_id)

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

Expand Down Expand Up @@ -361,6 +372,9 @@ def test_auto_create_cost_center_mappings(db, mocker):
mock_call.side_effect = WrongParamsError(msg='invalid params', response='invalid params')
auto_create_cost_center_mappings(workspace_id)

mock_call.side_effect = FyleInvalidTokenError(msg='Invalid Token for fyle', response='Invalid Token for fyle')
auto_create_cost_center_mappings(workspace_id)

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

Expand Down Expand Up @@ -493,6 +507,9 @@ def test_auto_create_vendors_as_merchants(db, mocker):
mock_call.side_effect = WrongParamsError(msg='invalid params', response='invalid params')
auto_create_vendors_as_merchants(workspace_id=workspace_id)

mock_call.side_effect = FyleInvalidTokenError(msg='Invalid Token for fyle', response='Invalid Token for fyle')
auto_create_vendors_as_merchants(workspace_id=workspace_id)

fyle_credentials = FyleCredential.objects.get(workspace_id=1)
fyle_credentials.delete()

Expand Down

0 comments on commit 0c953a9

Please sign in to comment.