@@ -104,30 +104,26 @@ def create_wallet(
104104 existing_wallets = session .exec (
105105 select (Wallet ).where (Wallet .user_id == user_id )
106106 ).all ()
107-
107+
108108 if len (existing_wallets ) >= MAX_WALLETS_PER_USER :
109109 raise HTTPException (
110- status_code = 400 ,
111- detail = "User cannot have more than 3 wallets"
110+ status_code = 400 , detail = "User cannot have more than 3 wallets"
112111 )
113-
112+
114113 # Check if user already has wallet with this currency
115114 existing_currency_wallet = session .exec (
116115 select (Wallet ).where (
117- Wallet .user_id == user_id ,
118- Wallet .currency == wallet_in .currency
116+ Wallet .user_id == user_id , Wallet .currency == wallet_in .currency
119117 )
120118 ).first ()
121-
119+
122120 if existing_currency_wallet :
123121 raise HTTPException (
124- status_code = 400 ,
125- detail = f"User already has a { wallet_in .currency } wallet"
122+ status_code = 400 , detail = f"User already has a { wallet_in .currency } wallet"
126123 )
127-
124+
128125 db_wallet = Wallet .model_validate (
129- wallet_in ,
130- update = {"user_id" : user_id , "balance" : Decimal ("0.00" )}
126+ wallet_in , update = {"user_id" : user_id , "balance" : Decimal ("0.00" )}
131127 )
132128 session .add (db_wallet )
133129 session .commit ()
@@ -142,99 +138,85 @@ def get_wallet_by_id(*, session: Session, wallet_id: uuid.UUID) -> Wallet | None
142138
143139def get_user_wallets (* , session : Session , user_id : uuid .UUID ) -> list [Wallet ]:
144140 """Get all wallets for a user."""
145- return session .exec (
146- select (Wallet ).where (Wallet .user_id == user_id )
147- ).all ()
141+ return session .exec (select (Wallet ).where (Wallet .user_id == user_id )).all ()
148142
149143
150144# Transaction CRUD operations
151145
152146
153147def convert_currency (
154- amount : Decimal ,
155- from_currency : CurrencyEnum ,
156- to_currency : CurrencyEnum
148+ amount : Decimal , from_currency : CurrencyEnum , to_currency : CurrencyEnum
157149) -> tuple [Decimal , Decimal ]:
158150 """Convert amount between currencies and return (converted_amount, fee)."""
159151 if from_currency == to_currency :
160152 return amount , Decimal ("0.00" )
161-
153+
162154 rate_key = (from_currency .value , to_currency .value )
163155 if rate_key not in EXCHANGE_RATES :
164156 raise HTTPException (
165157 status_code = 400 ,
166- detail = f"Exchange rate not available for { from_currency } to { to_currency } "
158+ detail = f"Exchange rate not available for { from_currency } to { to_currency } " ,
167159 )
168-
160+
169161 rate = EXCHANGE_RATES [rate_key ]
170162 converted_amount = amount * rate
171163 fee = converted_amount * CONVERSION_FEE_RATE
172164 final_amount = converted_amount - fee
173-
165+
174166 return final_amount , fee
175167
176168
177169def create_transaction (
178- * ,
179- session : Session ,
180- transaction_in : TransactionCreate ,
181- user_id : uuid .UUID
170+ * , session : Session , transaction_in : TransactionCreate , user_id : uuid .UUID
182171) -> Transaction :
183172 """Create a new transaction."""
184173 # Get the wallet
185174 wallet = session .get (Wallet , transaction_in .wallet_id )
186175 if not wallet :
187176 raise HTTPException (status_code = 404 , detail = "Wallet not found" )
188-
177+
189178 # Check if wallet belongs to user
190179 if wallet .user_id != user_id :
191180 raise HTTPException (
192- status_code = 403 ,
193- detail = "Not authorized to access this wallet"
181+ status_code = 403 , detail = "Not authorized to access this wallet"
194182 )
195-
183+
196184 # Convert currency if needed
197185 transaction_amount = transaction_in .amount
198186 if transaction_in .currency != wallet .currency :
199187 converted_amount , _ = convert_currency (
200- transaction_in .amount ,
201- transaction_in .currency ,
202- wallet .currency
188+ transaction_in .amount , transaction_in .currency , wallet .currency
203189 )
204190 transaction_amount = converted_amount
205-
191+
206192 # Calculate new balance
207193 if transaction_in .type == TransactionTypeEnum .CREDIT :
208194 new_balance = wallet .balance + transaction_amount
209195 else : # DEBIT
210196 new_balance = wallet .balance - transaction_amount
211-
197+
212198 # Check for negative balance
213199 if new_balance < 0 :
214200 raise HTTPException (
215201 status_code = 400 ,
216- detail = "Insufficient funds: transaction would result in negative balance"
202+ detail = "Insufficient funds: transaction would result in negative balance" ,
217203 )
218-
204+
219205 # Create transaction
220206 db_transaction = Transaction .model_validate (transaction_in )
221207 session .add (db_transaction )
222-
208+
223209 # Update wallet balance
224210 wallet .balance = new_balance
225211 session .add (wallet )
226-
212+
227213 session .commit ()
228214 session .refresh (db_transaction )
229215 return db_transaction
230216
231217
232218def get_wallet_transactions (
233- * ,
234- session : Session ,
235- wallet_id : uuid .UUID ,
236- skip : int = 0 ,
237- limit : int = 100
219+ * , session : Session , wallet_id : uuid .UUID , skip : int = 0 , limit : int = 100
238220) -> list [Transaction ]:
239221 """Get transactions for a wallet."""
240222 return session .exec (
0 commit comments