Adding code to schedule message

This commit is contained in:
2026-06-17 16:57:35 -04:00
parent 3c3f4d5ac8
commit 5daee39a4e
2 changed files with 36 additions and 6 deletions
+21 -4
View File
@@ -29,7 +29,7 @@ pub mod response {
}
pub mod endpoint {
use crate::repo::message as message_repo;
use crate::repo::scheduling as scheduling_repo;
/// Endpoint to create a Scheudled Message
#[utoipa::path(
@@ -57,10 +57,27 @@ pub mod endpoint {
if payload.is_valid() {
if payload.status != textsender_models::message::scheduling::PENDING {
response.message = String::from("scheduled message must be pending");
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
response.message = String::from("scheduled message must be pending");
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
} else {
if can_schedule(&payload.scheduled) {
let mut scheduled_message = textsender_models::message::scheduling::ScheduledMessage {
user_id: payload.user_id,
..Default::default()
};
match scheduling_repo::insert(&pool, &scheduled_message, &payload.user_id).await {
Ok((id, created)) => {
scheduled_message.id = id;
scheduled_message.created = Some(created);
response.message = String::from("Message scheduled");
(axum::http::StatusCode::CREATED, axum::Json(response))
}
Err(err) => {
eprintln!("Error: {err:?}");
response.message = String::from("Error inserting");
(axum::http::StatusCode::INTERNAL_SERVER_ERROR, axum::Json(response))
}
}
} else {
response.message = String::from("Cannot schedule message");
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
@@ -73,6 +90,6 @@ pub mod endpoint {
}
fn can_schedule(scheduled_time: &Option<time::OffsetDateTime>) -> bool {
false
scheduled_time.is_some_and(|t| t - time::OffsetDateTime::now_utc() >= time::Duration::minutes(10))
}
}