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

Accounting exports summary APIs #17

Merged
merged 2 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
21 changes: 19 additions & 2 deletions apps/accounting_exports/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
StringNullField,
CustomJsonField,
CustomDateTimeField,
StringOptionsField
StringOptionsField,
IntegerNullField
)
from apps.workspaces.models import BaseForeignWorkspaceModel
from apps.workspaces.models import BaseForeignWorkspaceModel, BaseModel
from apps.fyle.models import Expense

TYPE_CHOICES = (
Expand Down Expand Up @@ -44,3 +45,19 @@ class AccountingExport(BaseForeignWorkspaceModel):

class Meta:
db_table = 'accounting_exports'


class AccountingExportSummary(BaseModel):
"""
Table to store accounting export summary
"""
id = models.AutoField(primary_key=True)
last_exported_at = CustomDateTimeField(help_text='Last exported at datetime')
next_export_at = CustomDateTimeField(help_text='next export datetime')
export_mode = StringOptionsField(choices=EXPORT_MODE_CHOICES, help_text='Export mode')
total_accounting_export_count = IntegerNullField(help_text='Total count of accounting export exported')
successful_accounting_export_count = IntegerNullField(help_text='count of successful accounting export')
failed_accounting_export_count = IntegerNullField(help_text='count of failed accounting export')

class Meta:
db_table = 'accounting_export_summary'
12 changes: 11 additions & 1 deletion apps/accounting_exports/serializers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from rest_framework import serializers

from apps.accounting_exports.models import AccountingExport
from apps.accounting_exports.models import AccountingExport, AccountingExportSummary


class AccountingExportSerializer(serializers.ModelSerializer):
Expand All @@ -11,3 +11,13 @@ class AccountingExportSerializer(serializers.ModelSerializer):
class Meta:
model = AccountingExport
fields = '__all__'


class AccountingExportSummarySerializer(serializers.ModelSerializer):
"""
Accounting Export Summary serializer
"""

class Meta:
model = AccountingExportSummary
fields = '__all__'
3 changes: 2 additions & 1 deletion apps/accounting_exports/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
"""
from django.urls import path

from apps.accounting_exports.views import AccountingExportView, AccountingExportCountView
from apps.accounting_exports.views import AccountingExportView, AccountingExportCountView, AccountingExportSummaryView


urlpatterns = [
path('', AccountingExportView.as_view(), name='accounting-exports'),
path('count/', AccountingExportCountView.as_view(), name='accounting-exports-count'),
path('summary/', AccountingExportSummaryView.as_view(), name='accounting-exports-summary'),
]
15 changes: 13 additions & 2 deletions apps/accounting_exports/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
from rest_framework.response import Response

from ms_business_central_api.utils import LookupFieldMixin
from apps.accounting_exports.serializers import AccountingExportSerializer
from apps.accounting_exports.models import AccountingExport
from apps.accounting_exports.serializers import AccountingExportSerializer, AccountingExportSummarySerializer
from apps.accounting_exports.models import AccountingExport, AccountingExportSummary


logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -35,3 +35,14 @@ def get(self, request, *args, **kwargs):
params["status__in"] = request.query_params.get("status__in").split(",")

return Response({"count": AccountingExport.objects.filter(**params).count()})


class AccountingExportSummaryView(generics.RetrieveAPIView):
"""
Retrieve Accounting Export Summary
"""
lookup_field = 'workspace_id'
lookup_url_kwarg = 'workspace_id'

queryset = AccountingExportSummary.objects.filter(last_exported_at__isnull=False, total_accounting_export_count__gt=0)
serializer_class = AccountingExportSummarySerializer
23 changes: 22 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
Workspace,
FyleCredential,
)
from apps.accounting_exports.models import AccountingExport
from apps.accounting_exports.models import AccountingExport, AccountingExportSummary
from ms_business_central_api.tests import settings

from .test_fyle.fixtures import fixtures as fyle_fixtures
Expand Down Expand Up @@ -167,3 +167,24 @@ def add_accounting_export_expenses():
'status': 'IN_PROGRESS'
}
)


@pytest.fixture()
@pytest.mark.django_db(databases=['default'])
def add_accounting_export_summary():
"""
Pytest fixture to add accounting export summary to a workspace
"""
workspace_ids = [
1, 2, 3
]
for workspace_id in workspace_ids:
AccountingExportSummary.objects.create(
workspace_id=workspace_id,
last_exported_at = datetime.now(tz=timezone.utc),
next_export_at = datetime.now(tz=timezone.utc),
export_mode = 'AUTO',
total_accounting_export_count = 10,
successful_accounting_export_count = 5,
failed_accounting_export_count = 5
)
11 changes: 11 additions & 0 deletions tests/test_accounting_exports/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,14 @@ def test_get_accounting_exports(api_client, test_connection, create_temp_workspa
response = json.loads(response.content)

assert response['count'] == 2, 'accounting export count api return diffs in keys'


def test_get_accounting_export_summary(api_client, test_connection, create_temp_workspace, add_fyle_credentials, add_accounting_export_summary):
url = reverse('accounting-exports-summary', kwargs={'workspace_id': 1})

api_client.credentials(HTTP_AUTHORIZATION='Bearer {}'.format(test_connection.access_token))

response = api_client.get(url)
assert response.status_code == 200
response = json.loads(response.content)
assert dict_compare_keys(response, data['accounting_export_summary_response']) == [], 'expense group api return diffs in keys'
12 changes: 12 additions & 0 deletions tests/test_fyle/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,16 @@
}
]
},
'accounting_export_summary_response': {
"id":1,
"created_at":"2023-10-27T04:53:59.287745Z",
"updated_at":"2023-10-27T04:53:59.287750Z",
"last_exported_at":"2023-10-27T04:53:59.287618Z",
"next_export_at":"2023-10-27T04:53:59.287619Z",
"export_mode":"AUTO",
"total_accounting_export_count":10,
"successful_accounting_export_count":5,
"failed_accounting_export_count":5,
"workspace":1
}
}
Loading