diff --git a/apps/fyle/serializers.py b/apps/fyle/serializers.py index 0cc5d045..71d608e1 100644 --- a/apps/fyle/serializers.py +++ b/apps/fyle/serializers.py @@ -5,13 +5,24 @@ from .models import Expense, ExpenseFilter, ExpenseGroup, ExpenseGroupSettings +class ExpenseSerializer(serializers.ModelSerializer): + """ + Expense serializer + """ + class Meta: + model = Expense + fields = ['updated_at', 'claim_number', 'employee_email', 'employee_name', 'fund_source', 'expense_number', 'vendor', 'category', 'amount', + 'report_id', 'settlement_id', 'expense_id'] + class ExpenseGroupSerializer(serializers.ModelSerializer): """ Expense group serializer """ + expenses = ExpenseSerializer(many=True) class Meta: model = ExpenseGroup fields = '__all__' + extra_fields = ['expenses'] class ExpenseGroupExpenseSerializer(serializers.ModelSerializer): @@ -60,12 +71,3 @@ def create(self, validated_data): ) return expense_filter - - -class ExpenseSerializer(serializers.ModelSerializer): - """ - Expense serializer - """ - class Meta: - model = Expense - fields = ['updated_at', 'claim_number', 'employee_email', 'employee_name', 'fund_source'] diff --git a/apps/fyle/tasks.py b/apps/fyle/tasks.py index f3ed39f8..e3b9ab40 100644 --- a/apps/fyle/tasks.py +++ b/apps/fyle/tasks.py @@ -24,27 +24,32 @@ 'CCC': 'PERSONAL_CORPORATE_CREDIT_CARD_ACCOUNT' } -def schedule_expense_group_creation(workspace_id: int): - """ - Schedule Expense group creation - :param workspace_id: Workspace id - :param user: User email - :return: None - """ +def get_task_log_and_fund_source(workspace_id: int): task_log, _ = TaskLog.objects.update_or_create( workspace_id=workspace_id, type='FETCHING_EXPENSES', defaults={ - 'status': 'IN_PROGRESS' + 'status': 'IN_PROGRESS' } ) configuration = Configuration.objects.get(workspace_id=workspace_id) - fund_source = ['PERSONAL'] if configuration.corporate_credit_card_expenses_object is not None: fund_source.append('CCC') + return task_log, fund_source, configuration + +def schedule_expense_group_creation(workspace_id: int): + """ + Schedule Expense group creation + :param workspace_id: Workspace id + :param user: User email + :return: None + """ + + task_log, fund_source, configuration = get_task_log_and_fund_source(workspace_id) + async_task('apps.fyle.tasks.create_expense_groups', workspace_id, configuration, fund_source, task_log) diff --git a/apps/fyle/urls.py b/apps/fyle/urls.py index ceee784b..2333ba8c 100644 --- a/apps/fyle/urls.py +++ b/apps/fyle/urls.py @@ -2,7 +2,7 @@ from django.urls import path -from .views import ExpenseGroupView, ExpenseGroupByIdView, ExpenseGroupScheduleView, ExportableExpenseGroupsView, FyleFieldsView, ExpenseView,\ +from .views import ExpenseGroupSyncView, ExpenseGroupView, ExpenseGroupByIdView, ExpenseGroupScheduleView, ExportableExpenseGroupsView, FyleFieldsView, ExpenseView,\ ExpenseAttributesView, ExpenseGroupSettingsView, SyncFyleDimensionView, RefreshFyleDimensionView,\ ExpenseGroupCountView, ExpenseFilterView, ExpenseGroupExpenseView, CustomFieldView @@ -13,7 +13,8 @@ path('expense_groups//', ExpenseGroupByIdView.as_view(), name='expense-group-by-id'), path('expense_groups//expenses/', ExpenseGroupExpenseView.as_view(), name='expense-group-expenses'), path('expense_group_settings/', ExpenseGroupSettingsView.as_view(), name='expense-group-settings'), - path('exportable_expense_groups/', ExportableExpenseGroupsView.as_view(), name='expense-expense-groups') + path('exportable_expense_groups/', ExportableExpenseGroupsView.as_view(), name='expense-expense-groups'), + path('expense_groups/sync/', ExpenseGroupSyncView.as_view(), name='sync-expense-groups'), ] fyle_dimension_paths = [ diff --git a/apps/fyle/views.py b/apps/fyle/views.py index 561d0670..75ad45fc 100644 --- a/apps/fyle/views.py +++ b/apps/fyle/views.py @@ -14,7 +14,7 @@ from apps.workspaces.models import Configuration, FyleCredential, Workspace -from .tasks import schedule_expense_group_creation +from .tasks import schedule_expense_group_creation, get_task_log_and_fund_source, create_expense_groups from .helpers import check_interval_and_sync_dimension, sync_dimensions from .models import Expense, ExpenseGroup, ExpenseGroupSettings, ExpenseFilter from .serializers import ExpenseGroupSerializer, ExpenseSerializer, ExpenseFieldSerializer, \ @@ -419,3 +419,20 @@ def get(self, request, *args, **kwargs): }, status=status.HTTP_400_BAD_REQUEST ) + + +class ExpenseGroupSyncView(generics.CreateAPIView): + """ + Create expense groups + """ + def post(self, request, *args, **kwargs): + """ + Post expense groups creation + """ + task_log, fund_source, configuration = get_task_log_and_fund_source(kwargs['workspace_id']) + + create_expense_groups(kwargs['workspace_id'], configuration ,fund_source, task_log) + + return Response( + status=status.HTTP_200_OK + ) diff --git a/tests/test_fyle/fixtures.py b/tests/test_fyle/fixtures.py index 15995fd7..81690a29 100644 --- a/tests/test_fyle/fixtures.py +++ b/tests/test_fyle/fixtures.py @@ -131,7 +131,14 @@ 'claim_number': ' C/2021/12/R/198', 'employee_email': 'jhonsnow@fyle.in', 'employee_name': None, - 'fund_source': 'CCC' + 'fund_source': 'CCC', + 'expense_number': 'E/2023/09/T/7', + 'vendor': None, + 'category': 'Train', + 'amount': 101.0, + 'report_id': 'dummy_report_id', + 'settlement_id': 'dummy_settlement_id', + 'expense_id': 'dummy_expense_id' }, { 'updated_at': '2021-12-03T11:26:58.702209Z', @@ -547,7 +554,20 @@ "exported_at": "2021-11-15T11:02:55.125205Z", "updated_at": "2021-11-15T11:02:55.125634Z", "workspace": 1, - "expenses": [1], + "expenses": [ { + "updated_at": "2023-11-21T10:41:09.919000Z", + "claim_number": "C/2023/06/R/4", + "employee_email": "admin1@fyleforbadassashu.in", + "employee_name": "Theresa Brown", + "fund_source": "PERSONAL", + "expense_number": "E/2023/06/T/21", + "vendor": "95110", + "category": "Airlines", + "amount": 6377.0, + "report_id": "rp0kaXoqkJle", + "settlement_id": "setPzkM7eyQFd", + "expense_id": "txyZ1zJDQfiK" + }], }, "expense_group_setting_response": { "id": 1, diff --git a/tests/test_fyle/test_views.py b/tests/test_fyle/test_views.py index bcc8add0..81889754 100644 --- a/tests/test_fyle/test_views.py +++ b/tests/test_fyle/test_views.py @@ -401,4 +401,4 @@ def test_exportable_expense_group_view(api_client, access_token): assert response.status_code==200 response = json.loads(response.content) - assert response['exportable_expense_group_ids'] == [1, 2] \ No newline at end of file + assert response['exportable_expense_group_ids'] == [1, 2]