Update scheduled message status endpoint (#20)
Reviewed-on: phoenix/textsender_api#20
This commit was merged in pull request #20.
This commit is contained in:
@@ -42,6 +42,30 @@ 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 {
|
||||
if !self.status.is_empty() || !self.scheduled_message_id.is_nil() {
|
||||
if self.status == textsender_models::message::scheduling::PENDING
|
||||
|| self.status == textsender_models::message::scheduling::READY
|
||||
|| self.status == textsender_models::message::scheduling::PROCESSING
|
||||
|| self.status == textsender_models::message::scheduling::DONE
|
||||
{
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub mod response {
|
||||
@@ -69,6 +93,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 +243,74 @@ 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;
|
||||
if old_status == payload.status {
|
||||
response.message = String::from("No change");
|
||||
(axum::http::StatusCode::NOT_MODIFIED, axum::Json(response))
|
||||
} else {
|
||||
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,
|
||||
|
||||
@@ -18,6 +18,8 @@ pub mod endpoints {
|
||||
pub const SCHEDULE_MESSAGE: &str = "/api/v1/schedule/message";
|
||||
/// Constant for getting scheduled message endpoint
|
||||
pub const GET_SCHEDULE_MESSAGE: &str = "/api/v1/schedule/message";
|
||||
/// Constant for updating scheduled message status endpoint
|
||||
pub const UPDATE_SCHEDULED_MESSAGE_STATUS: &str = "/api/v1/schedule/message/status/update";
|
||||
/// Constant for creating Scheduled Message Event endpoint
|
||||
pub const CREATE_SCHEDULED_MESSAGE_EVENT: &str = "/api/v1/schedule/message/event";
|
||||
/// Constant for getting Scheduled Message Event endpoint
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
@@ -84,6 +84,26 @@ pub async fn get_with_user_id(
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn update_status(
|
||||
pool: &sqlx::PgPool,
|
||||
id: &uuid::Uuid,
|
||||
status: &str,
|
||||
) -> Result<(), sqlx::Error> {
|
||||
match sqlx::query(
|
||||
r#"
|
||||
UPDATE "scheduled_messages" SET status = $1 WHERE id = $2;
|
||||
"#,
|
||||
)
|
||||
.bind(status)
|
||||
.bind(id)
|
||||
.execute(pool)
|
||||
.await
|
||||
{
|
||||
Ok(_row) => Ok(()),
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
|
||||
async fn parse_row(
|
||||
row: &sqlx::postgres::PgRow,
|
||||
) -> Result<textsender_models::message::scheduling::ScheduledMessage, sqlx::Error> {
|
||||
|
||||
Reference in New Issue
Block a user