Events #11

Merged
phoenix merged 5 commits from events into v0.3.0 2026-06-24 22:44:14 -04:00
5 changed files with 94 additions and 3 deletions
Generated
+1
View File
@@ -138,6 +138,7 @@ dependencies = [
"textsender_models", "textsender_models",
"time", "time",
"tokio", "tokio",
"uuid",
] ]
[[package]] [[package]]
+1
View File
@@ -11,6 +11,7 @@ reqwest = { version = "0.13.4", features = ["json", "stream", "multipart"] }
serde = { version = "1.0.228", features = ["derive"] } serde = { version = "1.0.228", features = ["derive"] }
serde_json = { version = "1.0.150" } serde_json = { version = "1.0.150" }
time = { version = "0.3.49", features = ["formatting", "macros", "parsing", "serde"] } time = { version = "0.3.49", features = ["formatting", "macros", "parsing", "serde"] }
uuid = { version = "1.23.3", features = ["v4", "serde"] }
textsender_models = { git = "ssh://git@git.kundeng.us/phoenix/textsender_models.git", tag = "v0.4.3-29-720f3d8f75-111" } textsender_models = { git = "ssh://git@git.kundeng.us/phoenix/textsender_models.git", tag = "v0.4.3-29-720f3d8f75-111" }
swoosh = { git = "ssh://git@git.kundeng.us/phoenix/swoosh.git", tag = "v0.4.1-10-6bdec12084-871" } swoosh = { git = "ssh://git@git.kundeng.us/phoenix/swoosh.git", tag = "v0.4.1-10-6bdec12084-871" }
+23 -3
View File
@@ -37,6 +37,25 @@ impl Service {
println!("Can schedule"); println!("Can schedule");
let events_req = super::event::Event {
app: self.app.clone(),
token: self.token.clone(),
};
let scheduled_message_id = queue_item.id;
let events = match events_req.get(&scheduled_message_id).await {
Ok(events) => Some(events),
Err(err) => {
eprintln!("Error: {err:?}");
None
}
};
let events = events.unwrap_or_default();
for event in events {
println!("Event: {event:?}");
}
todo!( todo!(
r#" r#"
Need to finish this Need to finish this
@@ -48,12 +67,13 @@ impl Service {
1. Check the queue - Done 1. Check the queue - Done
2. If there is an item, get the data - Done 2. If there is an item, get the data - Done
3. Evaluate if it is schedulable 3. Evaluate if it is schedulable - Done
4. Get the events and iterate each one 4. Get the events and iterate each one - Done
5. starting with the message 5. starting with the message
6. Get the contact 6. Get the contact
7. Send the message 7. Send the message
8. Record the message event response 8. Record the message event response
9. Add to scheduler
"# "#
); );
} }
@@ -65,7 +85,7 @@ impl Service {
let now = time::OffsetDateTime::now_utc(); let now = time::OffsetDateTime::now_utc();
match queue_item.scheduled { match queue_item.scheduled {
Some(date) => date <= now, Some(date) => date > now,
None => false, None => false,
} }
} }
+68
View File
@@ -0,0 +1,68 @@
pub struct Event {
pub app: crate::app::App,
pub token: textsender_models::token::LoginResult,
}
impl Event {
pub async fn get(
&self,
scheduled_message_id: &uuid::Uuid,
) -> Result<Vec<textsender_models::message::scheduling::ScheduledMessageEvent>, std::io::Error>
{
let client = reqwest::Client::new();
let endpoint =
format!("api/v1/schedule/message/event?scheduled_message_id={scheduled_message_id}");
let api_url = format!("{}/{endpoint}", self.app.api_url);
println!("Url: {api_url:?}");
let (key, header) = super::auth_header(&self.token.access_token);
match client.get(api_url).header(key, header).send().await {
Ok(response) => match response.status() {
reqwest::StatusCode::OK => match response.text().await {
Ok(val) => match parse_response(&val).await {
Ok(login_result) => Ok(login_result),
Err(err) => Err(err),
},
Err(err) => Err(std::io::Error::other(err)),
},
_ => Err(std::io::Error::other("Error getting token")),
},
Err(err) => Err(std::io::Error::other(err.to_string())),
}
}
}
async fn parse_response(
response: &str,
) -> Result<Vec<textsender_models::message::scheduling::ScheduledMessageEvent>, std::io::Error> {
match serde_json::from_str::<serde_json::Value>(response) {
Ok(j) => match j.get("data") {
Some(serde_json::Value::Array(lrs)) => {
if lrs.is_empty() {
Err(std::io::Error::other("Error response is empty"))
} else {
let mut events: Vec<
textsender_models::message::scheduling::ScheduledMessageEvent,
> = Vec::new();
for event in lrs.iter() {
let ll: textsender_models::message::scheduling::ScheduledMessageEvent =
match serde_json::from_value(event.clone()) {
Ok(lr) => lr,
Err(err) => {
return Err(std::io::Error::other(err.to_string()));
}
};
events.push(ll);
}
Ok(events)
}
}
_ => Err(std::io::Error::other("Error parsing response")),
},
Err(err) => Err(std::io::Error::other(err.to_string())),
}
}
+1
View File
@@ -1,4 +1,5 @@
pub mod core; pub mod core;
pub mod event;
pub mod queue; pub mod queue;
pub mod auth { pub mod auth {