Skip to content

Commit

Permalink
Add support for editing expenses
Browse files Browse the repository at this point in the history
  • Loading branch information
Hrishabh17 committed Jun 26, 2024
1 parent 16ba287 commit e4eff41
Show file tree
Hide file tree
Showing 8 changed files with 379 additions and 11 deletions.
5 changes: 5 additions & 0 deletions apps/fyle/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from fyle.platform.exceptions import NoPrivilegeError, RetryException, InvalidTokenError as FyleInvalidTokenError
from rest_framework.response import Response
from rest_framework.views import status
from rest_framework.exceptions import ValidationError

from apps.workspaces.models import FyleCredential, Workspace, ExportSettings, AdvancedSetting
from apps.tasks.models import AccountingExport
Expand Down Expand Up @@ -43,6 +44,10 @@ def new_fn(*args, **kwargs):
except ExportSettings.DoesNotExist:
return Response({'message': 'Export Settings does not exist in workspace'}, status=status.HTTP_400_BAD_REQUEST)

except ValidationError as e:
logger.exception(e)
return Response({"message": e.detail}, status=status.HTTP_400_BAD_REQUEST)

except Exception as exception:
logger.exception(exception)
return Response(data={'message': 'An unhandled error has occurred, please re-try later'}, status=status.HTTP_400_BAD_REQUEST)
Expand Down
13 changes: 12 additions & 1 deletion apps/fyle/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@

from django.conf import settings
from fyle.platform import Platform
from rest_framework.exceptions import ValidationError

from apps.workspaces.models import FyleCredential
from apps.workspaces.models import Workspace, FyleCredential


def post_request(url, body, refresh_token=None):
Expand Down Expand Up @@ -142,3 +143,13 @@ def download_iif_file(file_id: str, workspace_id: int):
)['data'][0]['download_url']

return download_url


def assert_valid_request(workspace_id:int, org_id:str):
"""
Assert if the request is valid by checking
the url_workspace_id and fyle_org_id workspace
"""
workspace = Workspace.objects.get(org_id=org_id)
if workspace.id != workspace_id:
raise ValidationError('Workspace mismatch')
17 changes: 14 additions & 3 deletions apps/fyle/queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@
* User Triggered Async Tasks
* Schedule Triggered Async Tasks
"""
import logging
from django_q.tasks import async_task
from apps.fyle.tasks import (
import_credit_card_expenses,
import_reimbursable_expenses
)
from apps.workspaces.models import Workspace
from apps.tasks.models import AccountingExport
from apps.fyle.helpers import assert_valid_request

logger = logging.getLogger(__name__)
logger.level = logging.INFO


def queue_import_reimbursable_expenses(workspace_id: int, synchronous: bool = False):
Expand Down Expand Up @@ -60,14 +65,20 @@ def queue_import_credit_card_expenses(workspace_id: int, synchronous: bool = Fal
import_credit_card_expenses(workspace_id, accounting_export)


def async_handle_webhook_callback(body: dict) -> None:
def async_handle_webhook_callback(body: dict, workspace_id: int) -> None:
"""
Async'ly import and export expenses
:param body: bodys
:param body: body
:return: None
"""
if body.get('action') == 'ACCOUNTING_EXPORT_INITIATED' and body.get('data'):
org_id = body['data']['org_id']

assert_valid_request(workspace_id=workspace_id, org_id=org_id)
workspace = Workspace.objects.get(org_id=org_id)
async_task('apps.workspaces.tasks.run_import_export', workspace.id)

elif body.get('action') == 'UPDATED_AFTER_APPROVAL' and body.get('data') and body.get('resource') == 'EXPENSE':
org_id = body['data']['org_id']
logger.info("| Updating non-exported expenses through webhook | Content: {{WORKSPACE_ID: {} Payload: {}}}".format(workspace_id, body.get('data')))
assert_valid_request(workspace_id=workspace_id, org_id=org_id)
async_task('apps.fyle.tasks.update_non_exported_expenses', body['data'])
20 changes: 20 additions & 0 deletions apps/fyle/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
2. Import Credit Card Expenses from Fyle
"""
import logging
from typing import Dict
from datetime import datetime
import traceback

from django.db import transaction

from fyle_integrations_platform_connector import PlatformConnector
from fyle.platform.exceptions import RetryException, NoPrivilegeError
from fyle_integrations_platform_connector.apis.expenses import Expenses as FyleExpenses

from apps.tasks.models import AccountingExport
from apps.workspaces.models import Workspace, ExportSettings, FyleCredential
Expand Down Expand Up @@ -151,3 +153,21 @@ def import_credit_card_expenses(workspace_id, accounting_export: AccountingExpor
accounting_export.status = 'FATAL'
accounting_export.save()
logger.exception('Something unexpected happened workspace_id: %s %s', accounting_export.workspace_id, accounting_export.errors)


def update_non_exported_expenses(data: Dict) -> None:
"""
To update expenses not in COMPLETE, IN_PROGRESS state
"""
org_id = data['org_id']
expense_id = data['id']
workspace = Workspace.objects.get(org_id=org_id)
expense = Expense.objects.filter(workspace_id=workspace.id, expense_id=expense_id).first()

if expense and not expense.exported:
expense_obj = []
expense_obj.append(data)
expense_objects = FyleExpenses().construct_expense_object(expense_obj, expense.workspace_id)
Expense.create_expense_objects(
expense_objects, expense.workspace_id
)
2 changes: 1 addition & 1 deletion apps/fyle/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ class WebhookCallbackView(generics.CreateAPIView):

@handle_view_exceptions()
def post(self, request, *args, **kwargs):
async_handle_webhook_callback(request.data)
async_handle_webhook_callback(request.data, int(kwargs['workspace_id']))

return Response(data={}, status=status.HTTP_200_OK)
6 changes: 3 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ gevent==23.9.1
gunicorn==20.1.0

# Platform SDK
fyle==0.36.1
fyle==0.37.0

# Reusable Fyle Packages
fyle-rest-auth==1.7.2
fyle-accounting-mappings==1.32.3
fyle-integrations-platform-connector==1.37.4
fyle-accounting-mappings==1.33.1
fyle-integrations-platform-connector==1.38.1

# Postgres Dependincies
psycopg2-binary==2.8.4
Expand Down
Loading

0 comments on commit e4eff41

Please sign in to comment.