From 96007b4c1e0925d4d03a1565f5bc739914cdc728 Mon Sep 17 00:00:00 2001 From: labhvam5 <88420539+labhvam5@users.noreply.github.com> Date: Mon, 14 Nov 2022 16:53:35 +0530 Subject: [PATCH] adding task logs filters and errors msg in details col (#189) * adding task logs filters and errors msg in details col * fixing test and adding validation --- apps/tasks/views.py | 21 +++++++++++++++------ apps/xero/tasks.py | 9 ++++++++- tests/test_xero/test_tasks.py | 6 ++++++ 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/apps/tasks/views.py b/apps/tasks/views.py index 923632bd..d8a650e3 100644 --- a/apps/tasks/views.py +++ b/apps/tasks/views.py @@ -20,15 +20,24 @@ def get_queryset(self): """ Return task logs in workspace """ - task_status = self.request.query_params.getlist('status') + task_type = self.request.query_params.get('task_type') + expense_group_ids = self.request.query_params.get('expense_group_ids') + task_status = self.request.query_params.get('status') - if len(task_status) == 1 and task_status[0] == 'ALL': - task_status = ['ENQUEUED', 'IN_PROGRESS', 'FAILED', 'COMPLETE'] + filters = { + 'workspace_id': self.kwargs['workspace_id'] + } - task_logs = TaskLog.objects.filter(~Q(type='CREATING_PAYMENT'), - workspace_id=self.kwargs['workspace_id'], status__in=task_status).order_by('-updated_at').all() + if task_type: + filters['type__in'] = task_type.split(',') - return task_logs + if expense_group_ids: + filters['expense_group_id__in'] = expense_group_ids.split(',') + + if task_status: + filters['status__in'] = task_status.split(',') + + return TaskLog.objects.filter(**filters).order_by('-updated_at').all() class TasksByIdView(generics.RetrieveAPIView): diff --git a/apps/xero/tasks.py b/apps/xero/tasks.py index 8b695587..55a3ae2c 100644 --- a/apps/xero/tasks.py +++ b/apps/xero/tasks.py @@ -95,13 +95,20 @@ def handle_xero_error(exception, expense_group: ExpenseGroup, task_log: TaskLog) 'message': detail['message']['Message'], 'error': detail['message'] }) + + error_detail = 'Something unexcepted happen' + if 'Elements' in detail['message'] and len(detail['message']['Elements']) > 0 and\ + 'ValidationErrors' in detail['message']['Elements'][0] and len(detail['message']['Elements'][0]['ValidationErrors']) > 0 and\ + 'Message' in detail['message']['Elements'][0]['ValidationErrors'][0]: + error_detail = detail['message']['Elements'][0]['ValidationErrors'][0]['Message'] + Error.objects.update_or_create( workspace_id=expense_group.workspace_id, expense_group=expense_group, defaults={ 'type': 'XERO_ERROR', 'error_title': detail['message']['Message'], - 'error_detail': detail['message'], + 'error_detail': error_detail, 'is_resolved': False } ) diff --git a/tests/test_xero/test_tasks.py b/tests/test_xero/test_tasks.py index 5cf2739d..3b10b777 100644 --- a/tests/test_xero/test_tasks.py +++ b/tests/test_xero/test_tasks.py @@ -229,6 +229,9 @@ def test_create_bill_exceptions(db): }, response='Invalid params') create_bill(expense_group.id, task_log.id, xero_connection, False) + mock_call.side_effect = WrongParamsError({'ErrorNumber': 10, 'Type': 'ValidationException', 'Message': 'A validation exception occurred', 'Elements': [{'BankAccount': {'AccountID': '562555f2-8cde-4ce9-8203-0363922537a4', 'Code': '090', 'ValidationErrors': []}, 'Type': 'SPEND', 'Reference': 'E/2022/03/T/1', 'Url': 'None/app/admin/#/view_expense/txkImp4VID2Z?org_id=orhlmPm4H0wn', 'IsReconciled': False, 'Contact': {'ContactID': '73e6b7fb-ba7e-4b0a-a08b-f971b8ebbed8', 'Addresses': [], 'Phones': [], 'ContactGroups': [], 'ContactPersons': [], 'HasValidationErrors': False, 'ValidationErrors': []}, 'DateString': '2022-03-30T00:00:00', 'Date': '/Date(1648598400000+0000)/', 'Status': 'AUTHORISED', 'LineAmountTypes': 'Exclusive', 'LineItems': [{'Description': 'ashwin.t@fyle.in, category - Food spent on 2022-03-30, report number - C/2022/03/R/1 - https://staging.fyle.tech/app/main/#/enterprise/view_expense/txkImp4VID2Z?org_id=orhlmPm4H0wn', 'UnitAmount': 92.38, 'TaxType': 'OUTPUT', 'TaxAmount': 7.62, 'LineAmount': 92.38, 'AccountCode': '425', 'Tracking': [], 'Quantity': 1.0, 'AccountID': 'c4b1c463-9913-4672-a8b8-01a3b546126f', 'ValidationErrors': []}], 'SubTotal': 92.38, 'TotalTax': 7.62, 'Total': 100.0, 'CurrencyCode': 'USD', 'ValidationErrors': [{'Message': 'Url must be a valid absolute url'}]}]}) + create_bill(expense_group.id, task_log.id, xero_connection, False) + task_log = TaskLog.objects.get(id=task_log.id) assert task_log.status == 'FAILED' @@ -376,6 +379,9 @@ def test_create_bank_transactions_exceptions(db): }, response='Invalid params') create_bank_transaction(expense_group.id, task_log.id, xero_connection, False) + mock_call.side_effect = WrongParamsError({'ErrorNumber': 10, 'Type': 'ValidationException', 'Message': 'A validation exception occurred', 'Elements': [{'BankAccount': {'AccountID': '562555f2-8cde-4ce9-8203-0363922537a4', 'Code': '090', 'ValidationErrors': []}, 'Type': 'SPEND', 'Reference': 'E/2022/03/T/1', 'Url': 'None/app/admin/#/view_expense/txkImp4VID2Z?org_id=orhlmPm4H0wn', 'IsReconciled': False, 'Contact': {'ContactID': '73e6b7fb-ba7e-4b0a-a08b-f971b8ebbed8', 'Addresses': [], 'Phones': [], 'ContactGroups': [], 'ContactPersons': [], 'HasValidationErrors': False, 'ValidationErrors': []}, 'DateString': '2022-03-30T00:00:00', 'Date': '/Date(1648598400000+0000)/', 'Status': 'AUTHORISED', 'LineAmountTypes': 'Exclusive', 'LineItems': [{'Description': 'ashwin.t@fyle.in, category - Food spent on 2022-03-30, report number - C/2022/03/R/1 - https://staging.fyle.tech/app/main/#/enterprise/view_expense/txkImp4VID2Z?org_id=orhlmPm4H0wn', 'UnitAmount': 92.38, 'TaxType': 'OUTPUT', 'TaxAmount': 7.62, 'LineAmount': 92.38, 'AccountCode': '425', 'Tracking': [], 'Quantity': 1.0, 'AccountID': 'c4b1c463-9913-4672-a8b8-01a3b546126f', 'ValidationErrors': []}], 'SubTotal': 92.38, 'TotalTax': 7.62, 'Total': 100.0, 'CurrencyCode': 'USD', 'ValidationErrors': [{'Message': 'Url must be a valid absolute url'}]}]}) + create_bank_transaction(expense_group.id, task_log.id, xero_connection, False) + task_log = TaskLog.objects.get(id=task_log.id) assert task_log.status == 'FAILED'