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))
}
}
+15 -2
View File
@@ -16,10 +16,14 @@ pub mod init {
use crate::caller::contact as contact_caller;
use crate::caller::message as message_caller;
use crate::caller::message::scheduling as scheduling_caller;
use contact_caller::endpoint as contact_endpoints;
use contact_caller::response as contact_responses;
use message_caller::endpoint as message_endpoints;
use message_caller::response as message_responses;
use scheduling_caller::response as scheduling_responses;
use scheduling_caller::endpoint as scheduling_endpoints;
mod cors {
pub async fn configure_cors() -> tower_http::cors::CorsLayer {
@@ -77,8 +81,11 @@ pub mod init {
#[derive(utoipa::OpenApi)]
#[openapi(
paths(contact_endpoints::create_contact,
message_endpoints::create_message),
components(schemas(contact_responses::AddContactResponse, message_responses::AddMessageResponse)),
message_endpoints::create_message,
scheduling_endpoints::schedule_message,
),
components(schemas(contact_responses::AddContactResponse, message_responses::AddMessageResponse,
scheduling_responses::ScheduleMessageResponse)),
tags(
(name = "textsender API", description = "Web API to manage texting")
)
@@ -117,6 +124,12 @@ pub mod init {
crate::auth::auth::<axum::body::Body>,
)),
)
.route(
crate::caller::endpoints::SCHEDULE_MESSAGE,
get(scheduling_endpoints::schedule_message).route_layer(axum::middleware::from_fn(
crate::auth::auth::<axum::body::Body>,
)),
)
.layer(cors::configure_cors().await)
}