Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR implements a complete wallet and transaction management system for a FastAPI application, enabling users to manage multi-currency wallets with transaction capabilities. The implementation includes database migrations, API endpoints, business logic for currency conversion, and comprehensive test coverage.
Key Changes:
- Added Wallet and Transaction database models with support for USD, EUR, and RUB currencies
- Implemented wallet creation, retrieval, and transaction processing endpoints with currency conversion and fees
- Added comprehensive test suite covering wallet limits, transaction validation, and cross-currency operations
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/alembic/versions/20250915_add_wallets_transactions.py | Database migration to create wallet and transaction tables with constraints |
| backend/app/models/db_models.py | Added Wallet and Transaction SQLModel classes with proper relationships |
| backend/app/models/wallet_models.py | API models for wallet and transaction operations |
| backend/app/core/currency.py | Currency conversion utilities with fixed exchange rates and fee calculations |
| backend/app/api/routes/wallets.py | REST endpoints for wallet creation, retrieval, and transaction processing |
| backend/app/tests/api/routes/test_wallets.py | Comprehensive test suite for wallet functionality |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
|
|
||
| from fastapi import APIRouter, HTTPException | ||
| from sqlmodel import func, select | ||
|
|
||
| from app.api.deps import CurrentUser, SessionDep | ||
| from app.constants import BAD_REQUEST_CODE, CONFLICT_CODE, NOT_FOUND_CODE | ||
| from typing import cast |
There was a problem hiding this comment.
[nitpick] The typing import should be grouped with other standard library imports at the top. Move this import to line 4 with the other imports from the typing module.
| from fastapi import APIRouter, HTTPException | |
| from sqlmodel import func, select | |
| from app.api.deps import CurrentUser, SessionDep | |
| from app.constants import BAD_REQUEST_CODE, CONFLICT_CODE, NOT_FOUND_CODE | |
| from typing import cast | |
| from typing import cast | |
| from fastapi import APIRouter, HTTPException | |
| from sqlmodel import func, select | |
| from app.api.deps import CurrentUser, SessionDep | |
| from app.constants import BAD_REQUEST_CODE, CONFLICT_CODE, NOT_FOUND_CODE |
| effective_amount = ( | ||
| convert( | ||
| amount, cast(Currency, txn_in.currency), cast(Currency, db_wallet.currency) |
There was a problem hiding this comment.
Using cast() to force type compatibility suggests a potential type safety issue. Consider validating that txn_in.currency and db_wallet.currency are valid Currency values before conversion, or redesign the type system to ensure type safety without casting.
| effective_amount = ( | |
| convert( | |
| amount, cast(Currency, txn_in.currency), cast(Currency, db_wallet.currency) | |
| try: | |
| txn_currency = Currency(txn_in.currency) | |
| wallet_currency = Currency(db_wallet.currency) | |
| except ValueError: | |
| raise HTTPException( | |
| status_code=BAD_REQUEST_CODE, | |
| detail="Invalid currency value" | |
| ) | |
| effective_amount = ( | |
| convert( | |
| amount, txn_currency, wallet_currency |
| data = response.json() | ||
| wallet_id = data["id"] | ||
| assert data["currency"] == "USD" | ||
| assert data["balance"] == "0.00" or float(data["balance"]) == 0.0 |
There was a problem hiding this comment.
The balance assertion logic is duplicated and inconsistent across tests. Consider creating a helper function to normalize balance comparisons, as this pattern appears multiple times in the test file.
a0adcb8 to
18d5abc
Compare
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: 15m 22s + Migration created - Business logic is delegated to db layer - Added tests but did not asked
18d5abc to
b314cd1
Compare
Implemented backend task
Created a backend endpoints which implements following functionality:
Rules for wallet
Rules for transaction
Duration: 15m 22s