Skip to content

Commit

Permalink
Fix category sync (#175)
Browse files Browse the repository at this point in the history
* Delete purchase_invoice, line_items on failed exports from hh2, type-check for current_state

* add loggers and fix category sync

* moved category_object flow inside if
  • Loading branch information
Hrishabh17 authored May 27, 2024
1 parent 312eb99 commit cc4da46
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 20 deletions.
46 changes: 27 additions & 19 deletions apps/sage300/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
from typing import List, Dict
from django.db import models

Expand All @@ -12,6 +13,9 @@
)
from sage_desktop_sdk.core.schema.read_only import Category

logger = logging.getLogger(__name__)
logger.level = logging.INFO


class CostCategory(BaseForeignWorkspaceModel):
"""
Expand Down Expand Up @@ -78,25 +82,29 @@ def bulk_create_or_update(categories: List[Dict], workspace_id: int):
for category in list_of_categories:
job_name = job_name_mapping.get(category.job_id)
cost_code_name = cost_code_name_mapping.get(category.cost_code_id)
category_object = CostCategory(
job_id=category.job_id,
job_name=job_name,
cost_code_id=category.cost_code_id,
cost_code_name=" ".join(cost_code_name.split()),
name=" ".join(category.name.split()),
status=category.is_active,
cost_category_id=category.id,
workspace_id=workspace_id
)

if category.id not in existing_cost_type_record_numbers:
cost_category_to_be_created.append(category_object)

elif category.id in primary_key_map.keys() and (
category.name != primary_key_map[category.id]['name'] or category.is_active != primary_key_map[category.id]['status']
):
category_object.id = primary_key_map[category.id]['cost_category_id']
cost_category_to_be_updated.append(category_object)
if job_name and cost_code_name:
category_object = CostCategory(
job_id=category.job_id,
job_name=job_name,
cost_code_id=category.cost_code_id,
cost_code_name=" ".join(cost_code_name.split()),
name=" ".join(category.name.split()),
status=category.is_active,
cost_category_id=category.id,
workspace_id=workspace_id
)

if category.id not in existing_cost_type_record_numbers:
cost_category_to_be_created.append(category_object)

elif category.id in primary_key_map.keys() and (
category.name != primary_key_map[category.id]['name'] or category.is_active != primary_key_map[category.id]['status']
):
category_object.id = primary_key_map[category.id]['cost_category_id']
cost_category_to_be_updated.append(category_object)

else:
logger.error(f"Job name with job_id {category.job_id} or cost code name with cost_code_id {category.cost_code_id} not found in workspace {workspace_id}")

if cost_category_to_be_created:
CostCategory.objects.bulk_create(cost_category_to_be_created, batch_size=2000)
Expand Down
2 changes: 1 addition & 1 deletion apps/sage300/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def _remove_credit_card_vendors(self):
detail__type='Credit Card'
)
vendor_count = credit_card_vendor.count()
logger.info(f'Deleting {vendor_count} credit card vendors')
logger.info(f'Deleting {vendor_count} credit card vendors from workspace_id {self.workspace_id}')
credit_card_vendor.delete()

def _sync_data(self, data_gen, attribute_type, display_name, workspace_id, field_names, is_generator: bool = True, vendor_type_mapping = None):
Expand Down

0 comments on commit cc4da46

Please sign in to comment.