Skip to content

Commit

Permalink
handle delete case for categories and projects in expense attributes (#…
Browse files Browse the repository at this point in the history
…105)

* handle delete case for categories and projects in expense attributes

* fix lint

* fix lint
  • Loading branch information
ruuushhh authored Mar 21, 2024
1 parent adecfb7 commit 7a94be0
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Generated by Django 3.2.14 on 2024-03-20 11:13

import django.contrib.postgres.fields
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('workspaces', '0038_alter_workspace_onboarding_state'),
('fyle_accounting_mappings', '0024_auto_20230922_0819'),
]

operations = [
migrations.CreateModel(
name='ExpenseAttributesDeletionCache',
fields=[
('id', models.AutoField(primary_key=True, serialize=False)),
('category_ids', django.contrib.postgres.fields.ArrayField(base_field=models.CharField(max_length=255), default=[], size=None)),
('project_ids', django.contrib.postgres.fields.ArrayField(base_field=models.CharField(max_length=255), default=[], size=None)),
('workspace', models.OneToOneField(help_text='Reference to Workspace model', on_delete=django.db.models.deletion.PROTECT, to='workspaces.workspace')),
],
options={
'db_table': 'expense_attributes_deletion_cache',
},
),
]
47 changes: 47 additions & 0 deletions fyle_accounting_mappings/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from datetime import datetime
from django.db import models, transaction
from django.db.models import JSONField
from django.contrib.postgres.fields import ArrayField

from .exceptions import BulkError
from .utils import assert_valid
Expand Down Expand Up @@ -106,6 +107,16 @@ def get_existing_source_ids(destination_type: str, workspace_id: int):
return existing_source_ids


class ExpenseAttributesDeletionCache(models.Model):
id = models.AutoField(primary_key=True)
category_ids = ArrayField(default=[], base_field=models.CharField(max_length=255))
project_ids = ArrayField(default=[], base_field=models.CharField(max_length=255))
workspace = models.OneToOneField(Workspace, on_delete=models.PROTECT, help_text='Reference to Workspace model')

class Meta:
db_table = 'expense_attributes_deletion_cache'


class ExpenseAttribute(models.Model):
"""
Fyle Expense Attributes
Expand Down Expand Up @@ -146,6 +157,42 @@ def create_or_update_expense_attribute(attribute: Dict, workspace_id):
)
return expense_attribute

@staticmethod
def bulk_update_deleted_expense_attributes(attribute_type: str, workspace_id: int):
"""
Bulk update deleted expense attributes
:param attribute_type: Attribute type
:param workspace_id: Workspace Id
"""
expense_attributes_deletion_cache = ExpenseAttributesDeletionCache.objects.get(workspace_id=workspace_id)
attributes_to_be_updated = []

if attribute_type == 'CATEGORY':
deleted_attributes = ExpenseAttribute.objects.filter(
attribute_type=attribute_type, workspace_id=workspace_id, active=True
).exclude(source_id__in=expense_attributes_deletion_cache.category_ids)
expense_attributes_deletion_cache.category_ids = []
expense_attributes_deletion_cache.save()
else:
deleted_attributes = ExpenseAttribute.objects.filter(
attribute_type=attribute_type, workspace_id=workspace_id, active=True
).exclude(source_id__in=expense_attributes_deletion_cache.project_ids)
expense_attributes_deletion_cache.project_ids = []
expense_attributes_deletion_cache.save()

for attribute in deleted_attributes:
attributes_to_be_updated.append(
ExpenseAttribute(
id=attribute.id,
active=False,
updated_at=datetime.now()
)
)

if attributes_to_be_updated:
ExpenseAttribute.objects.bulk_update(
attributes_to_be_updated, fields=['active', 'updated_at'], batch_size=50)

@staticmethod
def bulk_create_or_update_expense_attributes(
attributes: List[Dict], attribute_type: str, workspace_id: int, update: bool = False):
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.31.5',
version='1.32.0',
author='Shwetabh Kumar',
author_email='[email protected]',
description='Django application to store the fyle accounting mappings in a generic manner',
Expand Down

0 comments on commit 7a94be0

Please sign in to comment.