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

fix: bug fix for support qbd (minor fix) #102

Merged
merged 10 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
41 changes: 22 additions & 19 deletions apps/mappings/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,21 +112,22 @@ def sync_projects(self, source_type: str):
'order': 'updated_at.desc'
}
projects_generator = self.platform.v1beta.admin.projects.list_all(query)
existing_projects = QBDMapping.objects.filter(workspace_id=self.workspace_id, attribute_type=source_type)

existing_projects = QBDMapping.objects.filter(
workspace_id=self.workspace_id,
attribute_type=source_type).values_list('source_value', flat=True)

source_attributes = []

for projects in projects_generator:
for project in projects.get('data'):
if project['sub_project']:
project['name'] = '{0} / {1}'.format(project['name'], project['sub_project'])
if project['name'] not in existing_projects:
source_attributes.append({
'attribute_type': source_type,
'value': project['name'],
'source_id': project['id']
})

if project['name'] not in existing_projects:
anishfyle marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indent fixed

source_attributes.append({
'attribute_type': source_type,
'value': project['name'],
'source_id': project['id']
})

if source_attributes:
QBDMapping.update_or_create_mapping_objects(source_attributes, self.workspace_id)
Expand All @@ -139,20 +140,22 @@ def sync_cost_center(self, source_type: str):
query = {
'order': 'updated_at.desc'
}

cost_center_generator = self.platform.v1beta.admin.cost_centers.list_all(query)
existing_cost_centers = QBDMapping.objects.filter(workspace_id=self.workspace_id, attribute_type=source_type)
existing_cost_centers = QBDMapping.objects.filter(
workspace_id=self.workspace_id,
attribute_type=source_type).values_list('source_value', flat=True)

source_attributes = []

for cost_centers in cost_center_generator:
for cost_center in cost_centers.get('data'):
if cost_center not in existing_cost_centers:
source_attributes.append({
'attribute_type': source_type,
'value': cost_center,
'source_id': cost_center
})

for cost_center_dict in cost_centers:
for cost_center in cost_center_dict.get('data', []):
if cost_center['name'] not in existing_cost_centers:
source_attributes.append({
'attribute_type': source_type,
'value': cost_center['name'],
'source_id': cost_center['id']
})

if source_attributes:
QBDMapping.update_or_create_mapping_objects(source_attributes, self.workspace_id)
24 changes: 19 additions & 5 deletions tests/test_mapping/test_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,23 @@ def test_sync_cost_center(create_temp_workspace, add_fyle_credentials, mocker):
workspace_id = 1
source_type = 'COST_CENTER'
mock_response = [
{'data': ['Cost Center 1', 'Cost Center 2']}
[
{
'data': [
{
'code': None,
'created_at': '2024-07-29T12:30:58.972642+00:00',
'description': 'Cost Center - RENT, Id - 1',
'id': 24714,
'is_enabled': True,
'name': 'RENT',
'org_id': 'orU77Fyqx0YH',
'restricted_spender_user_ids': None,
'updated_at': '2024-07-29T12:30:58.972644+00:00'
}
]
}
]
]
mocker.patch(
'fyle.platform.apis.v1beta.admin.cost_centers.list_all',
Expand All @@ -59,11 +75,9 @@ def test_sync_cost_center(create_temp_workspace, add_fyle_credentials, mocker):
qbd_connection = PlatformConnector(workspace_id=workspace_id)
qbd_connection.sync_cost_center(source_type)
qbd_mappings = QBDMapping.objects.filter(workspace_id=workspace_id, attribute_type=source_type)
assert len(qbd_mappings) == len(mock_response[0]['data'])
assert len(qbd_mappings) == len(mock_response[0][0])
for i, mapping in enumerate(qbd_mappings):
cost_center = mock_response[0]['data'][i]
assert mapping.source_value == cost_center

assert mapping.source_value == mock_response[0][0]['data'][i]['name']

@pytest.mark.django_db(databases=['default'], transaction=True)
def test_sync_custom_field(mocker, api_client, test_connection):
Expand Down
Loading