Skip to content

Commit

Permalink
Fix Edit Expenses changes (#204)
Browse files Browse the repository at this point in the history
* fixing payload logging

* fix edit expenses

* bump version of platform-connector, add fix for expense update

* add logger

* remove extra loggers

* increase poll

* fix created_at, updated_at null constraint
  • Loading branch information
Hrishabh17 committed Jul 15, 2024
1 parent 2fbd44c commit d7564b0
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 66 deletions.
80 changes: 45 additions & 35 deletions apps/fyle/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class Meta:
db_table = 'expenses'

@staticmethod
def create_expense_objects(expenses: List[Dict], workspace_id: int):
def create_expense_objects(expenses: List[Dict], workspace_id: int, skip_update: bool = False):
"""
Bulk create expense objects
"""
Expand All @@ -129,45 +129,55 @@ def create_expense_objects(expenses: List[Dict], workspace_id: int):
if expense['custom_properties'][custom_property_field] == '':
expense['custom_properties'][custom_property_field] = None

# Create or update an Expense object based on expense_id
expense_object, _ = Expense.objects.update_or_create(
expense_id=expense['id'],
defaults={
'employee_email': expense['employee_email'],
'employee_name': expense['employee_name'],
'category': expense['category'],
'sub_category': expense['sub_category'],
'project': expense['project'],
'expense_number': expense['expense_number'],
'org_id': expense['org_id'],
expense_data_to_append = {}
if not skip_update:
expense_data_to_append = {
'claim_number': expense['claim_number'],
'amount': round(expense['amount'], 2),
'currency': expense['currency'],
'foreign_amount': expense['foreign_amount'],
'foreign_currency': expense['foreign_currency'],
'tax_amount': expense['tax_amount'],
'tax_group_id': expense['tax_group_id'],
'reimbursable': expense['reimbursable'],
'billable': expense['billable'] if expense['billable'] else False,
'state': expense['state'],
'vendor': expense['vendor'][:250] if expense['vendor'] else None,
'cost_center': expense['cost_center'],
'purpose': expense['purpose'],
'report_id': expense['report_id'],
'report_title': expense['report_title'],
'spent_at': expense['spent_at'],
'approved_at': expense['approved_at'],
'posted_at': expense['posted_at'],
'expense_created_at': expense['expense_created_at'],
'expense_updated_at': expense['expense_updated_at'],
'fund_source': SOURCE_ACCOUNT_MAP[expense['source_account_type']],
'verified_at': expense['verified_at'],
'custom_properties': expense['custom_properties'],
'payment_number': expense['payment_number'],
'file_ids': expense['file_ids'],
'corporate_card_id': expense['corporate_card_id'],
'workspace_id': workspace_id
'expense_created_at': expense['expense_created_at'],
'expense_updated_at': expense['expense_updated_at']
}

defaults = {
'employee_email': expense['employee_email'],
'employee_name': expense['employee_name'],
'category': expense['category'],
'sub_category': expense['sub_category'],
'project': expense['project'],
'expense_number': expense['expense_number'],
'org_id': expense['org_id'],
'amount': round(expense['amount'], 2),
'currency': expense['currency'],
'foreign_amount': expense['foreign_amount'],
'foreign_currency': expense['foreign_currency'],
'tax_amount': expense['tax_amount'],
'tax_group_id': expense['tax_group_id'],
'reimbursable': expense['reimbursable'],
'billable': expense['billable'] if expense['billable'] else False,
'state': expense['state'],
'vendor': expense['vendor'][:250] if expense['vendor'] else None,
'cost_center': expense['cost_center'],
'purpose': expense['purpose'],
'report_id': expense['report_id'],
'spent_at': expense['spent_at'],
'posted_at': expense['posted_at'],
'fund_source': SOURCE_ACCOUNT_MAP[expense['source_account_type']],
'verified_at': expense['verified_at'],
'custom_properties': expense['custom_properties'],
'file_ids': expense['file_ids'],
'corporate_card_id': expense['corporate_card_id'],
'workspace_id': workspace_id
}

if expense_data_to_append:
defaults.update(expense_data_to_append)

# Create or update an Expense object based on expense_id
expense_object, _ = Expense.objects.update_or_create(
expense_id=expense['id'],
defaults=defaults
)

# Check if an AccountingExport related to the expense object already exists
Expand Down
2 changes: 1 addition & 1 deletion apps/fyle/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,5 +145,5 @@ def update_non_exported_expenses(data: Dict) -> None:
expense_obj.append(data)
expense_objects = FyleExpenses().construct_expense_object(expense_obj, expense.workspace_id)
Expense.create_expense_objects(
expense_objects, expense.workspace_id
expense_objects, expense.workspace_id, skip_update=True
)
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fyle==0.37.0
# Reusable Fyle Packages
fyle-rest-auth==1.7.2
fyle-accounting-mappings==1.33.1
fyle-integrations-platform-connector==1.38.1
fyle-integrations-platform-connector==1.38.4


# Postgres Dependincies
Expand Down
2 changes: 1 addition & 1 deletion sage_desktop_api/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@
'cached': False,
'orm': 'default',
'ack_failures': True,
'poll': 1,
'poll': 5,
'max_attempts': 1,
'attempt_count': 1,
# The number of tasks a worker will process before recycling.
Expand Down
2 changes: 1 addition & 1 deletion sage_desktop_api/tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
'cached': False,
'orm': 'default',
'ack_failures': True,
'poll': 1,
'poll': 5,
'max_attempts': 1,
'attempt_count': 1,
# The number of tasks a worker will process before recycling.
Expand Down
29 changes: 2 additions & 27 deletions tests/test_fyle/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
'name': 'Administration'
},
'cost_center_id': 23166,
'created_at': '2024-05-10T07:52:10.551260+00:00',
'created_at': None,
'creator_user_id': 'usVN2WTtPqE7',
'currency': 'USD',
'custom_fields': [
Expand Down Expand Up @@ -159,31 +159,6 @@
},
'project_id': 330241,
'purpose': None,
'report': {
'amount': 12,
'approvals': [
{
'approver_user': {
'email': '[email protected]',
'full_name': 'Theresa Brown',
'id': 'usVN2WTtPqE7'
},
'approver_user_id': 'usVN2WTtPqE7',
'state': 'APPROVAL_DONE'
}
],
'id': 'rpN41rGGnxNI',
'last_approved_at': '2024-05-10T07:53:25.774+00:00',
'last_paid_at': None,
'last_submitted_at': '2024-05-10T07:53:09.457+00:00',
'last_verified_at': '2024-05-10T07:55:02.32928+00:00',
'reimbursement_id': 'reimYNNUkKQiWp',
'reimbursement_seq_num': 'P/2024/05/T/P/2024/05/R/30',
'seq_num': 'C/2024/05/R/45',
'settlement_id': 'setUkp31alIp7',
'state': 'PAYMENT_PROCESSING',
'title': '#5: May 2024'
},
'report_id': 'rpN41rGGnxNI',
'report_last_approved_at': '2024-05-10T07:53:25.774000+00:00',
'report_last_paid_at': None,
Expand All @@ -205,7 +180,7 @@
'tax_group': None,
'tax_group_id': None,
'travel_classes': [],
'updated_at': '2024-06-10T11:41:40.779611+00:00',
'updated_at': None,
'user': {
'email': '[email protected]',
'full_name': 'Theresa Brown',
Expand Down

0 comments on commit d7564b0

Please sign in to comment.