fix todos in payment endpoint

This commit is contained in:
DSeeLP 2025-03-18 16:40:09 +01:00
parent 2d3cc8edc1
commit 02b56c745d

View File

@ -1,8 +1,8 @@
use std::{borrow::Cow, cell::RefCell, sync::Arc}; use std::{borrow::Cow, cell::RefCell, sync::Arc};
use axum::{Router, routing::post}; use axum::{Router, http::StatusCode, routing::post};
use bank_core::{ use bank_core::{
Name, NameOrUuid, USER_ACCOUNT_PATTERN, make_schemas, ApiError, Name, NameOrUuid, USER_ACCOUNT_PATTERN, make_schemas,
pagination::Pagination, pagination::Pagination,
transaction::{FullTransaction, Transaction, TransactionQuery}, transaction::{FullTransaction, Transaction, TransactionQuery},
}; };
@ -349,13 +349,28 @@ pub async fn make_payment(
NameOrUuid::Id(uuid) => Accounts::by_id(&client, uuid).await?, NameOrUuid::Id(uuid) => Accounts::by_id(&client, uuid).await?,
NameOrUuid::Name(name) => Accounts::get_for_user(&client, user_id, &*name).await?, NameOrUuid::Name(name) => Accounts::get_for_user(&client, user_id, &*name).await?,
}) else { }) else {
todo!("from account doesn't exist") return Err(ApiError::const_new(
StatusCode::NOT_FOUND,
"transaction.from.not_found",
"Not Found",
)
.into());
}; };
let Some((to_user, to)) = to.account_id(&client).await? else { let Some((to_user, to)) = to.account_id(&client).await? else {
todo!("to account doesn't exist") return Err(ApiError::const_new(
StatusCode::NOT_FOUND,
"transaction.target.not_found",
"Not Found",
)
.into());
}; };
if from.balance < amount { if from.balance < amount {
todo!("not enough money") return Err(ApiError::const_new(
StatusCode::BAD_REQUEST,
"transaction.insufficient_funds",
"Insufficient funds",
)
.into());
} }
let update_balance_stmt = client let update_balance_stmt = client
.prepare_cached("update accounts set balance = balance + $2 where id = $1") .prepare_cached("update accounts set balance = balance + $2 where id = $1")