Skip to content

Commit fe84ab0

Browse files
Ashutosh619-sudoAshutosh619-sudo
andauthored
Feat: Add org setting on workspaces creation (#896)
* Feat: Add org setting on workspaces creation * added tests --------- Co-authored-by: Ashutosh619-sudo <ashutosh.s@fyle.com>
1 parent 84c2f75 commit fe84ab0

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

apps/workspaces/tasks.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,3 +402,23 @@ def update_workspace_name(workspace_id: int, access_token: str) -> None:
402402

403403
except Exception as e:
404404
logger.exception("Error updating workspace name for workspace_id: %s | Error: %s", workspace_id, str(e))
405+
406+
407+
def sync_org_settings(workspace_id: int) -> None:
408+
"""
409+
Fetch and store org settings for a workspace
410+
:param workspace_id: Workspace ID
411+
:return: None
412+
"""
413+
try:
414+
fyle_credential = FyleCredential.objects.get(workspace_id=workspace_id)
415+
platform = PlatformConnector(fyle_credential)
416+
org_settings = platform.org_settings.get()
417+
418+
workspace = Workspace.objects.get(id=workspace_id)
419+
workspace.org_settings = {
420+
'regional_settings': org_settings.get('regional_settings', {})
421+
}
422+
workspace.save(update_fields=['org_settings', 'updated_at'])
423+
except Exception as e:
424+
logger.error('Error fetching org settings for workspace %s: %s', workspace_id, str(e))

apps/workspaces/views.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
from apps.fyle.helpers import get_cluster_domain
3535
from apps.fyle.models import ExpenseGroupSettings
3636
from apps.workspaces.actions import export_to_intacct
37-
from apps.workspaces.tasks import patch_integration_settings
37+
from apps.workspaces.tasks import patch_integration_settings, sync_org_settings
3838
from apps.sage_intacct.models import SageIntacctAttributesCount
3939
from apps.sage_intacct.helpers import get_sage_intacct_connection
4040
from apps.sage_intacct.enums import SageIntacctRestConnectionTypeEnum
@@ -162,6 +162,8 @@ def post(self, request: Request) -> Response:
162162
cluster_domain=cluster_domain
163163
)
164164

165+
sync_org_settings(workspace_id=workspace.id)
166+
165167
return Response(
166168
data=WorkspaceSerializer(workspace).data,
167169
status=status.HTTP_200_OK

tests/test_workspaces/test_tasks.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
post_to_integration_settings,
2424
run_sync_schedule,
2525
schedule_sync,
26+
sync_org_settings,
2627
trigger_email_notification,
2728
update_workspace_name,
2829
)
@@ -824,3 +825,35 @@ def test_update_workspace_name_invalid_token(db, mocker):
824825
side_effect=Exception('General error')
825826
)
826827
update_workspace_name(1, 'Bearer access_token')
828+
829+
830+
def test_sync_org_settings(db, mocker):
831+
"""
832+
Test sync org settings
833+
"""
834+
workspace_id = 1
835+
workspace = Workspace.objects.get(id=workspace_id)
836+
workspace.org_settings = {}
837+
workspace.save()
838+
839+
mock_platform = mocker.patch('apps.workspaces.tasks.PlatformConnector')
840+
mock_platform.return_value.org_settings.get.return_value = {
841+
'regional_settings': {
842+
'locale': {
843+
'date_format': 'DD/MM/YYYY',
844+
'timezone': 'Asia/Kolkata'
845+
}
846+
}
847+
}
848+
849+
sync_org_settings(workspace_id=workspace_id)
850+
851+
workspace.refresh_from_db()
852+
assert workspace.org_settings == {
853+
'regional_settings': {
854+
'locale': {
855+
'date_format': 'DD/MM/YYYY',
856+
'timezone': 'Asia/Kolkata'
857+
}
858+
}
859+
}

0 commit comments

Comments
 (0)