diff --git a/apps/fyle/models.py b/apps/fyle/models.py index 5e1d145b..1a16fff0 100644 --- a/apps/fyle/models.py +++ b/apps/fyle/models.py @@ -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 """ @@ -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 diff --git a/apps/fyle/tasks.py b/apps/fyle/tasks.py index 67f31522..66b7ed9e 100644 --- a/apps/fyle/tasks.py +++ b/apps/fyle/tasks.py @@ -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 ) diff --git a/requirements.txt b/requirements.txt index f201e87a..762a5039 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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 diff --git a/sage_desktop_api/settings.py b/sage_desktop_api/settings.py index ff8ac3f0..14dc532d 100644 --- a/sage_desktop_api/settings.py +++ b/sage_desktop_api/settings.py @@ -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. diff --git a/sage_desktop_api/tests/settings.py b/sage_desktop_api/tests/settings.py index 2f274aa3..56173289 100644 --- a/sage_desktop_api/tests/settings.py +++ b/sage_desktop_api/tests/settings.py @@ -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. diff --git a/tests/test_fyle/fixtures.py b/tests/test_fyle/fixtures.py index 04db471a..434ffe39 100644 --- a/tests/test_fyle/fixtures.py +++ b/tests/test_fyle/fixtures.py @@ -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': [ @@ -159,31 +159,6 @@ }, 'project_id': 330241, 'purpose': None, - 'report': { - 'amount': 12, - 'approvals': [ - { - 'approver_user': { - 'email': 'admin1@fyleforimporrttest.in', - '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, @@ -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': 'admin1@fyleforimporrttest.in', 'full_name': 'Theresa Brown',