Skip to content

Commit

Permalink
fix: Mappings View for Activity (#123)
Browse files Browse the repository at this point in the history
* fix: Mappings View for Activity

* add else condition

* Add fix

* Update version

* Fix

---------

Co-authored-by: GitHub Actions <integrations@fylehq.com>
  • Loading branch information
ruuushhh and GitHub Actions authored Oct 1, 2024
1 parent 5ea10da commit 34fe80a
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 19 deletions.
69 changes: 51 additions & 18 deletions fyle_accounting_mappings/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,31 +277,47 @@ def get_queryset(self):
source_type = self.request.query_params.get('source_type')
destination_type = self.request.query_params.get('destination_type', '')

# Process the 'mapped' parameter
if mapped and mapped.lower() == 'false':
mapped = False
elif mapped and mapped.lower() == 'true':
mapped = True
else:
mapped = None

filters = {
'workspace_id' : self.kwargs['workspace_id'],
'attribute_type': source_type,
}
# Prepare filters for the ExpenseAttribute
base_filters = Q(workspace_id=self.kwargs['workspace_id']) & Q(attribute_type=source_type)

# Add 'active' filter if the source_type is 'PROJECT' or 'CATEGORY'
if source_type in ('PROJECT', 'CATEGORY'):
filters['active'] = True
base_filters &= Q(active=True)

# Handle Activity attribute if attribute mapping is present then show mappings else don't return Activity attribute
if source_type == 'CATEGORY':
activity_mapping = Mapping.objects.filter(
source__value='Activity', source_type='CATEGORY', workspace_id=self.kwargs['workspace_id']).first()
activity_attribute = ExpenseAttribute.objects.filter(
attribute_type='CATEGORY', value='Activity', workspace_id=self.kwargs['workspace_id'], active=True).first()

# Adjust the filters if 'Activity' attribute exists but not mapped
if activity_attribute and not activity_mapping:
base_filters &= ~Q(value='Activity')

# Handle the 'mapped' parameter
param = None
if mapped:
if mapped is True:
param = Q(mapping__destination_type=destination_type)
elif mapped is False:
param = ~Q(mapping__destination_type=destination_type)
else:
return ExpenseAttribute.objects.filter(Q(**filters)).order_by('value')
final_filter = Q(**filters)
return ExpenseAttribute.objects.filter(base_filters).order_by('value')

# Combine the base filters with the param (if any)
final_filter = base_filters
if param:
final_filter = final_filter & param
final_filter &= param

# Return the final queryset
return ExpenseAttribute.objects.filter(final_filter).order_by('value')


Expand All @@ -317,42 +333,59 @@ def get_queryset(self):
mapped = self.request.query_params.get('mapped')
destination_type = self.request.query_params.get('destination_type', '')

# Process the 'mapped' parameter
if mapped and mapped.lower() == 'false':
mapped = False
elif mapped and mapped.lower() == 'true':
mapped = True
else:
mapped = None

# Prepare filters based on destination_type
filters = {}

if destination_type == 'ACCOUNT':
filters['destination_account__attribute_type'] = destination_type
else:
filters['destination_expense_head__attribute_type'] = destination_type

# Get source categories
source_categories = CategoryMapping.objects.filter(
**filters,
source_category__active=True,
workspace_id=self.kwargs['workspace_id'],
).values_list('source_category_id', flat=True)

filters = {
'workspace_id' : self.kwargs['workspace_id'],
'attribute_type': 'CATEGORY',
'active': True
}
# Prepare filters for ExpenseAttribute
base_filters = Q(workspace_id=self.kwargs['workspace_id']) & \
Q(attribute_type='CATEGORY') & \
Q(active=True)

# Get the 'Activity' mapping and attribute
activity_mapping = CategoryMapping.objects.filter(
source_category__value='Activity', workspace_id=self.kwargs['workspace_id']).first()
activity_attribute = ExpenseAttribute.objects.filter(
attribute_type='CATEGORY', value='Activity', workspace_id=self.kwargs['workspace_id'], active=True).first()

# Adjust the filters if 'Activity' attribute exists but not mapped
if activity_attribute and not activity_mapping:
base_filters &= ~Q(value='Activity')

# Handle the mapped parameter
param = None
if mapped:
if mapped is True:
param = Q(categorymapping__source_category_id__in=source_categories)
elif mapped is False:
param = ~Q(categorymapping__source_category_id__in=source_categories)
else:
return ExpenseAttribute.objects.filter(Q(**filters)).order_by('value')
final_filter = Q(**filters)
return ExpenseAttribute.objects.filter(base_filters).order_by('value')

# Combine the base filters with the param (if any)
final_filter = base_filters
if param:
final_filter = final_filter & param
final_filter &= param

# Return the final queryset
return ExpenseAttribute.objects.filter(final_filter).order_by('value')


Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

setuptools.setup(
name='fyle-accounting-mappings',
version='1.34.7',
version='1.34.8',
author='Shwetabh Kumar',
author_email='shwetabh.kumar@fyle.in',
description='Django application to store the fyle accounting mappings in a generic manner',
Expand Down

0 comments on commit 34fe80a

Please sign in to comment.