Skip to content

Commit

Permalink
First schedule should be triggered after interval hours and Handle Ad…
Browse files Browse the repository at this point in the history
…min GET in a safer way (#437)

* First schedule should be triggered after interval hours

* Handle Admin GET in a safer way

* Making reimbursable expense object nullable and checking edge cases  (#438)

* Making reimbursbale expense object nullable and checking edge cases for the same

* comment resolved

* resolving comments

* all comment resolved

* added code in test for the changes

* added test code for the changes

---------

Co-authored-by: Ashutosh619-sudo <[email protected]>

---------

Co-authored-by: Ashutosh619-sudo <[email protected]>
  • Loading branch information
Ashutosh619-sudo and Ashutosh619-sudo authored Nov 7, 2023
1 parent 023c901 commit 4b37401
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 21 deletions.
4 changes: 2 additions & 2 deletions apps/fyle/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,9 @@ def update_import_card_credits_flag(corporate_credit_card_expenses_object: str,
expense_group_settings = ExpenseGroupSettings.objects.get(workspace_id=workspace_id)
import_card_credits = None

if (corporate_credit_card_expenses_object == 'EXPENSE REPORT' or reimbursable_expenses_object in ['EXPENSE REPORT', 'JOURNAL ENTRY']) and not expense_group_settings.import_card_credits:
if (corporate_credit_card_expenses_object == 'EXPENSE REPORT' or (reimbursable_expenses_object and reimbursable_expenses_object in ['EXPENSE REPORT', 'JOURNAL ENTRY'])) and not expense_group_settings.import_card_credits:
import_card_credits = True
elif (corporate_credit_card_expenses_object != 'EXPENSE REPORT' and reimbursable_expenses_object not in ['EXPENSE REPORT', 'JOURNAL ENTRY']) and expense_group_settings.import_card_credits:
elif (corporate_credit_card_expenses_object != 'EXPENSE REPORT' and (reimbursable_expenses_object and reimbursable_expenses_object not in ['EXPENSE REPORT', 'JOURNAL ENTRY'])) and expense_group_settings.import_card_credits:
import_card_credits = False

if corporate_credit_card_expenses_object == 'CREDIT CARD CHARGE':
Expand Down
2 changes: 1 addition & 1 deletion apps/fyle/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ def create_expense_groups_by_report_id_fund_source(expense_objects: List[Expense

if total_amount < 0:
reimbursable_expenses = list(filter(lambda expense: expense.amount > 0, reimbursable_expenses))
elif configuration.reimbursable_expenses_object != 'JOURNAL ENTRY':
elif configuration.reimbursable_expenses_object != 'JOURNAL ENTRY':
reimbursable_expenses = list(filter(lambda expense: expense.amount > 0, reimbursable_expenses))

expense_groups = _group_expenses(reimbursable_expenses, reimbursable_expense_group_fields, workspace_id)
Expand Down
11 changes: 6 additions & 5 deletions apps/mappings/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,10 @@ def sync_expense_categories_and_accounts(configuration: Configuration, netsuite_
:netsuite_connection: NetSuite Connection Object
:return: None
"""
if configuration.reimbursable_expenses_object == 'EXPENSE REPORT' or configuration.corporate_credit_card_expenses_object == 'EXPENSE REPORT':
if (configuration.reimbursable_expenses_object and configuration.reimbursable_expenses_object == 'EXPENSE REPORT') or configuration.corporate_credit_card_expenses_object == 'EXPENSE REPORT':
netsuite_connection.sync_expense_categories()

if configuration.reimbursable_expenses_object in ('BILL', 'JOURNAL ENTRY') or \
if (configuration.reimbursable_expenses_object and configuration.reimbursable_expenses_object in ('BILL', 'JOURNAL ENTRY')) or \
configuration.corporate_credit_card_expenses_object in ('BILL', 'JOURNAL ENTRY', 'CREDIT CARD CHARGE'):
netsuite_connection.sync_accounts()

Expand Down Expand Up @@ -226,8 +226,8 @@ def upload_categories_to_fyle(workspace_id: int, configuration: Configuration, p
if configuration.import_categories:
netsuite_accounts = DestinationAttribute.objects.filter(
workspace_id=workspace_id,
attribute_type='EXPENSE_CATEGORY' if configuration.reimbursable_expenses_object == 'EXPENSE REPORT' else 'ACCOUNT',
display_name='Expense Category' if configuration.reimbursable_expenses_object == 'EXPENSE REPORT' else 'Account'
attribute_type='EXPENSE_CATEGORY' if configuration.employee_field_mapping == 'EMPLOYEE' else 'ACCOUNT',
display_name='Expense Category' if configuration.employee_field_mapping == 'EMPLOYEE' else 'Account'
)
if netsuite_accounts:
netsuite_attributes = netsuite_accounts
Expand Down Expand Up @@ -477,9 +477,10 @@ def auto_create_category_mappings(workspace_id):
configuration: Configuration = Configuration.objects.get(workspace_id=workspace_id)

reimbursable_expenses_object = configuration.reimbursable_expenses_object
employee_field_mapping = configuration.employee_field_mapping
corporate_credit_card_expenses_object = configuration.corporate_credit_card_expenses_object

if reimbursable_expenses_object == 'EXPENSE REPORT':
if employee_field_mapping and employee_field_mapping == 'EMPLOYEE':
reimbursable_destination_type = 'EXPENSE_CATEGORY'
else:
reimbursable_destination_type = 'ACCOUNT'
Expand Down
3 changes: 2 additions & 1 deletion apps/workspaces/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,15 @@ def schedule_sync(workspace_id: int, schedule_enabled: bool, hours: int, email_a
if email_added:
ws_schedule.additional_email_options.append(email_added)

next_run = datetime.now() + timedelta(hours=hours)

schedule, _ = Schedule.objects.update_or_create(
func='apps.workspaces.tasks.run_sync_schedule',
args='{}'.format(workspace_id),
defaults={
'schedule_type': Schedule.MINUTES,
'minutes': hours * 60,
'next_run': datetime.now()
'next_run': next_run
}
)

Expand Down
13 changes: 7 additions & 6 deletions apps/workspaces/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,16 +458,17 @@ def get(self, request, *args, **kwargs):
users = workspace.user.all()
for user in users:
admin = User.objects.get(user_id=user)
name = ExpenseAttribute.objects.get(
employee = ExpenseAttribute.objects.filter(
value=admin.email,
workspace_id=kwargs['workspace_id'],
attribute_type='EMPLOYEE'
).detail['full_name']
).first()

admin_email.append({
'name': name,
'email': admin.email
})
if employee:
admin_email.append({
'name': employee.detail['full_name'],
'email': admin.email
})

return Response(
data=admin_email,
Expand Down
8 changes: 5 additions & 3 deletions tests/test_mappings/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ def test_upload_categories_to_fyle(mocker, db):

configuration = Configuration.objects.filter(workspace_id=49).first()
configuration.reimbursable_expenses_object = 'EXPENSE REPORT'
configuration.employee_field_mapping = 'VENDOR'
configuration.corporate_credit_card_expenses_object = 'BILL'
configuration.import_categories = True
configuration.save()
Expand All @@ -290,20 +291,21 @@ def test_upload_categories_to_fyle(mocker, db):

assert expense_category_count == 36

assert len(netsuite_attributes) == 36
assert len(netsuite_attributes) == 137

count_of_accounts = DestinationAttribute.objects.filter(
attribute_type='ACCOUNT', workspace_id=49).count()

assert count_of_accounts == 137

configuration.employee_field_mapping = 'EMPLOYEE'
configuration.reimbursable_expenses_object = 'BILL'
configuration.corporate_credit_card_expenses_object = 'BILL'
configuration.save()

netsuite_attributes = upload_categories_to_fyle(49, configuration, platform)

assert len(netsuite_attributes) == 137
assert len(netsuite_attributes) == 36


def test_filter_unmapped_destinations(db, mocker):
Expand Down Expand Up @@ -403,7 +405,7 @@ def test_auto_create_category_mappings(db, mocker):
destination_attribute.save()

configuration = Configuration.objects.filter(workspace_id=49).first()

configuration.employee_field_mapping = 'VENDOR'
configuration.import_categories = True
configuration.save()

Expand Down
10 changes: 7 additions & 3 deletions tests/test_workspaces/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,9 +317,13 @@ def test_post_workspace_schedule(api_client, access_token):
api_client.credentials(HTTP_AUTHORIZATION='Bearer {}'.format(access_token))

response = api_client.post(url, {
'schedule_enabled': False,
'hours': 0
})
"hours": 2,
"schedule_enabled": True,
"added_email": None,
"selected_email": [
"[email protected]"
]
}, format='json')
assert response.status_code == 200

def test_get_workspace_schedule(api_client, access_token):
Expand Down

0 comments on commit 4b37401

Please sign in to comment.