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: transaction operations with UUID not updating balances correctly… #559

Conversation

fredcamaral
Copy link
Member

@fredcamaral fredcamaral commented Mar 5, 2025

…… 🐛 🐛

This PR resolves issues with transactions using account UUIDs instead of aliases:

  1. Fixed account matching in transaction operations: - Enhanced account lookup to match by both balance.ID and balance.AccountID - Simplified the string comparison logic with direct UUID comparisons - Improved logging to show which field (ID or AccountID) matched

  2. Fixed balance update logic for UUID-based accounts: - Modified SelectForUpdate in balance.postgresql.go to support UUIDs - Enhanced query to search for account_id and id in a single query - Added key lookup strategy in balance update function to find the correct entry in fromTo map by either alias, balance.ID or balance.AccountID

The issue was caused by account references not being correctly matched during transaction processing. UUIDs in the request body were not mapped to the correct balance record in the database during the balance update phase.

This fix ensures operations are created and balances are updated correctly regardless of whether accounts are referenced by UUID or alias.

Midaz Pull Request Checklist

Pull Request Type

  • Auth
  • Infra
  • Onboarding
  • Mdz
  • Transaction
  • Audit
  • Pipeline
  • Documentation

Checklist

Please check each item after it's completed.

  • I have tested these changes locally.
  • I have updated the documentation accordingly.
  • I have added necessary comments to the code, especially in complex areas.
  • I have ensured that my changes adhere to the project's coding standards.
  • I have checked for any potential security issues.
  • I have ensured that all tests pass.
  • I have updated the version appropriately (if applicable).
  • I have confirmed this code is ready for review.

Obs: Please, always remember to target your PR to develop branch instead of main.

Additional Notes

…… 🐛 🐛

This commit resolves issues with transactions using account UUIDs instead of aliases:

1. Fixed account matching in transaction operations:
         - Enhanced account lookup to match by both balance.ID and balance.AccountID
         - Simplified the string comparison logic with direct UUID comparisons
         - Improved logging to show which field (ID or AccountID) matched

2. Fixed balance update logic for UUID-based accounts:
         - Modified SelectForUpdate in balance.postgresql.go to support UUIDs
         - Enhanced query to search for account_id and id in a single query
         - Added key lookup strategy in balance update function to find the correct entry in fromTo map by either alias, balance.ID or balance.AccountID

The issue was caused by account references not being properly matched during transaction processing. UUIDs in the request body were not being mapped to the correct balance record in the database during the balance update phase.

This fix ensures operations are created and balances are updated correctly regardless of whether accounts are referenced by UUID or alias.

Co-Authored-By: Hugo Aguiar Martinez <2437999+MartinezAvellan@users.noreply.github.com>
Co-Authored-By: Felipe Gomes <178956569+gitfelipegomes@users.noreply.github.com>
@fredcamaral fredcamaral added the bug Something isn't working label Mar 5, 2025
@Copilot Copilot bot review requested due to automatic review settings March 5, 2025 01:33
@fredcamaral fredcamaral requested a review from a team as a code owner March 5, 2025 01:33
@github-actions github-actions bot requested a review from a team as a code owner March 5, 2025 01:33
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

PR Overview

This PR fixes issues with transaction operations using UUIDs by improving account matching and balance update logic. Key changes include:

  • Enhancing account lookup queries to match by both alias and UUID fields.
  • Refactoring the SelectForUpdate function to handle both aliases and accountIDs using a combined query.
  • Updating related services, mocks, and validations to support the new UUID-based logic.

Reviewed Changes

File Description
components/transaction/internal/adapters/postgres/balance/balance.postgresql.go Updated query in ListByAccountIDs and enhanced SelectForUpdate to handle both aliases and accountIDs.
components/transaction/internal/services/command/create-operation.go Modified account matching logic to support UUID comparisons with detailed logging.
components/transaction/internal/adapters/http/in/transaction.go Adjusted account comparison for UUIDs with improved log messages.
pkg/gold/transaction/model/validations.go Revised UUID comparison in validations and updated Scale function comments.
components/transaction/internal/services/query/get-balances.go Added debug logging for UUIDs and aliases during balance retrieval.
components/transaction/internal/services/command/update-balance.go Extracted UUIDs and aliases separately before updating balances using the new repository signature.
components/transaction/internal/adapters/postgres/balance/balance.mock.go Updated mock definitions to match the new SelectForUpdate interface signature.

Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.

Comments suppressed due to low confidence (2)

components/transaction/internal/adapters/postgres/balance/balance.postgresql.go:628

  • [nitpick] Consider refactoring the repeated UUID matching logic into a helper function to improve code readability and maintainability.
if pkg.IsUUID(balance.ID) {

components/transaction/internal/services/command/create-operation.go:35

  • [nitpick] Consider consolidating the UUID and alias comparison logic into a single utility function to reduce duplicated logic in account matching.
if fromTo[i].Account == blc.ID || fromTo[i].Account == blc.Alias {

1. ✓ Fixed the interface{} to any conversion in balance.postgresql.go
2. ✓ Added proper spacing between conditionals and for loops in get-balances.go
3. ✓ Fixed the nested if-statements in transaction.go
4. ✓ Fixed declaration cuddling issue in balance.postgresql.go by properly declaring the error variable before assignment
@fredcamaral fredcamaral closed this Mar 5, 2025
@fredcamaral fredcamaral deleted the fix/bug-on-transactions-with-account-as-uuid-not-persisting branch March 5, 2025 11:14
@fredcamaral fredcamaral restored the fix/bug-on-transactions-with-account-as-uuid-not-persisting branch March 5, 2025 11:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants