-
Notifications
You must be signed in to change notification settings - Fork 3
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
Setting Employee Mapping error, resolving and export exception decorator #445
Changes from 43 commits
5c31b3f
798e2bc
fdd7a9e
609594d
d15a8f5
ec4f4d1
c64ff14
67ddf59
b9e5d3b
3bce538
ac37706
4d291b3
173c5bb
932cf3f
8f6cbb7
5972c5c
cfb791b
02095ab
e9a0b17
8016d3d
aa56ca4
62697bb
35be2fd
3357a2e
3558f30
84ed5c9
16225f1
18ecafe
adf33da
2faa99f
fe4f88b
e7b1d40
48a39fd
2eb6d34
a21e0c1
6c2c439
7c41782
a06936f
0a4aee3
469efda
31696d2
69a0ce7
dc81ada
24fabfa
cc17962
1c66ec1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
import logging | ||
import json | ||
import traceback | ||
|
||
from apps.fyle.models import ExpenseGroup | ||
from apps.tasks.models import TaskLog | ||
from apps.workspaces.models import LastExportDetail, NetSuiteCredentials | ||
|
||
from netsuitesdk.internal.exceptions import NetSuiteRequestError | ||
from netsuitesdk import NetSuiteRateLimitError, NetSuiteLoginError | ||
from fyle_netsuite_api.exceptions import BulkError | ||
|
||
from django.db.models import Q | ||
|
||
logger = logging.getLogger(__name__) | ||
logger.level = logging.INFO | ||
|
||
netsuite_error_message = 'NetSuite System Error' | ||
|
||
def __handle_netsuite_connection_error(expense_group: ExpenseGroup, task_log: TaskLog) -> None: | ||
logger.info( | ||
'NetSuite Credentials not found for workspace_id %s / expense group %s', | ||
expense_group.id, | ||
expense_group.workspace_id | ||
) | ||
detail = { | ||
'expense_group_id': expense_group.id, | ||
'message': 'NetSuite Account not connected' | ||
} | ||
task_log.status = 'FAILED' | ||
task_log.detail = detail | ||
|
||
task_log.save() | ||
|
||
|
||
def __log_error(task_log: TaskLog) -> None: | ||
logger.exception('Something unexpected happened workspace_id: %s %s', task_log.workspace_id, task_log.detail) | ||
|
||
|
||
def update_last_export_details(workspace_id): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should be present in actions.py |
||
last_export_detail = LastExportDetail.objects.get(workspace_id=workspace_id) | ||
|
||
failed_exports = TaskLog.objects.filter( | ||
~Q(type__in=['CREATING_VENDOR_PAYMENT','FETCHING_EXPENSES']), workspace_id=workspace_id, status__in=['FAILED', 'FATAL'] | ||
).count() | ||
|
||
successful_exports = TaskLog.objects.filter( | ||
~Q(type__in=['CREATING_VENDOR_PAYMENT', 'FETCHING_EXPENSES']), | ||
workspace_id=workspace_id, | ||
status='COMPLETE', | ||
updated_at__gt=last_export_detail.last_exported_at | ||
).count() | ||
|
||
last_export_detail.failed_expense_groups_count = failed_exports | ||
last_export_detail.successful_expense_groups_count = successful_exports | ||
last_export_detail.total_expense_groups_count = failed_exports + successful_exports | ||
last_export_detail.save() | ||
|
||
return last_export_detail | ||
|
||
|
||
def handle_netsuite_exceptions(payment=False): | ||
def decorator(func): | ||
def wrapper(*args): | ||
if payment: | ||
entity_object = args[0] | ||
workspace_id = args[1] | ||
object_type = args[2] | ||
task_log, _ = TaskLog.objects.update_or_create( | ||
workspace_id=workspace_id, | ||
task_id='PAYMENT_{}'.format(entity_object['unique_id']), | ||
defaults={ | ||
'status': 'IN_PROGRESS', | ||
'type': 'CREATING_VENDOR_PAYMENT' | ||
} | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. indent is off |
||
else: | ||
expense_group = args[0] | ||
task_log_id = args[1] | ||
task_log = TaskLog.objects.get(id=task_log_id) | ||
last_export = args[2] | ||
|
||
try: | ||
func(*args) | ||
|
||
except NetSuiteCredentials.DoesNotExist: | ||
if payment: | ||
logger.info( | ||
'NetSuite Credentials not found for workspace_id %s', | ||
workspace_id | ||
) | ||
detail = { | ||
'message': 'NetSuite Account not connected' | ||
} | ||
task_log.status = 'FAILED' | ||
task_log.detail = detail | ||
|
||
task_log.save() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why special treatment for payment? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in the payment function there is no expense_group There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also the Error Model needs expense_group There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can assign expense_group = None inside 55 line block and then in __handle_netsuite_connection_error(), check for expense_group existence and update or create things |
||
else: | ||
__handle_netsuite_connection_error(expense_group, task_log) | ||
|
||
except (NetSuiteRequestError, NetSuiteLoginError) as exception: | ||
all_details = [] | ||
logger.info({'error': exception}) | ||
detail = json.dumps(exception.__dict__) | ||
detail = json.loads(detail) | ||
task_log.status = 'FAILED' | ||
|
||
all_details.append({ | ||
'value': netsuite_error_message, | ||
'type': detail['code'], | ||
'message': detail['message'] | ||
}) | ||
if not payment: | ||
all_details[0]['expense_group_id'] = expense_group.id | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this info isn't needed, can remove it off |
||
task_log.detail = all_details | ||
|
||
task_log.save() | ||
|
||
except BulkError as exception: | ||
logger.info(exception.response) | ||
detail = exception.response | ||
task_log.status = 'FAILED' | ||
task_log.detail = detail | ||
|
||
task_log.save() | ||
|
||
except NetSuiteRateLimitError: | ||
logger.info('Rate limit error, workspace_id - %s', workspace_id if payment else expense_group.workspace_id) | ||
task_log.status = 'FAILED' | ||
task_log.detail = { | ||
'error': 'Rate limit error' | ||
} | ||
|
||
task_log.save() | ||
|
||
except Exception: | ||
error = traceback.format_exc() | ||
task_log.detail = { | ||
'error': error | ||
} | ||
task_log.status = 'FATAL' | ||
task_log.save() | ||
__log_error(task_log) | ||
|
||
if not payment and last_export is True: | ||
update_last_export_details(expense_group.workspace_id) | ||
|
||
return wrapper | ||
return decorator |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is not needed, can treat payment and exports same
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure