feat: additionnal bal features: ended field, current_bal for users
This commit is contained in:
parent
61aac4bd80
commit
4d451ace79
5 changed files with 97 additions and 49 deletions
|
|
@ -6,7 +6,7 @@ use sea_orm::{ActiveModelTrait, ActiveValue::{NotSet, Set}, ColumnTrait, EntityT
|
|||
use serde::{Deserialize, Serialize};
|
||||
use utoipa::IntoParams;
|
||||
|
||||
use crate::{entities::{bal, prelude::*, user}, routes::auth::Claims, utils::auth::user_is_bal_owner, AppState};
|
||||
use crate::{entities::{bal, book_instance, prelude::*, user}, routes::auth::Claims, AppState};
|
||||
|
||||
|
||||
#[derive(IntoParams)]
|
||||
|
|
@ -72,6 +72,7 @@ pub async fn create_bal(
|
|||
id: NotSet,
|
||||
user_id: Set(claims.user_id),
|
||||
name: Set(instance_payload.name),
|
||||
ended: Set(false)
|
||||
};
|
||||
|
||||
let b = bal.save(state.db_conn.as_ref()).await;
|
||||
|
|
@ -188,16 +189,24 @@ pub async fn get_current_bal(
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize, PartialEq, utoipa::ToSchema)]
|
||||
pub enum BookInstanceTransferMode {
|
||||
All,
|
||||
None,
|
||||
OwnedOnly
|
||||
}
|
||||
|
||||
#[derive(Deserialize, utoipa::ToSchema)]
|
||||
pub struct BalIdParams{
|
||||
id: u32,
|
||||
pub struct SetCurrentBalParams{
|
||||
bal_id: u32,
|
||||
transfer_book_instances: Option<BookInstanceTransferMode>
|
||||
}
|
||||
|
||||
#[axum::debug_handler]
|
||||
#[utoipa::path(
|
||||
post,
|
||||
path = "/bal/current",
|
||||
request_body = BalIdParams,
|
||||
request_body = SetCurrentBalParams,
|
||||
security(("jwt" = [])),
|
||||
responses(
|
||||
(status = OK, description = "Successfully set current active BAL"),
|
||||
|
|
@ -210,17 +219,44 @@ pub struct BalIdParams{
|
|||
pub async fn set_current_bal(
|
||||
State(state): State<Arc<AppState>>,
|
||||
claims: Claims,
|
||||
Json(payload): Json<BalIdParams>,
|
||||
Json(payload): Json<SetCurrentBalParams>,
|
||||
) -> StatusCode {
|
||||
if !user_is_bal_owner(claims.user_id, payload.id, state.db_conn.as_ref()).await {
|
||||
return StatusCode::UNAUTHORIZED;
|
||||
}
|
||||
if let Ok(Some(user)) = User::find_by_id(claims.user_id).one(state.db_conn.as_ref()).await {
|
||||
if let Ok(Some(new_bal)) = Bal::find_by_id(payload.bal_id).one(state.db_conn.as_ref()).await
|
||||
&& let Ok(Some(user)) = User::find_by_id(claims.user_id).one(state.db_conn.as_ref()).await
|
||||
{
|
||||
if new_bal.user_id != claims.user_id {
|
||||
return StatusCode::UNAUTHORIZED;
|
||||
}
|
||||
|
||||
if let Some(old_bal_id) = user.current_bal_id {
|
||||
// Optional instance transfer
|
||||
if let Some(mode) = payload.transfer_book_instances && mode != BookInstanceTransferMode::None {
|
||||
let mut update_query = BookInstance::update_many()
|
||||
.set(book_instance::ActiveModel {
|
||||
bal_id: Set(new_bal.id),
|
||||
..Default::default()
|
||||
})
|
||||
.filter(book_instance::Column::BalId.eq(old_bal_id));
|
||||
if mode == BookInstanceTransferMode::OwnedOnly {
|
||||
update_query = update_query.filter(book_instance::Column::OwnerId.eq(user.owner_id));
|
||||
}
|
||||
|
||||
let _ = update_query.exec(state.db_conn.as_ref()).await;
|
||||
}
|
||||
// Set old bal as ended if it existed
|
||||
let _ = Bal::update(bal::ActiveModel {
|
||||
id: Set(old_bal_id),
|
||||
ended: Set(true),
|
||||
..Default::default()
|
||||
}).exec(state.db_conn.as_ref()).await;
|
||||
}
|
||||
|
||||
// Set current bal on user
|
||||
let mut user_active_model: user::ActiveModel = user.into_active_model();
|
||||
user_active_model.current_bal_id = Set(Some(payload.id));
|
||||
user_active_model.current_bal_id = Set(Some(payload.bal_id));
|
||||
let _ = User::update(user_active_model).exec(state.db_conn.as_ref()).await;
|
||||
StatusCode::OK
|
||||
} else {
|
||||
StatusCode::INTERNAL_SERVER_ERROR
|
||||
return StatusCode::NOT_FOUND;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Reference in a new issue