Skip to content

Commit

Permalink
Add missing workspace_ids to queries (#190)
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 missing workspace_ids to queries

* Fix failing tests

---------

Co-authored-by: Hrishabh Tiwari <[email protected]>
  • Loading branch information
ashwin1111 and Hrishabh17 authored Jun 12, 2024
1 parent 5d7dfc7 commit 21bc4ac
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 19 deletions.
6 changes: 3 additions & 3 deletions apps/sage300/exports/base_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def get_job_id(accounting_export: AccountingExport, expense: Expense):
elif job_settings.source_field == 'COST_CENTER':
source_value = expense.cost_center
else:
attribute = ExpenseAttribute.objects.filter(attribute_type=job_settings.source_field).first()
attribute = ExpenseAttribute.objects.filter(attribute_type=job_settings.source_field, workspace_id=accounting_export.workspace_id).first()
source_value = expense.custom_properties.get(attribute.display_name, None)

# Check for a mapping based on the source value
Expand Down Expand Up @@ -243,7 +243,7 @@ def get_standard_category_id(accounting_export: AccountingExport, expense: Expen

if standard_category_setting:
# Retrieve the attribute corresponding to the source field
attribute = ExpenseAttribute.objects.filter(attribute_type=standard_category_setting.source_field).first()
attribute = ExpenseAttribute.objects.filter(attribute_type=standard_category_setting.source_field, workspace_id=accounting_export.workspace_id).first()

# Determine the source value based on the configured source field
source_value = expense.custom_properties.get(attribute.display_name, None)
Expand Down Expand Up @@ -283,7 +283,7 @@ def get_standard_cost_code_id(accounting_export: AccountingExport, expense: Expe

if standard_cost_code_setting:
# Retrieve the attribute corresponding to the source field
attribute = ExpenseAttribute.objects.filter(attribute_type=standard_cost_code_setting.source_field).first()
attribute = ExpenseAttribute.objects.filter(attribute_type=standard_cost_code_setting.source_field, workspace_id=accounting_export.workspace_id).first()

# Determine the source value based on the configured source field
source_value = expense.custom_properties.get(attribute.display_name, None)
Expand Down
10 changes: 4 additions & 6 deletions apps/sage300/exports/purchase_invoice/queues.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,20 @@ def check_accounting_export_and_start_import(workspace_id: int, accounting_expor
"""
Check accounting export group and start export
"""

# fyle_credentials = FyleCredential.objects.filter(workspace_id=workspace_id).first()
fyle_credentials = FyleCredential.objects.filter(workspace_id=workspace_id).first()

accounting_exports = AccountingExport.objects.filter(
~Q(status__in=['IN_PROGRESS', 'COMPLETE', 'EXPORT_QUEUED']),
~Q(status__in=['ENQUEUED', 'IN_PROGRESS', 'COMPLETE', 'EXPORT_QUEUED']),
workspace_id=workspace_id, id__in=accounting_export_ids, purchaseinvoice__id__isnull=True,
exported_at__isnull=True
).all()

chain = Chain()

# Todo: uncomment this later
# chain.append('apps.fyle.helpers.sync_dimensions', fyle_credentials)
chain.append('apps.fyle.helpers.sync_dimensions', fyle_credentials)

for index, accounting_export_group in enumerate(accounting_exports):
accounting_export, _ = AccountingExport.objects.update_or_create(
accounting_export, _ = AccountingExport.objects.get_or_create(
workspace_id=accounting_export_group.workspace_id,
id=accounting_export_group.id,
defaults={
Expand Down
4 changes: 0 additions & 4 deletions apps/sage300/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from django.utils.module_loading import import_string

from apps.workspaces.models import Workspace, Sage300Credential, FyleCredential
from apps.mappings.models import Version
from fyle_accounting_mappings.models import ExpenseAttribute
from fyle_integrations_platform_connector import PlatformConnector
from apps.sage300.models import CostCategory
Expand Down Expand Up @@ -50,9 +49,6 @@ def sync_dimensions(sage300_credential: Sage300Credential, workspace_id: int) ->
This function syncs dimensions like accounts, vendors, commitments, jobs, categories, and cost codes.
"""

Version.objects.update_or_create(workspace_id=workspace_id)

# Initialize the Sage 300 connection using the provided credentials and workspace ID
sage300_connection = import_string('apps.sage300.utils.SageDesktopConnector')(sage300_credential, workspace_id)

Expand Down
2 changes: 2 additions & 0 deletions apps/workspaces/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
AdvancedSetting
)
from apps.accounting_exports.models import AccountingExportSummary
from apps.mappings.models import Version
from apps.users.models import User
from apps.fyle.helpers import get_cluster_domain
from apps.workspaces.triggers import ImportSettingsTrigger, AdvancedSettingsTriggers
Expand Down Expand Up @@ -70,6 +71,7 @@ def create(self, validated_data):
name=name,
org_id=org_id,
)
Version.objects.create(workspace_id=workspace.id)

workspace.user.add(User.objects.get(user_id=user))

Expand Down
9 changes: 4 additions & 5 deletions apps/workspaces/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def run_import_export(workspace_id: int, export_mode = None):
accounting_summary.last_exported_at = last_exported_at
accounting_summary.export_mode = export_mode or 'MANUAL'

if advance_settings:
if advance_settings and advance_settings.schedule_is_enabled:
accounting_summary.next_export_at = last_exported_at + timedelta(hours=advance_settings.interval_hours)

accounting_summary.save()
Expand Down Expand Up @@ -132,8 +132,7 @@ def export_to_sage300(workspace_id: int):
# Retrieve export settings for the given workspace
export_settings = ExportSetting.objects.get(workspace_id=workspace_id)

# Update or create an AccountingExportSummary for the workspace
accounting_summary, _ = AccountingExportSummary.objects.update_or_create(workspace_id=workspace_id)
accounting_summary = AccountingExportSummary.objects.get(workspace_id=workspace_id)
advance_settings = AdvancedSetting.objects.filter(workspace_id=workspace_id).first()

# Set the timestamp for the last export
Expand All @@ -152,7 +151,7 @@ def export_to_sage300(workspace_id: int):
if export_settings.reimbursable_expenses_export_type:
# Get IDs of unreexported accounting exports for personal fund source
accounting_export_ids = AccountingExport.objects.filter(
fund_source='PERSONAL', exported_at__isnull=True).values_list('id', flat=True)
fund_source='PERSONAL', exported_at__isnull=True, workspace_id=workspace_id).values_list('id', flat=True)

if len(accounting_export_ids):
# Set the flag indicating expenses are exported
Expand All @@ -165,7 +164,7 @@ def export_to_sage300(workspace_id: int):
if export_settings.credit_card_expense_export_type:
# Get IDs of unreexported accounting exports for credit card fund source
accounting_export_ids = AccountingExport.objects.filter(
fund_source='CCC', exported_at__isnull=True).values_list('id', flat=True)
fund_source='CCC', exported_at__isnull=True, workspace_id=workspace_id).values_list('id', flat=True)

if len(accounting_export_ids):
# Set the flag indicating expenses are exported
Expand Down
1 change: 1 addition & 0 deletions tests/test_workspaces/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ def test_export_to_sage300(
add_accounting_export_expenses
):
workspace_id = 1
AccountingExportSummary.objects.create(workspace_id=workspace_id)
accounting_export = AccountingExport.objects.filter(workspace_id=workspace_id).first()

advanced_settings = AdvancedSetting.objects.get(workspace_id=workspace_id)
Expand Down
4 changes: 3 additions & 1 deletion tests/test_workspaces/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from django.urls import reverse
from fyle_accounting_mappings.models import MappingSetting

from apps.accounting_exports.models import AccountingExport
from apps.accounting_exports.models import AccountingExport, AccountingExportSummary
from apps.workspaces.models import (
Workspace,
Sage300Credential,
Expand Down Expand Up @@ -425,6 +425,8 @@ def test_trigger_export(
"""
Test Export Trigger API
"""
workspace_id = 1
AccountingExportSummary.objects.create(workspace_id=workspace_id)

url = reverse('trigger-exports', kwargs={'workspace_id': 1})
api_client.credentials(HTTP_AUTHORIZATION='Bearer {}'.format(test_connection.access_token))
Expand Down

0 comments on commit 21bc4ac

Please sign in to comment.