Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Sync Inactive Employee Fyle #365

Merged
merged 2 commits into from
May 17, 2024
Merged

Fix: Sync Inactive Employee Fyle #365

merged 2 commits into from
May 17, 2024

Conversation

ruuushhh
Copy link
Contributor

No description provided.

@ruuushhh ruuushhh self-assigned this May 17, 2024
@ruuushhh ruuushhh temporarily deployed to CI Environment May 17, 2024 05:56 — with GitHub Actions Inactive
@ruuushhh ruuushhh temporarily deployed to CI Environment May 17, 2024 05:56 — with GitHub Actions Inactive
Copy link

coderabbitai bot commented May 17, 2024

Walkthrough

The changes enhance the Xero integration by introducing two new functions in apps/xero/tasks.py to manage employee data efficiently. These functions, get_employee_expense_attribute and sync_inactive_employee, focus on retrieving employee expense attributes and synchronizing inactive employees, respectively. They are integrated into the existing create_or_update_employee_mapping function for improved data handling within the Xero system.

Changes

File Change Summary
apps/xero/tasks.py Added get_employee_expense_attribute to retrieve employee expense attributes.
Added sync_inactive_employee to synchronize inactive employees.
Modified create_or_update_employee_mapping to incorporate the new functions for better data management.

In the code where data flows,
New functions sprout like springtime shows.
Employees, active or in rest,
Are mapped and synced with utmost zest.
Xero's realm now gleams and glows! 🌟


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@ruuushhh ruuushhh temporarily deployed to CI Environment May 17, 2024 05:59 — with GitHub Actions Inactive
Copy link

Tests Skipped Failures Errors Time
159 0 💤 0 ❌ 0 🔥 1m 10s ⏱️

1 similar comment
Copy link

Tests Skipped Failures Errors Time
159 0 💤 0 ❌ 0 🔥 1m 10s ⏱️

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Out of diff range and nitpick comments (1)
apps/xero/tasks.py (1)

Line range hint 600-600: Use is None for comparison to None to ensure correct behavior in Python.

- or lineitem.sub_category == None
+ or lineitem.sub_category is None
Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 1455132 and d8ee33e.
Files selected for processing (1)
  • apps/xero/tasks.py (1 hunks)
Additional Context Used
Ruff (1)
apps/xero/tasks.py (1)

600-600: Comparison to None should be cond is None

Additional comments not posted (1)
apps/xero/tasks.py (1)

121-131: The implementation of get_employee_expense_attribute looks correct and efficient.

Copy link

Tests Skipped Failures Errors Time
159 0 💤 0 ❌ 0 🔥 1m 9s ⏱️

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

Out of diff range and nitpick comments (1)
apps/xero/tasks.py (1)

Line range hint 600-600: Use is None for comparison to None.

- lineitem.sub_category == None
+ lineitem.sub_category is None
Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between d8ee33e and ad3a036.
Files selected for processing (1)
  • apps/xero/tasks.py (1 hunks)
Additional Context Used
Ruff (1)
apps/xero/tasks.py (1)

600-600: Comparison to None should be cond is None

Additional comments not posted (1)
apps/xero/tasks.py (1)

121-131: Ensure proper handling of ExpenseAttribute.DoesNotExist exception.

Verification successful

The function get_employee_expense_attribute does not need to handle the ExpenseAttribute.DoesNotExist exception because it uses the first() method, which returns None if no matching record is found. Additionally, the places where this function is called include checks for None, ensuring that the case where no matching record is found is handled appropriately.

  • if contact is None check found in the places where get_employee_expense_attribute is called.
Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that the function `get_employee_expense_attribute` handles cases where no matching `ExpenseAttribute` is found.

# Test: Search for exception handling related to `ExpenseAttribute.DoesNotExist`.
ast-grep --lang python --pattern $'def get_employee_expense_attribute($_, $_) {
  $$$
  try {
    $$$
  } catch ExpenseAttribute.DoesNotExist {
    $$$
  }
}'

Length of output: 704



Script:

#!/bin/bash
# Description: Verify that the function `get_employee_expense_attribute` handles cases where no matching `ExpenseAttribute` is found.

# Test: Search for any exception handling within the function `get_employee_expense_attribute`.
ast-grep --lang python --pattern $'def get_employee_expense_attribute($_, $_):\n    $$$\n    try:\n        $$$\n    except ExpenseAttribute.DoesNotExist:\n        $$$'

