diff --git a/crates/api_clients/artcraft/artcraft_api_defs/src/moderation/wallets/mod.rs b/crates/api_clients/artcraft/artcraft_api_defs/src/moderation/wallets/mod.rs index 5c9d1f8947..80b7c4b7f9 100644 --- a/crates/api_clients/artcraft/artcraft_api_defs/src/moderation/wallets/mod.rs +++ b/crates/api_clients/artcraft/artcraft_api_defs/src/moderation/wallets/mod.rs @@ -1,2 +1,3 @@ pub mod list_user_wallets; +pub mod moderator_add_banked_balance_to_wallet; pub mod moderator_get_wallet; diff --git a/crates/api_clients/artcraft/artcraft_api_defs/src/moderation/wallets/moderator_add_banked_balance_to_wallet.rs b/crates/api_clients/artcraft/artcraft_api_defs/src/moderation/wallets/moderator_add_banked_balance_to_wallet.rs new file mode 100644 index 0000000000..04faf6278c --- /dev/null +++ b/crates/api_clients/artcraft/artcraft_api_defs/src/moderation/wallets/moderator_add_banked_balance_to_wallet.rs @@ -0,0 +1,20 @@ +use serde_derive::{Deserialize, Serialize}; +use tokens::tokens::wallets::WalletToken; +use utoipa::ToSchema; + +pub const MODERATOR_ADD_BANKED_BALANCE_TO_WALLET_PATH: &str = "/v1/moderation/wallet/{wallet_token}/add_banked_balance"; + +#[derive(Deserialize, ToSchema)] +pub struct ModeratorAddBankedBalanceToWalletPathInfo { + pub wallet_token: WalletToken, +} + +#[derive(Deserialize, ToSchema)] +pub struct ModeratorAddBankedBalanceToWalletRequest { + pub credits: u16, +} + +#[derive(Serialize, ToSchema)] +pub struct ModeratorAddBankedBalanceToWalletResponse { + pub success: bool, +} diff --git a/crates/service/web/storyteller_web/src/docs/api_doc.rs b/crates/service/web/storyteller_web/src/docs/api_doc.rs index 4ecd66a5a6..402a40d6e1 100644 --- a/crates/service/web/storyteller_web/src/docs/api_doc.rs +++ b/crates/service/web/storyteller_web/src/docs/api_doc.rs @@ -271,6 +271,7 @@ use artcraft_api_defs::moderation::jobs::user::list_user_jobs::*; use artcraft_api_defs::moderation::wallet_ledger_entries::list_wallet_ledger_entries_by_wallet::*; use artcraft_api_defs::moderation::wallet_ledger_entries::moderator_get_wallet_ledger_entry::*; use artcraft_api_defs::moderation::wallets::list_user_wallets::*; +use artcraft_api_defs::moderation::wallets::moderator_add_banked_balance_to_wallet::*; use artcraft_api_defs::moderation::wallets::moderator_get_wallet::*; use crate::http_server::endpoints::webhooks::fal_webhook_handler::*; use crate::http_server::endpoints::image_studio::update_gpt_image_job_status_handler::*; @@ -447,6 +448,7 @@ use crate::http_server::endpoints::media_files::list::list_batch_generated_redux crate::http_server::endpoints::moderation::wallet_ledger_entries::list_wallet_ledger_entries_by_wallet_handler::list_wallet_ledger_entries_by_wallet_handler, crate::http_server::endpoints::moderation::wallet_ledger_entries::moderator_get_wallet_ledger_entry_handler::moderator_get_wallet_ledger_entry_handler, crate::http_server::endpoints::moderation::wallets::list_user_wallets_handler::list_user_wallets_handler, + crate::http_server::endpoints::moderation::wallets::moderator_add_banked_balance_to_wallet_handler::moderator_add_banked_balance_to_wallet_handler, crate::http_server::endpoints::moderation::wallets::moderator_get_wallet_handler::moderator_get_wallet_handler, // Credits crate::http_server::endpoints::credits::get_session_credits_handler::get_session_credits_handler, @@ -640,6 +642,9 @@ use crate::http_server::endpoints::media_files::list::list_batch_generated_redux ListWalletLedgerEntriesByWalletPathInfo, ListWalletLedgerEntriesByWalletResponse, ListWalletLedgerEntriesByWalletEntry, + ModeratorAddBankedBalanceToWalletPathInfo, + ModeratorAddBankedBalanceToWalletRequest, + ModeratorAddBankedBalanceToWalletResponse, ModeratorGetWalletPathInfo, ModeratorGetWalletResponse, ModeratorGetWalletDetails, diff --git a/crates/service/web/storyteller_web/src/http_server/endpoints/moderation/wallets/mod.rs b/crates/service/web/storyteller_web/src/http_server/endpoints/moderation/wallets/mod.rs index 73edb010d9..a3e0a56870 100644 --- a/crates/service/web/storyteller_web/src/http_server/endpoints/moderation/wallets/mod.rs +++ b/crates/service/web/storyteller_web/src/http_server/endpoints/moderation/wallets/mod.rs @@ -1,2 +1,3 @@ pub mod list_user_wallets_handler; +pub mod moderator_add_banked_balance_to_wallet_handler; pub mod moderator_get_wallet_handler; diff --git a/crates/service/web/storyteller_web/src/http_server/endpoints/moderation/wallets/moderator_add_banked_balance_to_wallet_handler.rs b/crates/service/web/storyteller_web/src/http_server/endpoints/moderation/wallets/moderator_add_banked_balance_to_wallet_handler.rs new file mode 100644 index 0000000000..575b1dab66 --- /dev/null +++ b/crates/service/web/storyteller_web/src/http_server/endpoints/moderation/wallets/moderator_add_banked_balance_to_wallet_handler.rs @@ -0,0 +1,75 @@ +use std::sync::Arc; + +use actix_web::web::{Json, Path}; +use actix_web::{web, HttpRequest}; +use log::warn; + +use artcraft_api_defs::moderation::wallets::moderator_add_banked_balance_to_wallet::{ + ModeratorAddBankedBalanceToWalletPathInfo, + ModeratorAddBankedBalanceToWalletRequest, + ModeratorAddBankedBalanceToWalletResponse, +}; +use enums::by_table::wallet_ledger_entries::wallet_ledger_entry_type::WalletLedgerEntryType; +use mysql_queries::queries::wallets::add_durable_banked_balance_to_wallet::add_durable_banked_balance_to_wallet; + +use crate::http_server::common_responses::common_web_error::CommonWebError; +use crate::http_server::web_utils::user_session::require_moderator::{require_moderator, UseDatabase}; +use crate::state::server_state::ServerState; + +/// Add banked credits to a wallet (moderation) +#[utoipa::path( + post, + tag = "Moderation", + path = "/v1/moderation/wallet/{wallet_token}/add_banked_balance", + request_body = ModeratorAddBankedBalanceToWalletRequest, + responses( + (status = 200, description = "Success", body = ModeratorAddBankedBalanceToWalletResponse), + (status = 401, description = "Unauthorized"), + (status = 500, description = "Server error"), + ), + params( + ("wallet_token" = WalletToken, Path, description = "Wallet token to add credits to"), + ) +)] +pub async fn moderator_add_banked_balance_to_wallet_handler( + http_request: HttpRequest, + path: Path, + request: Json, + server_state: web::Data>, +) -> Result, CommonWebError> { + + let _user_session = require_moderator(&http_request, &server_state, UseDatabase::GrabNewConnection) + .await + .map_err(|_| CommonWebError::NotAuthorized)?; + + let mut transaction = server_state.mysql_pool.begin() + .await + .map_err(|err| { + warn!("moderator_add_banked_balance_to_wallet transaction begin error: {:?}", err); + CommonWebError::ServerError + })?; + + let _result = add_durable_banked_balance_to_wallet( + &path.wallet_token, + request.credits as u64, + None, + Some(WalletLedgerEntryType::StaffAddBanked), + &mut transaction, + ) + .await + .map_err(|err| { + warn!("moderator_add_banked_balance_to_wallet error: {:?}", err); + CommonWebError::ServerError + })?; + + transaction.commit() + .await + .map_err(|err| { + warn!("moderator_add_banked_balance_to_wallet commit error: {:?}", err); + CommonWebError::ServerError + })?; + + Ok(Json(ModeratorAddBankedBalanceToWalletResponse { + success: true, + })) +} diff --git a/crates/service/web/storyteller_web/src/http_server/routes/application_routes/moderation_routes.rs b/crates/service/web/storyteller_web/src/http_server/routes/application_routes/moderation_routes.rs index 9536c9343b..545b92eff0 100644 --- a/crates/service/web/storyteller_web/src/http_server/routes/application_routes/moderation_routes.rs +++ b/crates/service/web/storyteller_web/src/http_server/routes/application_routes/moderation_routes.rs @@ -23,6 +23,7 @@ use crate::http_server::endpoints::moderation::jobs::user::list_user_jobs_handle use crate::http_server::endpoints::moderation::wallet_ledger_entries::list_wallet_ledger_entries_by_wallet_handler::list_wallet_ledger_entries_by_wallet_handler; use crate::http_server::endpoints::moderation::wallet_ledger_entries::moderator_get_wallet_ledger_entry_handler::moderator_get_wallet_ledger_entry_handler; use crate::http_server::endpoints::moderation::wallets::list_user_wallets_handler::list_user_wallets_handler; +use crate::http_server::endpoints::moderation::wallets::moderator_add_banked_balance_to_wallet_handler::moderator_add_banked_balance_to_wallet_handler; use crate::http_server::endpoints::moderation::wallets::moderator_get_wallet_handler::moderator_get_wallet_handler; use crate::http_server::endpoints::moderation::jobs::get_tts_inference_queue_count::get_tts_inference_queue_count_handler; use crate::http_server::endpoints::moderation::jobs::get_w2l_inference_queue_count::get_w2l_inference_queue_count_handler; @@ -69,6 +70,10 @@ pub fn add_moderator_routes (app: App) -> App .route(web::get().to(moderator_get_wallet_handler)) .route(web::head().to(|| HttpResponse::Ok())) ) + .service(web::resource("/wallet/{wallet_token}/add_banked_balance") + .route(web::post().to(moderator_add_banked_balance_to_wallet_handler)) + .route(web::head().to(|| HttpResponse::Ok())) + ) .service(web::scope("/wallets") .service(web::resource("/user/{user_token}/list") .route(web::get().to(list_user_wallets_handler))