
Some checks are pending
iOS UniFFI Build / build-ios (push) Waiting to run
Add church management API library with cross-platform support for iOS, Android, and WASM. Features include event management, bulletin handling, contact forms, and authentication.
125 lines
4 KiB
Rust
125 lines
4 KiB
Rust
use crate::{
|
|
client::ChurchApiClient,
|
|
error::Result,
|
|
models::{ContactForm, ContactSubmission, ContactStatus, PaginationParams, ApiListResponse, ApiVersion},
|
|
};
|
|
|
|
pub async fn submit_contact_form(client: &ChurchApiClient, form: ContactForm) -> Result<String> {
|
|
// Create payload matching the expected format from iOS app
|
|
let payload = serde_json::json!({
|
|
"first_name": form.name.split_whitespace().next().unwrap_or(&form.name),
|
|
"last_name": form.name.split_whitespace().nth(1).unwrap_or(""),
|
|
"email": form.email,
|
|
"phone": form.phone.unwrap_or_default(),
|
|
"message": form.message
|
|
});
|
|
|
|
// Use the main API subdomain for consistency
|
|
let contact_url = client.build_url("/contact");
|
|
|
|
let response = client.client
|
|
.post(contact_url)
|
|
.header("Content-Type", "application/json")
|
|
.json(&payload)
|
|
.send()
|
|
.await?;
|
|
|
|
if response.status().is_success() {
|
|
Ok("Contact form submitted successfully".to_string())
|
|
} else {
|
|
Err(crate::error::ChurchApiError::Api(format!("Contact form submission failed with status: {}", response.status())))
|
|
}
|
|
}
|
|
|
|
pub async fn get_contact_submissions(client: &ChurchApiClient, params: Option<PaginationParams>) -> Result<ApiListResponse<ContactSubmission>> {
|
|
let mut path = "/contact/submissions".to_string();
|
|
|
|
if let Some(params) = params {
|
|
let mut query_params = Vec::new();
|
|
|
|
if let Some(page) = params.page {
|
|
query_params.push(("page", page.to_string()));
|
|
}
|
|
|
|
if let Some(per_page) = params.per_page {
|
|
query_params.push(("per_page", per_page.to_string()));
|
|
}
|
|
|
|
if let Some(sort) = ¶ms.sort {
|
|
query_params.push(("sort", sort.clone()));
|
|
}
|
|
|
|
if let Some(filter) = ¶ms.filter {
|
|
query_params.push(("filter", filter.clone()));
|
|
}
|
|
|
|
if !query_params.is_empty() {
|
|
let query_string = query_params
|
|
.iter()
|
|
.map(|(k, v)| format!("{}={}", k, urlencoding::encode(v)))
|
|
.collect::<Vec<_>>()
|
|
.join("&");
|
|
path.push_str(&format!("?{}", query_string));
|
|
}
|
|
}
|
|
|
|
client.get_api_list(&path).await
|
|
}
|
|
|
|
pub async fn get_contact_submission(client: &ChurchApiClient, id: &str) -> Result<Option<ContactSubmission>> {
|
|
let path = format!("/contact/submissions/{}", id);
|
|
|
|
match client.get_api(&path).await {
|
|
Ok(submission) => Ok(Some(submission)),
|
|
Err(crate::error::ChurchApiError::NotFound) => Ok(None),
|
|
Err(e) => Err(e),
|
|
}
|
|
}
|
|
|
|
pub async fn update_contact_submission(
|
|
client: &ChurchApiClient,
|
|
id: &str,
|
|
status: ContactStatus,
|
|
response: Option<String>
|
|
) -> Result<()> {
|
|
let path = format!("/contact/submissions/{}", id);
|
|
|
|
let update_data = serde_json::json!({
|
|
"status": status,
|
|
"response": response
|
|
});
|
|
|
|
client.put_api(&path, &update_data).await
|
|
}
|
|
|
|
// V2 API methods
|
|
pub async fn submit_contact_form_v2(client: &ChurchApiClient, form: ContactForm) -> Result<String> {
|
|
let mut payload = serde_json::json!({
|
|
"name": form.name,
|
|
"email": form.email,
|
|
"subject": form.subject,
|
|
"message": form.message
|
|
});
|
|
|
|
// Add phone field if provided
|
|
if let Some(phone) = &form.phone {
|
|
if !phone.trim().is_empty() {
|
|
payload["phone"] = serde_json::json!(phone);
|
|
}
|
|
}
|
|
|
|
let url = client.build_url_with_version("/contact", ApiVersion::V2);
|
|
|
|
let response = client.client
|
|
.post(url)
|
|
.header("Content-Type", "application/json")
|
|
.json(&payload)
|
|
.send()
|
|
.await?;
|
|
|
|
if response.status().is_success() {
|
|
Ok("Contact form submitted successfully".to_string())
|
|
} else {
|
|
Err(crate::error::ChurchApiError::Api(format!("Contact form submission failed with status: {}", response.status())))
|
|
}
|
|
} |