Length of output: 163



Script:

#!/bin/bash
# Description: Verify that the function `get_employee_expense_attribute` handles cases where no matching `ExpenseAttribute` is found.

# Test: Search for `None` checks within the function `get_employee_expense_attribute`.
ast-grep --lang python --pattern $'def get_employee_expense_attribute($_, $_):\n    $$$\n    if $_ is None:\n        $$$'

# Test: Search for `None` checks in the places where `get_employee_expense_attribute` is called.
rg 'get_employee_expense_attribute' --files-with-matches | xargs -I {} rg 'if .* is None' {}

Length of output: 248

Comment on lines +134 to +158
def sync_inactive_employee(expense_group: ExpenseGroup) -> ExpenseAttribute:
fyle_credentials = FyleCredential.objects.get(workspace_id=expense_group.workspace_id)
platform = PlatformConnector(fyle_credentials=fyle_credentials)
fyle_employee = platform.employees.get_employee_by_email(expense_group.description.get('employee_email'))
if len(fyle_employee):
fyle_employee = fyle_employee[0]
attribute = {
'attribute_type': 'EMPLOYEE',
'display_name': 'Employee',
'value': fyle_employee['user']['email'],
'source_id': fyle_employee['id'],
'active': True if fyle_employee['is_enabled'] and fyle_employee['has_accepted_invite'] else False,
'detail': {
'user_id': fyle_employee['user_id'],
'employee_code': fyle_employee['code'],
'full_name': fyle_employee['user']['full_name'],
'location': fyle_employee['location'],
'department': fyle_employee['department']['name'] if fyle_employee['department'] else None,
'department_id': fyle_employee['department_id'],
'department_code': fyle_employee['department']['code'] if fyle_employee['department'] else None
}
}
ExpenseAttribute.bulk_create_or_update_expense_attributes([attribute], 'EMPLOYEE', expense_group.workspace_id, True)
return get_employee_expense_attribute(expense_group.description.get('employee_email'), expense_group.workspace_id)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactor the boolean expression for clarity.

- 'active': True if fyle_employee['is_enabled'] and fyle_employee['has_accepted_invite'] else False,
+ 'active': fyle_employee['is_enabled'] and fyle_employee['has_accepted_invite'],

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
def sync_inactive_employee(expense_group: ExpenseGroup) -> ExpenseAttribute:
fyle_credentials = FyleCredential.objects.get(workspace_id=expense_group.workspace_id)
platform = PlatformConnector(fyle_credentials=fyle_credentials)
fyle_employee = platform.employees.get_employee_by_email(expense_group.description.get('employee_email'))
if len(fyle_employee):
fyle_employee = fyle_employee[0]
attribute = {
'attribute_type': 'EMPLOYEE',
'display_name': 'Employee',
'value': fyle_employee['user']['email'],
'source_id': fyle_employee['id'],
'active': True if fyle_employee['is_enabled'] and fyle_employee['has_accepted_invite'] else False,
'detail': {
'user_id': fyle_employee['user_id'],
'employee_code': fyle_employee['code'],
'full_name': fyle_employee['user']['full_name'],
'location': fyle_employee['location'],
'department': fyle_employee['department']['name'] if fyle_employee['department'] else None,
'department_id': fyle_employee['department_id'],
'department_code': fyle_employee['department']['code'] if fyle_employee['department'] else None
}
}
ExpenseAttribute.bulk_create_or_update_expense_attributes([attribute], 'EMPLOYEE', expense_group.workspace_id, True)
return get_employee_expense_attribute(expense_group.description.get('employee_email'), expense_group.workspace_id)
def sync_inactive_employee(expense_group: ExpenseGroup) -> ExpenseAttribute:
fyle_credentials = FyleCredential.objects.get(workspace_id=expense_group.workspace_id)
platform = PlatformConnector(fyle_credentials=fyle_credentials)
fyle_employee = platform.employees.get_employee_by_email(expense_group.description.get('employee_email'))
if len(fyle_employee):
fyle_employee = fyle_employee[0]
attribute = {
'attribute_type': 'EMPLOYEE',
'display_name': 'Employee',
'value': fyle_employee['user']['email'],
'source_id': fyle_employee['id'],
'active': fyle_employee['is_enabled'] and fyle_employee['has_accepted_invite'],
'detail': {
'user_id': fyle_employee['user_id'],
'employee_code': fyle_employee['code'],
'full_name': fyle_employee['user']['full_name'],
'location': fyle_employee['location'],
'department': fyle_employee['department']['name'] if fyle_employee['department'] else None,
'department_id': fyle_employee['department_id'],
'department_code': fyle_employee['department']['code'] if fyle_employee['department'] else None
}
}
ExpenseAttribute.bulk_create_or_update_expense_attributes([attribute], 'EMPLOYEE', expense_group.workspace_id, True)
return get_employee_expense_attribute(expense_group.description.get('employee_email'), expense_group.workspace_id)

