Create scheduled message event (#17)
Reviewed-on: phoenix/textsender_api#17
This commit was merged in pull request #17.
This commit is contained in:
@@ -21,6 +21,21 @@ 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 +53,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::scheduling as scheduling_repo;
|
||||
use crate::repo::contact as contact_repo;
|
||||
use crate::repo::message as message_repo;
|
||||
use message_repo::scheduling as scheduling_repo;
|
||||
|
||||
/// Endpoint to create a Scheudled Message
|
||||
#[utoipa::path(
|
||||
@@ -174,4 +197,85 @@ 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))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user