Adding code for create sme

This commit is contained in:
2026-06-18 17:10:29 -04:00
parent e211fa7ff4
commit cb81d0479f
2 changed files with 218 additions and 1 deletions
+113 -1
View File
@@ -21,6 +21,19 @@ pub mod request {
pub id: Option<uuid::Uuid>,
pub user_id: Option<uuid::Uuid>,
}
#[derive(Debug, Default, serde::Deserialize, serde::Serialize, utoipa::ToSchema)]
pub struct CreateScheduleMessageEventRequest {
pub contact_id: uuid::Uuid,
pub message_id: uuid::Uuid,
pub scheduled_message_id: uuid::Uuid,
}
impl CreateScheduleMessageEventRequest {
pub fn is_valid(&self) -> bool {
!self.contact_id.is_nil() || !self.message_id.is_nil() || !self.scheduled_message_id.is_nil()
}
}
}
pub mod response {
@@ -38,10 +51,18 @@ pub mod response {
pub message: String,
pub data: Vec<textsender_models::message::scheduling::ScheduledMessage>,
}
#[derive(Debug, Default, Deserialize, Serialize, ToSchema)]
pub struct CreateScheduleMessageEventResponse {
pub message: String,
pub data: Vec<textsender_models::message::scheduling::ScheduledMessageEvent>,
}
}
pub mod endpoint {
use crate::repo::message::scheduling as scheduling_repo;
use crate::repo::message as message_repo;
use message_repo::scheduling as scheduling_repo;
use crate::repo::contact as contact_repo;
/// Endpoint to create a Scheudled Message
#[utoipa::path(
@@ -174,4 +195,95 @@ pub mod endpoint {
scheduled_time
.is_some_and(|t| t - time::OffsetDateTime::now_utc() >= time::Duration::minutes(10))
}
/// Endpoint to create a Scheudled Message Event
#[utoipa::path(
post,
path = crate::caller::endpoints::CREATE_SCHEDULED_MESSAGE_EVENT,
request_body(
content = super::request::CreateScheduleMessageEventRequest,
description = "Data needed to create a Scheduled Message Event",
content_type = "application/json"
),
responses(
(status = 201, description = "Scheduled Message Event created", body = super::response::CreateScheduleMessageEventResponse),
(status = 400, description = "Error", body = super::response::CreateScheduleMessageEventResponse),
(status = 500, description = "Error creating Scheduled Message Event", body = super::response::CreateScheduleMessageEventResponse)
)
)]
pub async fn create_scheduled_message_event(
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
axum::Json(payload): axum::Json<super::request::CreateScheduleMessageEventRequest>,
) -> (
axum::http::StatusCode,
axum::Json<super::response::CreateScheduleMessageEventResponse>,
) {
let mut response = super::response::CreateScheduleMessageEventResponse::default();
if payload.is_valid() {
match scheduling_repo::get(&pool, &payload.scheduled_message_id).await {
Ok(_) => { }
Err(err) => {
eprintln!("Error: {err:?}");
response.message = String::from("Scheduled Message does not exist");
return (
axum::http::StatusCode::BAD_REQUEST,
axum::Json(response),
);
}
}
match contact_repo::get(&pool, &payload.contact_id).await {
Ok(_) => { }
Err(err) => {
eprintln!("Error: {err:?}");
response.message = String::from("Contact does not exist");
return (
axum::http::StatusCode::BAD_REQUEST,
axum::Json(response),
);
}
}
match message_repo::get(&pool, &payload.message_id).await {
Ok(_) => { }
Err(err) => {
eprintln!("Error: {err:?}");
response.message = String::from("Contact does not exist");
return (
axum::http::StatusCode::BAD_REQUEST,
axum::Json(response),
);
}
}
let mut scme = textsender_models::message::scheduling::ScheduledMessageEvent {
contact_id: payload.contact_id,
message_id: payload.message_id,
scheduled_message_id: payload.scheduled_message_id,
..Default::default()
};
match scheduling_repo::sched_msg_event::insert(&pool, &scme).await {
Ok((id, created)) => {
scme.id = id;
scme.created = Some(created);
response.message = String::from(super::super::super::response::SUCCESSFUL);
response.data.push(scme);
(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("Request body is not valid");
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
}
}
}