diff --git a/crates/api_clients/artcraft_api_defs/src/credits/get_session_credits.rs b/crates/api_clients/artcraft_api_defs/src/credits/get_session_credits.rs new file mode 100644 index 0000000000..a24f00685f --- /dev/null +++ b/crates/api_clients/artcraft_api_defs/src/credits/get_session_credits.rs @@ -0,0 +1,20 @@ +use serde_derive::{Deserialize, Serialize}; +use utoipa::ToSchema; + +#[derive(Serialize, Deserialize, ToSchema)] +pub struct GetSessionCreditsResponse { + pub success: bool, + + /// Any free credits the user might have + /// We might support daily free credits. + pub free_credits: u64, + + /// Any monthly refilling credits the user might have + pub monthly_credits: u64, + + /// Any banked credits the user might have. + pub banked_credits: u64, + + /// All the credit amounts added together. + pub sum_total_credits: u64, +} diff --git a/crates/api_clients/artcraft_api_defs/src/credits/mod.rs b/crates/api_clients/artcraft_api_defs/src/credits/mod.rs new file mode 100644 index 0000000000..c6a4be341d --- /dev/null +++ b/crates/api_clients/artcraft_api_defs/src/credits/mod.rs @@ -0,0 +1 @@ +pub mod get_session_credits; \ No newline at end of file diff --git a/crates/api_clients/artcraft_api_defs/src/lib.rs b/crates/api_clients/artcraft_api_defs/src/lib.rs index 10c12df3b7..7885c1a710 100644 --- a/crates/api_clients/artcraft_api_defs/src/lib.rs +++ b/crates/api_clients/artcraft_api_defs/src/lib.rs @@ -1,7 +1,7 @@ pub mod common; +pub mod credits; pub mod generate; pub mod jobs; pub mod media_file; pub mod prompts; -pub mod stripe_artcraft; -pub mod wallets; \ No newline at end of file +pub mod stripe_artcraft; \ No newline at end of file diff --git a/crates/api_clients/artcraft_api_defs/src/wallets/get_primary_wallet_for_user.rs b/crates/api_clients/artcraft_api_defs/src/wallets/get_primary_wallet_for_user.rs deleted file mode 100644 index 699816c2a1..0000000000 --- a/crates/api_clients/artcraft_api_defs/src/wallets/get_primary_wallet_for_user.rs +++ /dev/null @@ -1,20 +0,0 @@ -use enums::common::payments_namespace::PaymentsNamespace; -use serde_derive::{Deserialize, Serialize}; -use tokens::tokens::wallets::WalletToken; -use utoipa::ToSchema; - -#[derive(Serialize, Deserialize, ToSchema)] -pub struct GetPrimaryWalletForUserResponse { - pub success: bool, - - /// The primary wallet, if it exists. - pub maybe_primary_wallet: Option, -} - -#[derive(Serialize, Deserialize, ToSchema)] -pub struct WalletDetails { - pub token: WalletToken, - pub namespace: PaymentsNamespace, - pub banked_credits: u64, - pub monthly_credits: u64, -} diff --git a/crates/api_clients/artcraft_api_defs/src/wallets/mod.rs b/crates/api_clients/artcraft_api_defs/src/wallets/mod.rs deleted file mode 100644 index 2cb9d950f1..0000000000 --- a/crates/api_clients/artcraft_api_defs/src/wallets/mod.rs +++ /dev/null @@ -1 +0,0 @@ -pub mod get_primary_wallet_for_user; \ No newline at end of file diff --git a/crates/api_clients/storyteller_client/src/wallets/get_primary_wallet_for_user.rs b/crates/api_clients/storyteller_client/src/credits/get_session_credits.rs similarity index 54% rename from crates/api_clients/storyteller_client/src/wallets/get_primary_wallet_for_user.rs rename to crates/api_clients/storyteller_client/src/credits/get_session_credits.rs index 1bf0eb04e5..5583b212c4 100644 --- a/crates/api_clients/storyteller_client/src/wallets/get_primary_wallet_for_user.rs +++ b/crates/api_clients/storyteller_client/src/credits/get_session_credits.rs @@ -2,18 +2,15 @@ use crate::credentials::storyteller_credential_set::StorytellerCredentialSet; use crate::error::storyteller_error::StorytellerError; use crate::utils::api_host::ApiHost; use crate::utils::basic_json_get_request::basic_json_get_request; -use crate::utils::basic_json_post_request::basic_json_post_request; -use artcraft_api_defs::media_file::list_batch_generated_media_files::LIST_BATCH_GENERATED_REDUX_MEDIA_FILES_URL_PATH; -use artcraft_api_defs::stripe_artcraft::create_credits_pack_checkout::{StripeArtcraftCreateCreditsPackCheckoutRequest, StripeArtcraftCreateCreditsPackCheckoutResponse, CREATE_CREDITS_PACK_CHECKOUT_URL_PATH}; -use artcraft_api_defs::wallets::get_primary_wallet_for_user::GetPrimaryWalletForUserResponse; +use artcraft_api_defs::credits::get_session_credits::GetSessionCreditsResponse; use enums::common::payments_namespace::PaymentsNamespace; use log::debug; -pub async fn get_primary_wallet_for_user( +pub async fn get_session_credits( api_host: &ApiHost, maybe_creds: Option<&StorytellerCredentialSet>, payments_namespace: PaymentsNamespace -) -> Result { +) -> Result { let url = get_url(api_host, payments_namespace); debug!("Requesting {:?}", &url); @@ -29,6 +26,6 @@ pub async fn get_primary_wallet_for_user( fn get_url(api_host: &ApiHost, payments_namespace: PaymentsNamespace) -> String { let api_hostname_and_scheme = api_host.to_api_hostname_and_scheme(); let payments_namespace = payments_namespace.to_str(); - format!("{}/v1/wallets/primary_wallet/{}", api_hostname_and_scheme, payments_namespace) + format!("{}/v1/credits/{}", api_hostname_and_scheme, payments_namespace) } diff --git a/crates/api_clients/storyteller_client/src/credits/mod.rs b/crates/api_clients/storyteller_client/src/credits/mod.rs new file mode 100644 index 0000000000..c6a4be341d --- /dev/null +++ b/crates/api_clients/storyteller_client/src/credits/mod.rs @@ -0,0 +1 @@ +pub mod get_session_credits; \ No newline at end of file diff --git a/crates/api_clients/storyteller_client/src/lib.rs b/crates/api_clients/storyteller_client/src/lib.rs index ae9d6eb20c..ea23161c79 100644 --- a/crates/api_clients/storyteller_client/src/lib.rs +++ b/crates/api_clients/storyteller_client/src/lib.rs @@ -1,6 +1,7 @@ //! An API client for Storyteller / FakeYou pub mod credentials; +pub mod credits; pub mod error; pub mod generate; pub mod jobs; @@ -9,4 +10,3 @@ pub mod prompts; pub mod recipes; pub mod stripe_artcraft; pub mod utils; -pub mod wallets; diff --git a/crates/api_clients/storyteller_client/src/wallets/mod.rs b/crates/api_clients/storyteller_client/src/wallets/mod.rs deleted file mode 100644 index 2cb9d950f1..0000000000 --- a/crates/api_clients/storyteller_client/src/wallets/mod.rs +++ /dev/null @@ -1 +0,0 @@ -pub mod get_primary_wallet_for_user; \ No newline at end of file diff --git a/crates/service/web/storyteller_web/src/http_server/endpoints/wallets/get_primary_wallet_for_user_handler.rs b/crates/service/web/storyteller_web/src/http_server/endpoints/credits/get_session_credits_handler.rs similarity index 57% rename from crates/service/web/storyteller_web/src/http_server/endpoints/wallets/get_primary_wallet_for_user_handler.rs rename to crates/service/web/storyteller_web/src/http_server/endpoints/credits/get_session_credits_handler.rs index 82f9511341..ac01e43d83 100644 --- a/crates/service/web/storyteller_web/src/http_server/endpoints/wallets/get_primary_wallet_for_user_handler.rs +++ b/crates/service/web/storyteller_web/src/http_server/endpoints/credits/get_session_credits_handler.rs @@ -3,23 +3,12 @@ use std::sync::Arc; use crate::http_server::common_responses::common_web_error::CommonWebError; use crate::http_server::common_responses::media::media_links_builder::MediaLinksBuilder; -use crate::http_server::endpoints::media_files::helpers::get_media_domain::get_media_domain; -use crate::http_server::web_utils::response_error_helpers::to_simple_json_error; use crate::state::server_state::ServerState; -use actix_web::error::ResponseError; -use actix_web::http::StatusCode; use actix_web::web::{Json, Path}; use actix_web::{web, HttpMessage, HttpRequest, HttpResponse}; -use artcraft_api_defs::common::responses::media_links::MediaLinks; -use artcraft_api_defs::wallets::get_primary_wallet_for_user::{GetPrimaryWalletForUserResponse, WalletDetails}; -use bucket_paths::legacy::typified_paths::public::media_files::bucket_file_path::MediaFileBucketPath; +use artcraft_api_defs::credits::get_session_credits::GetSessionCreditsResponse; use chrono::{DateTime, Utc}; -use enums::by_table::prompt_context_items::prompt_context_semantic_type::PromptContextSemanticType; -use enums::by_table::prompts::prompt_type::PromptType; -use enums::common::generation_provider::GenerationProvider; -use enums::common::model_type::ModelType; use enums::common::payments_namespace::PaymentsNamespace; -use enums::no_table::style_transfer::style_transfer_name::StyleTransferName; use log::{error, warn}; use mysql_queries::queries::prompt_context_items::list_prompt_context_items::list_prompt_context_items; use mysql_queries::queries::prompts::get_prompt::{get_prompt, get_prompt_from_connection}; @@ -30,26 +19,26 @@ use utoipa::ToSchema; /// For the URL PathInfo #[derive(Deserialize, ToSchema)] -pub struct GetPrimaryWalletForUserPathInfo { +pub struct GetSessionCreditsPathInfo { namespace: PaymentsNamespace, } #[utoipa::path( get, - tag = "Wallets", - path = "/v1/wallets/primary_wallet/{namespace}", + tag = "Credits", + path = "/v1/credits/namespace/{namespace}", responses( - (status = 200, description = "Success", body = GetPrimaryWalletForUserResponse), + (status = 200, description = "Success", body = GetSessionCreditsResponse), ), params( - ("path" = GetPromptPathInfo, description = "Path for Request") + ("path" = GetSessionCreditsPathInfo, description = "Path for Request") ) )] -pub async fn get_prompt_handler( +pub async fn get_session_credits_handler( http_request: HttpRequest, - path: Path, + path: Path, server_state: web::Data> -) -> Result, CommonWebError> +) -> Result, CommonWebError> { let mut mysql_connection = server_state.mysql_pool .acquire() @@ -67,7 +56,7 @@ pub async fn get_prompt_handler( warn!("Session checker error: {:?}", e); CommonWebError::ServerError })?; - + let user_token = match maybe_user_session { Some(session) => session.user_token, None => return Err(CommonWebError::NotAuthorized), @@ -82,13 +71,27 @@ pub async fn get_prompt_handler( CommonWebError::ServerError })?; - Ok(Json(GetPrimaryWalletForUserResponse { + let free_credits = 0u64; + + let monthly_credits = maybe_wallet + .as_ref() + .map(|w| w.monthly_credits) + .unwrap_or(0); + + let banked_credits = maybe_wallet + .as_ref() + .map(|w| w.banked_credits) + .unwrap_or(0); + + let sum_total_credits = free_credits + .saturating_add(monthly_credits) + .saturating_add(banked_credits); + + Ok(Json(GetSessionCreditsResponse { success: true, - maybe_primary_wallet: maybe_wallet.map(|wallet| WalletDetails { - token: wallet.token, - namespace: wallet.namespace, - banked_credits: wallet.banked_credits, - monthly_credits: wallet.monthly_credits, - }) + free_credits, + monthly_credits, + banked_credits, + sum_total_credits, })) } diff --git a/crates/service/web/storyteller_web/src/http_server/endpoints/credits/mod.rs b/crates/service/web/storyteller_web/src/http_server/endpoints/credits/mod.rs new file mode 100644 index 0000000000..75ad61f200 --- /dev/null +++ b/crates/service/web/storyteller_web/src/http_server/endpoints/credits/mod.rs @@ -0,0 +1 @@ +pub mod get_session_credits_handler; \ No newline at end of file diff --git a/crates/service/web/storyteller_web/src/http_server/endpoints/mod.rs b/crates/service/web/storyteller_web/src/http_server/endpoints/mod.rs index 28e1188bab..0abb5d45aa 100644 --- a/crates/service/web/storyteller_web/src/http_server/endpoints/mod.rs +++ b/crates/service/web/storyteller_web/src/http_server/endpoints/mod.rs @@ -2,6 +2,7 @@ pub mod analytics; pub mod app_state; pub mod beta_keys; pub mod comments; +pub mod credits; pub mod download_job; pub mod featured_items; pub mod generate; @@ -25,6 +26,5 @@ pub mod users; pub mod voice_clone_requests; pub mod voice_conversion; pub mod voice_designer; -pub mod wallets; pub mod webhooks; pub mod weights; diff --git a/crates/service/web/storyteller_web/src/http_server/endpoints/wallets/mod.rs b/crates/service/web/storyteller_web/src/http_server/endpoints/wallets/mod.rs deleted file mode 100644 index 7e06d6d437..0000000000 --- a/crates/service/web/storyteller_web/src/http_server/endpoints/wallets/mod.rs +++ /dev/null @@ -1 +0,0 @@ -pub mod get_primary_wallet_for_user_handler; \ No newline at end of file diff --git a/crates/service/web/storyteller_web/src/http_server/routes/add_credits_routes.rs b/crates/service/web/storyteller_web/src/http_server/routes/add_credits_routes.rs new file mode 100644 index 0000000000..73fc940b51 --- /dev/null +++ b/crates/service/web/storyteller_web/src/http_server/routes/add_credits_routes.rs @@ -0,0 +1,24 @@ +use crate::http_server::endpoints::credits::get_session_credits_handler::get_session_credits_handler; +use actix_http::body::MessageBody; +use actix_service::ServiceFactory; +use actix_web::dev::{ServiceRequest, ServiceResponse}; +use actix_web::{web, App, Error, HttpResponse}; + +pub fn add_credits_routes (app: App) -> App +where + B: MessageBody, + T: ServiceFactory< + ServiceRequest, + Config = (), + Response = ServiceResponse, + Error = Error, + InitError = (), + >, +{ + app.service(web::scope("/v1/credits") + .service(web::resource("/namespace/{namespace}") + .route(web::post().to(get_session_credits_handler)) + .route(web::head().to(|| HttpResponse::Ok())) + ) + ) +} diff --git a/crates/service/web/storyteller_web/src/http_server/routes/add_routes.rs b/crates/service/web/storyteller_web/src/http_server/routes/add_routes.rs index 57aa5ecdd1..3748aa3db6 100644 --- a/crates/service/web/storyteller_web/src/http_server/routes/add_routes.rs +++ b/crates/service/web/storyteller_web/src/http_server/routes/add_routes.rs @@ -125,6 +125,7 @@ use crate::http_server::endpoints::voice_designer::voices::list_voices_by_user:: use crate::http_server::endpoints::voice_designer::voices::search_voices::search_voices; use crate::http_server::endpoints::voice_designer::voices::update_voice::update_voice_handler; use crate::http_server::routes::add_control_plane_routes::add_control_plane_routes; +use crate::http_server::routes::add_credits_routes::add_credits_routes; use crate::http_server::routes::add_generate_routes::add_generate_routes; use crate::http_server::routes::add_image_studio_routes::add_image_studio_routes; use crate::http_server::routes::add_stripe_artcraft_routes::add_stripe_artcraft_routes; @@ -151,39 +152,40 @@ pub fn add_routes (app: App, server_environment: ServerEnvironment) -> InitError = (), >, { - let mut app = add_moderator_routes(app); /* /moderation */ + let mut app = add_moderator_routes(app); // /moderation app = add_user_routes(app); // /create_account, /session, /login, /logout, etc. - app = add_tts_routes(app); /* /tts */ - app = add_w2l_routes(app); /* /w2l */ - app = add_web_vc_routes(app); /* /v1/voice_conversion */ - app = add_vocoder_routes(app); /* /vocoder */ - app = add_remote_download_routes(app); /* /v1/remote_downloads (prev. /retrieval, aka. "generic_download_jobs") */ - app = add_category_routes(app); /* /category */ - app = add_api_token_routes(app); /* /api_tokens */ - app = add_voice_clone_request_routes(app); /* /voice_clone_requests */ - app = add_investor_demo_routes(app); /* /demo_mode */ // TODO: DEFINITELY TEMPORARY - app = add_flag_routes(app); /* /flag */ - app = add_desktop_app_routes(app); /* /v1/vc/... */ - app = add_media_file_routes(app); /* /v1/media_files/... */ - app = add_media_upload_routes(app); /* /v1/media_upload/... */ - app = add_trending_routes(app); /* /v1/trending/... */ - app = add_user_rating_routes(app); /* /v1/user_rating/... */ - app = add_featured_item_routes(app); /* /v1/featured_item/... */ - app = add_subscription_routes(app); /* /v1/subscriptions/... */ - app = add_voice_designer_routes(app); /* /v1/voice_designer */ - app = add_beta_key_routes(app); /* /v1/beta_keys */ + app = add_tts_routes(app); // /tts + app = add_w2l_routes(app); // /w2l + app = add_web_vc_routes(app); // /v1/voice_conversion + app = add_vocoder_routes(app); // /vocoder + app = add_remote_download_routes(app); // /v1/remote_downloads (prev. /retrieval, aka. "generic_download_jobs") + app = add_category_routes(app); // /category + app = add_api_token_routes(app); // /api_tokens + app = add_voice_clone_request_routes(app); // /voice_clone_requests + app = add_investor_demo_routes(app); // /demo_mode // TODO: DEFINITELY TEMPORARY + app = add_flag_routes(app); // /flag + app = add_desktop_app_routes(app); // /v1/vc/... + app = add_media_file_routes(app); // /v1/media_files/... + app = add_media_upload_routes(app); // /v1/media_upload/... + app = add_trending_routes(app); // /v1/trending/... + app = add_user_rating_routes(app); // /v1/user_rating/... + app = add_featured_item_routes(app); // /v1/featured_item/... + app = add_subscription_routes(app); // /v1/subscriptions/... + app = add_voice_designer_routes(app); // /v1/voice_designer + app = add_beta_key_routes(app); // /v1/beta_keys app = add_weights_routes(app); - app = add_tag_routes(app); /* /v1/tags */ + app = add_tag_routes(app); // /v1/tags app = add_model_download_routes(app); app = add_workflow_routes(app); app = add_studio_gen2_routes(app); app = add_image_studio_routes(app); app = add_job_routes(app); - app = add_engine_routes(app); /* /v1/engine/... */ - app = add_control_plane_routes(app); /* /v1/control_plane/... */ - app = add_generate_routes(app); /* /v1/generate/... */ - app = add_webhook_routes(app); /* /v1/webhooks/... */ - app = add_stripe_artcraft_routes(app); /* /v1/stripe_artcraft/... */ + app = add_engine_routes(app); // /v1/engine/... + app = add_control_plane_routes(app); // /v1/control_plane/... + app = add_generate_routes(app); // /v1/generate/... + app = add_webhook_routes(app); // /v1/webhooks/... + app = add_stripe_artcraft_routes(app); // /v1/stripe_artcraft/... + app = add_credits_routes(app); // /v1/credits/... // app = add_image_gen_routes(app); diff --git a/crates/service/web/storyteller_web/src/http_server/routes/mod.rs b/crates/service/web/storyteller_web/src/http_server/routes/mod.rs index 6463359aae..ef07a16865 100644 --- a/crates/service/web/storyteller_web/src/http_server/routes/mod.rs +++ b/crates/service/web/storyteller_web/src/http_server/routes/mod.rs @@ -1,6 +1,8 @@ mod add_control_plane_routes; +mod add_credits_routes; mod add_generate_routes; mod add_image_studio_routes; +mod add_stripe_artcraft_routes; mod add_studio_gen2_routes; mod add_webhook_routes; mod beta_key_routes; @@ -12,5 +14,4 @@ mod tag_routes; mod user_routes; mod weights_routes; mod workflow_routes; -pub mod add_routes; -mod add_stripe_artcraft_routes; \ No newline at end of file +pub mod add_routes; \ No newline at end of file