Comment on lines +161 to +173

def create_or_update_employee_mapping(
expense_group: ExpenseGroup,
xero_connection: XeroConnector,
auto_map_employees_preference: str,
):
try:
employee = get_employee_expense_attribute(expense_group.description.get('employee_email'), expense_group.workspace_id)

if not employee:
# Sync inactive employee and gracefully handle export failure
sync_inactive_employee(expense_group)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tip

Codebase Verification

No handling for cases where sync_inactive_employee does not find an employee

The function create_or_update_employee_mapping does not handle cases where sync_inactive_employee does not find an employee. There is no exception handling or conditional checks related to sync_inactive_employee in the codebase.

  • sync_inactive_employee is defined in apps/xero/tasks.py.
  • It is called within create_or_update_employee_mapping in apps/xero/tasks.py.

Please consider adding appropriate handling for cases where sync_inactive_employee does not find an employee.

Analysis chain

Consider handling the case where sync_inactive_employee does not find an employee.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that the function `create_or_update_employee_mapping` handles cases where `sync_inactive_employee` does not find an employee to sync.

# Test: Search for exception handling related to the return value of `sync_inactive_employee`.
ast-grep --lang python --pattern $'def create_or_update_employee_mapping($_, $_, $_) {
  $$$
  if not $_ {
    $$$
  }
}'

Length of output: 467



Script:

#!/bin/bash
# Locate the implementation of sync_inactive_employee
ast-grep --lang python --pattern 'def sync_inactive_employee($_) {
  $$$
}'

# Check the usage of sync_inactive_employee within create_or_update_employee_mapping
ast-grep --lang python --pattern 'def create_or_update_employee_mapping($_, $_, $_) {
  $$$
  sync_inactive_employee($_)
  $$$
}'

# Look for any exception handling or conditional checks related to sync_inactive_employee within create_or_update_employee_mapping
ast-grep --lang python --pattern 'def create_or_update_employee_mapping($_, $_, $_) {
  $$$
  try:
    sync_inactive_employee($_)
    $$$
  except $_:
    $$$
}'

Length of output: 1730



Script:

#!/bin/bash
# Locate the implementation of sync_inactive_employee
rg 'def sync_inactive_employee' -t py

# Check the usage of sync_inactive_employee within create_or_update_employee_mapping
rg 'sync_inactive_employee' -t py

# Look for any exception handling or conditional checks related to sync_inactive_employee within create_or_update_employee_mapping
rg 'try:\n\s*sync_inactive_employee' -A 5 -t py

Length of output: 576



Script:

#!/bin/bash
# Locate the implementation of sync_inactive_employee
rg 'def sync_inactive_employee' -t py

# Check the usage of sync_inactive_employee within create_or_update_employee_mapping
rg 'sync_inactive_employee' -t py

# Look for any exception handling or conditional checks related to sync_inactive_employee within create_or_update_employee_mapping
rg -U 'try:\n\s*sync_inactive_employee' -A 5 -t py

Length of output: 387



Script:

#!/bin/bash
# Locate the implementation of sync_inactive_employee
rg 'def sync_inactive_employee' -t py

# Check the usage of sync_inactive_employee within create_or_update_employee_mapping
rg 'sync_inactive_employee' -t py

# Look for any exception handling or conditional checks related to sync_inactive_employee within create_or_update_employee_mapping
rg -U 'try:\n\s*sync_inactive_employee' -A 5 -t py

Length of output: 387

@ruuushhh ruuushhh merged commit 5b32489 into master May 17, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants