Go go go
This commit is contained in:
+29
-15
@@ -23,12 +23,14 @@ pub mod response {
|
|||||||
#[derive(Debug, Default, Deserialize, Serialize, ToSchema)]
|
#[derive(Debug, Default, Deserialize, Serialize, ToSchema)]
|
||||||
pub struct InstantMessageResponse {
|
pub struct InstantMessageResponse {
|
||||||
pub message: String,
|
pub message: String,
|
||||||
|
pub data: Vec<textsender_models::message::event::MessageEventResponse>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub mod endpoint {
|
pub mod endpoint {
|
||||||
use crate::repo::contact as contact_repo;
|
use crate::repo::contact as contact_repo;
|
||||||
use crate::repo::message as message_repo;
|
use crate::repo::message as message_repo;
|
||||||
|
use message_repo::event as event_repo;
|
||||||
|
|
||||||
/// Endpoint to create Message
|
/// Endpoint to create Message
|
||||||
#[utoipa::path(
|
#[utoipa::path(
|
||||||
@@ -57,7 +59,7 @@ pub mod endpoint {
|
|||||||
if payload.is_valid() {
|
if payload.is_valid() {
|
||||||
let mut contacts: Vec<textsender_models::contact::Contact> = Vec::new();
|
let mut contacts: Vec<textsender_models::contact::Contact> = Vec::new();
|
||||||
|
|
||||||
for contact_id in payload.contact_ids {
|
for contact_id in &payload.contact_ids {
|
||||||
match contact_repo::get(&pool, &contact_id).await {
|
match contact_repo::get(&pool, &contact_id).await {
|
||||||
Ok(contact) => {
|
Ok(contact) => {
|
||||||
contacts.push(contact);
|
contacts.push(contact);
|
||||||
@@ -74,8 +76,11 @@ pub mod endpoint {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !contacts.is_empty() {
|
if !contacts.is_empty() {
|
||||||
|
println!("Valid contacts");
|
||||||
|
|
||||||
match message_repo::get(&pool, &payload.message_id).await {
|
match message_repo::get(&pool, &payload.message_id).await {
|
||||||
Ok(message) => {
|
Ok(message) => {
|
||||||
|
println!("Valid message");
|
||||||
let t_config = match load_config().await {
|
let t_config = match load_config().await {
|
||||||
Ok(config) => config,
|
Ok(config) => config,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
@@ -84,9 +89,10 @@ pub mod endpoint {
|
|||||||
return (axum::http::StatusCode::INTERNAL_SERVER_ERROR, axum::Json(response));
|
return (axum::http::StatusCode::INTERNAL_SERVER_ERROR, axum::Json(response));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let mut results: Vec<reqwest::Response> = Vec::new();
|
let mut results: Vec<textsender_models::message::event::MessageEventResponse> = Vec::new();
|
||||||
|
// let mut results: Vec<serde_json::Value> = Vec::new();
|
||||||
|
|
||||||
for contact in contacts {
|
for contact in &contacts {
|
||||||
let pp = swoosh::twilio::types::Parameters {
|
let pp = swoosh::twilio::types::Parameters {
|
||||||
schedule: false,
|
schedule: false,
|
||||||
schedule_at: None,
|
schedule_at: None,
|
||||||
@@ -97,7 +103,20 @@ pub mod endpoint {
|
|||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
Ok(result) => {
|
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) => {
|
Err(err) => {
|
||||||
eprintln!("Error: {err:?}");
|
eprintln!("Error: {err:?}");
|
||||||
@@ -112,23 +131,18 @@ pub mod endpoint {
|
|||||||
axum::Json(response),
|
axum::Json(response),
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
for result in results {
|
for result in &mut results {
|
||||||
let response_body = match result.text().await {
|
match event_repo::insert(&pool, &result).await {
|
||||||
Ok(response_body) => response_body,
|
Ok(i) => {
|
||||||
Err(err) => {
|
result.id = i;
|
||||||
eprintln!("Error: {err:?}");
|
|
||||||
response.message = String::from("Error parsing body after sending");
|
|
||||||
return (axum::http::StatusCode::INTERNAL_SERVER_ERROR, axum::Json(response));
|
|
||||||
}
|
}
|
||||||
};
|
|
||||||
let val: serde_json::Value = match serde_json::from_str(&response_body) {
|
|
||||||
Ok(val) => val,
|
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
eprintln!("Error: {err:?}");
|
eprintln!("Error: {err:?}");
|
||||||
return (axum::http::StatusCode::INTERNAL_SERVER_ERROR, axum::Json(response));
|
return (axum::http::StatusCode::INTERNAL_SERVER_ERROR, axum::Json(response));
|
||||||
}
|
}
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
response.data = results;
|
||||||
response.message = String::from(super::super::response::SUCCESSFUL);
|
response.message = String::from(super::super::response::SUCCESSFUL);
|
||||||
(axum::http::StatusCode::OK, axum::Json(response))
|
(axum::http::StatusCode::OK, axum::Json(response))
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user