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

Workspace admin apis #14

Merged
merged 4 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
26 changes: 26 additions & 0 deletions apps/workspaces/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from rest_framework import serializers
from fyle_rest_auth.helpers import get_fyle_admin
from fyle_rest_auth.models import AuthToken
from fyle_accounting_mappings.models import ExpenseAttribute

from ms_business_central_api.utils import assert_valid
from apps.workspaces.models import (
Expand Down Expand Up @@ -168,3 +169,28 @@ def create(self, validated_data):
workspace.save()

return advanced_setting


class WorkspaceAdminSerializer(serializers.Serializer):
"""
Workspace Admin Serializer
"""
admin_emails = serializers.SerializerMethodField()

def get_admin_emails(self, validated_data):
"""
Get Workspace Admins
"""
workspace_id = self.context['request'].parser_context.get('kwargs').get('workspace_id')
workspace = Workspace.objects.get(id=workspace_id)
admin_emails = []

users = workspace.user.all()

for user in users:
admin = User.objects.get(user_id=user)
employee = ExpenseAttribute.objects.filter(value=admin.email, workspace_id=workspace_id, attribute_type='EMPLOYEE').first()
if employee:
admin_emails.append({'name': employee.detail['full_name'], 'email': admin.email})

return admin_emails
4 changes: 3 additions & 1 deletion apps/workspaces/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
WorkspaceView,
ExportSettingView,
ImportSettingView,
AdvancedSettingView
AdvancedSettingView,
WorkspaceAdminsView
)


Expand All @@ -15,6 +16,7 @@
path('<int:workspace_id>/export_settings/', ExportSettingView.as_view(), name='export-settings'),
path('<int:workspace_id>/import_settings/', ImportSettingView.as_view(), name='import-settings'),
path('<int:workspace_id>/advanced_settings/', AdvancedSettingView.as_view(), name='advanced-settings'),
path('<int:workspace_id>/admins/', WorkspaceAdminsView.as_view(), name='admin'),
]

other_app_paths = []
Expand Down
11 changes: 10 additions & 1 deletion apps/workspaces/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
WorkspaceSerializer,
ExportSettingsSerializer,
ImportSettingsSerializer,
AdvancedSettingSerializer
AdvancedSettingSerializer,
WorkspaceAdminSerializer
)


Expand Down Expand Up @@ -105,3 +106,11 @@ class AdvancedSettingView(generics.CreateAPIView, generics.RetrieveAPIView):
lookup_url_kwarg = 'workspace_id'

queryset = AdvancedSetting.objects.all()


class WorkspaceAdminsView(generics.ListAPIView):
"""
Retrieve Workspace Admins
"""
serializer_class = WorkspaceAdminSerializer
queryset = Workspace.objects.all()
2 changes: 1 addition & 1 deletion ms_business_central_api/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
'corsheaders',
'django_q',
'fyle_rest_auth',
# 'fyle_accounting_mappings',
'fyle_accounting_mappings',

# User Created Apps
'apps.users',
Expand Down
1 change: 1 addition & 0 deletions ms_business_central_api/tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
'corsheaders',
'django_q',
'fyle_rest_auth',
'fyle_accounting_mappings',

# User Created Apps
'apps.users',
Expand Down
17 changes: 17 additions & 0 deletions tests/test_workspaces/test_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,3 +273,20 @@ def test_advanced_settings(api_client, test_connection):
'email': '[email protected]'
},
]


def test_get_workspace_admins(api_client, test_connection):
'''
Test get workspace admins
'''
url = reverse('workspaces')
api_client.credentials(HTTP_AUTHORIZATION='Bearer {}'.format(test_connection.access_token))
response = api_client.post(url)

workspace_id = response.data['id']

url = reverse('admin', kwargs={'workspace_id': workspace_id})
api_client.credentials(HTTP_AUTHORIZATION='Bearer {}'.format(test_connection.access_token))

response = api_client.get(url)
assert response.status_code == 200
Loading