You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
85 lines
2.3 KiB
85 lines
2.3 KiB
use axum::extract::{Path, Query, State};
|
|
use axum::Json;
|
|
use sea_orm::{ColumnTrait, EntityTrait, Order, QueryFilter, TransactionTrait};
|
|
|
|
use crate::db::paginate::{paginate, PaginatedResponse};
|
|
use crate::error::AppError;
|
|
use crate::model::transaction;
|
|
use crate::model::{company, in_process_transaction};
|
|
use crate::AppState;
|
|
|
|
use super::Pagination;
|
|
|
|
pub async fn get_all(
|
|
state: State<AppState>,
|
|
Query(pagination): Query<Pagination>,
|
|
) -> Result<Json<PaginatedResponse<in_process_transaction::Model>>, AppError> {
|
|
let db = &state.db;
|
|
let page = pagination.page;
|
|
let size = pagination.size;
|
|
let res = paginate::<
|
|
in_process_transaction::Entity,
|
|
in_process_transaction::Model,
|
|
in_process_transaction::Column,
|
|
>(
|
|
db,
|
|
page,
|
|
size,
|
|
Some(in_process_transaction::Column::CreatedAt),
|
|
Some(Order::Asc),
|
|
)
|
|
.await?;
|
|
|
|
Ok(Json(res))
|
|
}
|
|
|
|
pub async fn retry_failed_transaction(
|
|
state: State<AppState>,
|
|
Path(foreign_id): Path<String>,
|
|
) -> Result<Json<(transaction::Model, Option<company::Model>)>, AppError> {
|
|
let db = &state.db;
|
|
let txn = db.begin().await?;
|
|
|
|
let mut tr = in_process_transaction::Entity::find()
|
|
.filter(in_process_transaction::Column::Failed.eq(1))
|
|
.filter(in_process_transaction::Column::ForeignId.eq(foreign_id.to_owned()))
|
|
.one(&txn)
|
|
.await?
|
|
.ok_or_else(|| {
|
|
AppError::NotFound(format!("Failed transaction {} doesn't exist", foreign_id))
|
|
})?;
|
|
|
|
tr.process(&txn).await?;
|
|
|
|
let res = transaction::Entity::find()
|
|
.filter(transaction::Column::ForeignId.eq(foreign_id.to_owned()))
|
|
.find_also_related(company::Entity)
|
|
.one(&txn)
|
|
.await?
|
|
.ok_or_else(|| {
|
|
AppError::NotFound("Failed to fetch just created transaction".to_string())
|
|
})?;
|
|
|
|
txn.commit().await?;
|
|
|
|
Ok(Json(res))
|
|
}
|
|
|
|
pub async fn retry_all(state: State<AppState>) -> Result<(), AppError> {
|
|
let db = &state.db;
|
|
let list = in_process_transaction::Entity::find()
|
|
.all(db)
|
|
.await
|
|
.map_err(|e| AppError::NotFound(format!("Database error: {}", e)))?;
|
|
|
|
let mut res_list = vec![];
|
|
for mut tr in list {
|
|
let res = tr.process(db).await;
|
|
if let Ok(val) = res {
|
|
res_list.push(val);
|
|
}
|
|
}
|
|
|
|
Ok(())
|
|
}
|