A lot of changes #12
Generated
+4
-3
@@ -2277,7 +2277,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "textsender_api"
|
||||
version = "0.1.15"
|
||||
version = "0.1.16"
|
||||
dependencies = [
|
||||
"axum",
|
||||
"axum-extra",
|
||||
@@ -2303,14 +2303,15 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "textsender_models"
|
||||
version = "0.4.1"
|
||||
source = "git+ssh://git@git.kundeng.us/phoenix/textsender_models.git?tag=v0.4.1-29-ad632fca16-111#ad632fca168ec817e61dd364716f8030c9cfb0b3"
|
||||
version = "0.4.3"
|
||||
source = "git+ssh://git@git.kundeng.us/phoenix/textsender_models.git?tag=v0.4.3-29-384dd41690-111#384dd416902c88aa6c9c58bb5a9dfa04567ae455"
|
||||
dependencies = [
|
||||
"const_format",
|
||||
"dotenvy",
|
||||
"josekit",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"sqlx",
|
||||
"time",
|
||||
"utoipa",
|
||||
"uuid",
|
||||
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "textsender_api"
|
||||
version = "0.1.15"
|
||||
version = "0.1.16"
|
||||
edition = "2024"
|
||||
rust-version = "1.96"
|
||||
|
||||
@@ -22,7 +22,7 @@ jsonwebtoken = { version = "10.4.0", features = ["rust_crypto"] }
|
||||
josekit = { version = "0.10.3" }
|
||||
utoipa = { version = "5.5.0", features = ["axum_extras"] }
|
||||
utoipa-swagger-ui = { version = "9.0.2", features = ["axum"] }
|
||||
textsender_models = { git = "ssh://git@git.kundeng.us/phoenix/textsender_models.git", tag = "v0.4.1-29-ad632fca16-111" }
|
||||
textsender_models = { git = "ssh://git@git.kundeng.us/phoenix/textsender_models.git", tag = "v0.4.3-29-384dd41690-111" }
|
||||
|
||||
[dev-dependencies]
|
||||
url = { version = "2.5.8" }
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
pub mod request {
|
||||
use serde::{Deserialize, Serialize};
|
||||
use utoipa::ToSchema;
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize, ToSchema)]
|
||||
pub struct RecordMessageEventRequest {
|
||||
pub scheduled_message_event_id: uuid::Uuid,
|
||||
pub response: serde_json::Value,
|
||||
pub user_id: uuid::Uuid,
|
||||
#[serde(with = "time::serde::rfc3339::option")]
|
||||
pub sent: Option<time::OffsetDateTime>,
|
||||
pub status: String,
|
||||
pub contact_id: uuid::Uuid,
|
||||
pub message_id: uuid::Uuid,
|
||||
}
|
||||
|
||||
impl RecordMessageEventRequest {
|
||||
pub fn is_valid(&self) -> bool {
|
||||
!self.scheduled_message_event_id.is_nil()
|
||||
|| !self.response.is_null()
|
||||
|| !self.user_id.is_nil()
|
||||
|| self.sent.is_some()
|
||||
|| !self.status.is_empty()
|
||||
|| !self.contact_id.is_nil()
|
||||
|| !self.message_id.is_nil()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub mod response {
|
||||
use serde::{Deserialize, Serialize};
|
||||
use utoipa::ToSchema;
|
||||
|
||||
#[derive(Debug, Default, Deserialize, Serialize, ToSchema)]
|
||||
pub struct RecordMessageEventResponse {
|
||||
pub message: String,
|
||||
pub data: Vec<textsender_models::message::event::MessageEventResponse>,
|
||||
}
|
||||
}
|
||||
|
||||
pub mod endpoint {
|
||||
use crate::repo::message::event as event_repo;
|
||||
|
||||
/// Endpoint to create a Message Event Response
|
||||
#[utoipa::path(
|
||||
post,
|
||||
path = crate::caller::endpoints::RECORD_MESSAGE_EVENT_RESPONSE,
|
||||
request_body(
|
||||
content = super::request::RecordMessageEventRequest,
|
||||
description = "Data needed to create a Message Event Response",
|
||||
content_type = "application/json"
|
||||
),
|
||||
responses(
|
||||
(status = 201, description = "Message Event Response created", body = super::response::RecordMessageEventResponse),
|
||||
(status = 400, description = "Error", body = super::response::RecordMessageEventResponse),
|
||||
(status = 500, description = "Error creating Message Event Response", body = super::response::RecordMessageEventResponse)
|
||||
)
|
||||
)]
|
||||
pub async fn record_message_event_response(
|
||||
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
||||
axum::Json(payload): axum::Json<super::request::RecordMessageEventRequest>,
|
||||
) -> (
|
||||
axum::http::StatusCode,
|
||||
axum::Json<super::response::RecordMessageEventResponse>,
|
||||
) {
|
||||
let mut response = super::response::RecordMessageEventResponse::default();
|
||||
|
||||
if payload.is_valid() {
|
||||
let mut mer = textsender_models::message::event::MessageEventResponse {
|
||||
scheduled_message_event_id: payload.scheduled_message_event_id,
|
||||
user_id: payload.user_id,
|
||||
sent: payload.sent,
|
||||
status: payload.status,
|
||||
contact_id: payload.contact_id,
|
||||
message_id: payload.message_id,
|
||||
response: payload.response,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
match event_repo::insert(&pool, &mer).await {
|
||||
Ok(id) => {
|
||||
mer.id = id;
|
||||
response.message = String::from(super::super::super::response::SUCCESSFUL);
|
||||
response.data.push(mer);
|
||||
(axum::http::StatusCode::CREATED, axum::Json(response))
|
||||
}
|
||||
Err(err) => {
|
||||
eprintln!("Error: {err:?}");
|
||||
response.message = String::from("Request body is not valid");
|
||||
(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))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
pub mod event;
|
||||
pub mod scheduling;
|
||||
|
||||
pub mod request {
|
||||
|
||||
@@ -28,6 +28,9 @@ pub mod endpoints {
|
||||
pub const DELETE_SCHEDULED_MESSAGE_EVENT: &str = "/api/v1/schedule/message/event/{id}";
|
||||
/// Constant for fetching Scheduled Message endpoint
|
||||
pub const FETCH_SCHEDULED_MESSAGE: &str = "/api/v1/schedule/message/fetch";
|
||||
/// Constant for recording Message Event Response endpoint
|
||||
pub const RECORD_MESSAGE_EVENT_RESPONSE: &str =
|
||||
"/api/v1/schedule/message/event/response/record";
|
||||
}
|
||||
|
||||
pub mod response {
|
||||
|
||||
+12
-1
@@ -16,10 +16,13 @@ pub mod init {
|
||||
|
||||
use crate::caller::contact as contact_caller;
|
||||
use crate::caller::message as message_caller;
|
||||
use crate::caller::message::event as event_caller;
|
||||
use crate::caller::message::scheduling as scheduling_caller;
|
||||
|
||||
use contact_caller::endpoint as contact_endpoints;
|
||||
use contact_caller::response as contact_responses;
|
||||
use event_caller::endpoint as event_endpoints;
|
||||
use event_caller::response as event_responses;
|
||||
use message_caller::endpoint as message_endpoints;
|
||||
use message_caller::response as message_responses;
|
||||
use scheduling_caller::endpoint as scheduling_endpoints;
|
||||
@@ -85,7 +88,9 @@ pub mod init {
|
||||
scheduling_endpoints::schedule_message,
|
||||
),
|
||||
components(schemas(contact_responses::AddContactResponse, message_responses::AddMessageResponse,
|
||||
scheduling_responses::ScheduleMessageResponse)),
|
||||
scheduling_responses::ScheduleMessageResponse,
|
||||
event_responses::RecordMessageEventResponse
|
||||
)),
|
||||
tags(
|
||||
(name = "textsender API", description = "Web API to manage texting")
|
||||
)
|
||||
@@ -166,6 +171,12 @@ pub mod init {
|
||||
axum::middleware::from_fn(crate::auth::auth::<axum::body::Body>),
|
||||
),
|
||||
)
|
||||
.route(
|
||||
crate::caller::endpoints::RECORD_MESSAGE_EVENT_RESPONSE,
|
||||
post(event_endpoints::record_message_event_response).route_layer(
|
||||
axum::middleware::from_fn(crate::auth::auth::<axum::body::Body>),
|
||||
),
|
||||
)
|
||||
.layer(cors::configure_cors().await)
|
||||
}
|
||||
|
||||
|
||||
@@ -89,8 +89,9 @@ pub async fn get_with_user_id(
|
||||
async fn parse_row(
|
||||
row: &sqlx::postgres::PgRow,
|
||||
) -> Result<textsender_models::message::event::MessageEventResponse, sqlx::Error> {
|
||||
println!("Parsing MER");
|
||||
let id: uuid::Uuid = row.try_get("id")?;
|
||||
let response: String = row.try_get("response")?;
|
||||
let response: serde_json::Value = row.try_get("response")?;
|
||||
let status: String = row.try_get("status")?;
|
||||
let contact_id: uuid::Uuid = row.try_get("contact_id")?;
|
||||
let message_id: uuid::Uuid = row.try_get("message_id")?;
|
||||
|
||||
+213
-1
@@ -154,6 +154,33 @@ pub const TEST_CONTACT_UPDATED_NICKNAME: &str = "The Man in Black";
|
||||
/// Test message content
|
||||
pub const TEST_MESSAGE_CONTENT: &str = "The wind cries mary";
|
||||
|
||||
pub fn test_mer_response() -> serde_json::Value {
|
||||
serde_json::json!({
|
||||
"account_sid": "34n97yf342y5fc349287yndx3987i4yf5c3w98dy398",
|
||||
"api_version": "2010-04-01",
|
||||
"body": "HEALINGHURTS.LETITIN.TONIGHT!GOATROPE.TRASHTHERENTAL.THEGHOSTYOUKNOW.SUMOFTHEBROTHERS.10PM.THESTAMP!VERO.LOVEYOURBLOOD.LOVE&LIGHT🙏🔥",
|
||||
"date_created": "Sat, 20 Jun 2026 15:45:01 +0000",
|
||||
"date_sent": null,
|
||||
"date_updated": "Sat, 20 Jun 2026 15:45:01 +0000",
|
||||
"direction": "outbound-api",
|
||||
"error_code": null,
|
||||
"error_message": null,
|
||||
"from": "+10123456789",
|
||||
"messaging_service_sid": "329nc29yrc394ytw37yc4t9w37ytnfdxcw39874ydx98347yr",
|
||||
"num_media": "0",
|
||||
"num_segments": "0",
|
||||
"price": null,
|
||||
"price_unit": null,
|
||||
"sid": "34niyt3i47ytfdi37w4ydtrn837q4itfyb3qi4ftciq38w7t83qw49td38dtn",
|
||||
"status": "accepted",
|
||||
"subresource_uris": {
|
||||
"media": "/2010-04-01/Accounts/34n97yf342y5fc349287yndx3987i4yf5c3w98dy398/Messages/329nc29yrc394ytw37yc4t9w37ytnfdxcw39874ydx98347yr/Media.json"
|
||||
},
|
||||
"to": "+11234567890",
|
||||
"uri": "/2010-04-01/Accounts/34n97yf342y5fc349287yndx3987i4yf5c3w98dy398/Messages/329nc29yrc394ytw37yc4t9w37ytnfdxcw39874ydx98347yr.json"
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn test_token() -> Result<String, josekit::JoseError> {
|
||||
let key: String = textsender_models::envy::environment::get_secret_main_key()
|
||||
.await
|
||||
@@ -321,6 +348,51 @@ mod request {
|
||||
}
|
||||
}
|
||||
|
||||
pub mod event {
|
||||
use tower::ServiceExt;
|
||||
|
||||
pub async fn record_message_event_response(
|
||||
app: &axum::Router,
|
||||
scheduled_message_event_id: &uuid::Uuid,
|
||||
user_id: &uuid::Uuid,
|
||||
contact_id: &uuid::Uuid,
|
||||
message_id: &uuid::Uuid,
|
||||
) -> Result<axum::response::Response, axum::http::Error> {
|
||||
let sent = time::OffsetDateTime::now_utc();
|
||||
let sent = match super::super::super::convert_time_to_iso(sent) {
|
||||
Ok(t) => t,
|
||||
Err(err) => {
|
||||
assert!(false, "Error: {err:?}");
|
||||
String::new()
|
||||
}
|
||||
};
|
||||
let payload = serde_json::json!({
|
||||
"scheduled_message_event_id": scheduled_message_event_id,
|
||||
"response": super::super::super::test_mer_response(),
|
||||
"user_id": user_id,
|
||||
"sent": sent,
|
||||
"status": textsender_models::message::event::MESSAGE_EVENT_RESPONSE_STATUS_SCHEDULED,
|
||||
"contact_id": contact_id,
|
||||
"message_id": message_id
|
||||
});
|
||||
|
||||
match super::super::run_post(
|
||||
Some(&payload),
|
||||
textsender_api::caller::endpoints::RECORD_MESSAGE_EVENT_RESPONSE,
|
||||
axum::http::Method::POST,
|
||||
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 mod scheduling {
|
||||
use tower::ServiceExt;
|
||||
|
||||
@@ -507,7 +579,10 @@ mod request {
|
||||
match axum::http::Request::builder()
|
||||
.method(method)
|
||||
.uri(uri)
|
||||
.header(axum::http::header::CONTENT_TYPE, "application/json")
|
||||
.header(
|
||||
axum::http::header::CONTENT_TYPE,
|
||||
"application/json; charset=utf-8",
|
||||
)
|
||||
.header(
|
||||
axum::http::header::AUTHORIZATION,
|
||||
super::bearer_auth().await,
|
||||
@@ -1269,3 +1344,140 @@ async fn test_fetch_scheduled_message() {
|
||||
|
||||
let _ = db_mgr::drop_database(&tm_pool, &db_name).await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_record_message_event_response() {
|
||||
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();
|
||||
let mut scheduled_message_event_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");
|
||||
let scheduled_message_event = &resp.data[0];
|
||||
scheduled_message_event_id = scheduled_message_event.id;
|
||||
}
|
||||
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:?}");
|
||||
}
|
||||
}
|
||||
|
||||
match request::message::scheduling::fetch_scheduled_message(&app).await {
|
||||
Ok(response) => {
|
||||
let resp = util::get_resp_data::<
|
||||
textsender_api::caller::message::scheduling::response::FetchScheduledMessageResponse,
|
||||
>(response)
|
||||
.await;
|
||||
assert_eq!(false, resp.data.is_empty(), "Should not be empty");
|
||||
}
|
||||
Err(err) => {
|
||||
assert!(false, "Error: {err:?}");
|
||||
}
|
||||
}
|
||||
|
||||
assert_eq!(
|
||||
false,
|
||||
scheduled_message_event_id.is_nil(),
|
||||
"Scheduled Message Event Id should not be empty"
|
||||
);
|
||||
|
||||
match request::message::event::record_message_event_response(
|
||||
&app,
|
||||
&scheduled_message_event_id,
|
||||
&TEST_USER_ID,
|
||||
&contact_id,
|
||||
&message_id,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(response) => {
|
||||
let resp = util::get_resp_data::<
|
||||
textsender_api::caller::message::event::response::RecordMessageEventResponse,
|
||||
>(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