106 lines
2.9 KiB
Rust
106 lines
2.9 KiB
Rust
pub struct Service {
|
|
pub app: crate::app::App,
|
|
pub token: textsender_models::token::Login,
|
|
}
|
|
|
|
impl Service {
|
|
pub async fn do_the_work(&mut self) {
|
|
println!("Do some work");
|
|
println!("Checking queue");
|
|
|
|
use textsender_models::message::scheduling::ScheduledMessage;
|
|
|
|
let queue = super::queue::Queue {
|
|
app: self.app.clone(),
|
|
token: self.token.clone(),
|
|
};
|
|
let queue_item: Option<ScheduledMessage> = match queue.get_queue().await {
|
|
Ok(item) => Some(item),
|
|
Err(err) => {
|
|
eprintln!("Error: {err:?}");
|
|
None
|
|
}
|
|
};
|
|
|
|
if queue_item.is_none() {
|
|
println!("No queue item found");
|
|
return;
|
|
}
|
|
let queue_item = queue_item.unwrap();
|
|
|
|
println!("Queue item Id: {:?}", queue_item.id);
|
|
|
|
if !self.is_schedulable(&queue_item) {
|
|
println!("Not schedulable");
|
|
return;
|
|
}
|
|
|
|
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 contacts = super::contact::Contact {
|
|
app: self.app.clone(),
|
|
token: self.token.clone(),
|
|
};
|
|
let events = events.unwrap_or_default();
|
|
for event in events {
|
|
println!("Event: {event:?}");
|
|
let contact = &match contacts.get(&event.contact_id).await {
|
|
Ok(contact) => contact,
|
|
Err(err) => {
|
|
eprintln!("Error: {err:?}");
|
|
continue;
|
|
}
|
|
}[0];
|
|
|
|
println!("Contact Id: {:?}", contact.id);
|
|
}
|
|
|
|
todo!(
|
|
r#"
|
|
Need to finish this
|
|
|
|
[X] - This function should fetch a token, check if the token is valid, and obtain a refresh token if
|
|
it has expired.
|
|
|
|
Then do the real work.
|
|
|
|
1. Check the queue - Done
|
|
2. If there is an item, get the data - Done
|
|
3. Evaluate if it is schedulable - Done
|
|
4. Get the events and iterate each one - Done
|
|
5. Starting with the message
|
|
6. Get the contact - Done
|
|
7. Send the message
|
|
8. Record the message event response
|
|
9. Add to scheduler
|
|
"#
|
|
);
|
|
}
|
|
|
|
fn is_schedulable(
|
|
&self,
|
|
queue_item: &textsender_models::message::scheduling::ScheduledMessage,
|
|
) -> bool {
|
|
let now = time::OffsetDateTime::now_utc();
|
|
|
|
match queue_item.scheduled {
|
|
Some(date) => date > now,
|
|
None => false,
|
|
}
|
|
}
|
|
}
|