diff --git a/mobile/native/src/api.rs b/mobile/native/src/api.rs index 5f0398607..6130a9fc9 100644 --- a/mobile/native/src/api.rs +++ b/mobile/native/src/api.rs @@ -17,6 +17,7 @@ use crate::trade::order::api::NewOrder; use crate::trade::order::api::Order; use crate::trade::position; use crate::trade::position::api::Position; +use crate::trade::users; use anyhow::Context; use anyhow::Result; use flutter_rust_bridge::frb; @@ -326,7 +327,7 @@ pub fn get_seed_phrase() -> SyncReturn> { /// Enroll a user in the beta program #[tokio::main(flavor = "current_thread")] pub async fn register_beta(email: String) -> Result<()> { - order::register_beta(email).await + users::register_beta(email).await } /// Send the Firebase token to the LSP for push notifications diff --git a/mobile/native/src/trade/mod.rs b/mobile/native/src/trade/mod.rs index e1d7b46a7..546e6c3d6 100644 --- a/mobile/native/src/trade/mod.rs +++ b/mobile/native/src/trade/mod.rs @@ -1,2 +1,3 @@ pub mod order; pub mod position; +pub mod users; diff --git a/mobile/native/src/trade/order/mod.rs b/mobile/native/src/trade/order/mod.rs index 86d3698db..f584209cc 100644 --- a/mobile/native/src/trade/order/mod.rs +++ b/mobile/native/src/trade/order/mod.rs @@ -165,4 +165,3 @@ impl From for orderbook_commons::OrderType { } } } - diff --git a/mobile/native/src/trade/users/mod.rs b/mobile/native/src/trade/users/mod.rs new file mode 100644 index 000000000..7d4482851 --- /dev/null +++ b/mobile/native/src/trade/users/mod.rs @@ -0,0 +1,61 @@ +use crate::commons::reqwest_client; +use crate::config; +use crate::ln_dlc; +use anyhow::anyhow; +use anyhow::Context; +use anyhow::Result; +use coordinator_commons::RegisterParams; +use coordinator_commons::TokenUpdateParams; + +pub async fn update_fcm_token(fcm_token: String) -> Result<()> { + let token_update = TokenUpdateParams { + pubkey: ln_dlc::get_node_info()?.pubkey.to_string(), + fcm_token, + }; + + reqwest_client() + .post(format!( + "http://{}/api/fcm_token", + config::get_http_endpoint() + )) + .json(&token_update) + .send() + .await + .context("Failed to update FCM token with coordinator")? + .error_for_status()?; + Ok(()) +} + +/// Enroll the user in the beta program +pub async fn register_beta(email: String) -> Result<()> { + let register = RegisterParams { + pubkey: ln_dlc::get_node_info()?.pubkey, + email: Some(email), + nostr: None, + }; + + let client = reqwest_client(); + let response = client + .post(format!( + "http://{}/api/register", + config::get_http_endpoint() + )) + .json(®ister) + .send() + .await + .context("Failed to register beta program with coordinator")?; + + if !response.status().is_success() { + let response_text = match response.text().await { + Ok(text) => text, + Err(err) => { + format!("could not decode response {err:#}") + } + }; + return Err(anyhow!( + "Could not register email with coordinator: {response_text}" + )); + } + tracing::info!("Registered into beta program successfully"); + Ok(()) +}