Skip to content

Commit

Permalink
Adding validations for org_id in Sync (#61)
Browse files Browse the repository at this point in the history
* Adding validations for org_id in Sync

* Updating version in Setup.py
  • Loading branch information
Shwetabhk authored Aug 30, 2023
1 parent 9338216 commit 22c0ac3
Show file tree
Hide file tree
Showing 9 changed files with 146 additions and 130 deletions.
24 changes: 17 additions & 7 deletions connector/fyle_integrations_platform_connector/apis/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from datetime import datetime
from typing import Dict, List

from apps.workspaces.models import Workspace

from fyle_accounting_mappings.models import ExpenseAttribute


Expand All @@ -12,6 +14,7 @@ def __init__(self, attribute_type: str = None, query_params: dict = {}):
self.query_params = query_params
self.connection = None
self.workspace_id = None
self.workspace = None


def set_connection(self, connection):
Expand All @@ -20,6 +23,7 @@ def set_connection(self, connection):

def set_workspace_id(self, workspace_id):
self.workspace_id = workspace_id
self.workspace = Workspace.objects.get(workspace_id=self.workspace_id)


def format_date(self, last_synced_at: datetime) -> str:
Expand Down Expand Up @@ -98,16 +102,22 @@ def __construct_expense_attribute_objects(self, generator) -> List[dict]:
attributes = []
for items in generator:
for row in items['data']:
attributes.append({
'attribute_type': self.attribute_type,
'display_name': self.attribute_type.replace('_', ' ').title(),
'value': row['name'],
'active': True,
'source_id': row['id']
})
if self.attribute_is_valid(row):
attributes.append({
'attribute_type': self.attribute_type,
'display_name': self.attribute_type.replace('_', ' ').title(),
'value': row['name'],
'active': True,
'source_id': row['id']
})

return attributes

def attribute_is_valid(self, attribute):
"""
Validate whether attribute is from the same org or not
"""
return attribute['org_id'] == self.workspace.fyle_org_id

def sync(self, sync_after: datetime = None) -> None:
"""
Expand Down
21 changes: 11 additions & 10 deletions connector/fyle_integrations_platform_connector/apis/categories.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@ def sync(self, sync_after: datetime = None):
for items in generator:
category_attributes = []
for category in items['data']:
if category['sub_category'] and category['name'] != category['sub_category']:
category['name'] = '{0} / {1}'.format(category['name'], category['sub_category'])
if self.attribute_is_valid(category):
if category['sub_category'] and category['name'] != category['sub_category']:
category['name'] = '{0} / {1}'.format(category['name'], category['sub_category'])

category_attributes.append({
'attribute_type': self.attribute_type,
'display_name': self.attribute_type.replace('_', ' ').title(),
'value': category['name'],
'source_id': category['id'],
'active': category['is_enabled'],
'detail': None
})
category_attributes.append({
'attribute_type': self.attribute_type,
'display_name': self.attribute_type.replace('_', ' ').title(),
'value': category['name'],
'source_id': category['id'],
'active': category['is_enabled'],
'detail': None
})

self.bulk_create_or_update_expense_attributes(category_attributes, True)
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,23 @@ def sync(self):
card_attributes = []
unique_card_numbers = []
for card in items['data']:
value = '{} - {}'.format(
card['bank_name'],
card['card_number'][-6:].replace('-', '')
)
if self.attribute_is_valid(card):
value = '{} - {}'.format(
card['bank_name'],
card['card_number'][-6:].replace('-', '')
)

if value not in unique_card_numbers:
unique_card_numbers.append(value)
card_attributes.append({
'attribute_type': self.attribute_type,
'display_name': self.attribute_type.replace('_', ' ').title(),
'value': value,
'source_id': card['id'],
'active': None,
'detail': {
'cardholder_name': card['cardholder_name']
}
})
if value not in unique_card_numbers:
unique_card_numbers.append(value)
card_attributes.append({
'attribute_type': self.attribute_type,
'display_name': self.attribute_type.replace('_', ' ').title(),
'value': value,
'source_id': card['id'],
'active': None,
'detail': {
'cardholder_name': card['cardholder_name']
}
})

self.bulk_create_or_update_expense_attributes(card_attributes, True)
33 changes: 17 additions & 16 deletions connector/fyle_integrations_platform_connector/apis/employees.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,22 @@ def sync(self):
for items in generator:
employee_attributes = []
for employee in items['data']:
employee_attributes.append({
'attribute_type': self.attribute_type,
'display_name': self.attribute_type.replace('_', ' ').title(),
'value': employee['user']['email'],
'source_id': employee['id'],
'active': True,
'detail': {
'user_id': employee['user_id'],
'employee_code': employee['code'],
'full_name': employee['user']['full_name'],
'location': employee['location'],
'department': employee['department']['name'] if employee['department'] else None,
'department_id': employee['department_id'],
'department_code': employee['department']['code'] if employee['department'] else None
}
})
if self.attribute_is_valid(employee):
employee_attributes.append({
'attribute_type': self.attribute_type,
'display_name': self.attribute_type.replace('_', ' ').title(),
'value': employee['user']['email'],
'source_id': employee['id'],
'active': True,
'detail': {
'user_id': employee['user_id'],
'employee_code': employee['code'],
'full_name': employee['user']['full_name'],
'location': employee['location'],
'department': employee['department']['name'] if employee['department'] else None,
'department_id': employee['department_id'],
'department_code': employee['department']['code'] if employee['department'] else None
}
})

self.bulk_create_or_update_expense_attributes(employee_attributes, True)
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,26 @@ def sync(self):

for items in generator:
for row in items['data']:
attributes = []
count = 1
attribute_type = row['field_name'].upper().replace(' ', '_')
for option in row['options']:
attributes.append({
'attribute_type': attribute_type,
'display_name': row['field_name'],
'value': option,
'active': True,
'source_id': 'expense_custom_field.{}.{}'.format(row['field_name'].lower(), count),
'detail': {
'custom_field_id': row['id'],
'placeholder': row['placeholder'],
'is_mandatory': row['is_mandatory']
}
})
count = count + 1
self.attribute_type = attribute_type
self.bulk_create_or_update_expense_attributes(attributes, True)
if self.attribute_is_valid(row):
attributes = []
count = 1
attribute_type = row['field_name'].upper().replace(' ', '_')
for option in row['options']:
attributes.append({
'attribute_type': attribute_type,
'display_name': row['field_name'],
'value': option,
'active': True,
'source_id': 'expense_custom_field.{}.{}'.format(row['field_name'].lower(), count),
'detail': {
'custom_field_id': row['id'],
'placeholder': row['placeholder'],
'is_mandatory': row['is_mandatory']
}
})
count = count + 1
self.attribute_type = attribute_type
self.bulk_create_or_update_expense_attributes(attributes, True)

def list_all(self):
"""
Expand Down
84 changes: 42 additions & 42 deletions connector/fyle_integrations_platform_connector/apis/expenses.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,48 +145,48 @@ def __construct_expenses_objects(self, expenses: List[dict]) -> List[dict]:

matched_transaction = expense['matched_corporate_card_transactions'][0] if expense['matched_corporate_card_transactions'] else None
posted_at = matched_transaction['posted_at'] if matched_transaction and 'posted_at' in matched_transaction else None

objects.append({
'id': expense['id'],
'employee_email': expense['employee']['user']['email'],
'employee_name': expense['employee']['user']['full_name'],
'category': expense['category']['name'],
'sub_category': expense['category']['sub_category'],
'project': project_name,
'project_id': expense['project']['id'] if expense['project'] else None,
'expense_number': expense['seq_num'],
'org_id': expense['org_id'],
'claim_number': expense['report']['seq_num'] if expense['report'] else None,
'amount': expense['amount'],
'tax_amount': expense['tax_amount'],
'tax_group_id': expense['tax_group_id'],
'settled_at': expense['last_settled_at'],
'currency': expense['currency'],
'foreign_amount': expense['foreign_amount'],
'foreign_currency': expense['foreign_currency'],
'settlement_id': expense['report']['settlement_id'] if expense['report'] else None,
'reimbursable': expense['is_reimbursable'],
'billable': expense['is_billable'],
'state': expense['state'],
'vendor': expense['merchant'],
'cost_center': expense['cost_center']['name'] if expense['cost_center'] else None,
'corporate_card_id': expense['matched_corporate_card_transactions'][0]['corporate_card_id'] \
if expense['matched_corporate_card_transactions'] else None,
'purpose': expense['purpose'],
'report_id': expense['report_id'],
'report_title': expense['report']['title'],
'file_ids': expense['file_ids'],
'spent_at': self.__format_date(expense['spent_at']),
'approved_at': self.__format_date(expense['report']['last_approved_at']) if expense['report'] else None,
'posted_at': self.__format_date(posted_at) if posted_at else None,
'expense_created_at': expense['created_at'],
'expense_updated_at': expense['updated_at'],
'source_account_type': expense['source_account']['type'],
'verified_at': self.__format_date(expense['last_verified_at']),
'custom_properties': custom_properties,
'payment_number': expense['report']['reimbursement_seq_num'] if 'reimbursement_seq_num' in expense['report'] and \
expense['report']['reimbursement_seq_num'] else None
})
if self.attribute_is_valid(expense):
objects.append({
'id': expense['id'],
'employee_email': expense['employee']['user']['email'],
'employee_name': expense['employee']['user']['full_name'],
'category': expense['category']['name'],
'sub_category': expense['category']['sub_category'],
'project': project_name,
'project_id': expense['project']['id'] if expense['project'] else None,
'expense_number': expense['seq_num'],
'org_id': expense['org_id'],
'claim_number': expense['report']['seq_num'] if expense['report'] else None,
'amount': expense['amount'],
'tax_amount': expense['tax_amount'],
'tax_group_id': expense['tax_group_id'],
'settled_at': expense['last_settled_at'],
'currency': expense['currency'],
'foreign_amount': expense['foreign_amount'],
'foreign_currency': expense['foreign_currency'],
'settlement_id': expense['report']['settlement_id'] if expense['report'] else None,
'reimbursable': expense['is_reimbursable'],
'billable': expense['is_billable'],
'state': expense['state'],
'vendor': expense['merchant'],
'cost_center': expense['cost_center']['name'] if expense['cost_center'] else None,
'corporate_card_id': expense['matched_corporate_card_transactions'][0]['corporate_card_id'] \
if expense['matched_corporate_card_transactions'] else None,
'purpose': expense['purpose'],
'report_id': expense['report_id'],
'report_title': expense['report']['title'],
'file_ids': expense['file_ids'],
'spent_at': self.__format_date(expense['spent_at']),
'approved_at': self.__format_date(expense['report']['last_approved_at']) if expense['report'] else None,
'posted_at': self.__format_date(posted_at) if posted_at else None,
'expense_created_at': expense['created_at'],
'expense_updated_at': expense['updated_at'],
'source_account_type': expense['source_account']['type'],
'verified_at': self.__format_date(expense['last_verified_at']),
'custom_properties': custom_properties,
'payment_number': expense['report']['reimbursement_seq_num'] if 'reimbursement_seq_num' in expense['report'] and \
expense['report']['reimbursement_seq_num'] else None
})

return objects

Expand Down
19 changes: 10 additions & 9 deletions connector/fyle_integrations_platform_connector/apis/projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@ def sync(self, sync_after: datetime = None):
for items in generator:
project_attributes = []
for project in items['data']:
if project['sub_project']:
project['name'] = '{0} / {1}'.format(project['name'], project['sub_project'])
if self.attribute_is_valid(project):
if project['sub_project']:
project['name'] = '{0} / {1}'.format(project['name'], project['sub_project'])

project_attributes.append({
'attribute_type': self.attribute_type,
'display_name': self.attribute_type.replace('_', ' ').title(),
'value': project['name'],
'active': project['is_enabled'],
'source_id': project['id']
})
project_attributes.append({
'attribute_type': self.attribute_type,
'display_name': self.attribute_type.replace('_', ' ').title(),
'value': project['name'],
'active': project['is_enabled'],
'source_id': project['id']
})

self.bulk_create_or_update_expense_attributes(project_attributes, True)
21 changes: 11 additions & 10 deletions connector/fyle_integrations_platform_connector/apis/tax_groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@ def sync(self, sync_after: datetime = None):
for items in generator:
tax_attributes = []
for tax_group in items['data']:
tax_attributes.append({
'attribute_type': 'TAX_GROUP',
'display_name': 'Tax Group',
'value': tax_group['name'],
'source_id': tax_group['id'],
'active': tax_group['is_enabled'],
'detail': {
'tax_rate': tax_group['percentage']
}
})
if self.attribute_is_valid(tax_group):
tax_attributes.append({
'attribute_type': 'TAX_GROUP',
'display_name': 'Tax Group',
'value': tax_group['name'],
'source_id': tax_group['id'],
'active': tax_group['is_enabled'],
'detail': {
'tax_rate': tax_group['percentage']
}
})

self.bulk_create_or_update_expense_attributes(tax_attributes, True)
2 changes: 1 addition & 1 deletion connector/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name='fyle-integrations-platform-connector',
version='1.31.0',
version='1.32.0',
author='Shwetabh Kumar',
author_email='[email protected]',
description='A common platform connector for all the Fyle Integrations to interact with Fyle Platform APIs',
Expand Down

0 comments on commit 22c0ac3

Please sign in to comment.