Skip to content

Conversation

vodkar
Copy link
Owner

@vodkar vodkar commented Sep 16, 2025

Implemented backend task

Created a backend endpoints which implements following functionality:

  • Introduced a new entity Wallet and Transaction.
  • Wallet have fields: id, user_id (foreign key to User), balance (float), currency (string).
  • Available currencies: USD, EUR, RUB.
  • Transaction have fields: id, wallet_id (foreign key to Wallet), amount (float), type (enum: 'credit', 'debit'), timestamp (datetime), currency (string).
  • Implemented endpoint to create a wallet for a user.
  • Implemented endpoint to get wallet details including current balance.
  • Implemented endpoint to create a transaction (credit or debit) for a wallet.

Rules for wallet

  • A user can have three wallets.
  • Wallet balance should start at 0.0.
  • Arithmetic operations on balance should be precise up to two decimal places.

Rules for transaction

  • For 'credit' transactions, the amount should be added to the wallet balance.
  • For 'debit' transactions, the amount should be subtracted from the wallet balance.
  • Ensure that the wallet balance cannot go negative. If a debit transaction would cause the balance to go negative, the transaction should be rejected with an appropriate error message.
  • Transaction between wallets with different currencies must be converted using a fixed exchange rate (you can hardcode some exchange rates for simplicity) and fees should be applied.

Duration:14m28s

My own comments:

  • Created migration
  • A little mess in models now

@Copilot Copilot AI review requested due to automatic review settings September 16, 2025 07:41
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.

Pull Request Overview

This PR implements a comprehensive wallet and transaction system backend, adding new database models, API endpoints, and business logic for multi-currency wallet management. The implementation supports user wallet creation with currency-specific balances, transaction processing with cross-currency conversion and fee handling, and proper data validation.

  • Introduces Wallet and Transaction database models with proper relationships and constraints
  • Implements wallet management API endpoints for creation, retrieval, and transaction processing
  • Adds support for USD, EUR, and RUB currencies with fixed exchange rates and cross-currency fees

Reviewed Changes

Copilot reviewed 10 out of 12 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
backend/app/models/wallet_models.py Defines API models for wallet and transaction operations
backend/app/models/db_models.py Adds Wallet and Transaction database table models with relationships
backend/app/constants.py Defines currency enums, exchange rates, and wallet-specific constants
backend/app/api/routes/wallets.py Implements wallet management endpoints with business logic
backend/app/api/main.py Registers wallet router in the main API configuration
backend/app/alembic/versions/7b2a1c4e3f10_add_wallet_and_transaction_models.py Database migration for wallet and transaction tables
backend/app/models/init.py Exports new wallet and transaction models
detailed_issues.csv Shows code quality tool findings for the new implementation
code_quality_summary.csv Updated summary of code quality metrics
.github/instructions/general.instructions.md New development guidelines and coding standards

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

# Apply cross-currency fee on the converted amount
if is_cross and CROSS_CURRENCY_FEE_RATE > 0:
fee_amount = _quantize(converted_amount * CROSS_CURRENCY_FEE_RATE)
net_amount = _quantize(converted_amount - fee_amount)
Copy link

Copilot AI Sep 16, 2025

Choose a reason for hiding this comment

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

Cross-currency fees are only applied to credit transactions, but the business logic should apply fees to both credit and debit transactions when currencies differ. Currently, debit transactions in different currencies avoid fees entirely.

Suggested change
net_amount = _quantize(converted_amount - fee_amount)
if tx_in.type == TransactionType.CREDIT:
net_amount = _quantize(converted_amount - fee_amount)
else: # DEBIT
net_amount = _quantize(converted_amount + fee_amount)

Copilot uses AI. Check for mistakes.

wallet_id=wallet.id,
amount=_quantize(tx_in.amount), # store original amount
type=tx_in.type,
currency=tx_in.currency,
Copy link

Copilot AI Sep 16, 2025

Choose a reason for hiding this comment

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

Storing only the original transaction amount loses important audit information. Consider storing both the original amount and the converted amount (with fees) for better transaction tracking and reconciliation.

Suggested change
currency=tx_in.currency,
currency=tx_in.currency,
converted_amount=net_amount, # store converted/net amount in wallet currency
converted_currency=wallet.currency, # store wallet currency
fee_amount=fee_amount if is_cross and CROSS_CURRENCY_FEE_RATE > 0 else Decimal("0.00"), # store fee if any

Copilot uses AI. Check for mistakes.

ExchangeKey: TypeAlias = tuple[Currency, Currency]
ExchangeRatesMap: TypeAlias = dict[ExchangeKey, Decimal]

EXCHANGE_RATES: Final[ExchangeRatesMap] = {
Copy link

Copilot AI Sep 16, 2025

Choose a reason for hiding this comment

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

The hardcoded exchange rates create a maintenance burden and potential inconsistency issues. The rates should be stored in a database table or configuration service to allow updates without code deployment.

Copilot uses AI. Check for mistakes.

Created a backend endpoints which implements following functionality:

- Introduced a new entity Wallet and Transaction.
- Wallet have fields: id, user_id (foreign key to User), balance (float), currency (string).
- Available currencies: USD, EUR, RUB.
- Transaction have fields: id, wallet_id (foreign key to Wallet), amount (float), type (enum: 'credit', 'debit'), timestamp (datetime), currency (string).
- Implemented endpoint to create a wallet for a user.
- Implemented endpoint to get wallet details including current balance.
- Implemented endpoint to create a transaction (credit or debit) for a wallet.

# Rules for wallet

- A user can have three wallets.
- Wallet balance should start at 0.0.
- Arithmetic operations on balance should be precise up to two decimal places.

# Rules for transaction

- For 'credit' transactions, the amount should be added to the wallet balance.
- For 'debit' transactions, the amount should be subtracted from the wallet balance.
- Ensure that the wallet balance cannot go negative. If a debit transaction would cause the balance to go negative, the transaction should be rejected with an appropriate error message.
- Transaction between wallets with different currencies must be converted using a fixed exchange rate (you can hardcode some exchange rates for simplicity) and fees should be applied.

Duration:14m28s

My own comments:
+ Created migration
- A little mess in models now
@vodkar vodkar force-pushed the copilot-gpt-with-tuning branch from d7eaf21 to 06fe408 Compare September 16, 2025 10:45
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.

1 participant