diff --git a/src/caller/instant.rs b/src/caller/instant.rs index 1f325a1..92f4dde 100644 --- a/src/caller/instant.rs +++ b/src/caller/instant.rs @@ -23,12 +23,14 @@ pub mod response { #[derive(Debug, Default, Deserialize, Serialize, ToSchema)] pub struct InstantMessageResponse { pub message: String, + pub data: Vec } } pub mod endpoint { use crate::repo::contact as contact_repo; use crate::repo::message as message_repo; + use message_repo::event as event_repo; /// Endpoint to create Message #[utoipa::path( @@ -57,7 +59,7 @@ pub mod endpoint { if payload.is_valid() { let mut contacts: Vec = Vec::new(); - for contact_id in payload.contact_ids { + for contact_id in &payload.contact_ids { match contact_repo::get(&pool, &contact_id).await { Ok(contact) => { contacts.push(contact); @@ -74,8 +76,11 @@ pub mod endpoint { } if !contacts.is_empty() { + println!("Valid contacts"); + match message_repo::get(&pool, &payload.message_id).await { Ok(message) => { + println!("Valid message"); let t_config = match load_config().await { Ok(config) => config, Err(err) => { @@ -84,9 +89,10 @@ pub mod endpoint { return (axum::http::StatusCode::INTERNAL_SERVER_ERROR, axum::Json(response)); } }; - let mut results: Vec = Vec::new(); + let mut results: Vec = Vec::new(); + // let mut results: Vec = Vec::new(); - for contact in contacts { + for contact in &contacts { let pp = swoosh::twilio::types::Parameters { schedule: false, schedule_at: None, @@ -97,7 +103,20 @@ pub mod endpoint { .await { Ok(result) => { - results.push(result); + println!("Response: {result:?}"); + let val = swoosh::twilio::api::response_to_json(result).await; + // results.push(val); + let mer = textsender_models::message::event::MessageEventResponse { + user_id: payload.user_id, + // TODO: Make this more accurate + sent: Some(time::OffsetDateTime::now_utc()), + status: String::from(textsender_models::message::event::MESSAGE_EVENT_RESPONSE_STATUS_INSTANT), + contact_id: contact.id.unwrap(), + message_id: message.id.unwrap(), + response: val, + ..Default::default() + }; + results.push(mer); } Err(err) => { eprintln!("Error: {err:?}"); @@ -112,23 +131,18 @@ pub mod endpoint { axum::Json(response), ) } else { - for result in results { - let response_body = match result.text().await { - Ok(response_body) => response_body, - Err(err) => { - eprintln!("Error: {err:?}"); - response.message = String::from("Error parsing body after sending"); - return (axum::http::StatusCode::INTERNAL_SERVER_ERROR, axum::Json(response)); + for result in &mut results { + match event_repo::insert(&pool, &result).await { + Ok(i) => { + result.id = i; } - }; - let val: serde_json::Value = match serde_json::from_str(&response_body) { - Ok(val) => val, Err(err) => { eprintln!("Error: {err:?}"); return (axum::http::StatusCode::INTERNAL_SERVER_ERROR, axum::Json(response)); } - }; + } } + response.data = results; response.message = String::from(super::super::response::SUCCESSFUL); (axum::http::StatusCode::OK, axum::Json(response)) }