A lot of changes #12
Generated
+1
-1
@@ -2277,7 +2277,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "textsender_api"
|
||||
version = "0.1.13"
|
||||
version = "0.1.14"
|
||||
dependencies = [
|
||||
"axum",
|
||||
"axum-extra",
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "textsender_api"
|
||||
version = "0.1.13"
|
||||
version = "0.1.14"
|
||||
edition = "2024"
|
||||
rust-version = "1.96"
|
||||
|
||||
|
||||
@@ -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> {
|
||||
|
||||
+116
@@ -377,6 +377,28 @@ mod request {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn update_scheduled_message_status(
|
||||
app: &axum::Router,
|
||||
id: &uuid::Uuid,
|
||||
updated_status: &str,
|
||||
) -> Result<axum::response::Response, axum::http::Error> {
|
||||
let uri = textsender_api::caller::endpoints::UPDATE_SCHEDULED_MESSAGE_STATUS;
|
||||
let payload = serde_json::json!({
|
||||
"scheduled_message_id": id,
|
||||
"status": updated_status
|
||||
});
|
||||
|
||||
match super::super::run_post(Some(&payload), &uri, axum::http::Method::PATCH, true)
|
||||
.await
|
||||
{
|
||||
Ok(req) => match app.clone().oneshot(req).await {
|
||||
Ok(response) => Ok(response),
|
||||
Err(err) => Err(axum::http::Error::from(err)),
|
||||
},
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn create_scheduled_message_event(
|
||||
app: &axum::Router,
|
||||
contact_id: &uuid::Uuid,
|
||||
@@ -1027,3 +1049,97 @@ async fn test_delete_scheduled_message_event() {
|
||||
|
||||
let _ = db_mgr::drop_database(&tm_pool, &db_name).await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_update_scheduled_message_status() {
|
||||
let (tm_pool, db_name, pool) = db_mgr::get_database_ready().await;
|
||||
let app = init::app(pool).await;
|
||||
|
||||
let mut contact_id: uuid::Uuid = uuid::Uuid::nil();
|
||||
let mut message_id: uuid::Uuid = uuid::Uuid::nil();
|
||||
let mut scheduled_message_id: uuid::Uuid = uuid::Uuid::nil();
|
||||
|
||||
match request::create_contact(&app).await {
|
||||
Ok(response) => {
|
||||
let resp = util::get_resp_data::<
|
||||
textsender_api::caller::contact::response::AddContactResponse,
|
||||
>(response)
|
||||
.await;
|
||||
assert_eq!(false, resp.data.is_empty(), "Should not be empty");
|
||||
contact_id = resp.data[0].id.unwrap();
|
||||
}
|
||||
Err(err) => {
|
||||
assert!(false, "Error: {:?}", err);
|
||||
}
|
||||
};
|
||||
|
||||
match request::message::create_message(&app).await {
|
||||
Ok(response) => {
|
||||
let resp = util::get_resp_data::<
|
||||
textsender_api::caller::message::response::AddMessageResponse,
|
||||
>(response)
|
||||
.await;
|
||||
assert_eq!(false, resp.data.is_empty(), "Should not be empty");
|
||||
let message = &resp.data[0];
|
||||
message_id = message.id.unwrap();
|
||||
}
|
||||
Err(err) => {
|
||||
assert!(false, "Error: {:?}", err);
|
||||
}
|
||||
};
|
||||
|
||||
match request::message::scheduling::create_scheduled_message(&app).await {
|
||||
Ok(response) => {
|
||||
let resp = util::get_resp_data::<
|
||||
textsender_api::caller::message::scheduling::response::ScheduleMessageResponse,
|
||||
>(response)
|
||||
.await;
|
||||
assert_eq!(false, resp.data.is_empty(), "Should not be empty");
|
||||
scheduled_message_id = resp.data[0].id;
|
||||
}
|
||||
Err(err) => {
|
||||
assert!(false, "Error: {err:?}");
|
||||
}
|
||||
}
|
||||
|
||||
match request::message::scheduling::create_scheduled_message_event(
|
||||
&app,
|
||||
&contact_id,
|
||||
&message_id,
|
||||
&scheduled_message_id,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(response) => {
|
||||
let resp = util::get_resp_data::<
|
||||
textsender_api::caller::message::scheduling::response::CreateScheduleMessageEventResponse,
|
||||
>(response)
|
||||
.await;
|
||||
assert_eq!(false, resp.data.is_empty(), "Should not be empty");
|
||||
}
|
||||
Err(err) => {
|
||||
assert!(false, "Error: {err:?}");
|
||||
}
|
||||
}
|
||||
|
||||
match request::message::scheduling::update_scheduled_message_status(
|
||||
&app,
|
||||
&scheduled_message_id,
|
||||
textsender_models::message::scheduling::READY,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(response) => {
|
||||
let resp = util::get_resp_data::<
|
||||
textsender_api::caller::message::scheduling::response::UpdateScheduledMessageStatusResponse,
|
||||
>(response)
|
||||
.await;
|
||||
assert_eq!(false, resp.data.is_empty(), "Should not be empty");
|
||||
}
|
||||
Err(err) => {
|
||||
assert!(false, "Error: {err:?}");
|
||||
}
|
||||
}
|
||||
|
||||
let _ = db_mgr::drop_database(&tm_pool, &db_name).await;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user