Making endpoint available
This commit is contained in:
@@ -42,6 +42,18 @@ pub mod request {
|
||||
pub id: Option<uuid::Uuid>,
|
||||
pub scheduled_message_id: Option<uuid::Uuid>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Deserialize, Serialize, ToSchema)]
|
||||
pub struct UpdateScheduledMessageStatusRequest {
|
||||
pub status: String,
|
||||
pub scheduled_message_id: uuid::Uuid
|
||||
}
|
||||
|
||||
impl UpdateScheduledMessageStatusRequest {
|
||||
pub fn is_valid(&self) -> bool {
|
||||
!self.status.is_empty() || !self.scheduled_message_id.is_nil()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub mod response {
|
||||
@@ -69,6 +81,17 @@ pub mod response {
|
||||
pub use CreateScheduleMessageEventResponse as GetScheduledMessageEventResponse;
|
||||
|
||||
pub use CreateScheduleMessageEventResponse as DeleteScheduledMessageEventResponse;
|
||||
|
||||
#[derive(Debug, Default, Deserialize, Serialize, ToSchema)]
|
||||
pub struct UpdateScheduledMessageStatusResponse {
|
||||
pub message: String,
|
||||
pub data: Vec<ChangedStatus>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Deserialize, Serialize, ToSchema)]
|
||||
pub struct ChangedStatus {
|
||||
pub old_status: String,
|
||||
}
|
||||
}
|
||||
|
||||
pub mod endpoint {
|
||||
@@ -208,6 +231,61 @@ pub mod endpoint {
|
||||
.is_some_and(|t| t - time::OffsetDateTime::now_utc() >= time::Duration::minutes(10))
|
||||
}
|
||||
|
||||
/// Endpoint to update status of Scheduled Message
|
||||
#[utoipa::path(
|
||||
patch,
|
||||
path = crate::caller::endpoints::UPDATE_SCHEDULED_MESSAGE_STATUS,
|
||||
request_body(
|
||||
content = super::request::UpdateScheduledMessageStatusRequest,
|
||||
description = "Data needed to update status of Scheduled Message",
|
||||
content_type = "application/json"
|
||||
),
|
||||
responses(
|
||||
(status = 200, description = "Status updated", body = super::response::UpdateScheduledMessageStatusResponse),
|
||||
(status = 400, description = "Error", body = super::response::UpdateScheduledMessageStatusResponse),
|
||||
(status = 500, description = "Error updating", body = super::response::UpdateScheduledMessageStatusResponse)
|
||||
)
|
||||
)]
|
||||
pub async fn update_scheduled_message_status(
|
||||
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
||||
axum::Json(payload): axum::Json<super::request::UpdateScheduledMessageStatusRequest>,
|
||||
) -> (
|
||||
axum::http::StatusCode,
|
||||
axum::Json<super::response::UpdateScheduledMessageStatusResponse>,
|
||||
) {
|
||||
let mut response = super::response::UpdateScheduledMessageStatusResponse::default();
|
||||
|
||||
if payload.is_valid() {
|
||||
match scheduling_repo::get(&pool, &payload.scheduled_message_id).await {
|
||||
Ok(scheduled_message) => {
|
||||
let old_status = scheduled_message.status;
|
||||
match scheduling_repo::update_status(&pool, &scheduled_message.id, &payload.status).await {
|
||||
Ok(_) => {
|
||||
response.message = String::from(super::super::super::response::SUCCESSFUL);
|
||||
response.data = vec![super::response::ChangedStatus {
|
||||
old_status,
|
||||
}];
|
||||
(axum::http::StatusCode::OK, axum::Json(response))
|
||||
}
|
||||
Err(err) => {
|
||||
eprintln!("Error: {err:?}");
|
||||
response.message = String::from("Invalid");
|
||||
(axum::http::StatusCode::INTERNAL_SERVER_ERROR, axum::Json(response))
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
eprintln!("Error: {err:?}");
|
||||
response.message = String::from("Invalid");
|
||||
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
|
||||
}
|
||||
}
|
||||
} else {
|
||||
response.message = String::from("Request body is not valid");
|
||||
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
|
||||
}
|
||||
}
|
||||
|
||||
/// Endpoint to create a Scheudled Message Event
|
||||
#[utoipa::path(
|
||||
post,
|
||||
|
||||
@@ -154,6 +154,12 @@ pub mod init {
|
||||
axum::middleware::from_fn(crate::auth::auth::<axum::body::Body>),
|
||||
),
|
||||
)
|
||||
.route(
|
||||
crate::caller::endpoints::UPDATE_SCHEDULED_MESSAGE_STATUS,
|
||||
patch(scheduling_endpoints::update_scheduled_message_status).route_layer(
|
||||
axum::middleware::from_fn(crate::auth::auth::<axum::body::Body>),
|
||||
),
|
||||
)
|
||||
.layer(cors::configure_cors().await)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user