(feat) all transactions

This commit is contained in:
Rorik Star Platinum 2025-11-07 22:02:03 +03:00
parent aeb9514aa3
commit eef798df5f
4 changed files with 533 additions and 1 deletions

View file

@ -26,6 +26,9 @@ pub fn router(app_state: AppState) -> Router {
.route("/api/transactions/{bank}/{user_id}/{account_id}",
get(handlers::get_transactions_handler)
)
.route("/api/transactions/{bank_user_id}",
get(handlers::get_all_transactions_handler) // ← Новый эндпоинт
)
.route("/api/balances/{bank}/{account_id}",
get(handlers::get_balances_handler)
)

View file

@ -212,6 +212,90 @@ pub async fn get_transactions_handler(
Ok(Json(serde_json::to_value(transactions_response).unwrap()))
}
pub async fn get_all_transactions_handler(
State(state): State<AppState>,
Path(bank_user_id): Path<String>,
Query(params): Query<TransactionQuery>, // ← Добавили query params
) -> Result<Json<serde_json::Value>, (StatusCode, Json<serde_json::Value>)> {
let page = params.page.unwrap_or(1);
let limit = params.limit.unwrap_or(20); // Default: 20 на странице для ленты
info!("📊 Fetching ALL transactions page {} (limit {})", page, limit);
// Validate
if limit > 100 {
return Err((
StatusCode::BAD_REQUEST,
Json(json!({ "error": "limit max 100" }))
));
}
let offset = (page - 1) * limit;
// Get total count
let total_count: i64 = sqlx::query_scalar(
"SELECT COUNT(*) FROM transactions WHERE account_id IN (
SELECT account_id FROM accounts WHERE user_id = $1
)"
)
.bind(&bank_user_id)
.fetch_one(&state.db_pool)
.await
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, Json(json!({ "error": e.to_string() }))))?;
// Get paginated transactions
let transactions = sqlx::query!(
r#"
SELECT
transaction_id,
account_id,
bank_code,
amount,
currency,
credit_debit_indicator,
transaction_information,
booking_date_time
FROM transactions
WHERE account_id IN (
SELECT account_id FROM accounts WHERE user_id = $1
)
ORDER BY booking_date_time DESC
LIMIT $2 OFFSET $3
"#,
bank_user_id,
limit as i64,
offset as i64
)
.fetch_all(&state.db_pool)
.await
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, Json(json!({ "error": e.to_string() }))))?;
let total_pages = (total_count as f64 / limit as f64).ceil() as u32;
Ok(Json(json!({
"data": {
"transactions": transactions.into_iter().map(|tx| json!({
"transaction_id": tx.transaction_id,
"account_id": tx.account_id,
"bank_code": tx.bank_code,
"amount": tx.amount,
"currency": tx.currency,
"credit_debit_indicator": tx.credit_debit_indicator,
"transaction_information": tx.transaction_information,
"booking_date_time": tx.booking_date_time,
})).collect::<Vec<_>>()
},
"meta": {
"current_page": page,
"page_size": limit,
"total_pages": total_pages,
"total_records": total_count,
"has_next": page < total_pages,
"has_prev": page > 1,
}
})))
}
pub async fn delete_consent_handler(