Skip to content

Commit

Permalink
Fix: Replacing all Fyle Strings with enums (#277)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shwetabhk authored Nov 29, 2023
1 parent 0721b39 commit 2fe8c4c
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 33 deletions.
24 changes: 13 additions & 11 deletions apps/fyle/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@
from fyle_accounting_mappings.models import ExpenseAttribute
from fyle_integrations_platform_connector import PlatformConnector

from apps.fyle.models import ExpenseGroup
from apps.workspaces.models import FyleCredential, Workspace, WorkspaceGeneralSettings

from .enums import FyleAttributeEnum, FundSourceEnum
from .models import ExpenseGroup


def get_expense_field(workspace_id):
default_attributes = [
"EMPLOYEE",
"CATEGORY",
"PROJECT",
"COST_CENTER",
"CORPORATE_CARD",
"TAX_GROUP",
FyleAttributeEnum.EMPLOYEE,
FyleAttributeEnum.CATEGORY,
FyleAttributeEnum.PROJECT,
FyleAttributeEnum.COST_CENTER,
FyleAttributeEnum.CORPORATE_CARD,
FyleAttributeEnum.TAX_GROUP
]
attributes = (
ExpenseAttribute.objects.filter(
Expand All @@ -26,8 +28,8 @@ def get_expense_field(workspace_id):
)

expense_fields = [
{"attribute_type": "COST_CENTER", "display_name": "Cost Center"},
{"attribute_type": "PROJECT", "display_name": "Project"},
{"attribute_type": FyleAttributeEnum.COST_CENTER, "display_name": FyleAttributeEnum.COST_CENTER_DISPLAY},
{"attribute_type": FyleAttributeEnum.PROJECT, "display_name": FyleAttributeEnum.PROJECT_DISPLAY}
]

for attribute in attributes:
Expand Down Expand Up @@ -67,9 +69,9 @@ def exportable_expense_group(workspace_id):
fund_source = []

if configuration.reimbursable_expenses_object:
fund_source.append("PERSONAL")
fund_source.append(FundSourceEnum.PERSONAL)
if configuration.corporate_credit_card_expenses_object:
fund_source.append("CCC")
fund_source.append(FundSourceEnum.CCC)

expense_group_ids = ExpenseGroup.objects.filter(
workspace_id=workspace_id, exported_at__isnull=True, fund_source__in=fund_source
Expand Down
53 changes: 53 additions & 0 deletions apps/fyle/enums.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""
This file contains all the enums used in the Fyle app
"""


class FundSourceEnum:
"""
Enum for Fund Source
"""
PERSONAL = 'PERSONAL'
CCC = 'CCC'


class FyleAttributeEnum:
"""
Enum for Fyle Attributes
"""
CATEGORY = 'CATEGORY'
CATEGORY_DISPLAY = 'Category'

PROJECT = 'PROJECT'
PROJECT_DISPLAY = 'Project'

COST_CENTER = 'COST_CENTER'
COST_CENTER_DISPLAY = 'Cost Center'

CORPORATE_CARD = 'CORPORATE_CARD'
CORPORATE_CARD_DISPLAY = 'Corporate Card'

TAX_GROUP = 'TAX_GROUP'
TAX_GROUP_DISPLAY = 'Tax Group'

EMPLOYEE = 'EMPLOYEE'
EMPLOYEE_DISPLAY = 'Employee'


class ExpenseStateEnum:
"""
Enum for Expense State
"""
APPROVED = 'APPROVED'
PAYMENT_PROCESSING = 'PAYMENT_PROCESSING'
PAID = 'PAID'


class PlatformExpensesEnum:
"""
Enum for Platform Expenses
"""
PERSONAL_CASH_ACCOUNT = 'PERSONAL_CASH_ACCOUNT'
PERSONAL_CORPORATE_CREDIT_CARD_ACCOUNT = 'PERSONAL_CORPORATE_CREDIT_CARD_ACCOUNT'
REIMBURSEMENT_COMPLETE = 'COMPLETE'
REIMBURSEMENT_PENDING = 'PENDING'
19 changes: 11 additions & 8 deletions apps/fyle/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

from apps.workspaces.models import Workspace

from .enums import FundSourceEnum, PlatformExpensesEnum, ExpenseStateEnum


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

Expand Down Expand Up @@ -57,8 +60,8 @@
}

SOURCE_ACCOUNT_MAP = {
"PERSONAL_CASH_ACCOUNT": "PERSONAL",
"PERSONAL_CORPORATE_CREDIT_CARD_ACCOUNT": "CCC",
PlatformExpensesEnum.PERSONAL_CASH_ACCOUNT: FundSourceEnum.PERSONAL,
PlatformExpensesEnum.PERSONAL_CORPORATE_CREDIT_CARD_ACCOUNT: FundSourceEnum.CCC,
}


Expand Down Expand Up @@ -236,17 +239,17 @@ def get_default_ccc_expense_group_fields():


def get_default_expense_state():
return "PAYMENT_PROCESSING"
return ExpenseStateEnum.PAYMENT_PROCESSING


def get_default_ccc_expense_state():
return "PAID"
return ExpenseStateEnum.PAID


CCC_EXPENSE_STATE = (
("APPROVED", "APPROVED"),
("PAID", "PAID"),
("PAYMENT_PROCESSING", "PAYMENT_PROCESSING"),
(ExpenseStateEnum.APPROVED, ExpenseStateEnum.APPROVED),
(ExpenseStateEnum.PAYMENT_PROCESSING, ExpenseStateEnum.PAYMENT_PROCESSING),
(ExpenseStateEnum.PAID, ExpenseStateEnum.PAID)
)


Expand Down Expand Up @@ -597,7 +600,7 @@ def create_or_update_reimbursement_objects(

for reimbursement in reimbursements:
reimbursement["state"] = (
"COMPLETE" if reimbursement["is_paid"] else "PENDING"
PlatformExpensesEnum.REIMBURSEMENT_COMPLETE if reimbursement["is_paid"] else PlatformExpensesEnum.REIMBURSEMENT_PENDING
)
if reimbursement["id"] not in existing_reimbursement_ids:
attributes_to_be_created.append(
Expand Down
32 changes: 18 additions & 14 deletions apps/fyle/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@
from fyle.platform.exceptions import InvalidTokenError as FyleInvalidTokenError
from fyle_integrations_platform_connector import PlatformConnector

from apps.fyle.models import Expense, ExpenseGroup, ExpenseGroupSettings
from apps.tasks.models import TaskLog
from apps.workspaces.models import FyleCredential, Workspace, WorkspaceGeneralSettings

from .models import Expense, ExpenseGroup, ExpenseGroupSettings
from .enums import FundSourceEnum, PlatformExpensesEnum, ExpenseStateEnum


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


SOURCE_ACCOUNT_MAP = {
"PERSONAL": "PERSONAL_CASH_ACCOUNT",
"CCC": "PERSONAL_CORPORATE_CREDIT_CARD_ACCOUNT",
FundSourceEnum.PERSONAL: PlatformExpensesEnum.PERSONAL_CASH_ACCOUNT,
FundSourceEnum.CCC: PlatformExpensesEnum.PERSONAL_CORPORATE_CREDIT_CARD_ACCOUNT
}


Expand All @@ -31,9 +35,9 @@ def get_task_log_and_fund_source(workspace_id: int):

fund_source = []
if general_settings.reimbursable_expenses_object:
fund_source.append("PERSONAL")
fund_source.append(FundSourceEnum.PERSONAL)
if general_settings.corporate_credit_card_expenses_object is not None:
fund_source.append("CCC")
fund_source.append(FundSourceEnum.CCC)

return task_log, fund_source

Expand Down Expand Up @@ -77,18 +81,18 @@ def async_create_expense_groups(
expenses = []
reimbursable_expenses_count = 0

if "PERSONAL" in fund_source:
if FundSourceEnum.PERSONAL in fund_source:
expenses.extend(
platform.expenses.get(
source_account_type=["PERSONAL_CASH_ACCOUNT"],
source_account_type=[SOURCE_ACCOUNT_MAP[FundSourceEnum.PERSONAL]],
state=expense_group_settings.reimbursable_expense_state,
settled_at=last_synced_at
if expense_group_settings.reimbursable_expense_state
== "PAYMENT_PROCESSING"
== ExpenseStateEnum.PAYMENT_PROCESSING
else None,
filter_credit_expenses=True,
last_paid_at=last_synced_at
if expense_group_settings.reimbursable_expense_state == "PAID"
if expense_group_settings.reimbursable_expense_state == ExpenseStateEnum.PAID
else None,
)
)
Expand All @@ -97,21 +101,21 @@ def async_create_expense_groups(
workspace.last_synced_at = datetime.now()
reimbursable_expenses_count += len(expenses)

if "CCC" in fund_source:
if FundSourceEnum.CCC in fund_source:
expenses.extend(
platform.expenses.get(
source_account_type=["PERSONAL_CORPORATE_CREDIT_CARD_ACCOUNT"],
source_account_type=[SOURCE_ACCOUNT_MAP[FundSourceEnum.CCC]],
state=expense_group_settings.ccc_expense_state,
settled_at=ccc_last_synced_at
if expense_group_settings.ccc_expense_state
== "PAYMENT_PROCESSING"
== ExpenseStateEnum.PAYMENT_PROCESSING
else None,
approved_at=ccc_last_synced_at
if expense_group_settings.ccc_expense_state == "APPROVED"
if expense_group_settings.ccc_expense_state == ExpenseStateEnum.APPROVED
else None,
filter_credit_expenses=filter_credit_expenses,
last_paid_at=ccc_last_synced_at
if expense_group_settings.ccc_expense_state == "PAID"
if expense_group_settings.ccc_expense_state == ExpenseStateEnum.PAID
else None,
)
)
Expand Down

0 comments on commit 2fe8c4c

Please sign in to comment.