Skip to content
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

New API for marking expense as paid on fyle #601

Merged
merged 2 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
run: |
docker-compose -f docker-compose-pipeline.yml build
docker-compose -f docker-compose-pipeline.yml up -d
docker-compose -f docker-compose-pipeline.yml exec -T api pytest tests/ --cov --cov-report=xml --cov-fail-under=87
docker-compose -f docker-compose-pipeline.yml exec -T api pytest tests/ --cov --cov-report=xml --cov-fail-under=86
echo "STATUS=$(cat pytest-coverage.txt | grep 'Required test' | awk '{ print $1 }')" >> $GITHUB_ENV
echo "FAILED=$(cat test-reports/report.xml | awk -F'=' '{print $5}' | awk -F' ' '{gsub(/"/, "", $1); print $1}')" >> $GITHUB_ENV
env:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pytest_action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
run: |
docker-compose -f docker-compose-pipeline.yml build
docker-compose -f docker-compose-pipeline.yml up -d
docker-compose -f docker-compose-pipeline.yml exec -T api pytest tests/ --cov --cov-report=xml --cov-fail-under=87 --junit-xml=test-reports/report.xml | tee pytest-coverage.txt
docker-compose -f docker-compose-pipeline.yml exec -T api pytest tests/ --cov --cov-report=xml --cov-fail-under=86 --junit-xml=test-reports/report.xml | tee pytest-coverage.txt
echo "STATUS=$(cat pytest-coverage.txt | grep 'Required test' | awk '{ print $1 }')" >> $GITHUB_ENV
echo "FAILED=$(cat test-reports/report.xml | awk -F'=' '{print $5}' | awk -F' ' '{gsub(/"/, "", $1); print $1}')" >> $GITHUB_ENV
env:
Expand Down
1 change: 0 additions & 1 deletion apps/fyle/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ def create_expense_objects(expenses: List[Dict], workspace_id):
'currency': expense['currency'],
'foreign_amount': expense['foreign_amount'],
'foreign_currency': expense['foreign_currency'],
'settlement_id': expense['settlement_id'],
'reimbursable': expense['reimbursable'],
'billable': expense['billable'],
'state': expense['state'],
Expand Down
76 changes: 26 additions & 50 deletions apps/netsuite/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import itertools
from typing import List
import base64
from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone

from django.db import transaction

Expand Down Expand Up @@ -1335,60 +1335,36 @@ def get_valid_reimbursement_ids(reimbursement_ids: List, platform: PlatformConne
def process_reimbursements(workspace_id):
fyle_credentials = FyleCredential.objects.get(workspace_id=workspace_id)

try:
platform = PlatformConnector(fyle_credentials=fyle_credentials)
except InvalidTokenError:
logger.info('Invalid Fyle refresh token for workspace %s', workspace_id)
return

platform.reimbursements.sync()

reimbursements = Reimbursement.objects.filter(state='PENDING', workspace_id=workspace_id).all()

reimbursement_ids = []
expenses_paid_on_fyle = []

if reimbursements:
for reimbursement in reimbursements:
expenses = Expense.objects.filter(settlement_id=reimbursement.settlement_id, fund_source='PERSONAL').all()
paid_expenses = expenses.filter(paid_on_netsuite=True)

all_expense_paid = False
if len(expenses):
all_expense_paid = len(expenses) == len(paid_expenses)

if all_expense_paid:
reimbursement_ids.append(reimbursement.reimbursement_id)
expenses_paid_on_fyle.extend(expenses)

if reimbursement_ids:
# Validating deleted reimbursements
valid_reimbursement_ids = get_valid_reimbursement_ids(reimbursement_ids, platform)
platform = PlatformConnector(fyle_credentials=fyle_credentials)

chunk_size = 20
expenses_to_be_marked = []
payloads = []

for index in range(0, len(valid_reimbursement_ids), chunk_size):
partitioned_list = valid_reimbursement_ids[index:index + chunk_size]
report_ids = Expense.objects.filter(fund_source='PERSONAL', paid_on_fyle=False, workspace_id=workspace_id).values_list('report_id').distinct()
for report_id in report_ids:
report_id = report_id[0]
expenses = Expense.objects.filter(fund_source='PERSONAL', report_id=report_id, workspace_id=workspace_id).all()
paid_expenses = expenses.filter(paid_on_netsuite=True)

if partitioned_list:
reimbursements_list = []
for reimbursement_id in partitioned_list:
reimbursement_object = {'id': reimbursement_id}
reimbursements_list.append(reimbursement_object)
all_expense_paid = False
if len(expenses):
all_expense_paid = len(expenses) == len(paid_expenses)

try:
platform.reimbursements.bulk_post_reimbursements(reimbursements_list)
platform.reimbursements.sync()
except Exception as error:
error = traceback.format_exc()
error = {
'error': error
}
logger.exception(error)
if all_expense_paid:
payloads.append({'id': report_id, 'paid_notify_at': datetime.now(timezone.utc).strftime('%Y-%m-%dT%H:%M:%S.%fZ')})
expenses_to_be_marked.extend(paid_expenses)
try:
platform.reports.bulk_mark_as_paid(payloads)
except Exception as error:
error = traceback.format_exc()
error = {
'error': error
}
logger.exception(error)

for expense in expenses_paid_on_fyle:
expense.paid_on_fyle = True
expense.save()
if expenses_to_be_marked:
expense_ids_to_mark = [expense.id for expense in expenses_to_be_marked]
Expense.objects.filter(id__in=expense_ids_to_mark).update(paid_on_fyle=True)

def schedule_reimbursements_sync(sync_netsuite_to_fyle_payments, workspace_id):
if sync_netsuite_to_fyle_payments:
Expand Down
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ djangorestframework==3.11.2
django-sendgrid-v5==1.2.0
enum34==1.1.10
future==0.18.2
fyle==0.36.1
fyle==0.37.0
fyle-accounting-mappings==1.32.2
fyle-integrations-platform-connector==1.37.1
fyle-integrations-platform-connector==1.38.0
fyle-rest-auth==1.7.2
gunicorn==20.1.0
gevent==23.9.1
Expand Down
4 changes: 2 additions & 2 deletions tests/test_netsuite/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -997,7 +997,7 @@ def test_schedule_reimbursements_sync(db):
def test_process_reimbursements(db, mocker, add_fyle_credentials):

mocker.patch(
'fyle_integrations_platform_connector.apis.Reimbursements.bulk_post_reimbursements',
'fyle_integrations_platform_connector.apis.Reports.bulk_mark_as_paid',
return_value=[]
)

Expand Down Expand Up @@ -1053,7 +1053,7 @@ def test_process_reimbursements_exception(db, mocker, add_fyle_credentials):
reimbursement.state = 'PENDING'
reimbursement.save()

with mock.patch('fyle_integrations_platform_connector.apis.Reimbursements.bulk_post_reimbursements') as mock_call:
with mock.patch('fyle_integrations_platform_connector.apis.Reports.bulk_mark_as_paid') as mock_call:
mock_call.side_effect = InternalServerError(
msg='internal server error',
response='Internal server error.'
Expand Down
5 changes: 5 additions & 0 deletions tests/test_netsuite/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ def test_trigger_payment_view(api_client, access_token, add_fyle_credentials, mo
'fyle_integrations_platform_connector.apis.Reimbursements.sync',
return_value=[],
)

mocker.patch(
'fyle_integrations_platform_connector.apis.Reports.bulk_mark_as_paid',
return_value=[],
)
mocker.patch(
'apps.netsuite.connector.NetSuiteConnector.get_bill',
return_value=data['get_bill_response'][1]
Expand Down
Loading