Refactor handlers.rs into modular structure for better maintainability
- Split 1921-line handlers.rs into focused modules: - handlers/auth.rs: Authentication handlers (login, verify_token, get_user_info) - handlers/calendar.rs: Calendar management (create_calendar, delete_calendar) - handlers/events.rs: Event operations (CRUD operations, fetch events) - handlers/series.rs: Event series operations (recurring events management) - Main handlers.rs now serves as clean re-export module - All tests passing (14 integration + 7 unit + 3 doc tests) - Maintains backward compatibility with existing API routes - Improves code organization and separation of concerns 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -104,7 +104,7 @@ impl AuthService {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn generate_token(&self, username: &str, server_url: &str) -> Result<String, ApiError> {
|
||||
pub fn generate_token(&self, username: &str, server_url: &str) -> Result<String, ApiError> {
|
||||
let now = Utc::now();
|
||||
let expires_at = now + Duration::hours(24); // Token valid for 24 hours
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
159
backend/src/handlers/auth.rs
Normal file
159
backend/src/handlers/auth.rs
Normal file
@@ -0,0 +1,159 @@
|
||||
use axum::{
|
||||
extract::State,
|
||||
http::HeaderMap,
|
||||
response::Json,
|
||||
};
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::{AppState, models::{CalDAVLoginRequest, AuthResponse, ApiError, UserInfo, CalendarInfo}};
|
||||
use crate::calendar::CalDAVClient;
|
||||
use crate::config::CalDAVConfig;
|
||||
|
||||
pub fn extract_bearer_token(headers: &HeaderMap) -> Result<String, ApiError> {
|
||||
let auth_header = headers.get("authorization")
|
||||
.ok_or_else(|| ApiError::Unauthorized("Missing Authorization header".to_string()))?;
|
||||
|
||||
let auth_str = auth_header.to_str()
|
||||
.map_err(|_| ApiError::BadRequest("Invalid Authorization header".to_string()))?;
|
||||
|
||||
if let Some(token) = auth_str.strip_prefix("Bearer ") {
|
||||
Ok(token.to_string())
|
||||
} else {
|
||||
Err(ApiError::BadRequest("Authorization header must be Bearer token".to_string()))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn extract_password_header(headers: &HeaderMap) -> Result<String, ApiError> {
|
||||
let password_header = headers.get("x-caldav-password")
|
||||
.ok_or_else(|| ApiError::BadRequest("Missing X-CalDAV-Password header".to_string()))?;
|
||||
|
||||
password_header.to_str()
|
||||
.map(|s| s.to_string())
|
||||
.map_err(|_| ApiError::BadRequest("Invalid X-CalDAV-Password header".to_string()))
|
||||
}
|
||||
|
||||
pub async fn login(
|
||||
State(state): State<Arc<AppState>>,
|
||||
Json(request): Json<CalDAVLoginRequest>,
|
||||
) -> Result<Json<AuthResponse>, ApiError> {
|
||||
println!("🔐 Login attempt:");
|
||||
println!(" Server URL: {}", request.server_url);
|
||||
println!(" Username: {}", request.username);
|
||||
println!(" Password length: {}", request.password.len());
|
||||
|
||||
// Basic validation
|
||||
if request.username.is_empty() || request.password.is_empty() || request.server_url.is_empty() {
|
||||
return Err(ApiError::BadRequest("Username, password, and server URL are required".to_string()));
|
||||
}
|
||||
|
||||
println!("✅ Input validation passed");
|
||||
|
||||
// Create a token using the auth service
|
||||
println!("📝 Created CalDAV config");
|
||||
|
||||
// First verify the credentials are valid by attempting to discover calendars
|
||||
let config = CalDAVConfig {
|
||||
server_url: request.server_url.clone(),
|
||||
username: request.username.clone(),
|
||||
password: request.password.clone(),
|
||||
calendar_path: None,
|
||||
tasks_path: None,
|
||||
};
|
||||
let client = CalDAVClient::new(config);
|
||||
client.discover_calendars()
|
||||
.await
|
||||
.map_err(|e| ApiError::Unauthorized(format!("Authentication failed: {}", e)))?;
|
||||
|
||||
let token = state.auth_service.generate_token(&request.username, &request.server_url)?;
|
||||
|
||||
println!("🔗 Created CalDAV client, attempting to discover calendars...");
|
||||
|
||||
Ok(Json(AuthResponse {
|
||||
token,
|
||||
username: request.username,
|
||||
server_url: request.server_url,
|
||||
}))
|
||||
}
|
||||
|
||||
pub async fn verify_token(
|
||||
State(state): State<Arc<AppState>>,
|
||||
headers: HeaderMap,
|
||||
) -> Result<Json<serde_json::Value>, ApiError> {
|
||||
let token = extract_bearer_token(&headers)?;
|
||||
let is_valid = state.auth_service.verify_token(&token).is_ok();
|
||||
|
||||
Ok(Json(serde_json::json!({ "valid": is_valid })))
|
||||
}
|
||||
|
||||
pub async fn get_user_info(
|
||||
State(state): State<Arc<AppState>>,
|
||||
headers: HeaderMap,
|
||||
) -> Result<Json<UserInfo>, ApiError> {
|
||||
let token = extract_bearer_token(&headers)?;
|
||||
let password = extract_password_header(&headers)?;
|
||||
|
||||
// Create CalDAV config from token and password
|
||||
let config = state.auth_service.caldav_config_from_token(&token, &password)?;
|
||||
let client = CalDAVClient::new(config.clone());
|
||||
|
||||
// Discover calendars
|
||||
let calendar_paths = client.discover_calendars()
|
||||
.await
|
||||
.map_err(|e| ApiError::Internal(format!("Failed to discover calendars: {}", e)))?;
|
||||
|
||||
println!("✅ Authentication successful! Found {} calendars", calendar_paths.len());
|
||||
|
||||
let calendars: Vec<CalendarInfo> = calendar_paths.iter().map(|path| {
|
||||
CalendarInfo {
|
||||
path: path.clone(),
|
||||
display_name: extract_calendar_name(path),
|
||||
color: generate_calendar_color(path),
|
||||
}
|
||||
}).collect();
|
||||
|
||||
Ok(Json(UserInfo {
|
||||
username: config.username,
|
||||
server_url: config.server_url,
|
||||
calendars,
|
||||
}))
|
||||
}
|
||||
|
||||
fn generate_calendar_color(path: &str) -> String {
|
||||
// Generate a consistent color based on the calendar path
|
||||
// This is a simple hash-based approach
|
||||
let mut hash: u32 = 0;
|
||||
for byte in path.bytes() {
|
||||
hash = hash.wrapping_mul(31).wrapping_add(byte as u32);
|
||||
}
|
||||
|
||||
// Define a set of pleasant colors
|
||||
let colors = [
|
||||
"#3B82F6", "#10B981", "#F59E0B", "#EF4444", "#8B5CF6",
|
||||
"#06B6D4", "#84CC16", "#F97316", "#EC4899", "#6366F1",
|
||||
"#14B8A6", "#F3B806", "#8B5A2B", "#6B7280", "#DC2626",
|
||||
"#7C3AED", "#059669", "#D97706", "#BE185D", "#4F46E5"
|
||||
];
|
||||
|
||||
colors[(hash as usize) % colors.len()].to_string()
|
||||
}
|
||||
|
||||
fn extract_calendar_name(path: &str) -> String {
|
||||
// Extract calendar name from path
|
||||
// E.g., "/calendars/user/calendar-name/" -> "Calendar Name"
|
||||
path.split('/')
|
||||
.filter(|s| !s.is_empty())
|
||||
.last()
|
||||
.unwrap_or("Calendar")
|
||||
.replace('-', " ")
|
||||
.replace('_', " ")
|
||||
.split_whitespace()
|
||||
.map(|word| {
|
||||
let mut chars = word.chars();
|
||||
match chars.next() {
|
||||
None => String::new(),
|
||||
Some(first) => first.to_uppercase().collect::<String>() + chars.as_str(),
|
||||
}
|
||||
})
|
||||
.collect::<Vec<String>>()
|
||||
.join(" ")
|
||||
}
|
||||
71
backend/src/handlers/calendar.rs
Normal file
71
backend/src/handlers/calendar.rs
Normal file
@@ -0,0 +1,71 @@
|
||||
use axum::{
|
||||
extract::State,
|
||||
http::HeaderMap,
|
||||
response::Json,
|
||||
};
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::{AppState, models::{ApiError, CreateCalendarRequest, CreateCalendarResponse, DeleteCalendarRequest, DeleteCalendarResponse}};
|
||||
use crate::calendar::CalDAVClient;
|
||||
|
||||
use super::auth::{extract_bearer_token, extract_password_header};
|
||||
|
||||
pub async fn create_calendar(
|
||||
State(state): State<Arc<AppState>>,
|
||||
headers: HeaderMap,
|
||||
Json(request): Json<CreateCalendarRequest>,
|
||||
) -> Result<Json<CreateCalendarResponse>, ApiError> {
|
||||
let token = extract_bearer_token(&headers)?;
|
||||
let password = extract_password_header(&headers)?;
|
||||
|
||||
// Validate request
|
||||
if request.name.trim().is_empty() {
|
||||
return Err(ApiError::BadRequest("Calendar name is required".to_string()));
|
||||
}
|
||||
|
||||
// Create CalDAV config from token and password
|
||||
let config = state.auth_service.caldav_config_from_token(&token, &password)?;
|
||||
let client = CalDAVClient::new(config);
|
||||
|
||||
// Create calendar on CalDAV server
|
||||
match client.create_calendar(&request.name, request.description.as_deref(), request.color.as_deref()).await {
|
||||
Ok(_) => Ok(Json(CreateCalendarResponse {
|
||||
success: true,
|
||||
message: "Calendar created successfully".to_string(),
|
||||
})),
|
||||
Err(e) => {
|
||||
eprintln!("Failed to create calendar: {}", e);
|
||||
Err(ApiError::Internal(format!("Failed to create calendar: {}", e)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn delete_calendar(
|
||||
State(state): State<Arc<AppState>>,
|
||||
headers: HeaderMap,
|
||||
Json(request): Json<DeleteCalendarRequest>,
|
||||
) -> Result<Json<DeleteCalendarResponse>, ApiError> {
|
||||
let token = extract_bearer_token(&headers)?;
|
||||
let password = extract_password_header(&headers)?;
|
||||
|
||||
// Validate request
|
||||
if request.path.trim().is_empty() {
|
||||
return Err(ApiError::BadRequest("Calendar path is required".to_string()));
|
||||
}
|
||||
|
||||
// Create CalDAV config from token and password
|
||||
let config = state.auth_service.caldav_config_from_token(&token, &password)?;
|
||||
let client = CalDAVClient::new(config);
|
||||
|
||||
// Delete calendar on CalDAV server
|
||||
match client.delete_calendar(&request.path).await {
|
||||
Ok(_) => Ok(Json(DeleteCalendarResponse {
|
||||
success: true,
|
||||
message: "Calendar deleted successfully".to_string(),
|
||||
})),
|
||||
Err(e) => {
|
||||
eprintln!("Failed to delete calendar: {}", e);
|
||||
Err(ApiError::Internal(format!("Failed to delete calendar: {}", e)))
|
||||
}
|
||||
}
|
||||
}
|
||||
580
backend/src/handlers/events.rs
Normal file
580
backend/src/handlers/events.rs
Normal file
@@ -0,0 +1,580 @@
|
||||
use axum::{
|
||||
extract::{State, Query, Path},
|
||||
http::HeaderMap,
|
||||
response::Json,
|
||||
};
|
||||
use serde::Deserialize;
|
||||
use std::sync::Arc;
|
||||
use chrono::Datelike;
|
||||
|
||||
use calendar_models::{VEvent, EventStatus, EventClass, CalendarUser, Attendee, VAlarm, AlarmAction, AlarmTrigger};
|
||||
use crate::{AppState, models::{ApiError, DeleteEventRequest, DeleteEventResponse, CreateEventRequest, CreateEventResponse, UpdateEventRequest, UpdateEventResponse}};
|
||||
use crate::calendar::{CalDAVClient, CalendarEvent};
|
||||
|
||||
use super::auth::{extract_bearer_token, extract_password_header};
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct CalendarQuery {
|
||||
pub year: Option<i32>,
|
||||
pub month: Option<u32>,
|
||||
}
|
||||
|
||||
pub async fn get_calendar_events(
|
||||
State(state): State<Arc<AppState>>,
|
||||
Query(params): Query<CalendarQuery>,
|
||||
headers: HeaderMap,
|
||||
) -> Result<Json<Vec<CalendarEvent>>, ApiError> {
|
||||
// Extract and verify token
|
||||
let token = extract_bearer_token(&headers)?;
|
||||
let password = extract_password_header(&headers)?;
|
||||
println!("🔑 API call with password length: {}", password.len());
|
||||
|
||||
// Create CalDAV config from token and password
|
||||
let config = state.auth_service.caldav_config_from_token(&token, &password)?;
|
||||
let client = CalDAVClient::new(config);
|
||||
|
||||
// Discover calendars if needed
|
||||
let calendar_paths = client.discover_calendars()
|
||||
.await
|
||||
.map_err(|e| ApiError::Internal(format!("Failed to discover calendars: {}", e)))?;
|
||||
|
||||
if calendar_paths.is_empty() {
|
||||
return Ok(Json(vec![])); // No calendars found
|
||||
}
|
||||
|
||||
// Fetch events from all calendars
|
||||
let mut all_events = Vec::new();
|
||||
for calendar_path in &calendar_paths {
|
||||
match client.fetch_events(calendar_path).await {
|
||||
Ok(mut events) => {
|
||||
// Set calendar_path for each event to identify which calendar it belongs to
|
||||
for event in &mut events {
|
||||
event.calendar_path = Some(calendar_path.clone());
|
||||
}
|
||||
all_events.extend(events);
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("Failed to fetch events from calendar {}: {}", calendar_path, e);
|
||||
// Continue with other calendars instead of failing completely
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If year and month are specified, filter events
|
||||
if let (Some(year), Some(month)) = (params.year, params.month) {
|
||||
all_events.retain(|event| {
|
||||
let event_year = event.dtstart.year();
|
||||
let event_month = event.dtstart.month();
|
||||
event_year == year && event_month == month
|
||||
});
|
||||
}
|
||||
|
||||
println!("📅 Returning {} events", all_events.len());
|
||||
Ok(Json(all_events))
|
||||
}
|
||||
|
||||
pub async fn refresh_event(
|
||||
State(state): State<Arc<AppState>>,
|
||||
Path(uid): Path<String>,
|
||||
headers: HeaderMap,
|
||||
) -> Result<Json<Option<CalendarEvent>>, ApiError> {
|
||||
let token = extract_bearer_token(&headers)?;
|
||||
let password = extract_password_header(&headers)?;
|
||||
|
||||
// Create CalDAV config from token and password
|
||||
let config = state.auth_service.caldav_config_from_token(&token, &password)?;
|
||||
let client = CalDAVClient::new(config);
|
||||
|
||||
// Discover calendars
|
||||
let calendar_paths = client.discover_calendars()
|
||||
.await
|
||||
.map_err(|e| ApiError::Internal(format!("Failed to discover calendars: {}", e)))?;
|
||||
|
||||
// Search for the event by UID across all calendars
|
||||
for calendar_path in &calendar_paths {
|
||||
if let Ok(Some(mut event)) = client.fetch_event_by_uid(calendar_path, &uid).await {
|
||||
event.calendar_path = Some(calendar_path.clone());
|
||||
return Ok(Json(Some(event)));
|
||||
}
|
||||
}
|
||||
|
||||
Ok(Json(None))
|
||||
}
|
||||
|
||||
async fn fetch_event_by_href(client: &CalDAVClient, calendar_path: &str, event_href: &str) -> Result<Option<CalendarEvent>, crate::calendar::CalDAVError> {
|
||||
// This is a simplified implementation - in a real scenario, you'd want to fetch the specific event by href
|
||||
// For now, we'll fetch all events and find the matching one by href (inefficient but functional)
|
||||
let events = client.fetch_events(calendar_path).await?;
|
||||
|
||||
// Try to match by UID extracted from href
|
||||
let uid_from_href = event_href.trim_end_matches(".ics");
|
||||
for event in events {
|
||||
if event.uid == uid_from_href {
|
||||
return Ok(Some(event));
|
||||
}
|
||||
}
|
||||
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
pub async fn delete_event(
|
||||
State(state): State<Arc<AppState>>,
|
||||
headers: HeaderMap,
|
||||
Json(request): Json<DeleteEventRequest>,
|
||||
) -> Result<Json<DeleteEventResponse>, ApiError> {
|
||||
let token = extract_bearer_token(&headers)?;
|
||||
let password = extract_password_header(&headers)?;
|
||||
|
||||
// Create CalDAV config from token and password
|
||||
let config = state.auth_service.caldav_config_from_token(&token, &password)?;
|
||||
let client = CalDAVClient::new(config);
|
||||
|
||||
// Handle different delete actions for recurring events
|
||||
match request.delete_action.as_str() {
|
||||
"delete_this" => {
|
||||
// For single occurrence deletion, we need to:
|
||||
// 1. Fetch the recurring event
|
||||
// 2. Add an EXDATE for this occurrence
|
||||
// 3. Update the event
|
||||
|
||||
if let Some(mut event) = fetch_event_by_href(&client, &request.calendar_path, &request.event_href).await
|
||||
.map_err(|e| ApiError::Internal(format!("Failed to fetch event: {}", e)))? {
|
||||
|
||||
if let Some(occurrence_date) = &request.occurrence_date {
|
||||
// Parse the occurrence date and add it to EXDATE
|
||||
if let Ok(date) = chrono::DateTime::parse_from_rfc3339(occurrence_date) {
|
||||
let exception_utc = date.with_timezone(&chrono::Utc);
|
||||
event.exdate.push(exception_utc);
|
||||
|
||||
// Update the event with the new EXDATE
|
||||
client.update_event(&request.calendar_path, &event, &request.event_href)
|
||||
.await
|
||||
.map_err(|e| ApiError::Internal(format!("Failed to update event with EXDATE: {}", e)))?;
|
||||
|
||||
Ok(Json(DeleteEventResponse {
|
||||
success: true,
|
||||
message: "Single occurrence deleted successfully".to_string(),
|
||||
}))
|
||||
} else {
|
||||
Err(ApiError::BadRequest("Invalid occurrence date format".to_string()))
|
||||
}
|
||||
} else {
|
||||
Err(ApiError::BadRequest("Occurrence date is required for single occurrence deletion".to_string()))
|
||||
}
|
||||
} else {
|
||||
Err(ApiError::NotFound("Event not found".to_string()))
|
||||
}
|
||||
},
|
||||
"delete_following" => {
|
||||
// For "this and following" deletion, we need to:
|
||||
// 1. Fetch the recurring event
|
||||
// 2. Modify the RRULE to end before this occurrence
|
||||
// 3. Update the event
|
||||
|
||||
if let Some(mut event) = fetch_event_by_href(&client, &request.calendar_path, &request.event_href).await
|
||||
.map_err(|e| ApiError::Internal(format!("Failed to fetch event: {}", e)))? {
|
||||
|
||||
if let Some(occurrence_date) = &request.occurrence_date {
|
||||
if let Ok(date) = chrono::DateTime::parse_from_rfc3339(occurrence_date) {
|
||||
let until_date = date.with_timezone(&chrono::Utc);
|
||||
|
||||
// Modify the RRULE to add an UNTIL clause
|
||||
if let Some(rrule) = &event.rrule {
|
||||
// Remove existing UNTIL if present and add new one
|
||||
let parts: Vec<&str> = rrule.split(';').filter(|part| {
|
||||
!part.starts_with("UNTIL=") && !part.starts_with("COUNT=")
|
||||
}).collect();
|
||||
|
||||
let new_rrule = format!("{};UNTIL={}", parts.join(";"), until_date.format("%Y%m%dT%H%M%SZ"));
|
||||
event.rrule = Some(new_rrule);
|
||||
|
||||
// Update the event with the modified RRULE
|
||||
client.update_event(&request.calendar_path, &event, &request.event_href)
|
||||
.await
|
||||
.map_err(|e| ApiError::Internal(format!("Failed to update event with modified RRULE: {}", e)))?;
|
||||
|
||||
Ok(Json(DeleteEventResponse {
|
||||
success: true,
|
||||
message: "This and following occurrences deleted successfully".to_string(),
|
||||
}))
|
||||
} else {
|
||||
// No RRULE, just delete the single event
|
||||
client.delete_event(&request.calendar_path, &request.event_href)
|
||||
.await
|
||||
.map_err(|e| ApiError::Internal(format!("Failed to delete event: {}", e)))?;
|
||||
|
||||
Ok(Json(DeleteEventResponse {
|
||||
success: true,
|
||||
message: "Event deleted successfully".to_string(),
|
||||
}))
|
||||
}
|
||||
} else {
|
||||
Err(ApiError::BadRequest("Invalid occurrence date format".to_string()))
|
||||
}
|
||||
} else {
|
||||
Err(ApiError::BadRequest("Occurrence date is required for following deletion".to_string()))
|
||||
}
|
||||
} else {
|
||||
Err(ApiError::NotFound("Event not found".to_string()))
|
||||
}
|
||||
},
|
||||
"delete_series" | _ => {
|
||||
// Delete the entire event/series
|
||||
client.delete_event(&request.calendar_path, &request.event_href)
|
||||
.await
|
||||
.map_err(|e| ApiError::Internal(format!("Failed to delete event: {}", e)))?;
|
||||
|
||||
Ok(Json(DeleteEventResponse {
|
||||
success: true,
|
||||
message: "Event deleted successfully".to_string(),
|
||||
}))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn create_event(
|
||||
State(state): State<Arc<AppState>>,
|
||||
headers: HeaderMap,
|
||||
Json(request): Json<CreateEventRequest>,
|
||||
) -> Result<Json<CreateEventResponse>, ApiError> {
|
||||
println!("📝 Create event request received: title='{}', all_day={}, calendar_path={:?}",
|
||||
request.title, request.all_day, request.calendar_path);
|
||||
|
||||
// Extract and verify token
|
||||
let token = extract_bearer_token(&headers)?;
|
||||
let password = extract_password_header(&headers)?;
|
||||
|
||||
// Validate request
|
||||
if request.title.trim().is_empty() {
|
||||
return Err(ApiError::BadRequest("Event title is required".to_string()));
|
||||
}
|
||||
|
||||
if request.title.len() > 200 {
|
||||
return Err(ApiError::BadRequest("Event title too long (max 200 characters)".to_string()));
|
||||
}
|
||||
|
||||
// Create CalDAV config from token and password
|
||||
let config = state.auth_service.caldav_config_from_token(&token, &password)?;
|
||||
let client = CalDAVClient::new(config);
|
||||
|
||||
// Determine which calendar to use
|
||||
let calendar_path = if let Some(path) = request.calendar_path {
|
||||
path
|
||||
} else {
|
||||
// Use the first available calendar
|
||||
let calendar_paths = client.discover_calendars()
|
||||
.await
|
||||
.map_err(|e| ApiError::Internal(format!("Failed to discover calendars: {}", e)))?;
|
||||
|
||||
if calendar_paths.is_empty() {
|
||||
return Err(ApiError::BadRequest("No calendars available for event creation".to_string()));
|
||||
}
|
||||
|
||||
calendar_paths[0].clone()
|
||||
};
|
||||
|
||||
// Parse dates and times
|
||||
let start_datetime = parse_event_datetime(&request.start_date, &request.start_time, request.all_day)
|
||||
.map_err(|e| ApiError::BadRequest(format!("Invalid start date/time: {}", e)))?;
|
||||
|
||||
let end_datetime = parse_event_datetime(&request.end_date, &request.end_time, request.all_day)
|
||||
.map_err(|e| ApiError::BadRequest(format!("Invalid end date/time: {}", e)))?;
|
||||
|
||||
// Validate that end is after start
|
||||
if end_datetime <= start_datetime {
|
||||
return Err(ApiError::BadRequest("End date/time must be after start date/time".to_string()));
|
||||
}
|
||||
|
||||
// Generate a unique UID for the event
|
||||
let uid = format!("{}-{}", uuid::Uuid::new_v4(), chrono::Utc::now().timestamp());
|
||||
|
||||
// Parse status
|
||||
let status = match request.status.to_lowercase().as_str() {
|
||||
"tentative" => EventStatus::Tentative,
|
||||
"cancelled" => EventStatus::Cancelled,
|
||||
_ => EventStatus::Confirmed,
|
||||
};
|
||||
|
||||
// Parse class
|
||||
let class = match request.class.to_lowercase().as_str() {
|
||||
"private" => EventClass::Private,
|
||||
"confidential" => EventClass::Confidential,
|
||||
_ => EventClass::Public,
|
||||
};
|
||||
|
||||
// Parse attendees (comma-separated email list)
|
||||
let attendees: Vec<String> = if request.attendees.trim().is_empty() {
|
||||
Vec::new()
|
||||
} else {
|
||||
request.attendees
|
||||
.split(',')
|
||||
.map(|s| s.trim().to_string())
|
||||
.filter(|s| !s.is_empty())
|
||||
.collect()
|
||||
};
|
||||
|
||||
// Parse categories (comma-separated list)
|
||||
let categories: Vec<String> = if request.categories.trim().is_empty() {
|
||||
Vec::new()
|
||||
} else {
|
||||
request.categories
|
||||
.split(',')
|
||||
.map(|s| s.trim().to_string())
|
||||
.filter(|s| !s.is_empty())
|
||||
.collect()
|
||||
};
|
||||
|
||||
// Parse alarms - convert from minutes string to EventReminder structs
|
||||
let alarms: Vec<crate::calendar::EventReminder> = if request.reminder.trim().is_empty() {
|
||||
Vec::new()
|
||||
} else {
|
||||
match request.reminder.parse::<i32>() {
|
||||
Ok(minutes) => vec![crate::calendar::EventReminder {
|
||||
minutes_before: minutes,
|
||||
action: crate::calendar::ReminderAction::Display,
|
||||
description: None,
|
||||
}],
|
||||
Err(_) => Vec::new(),
|
||||
}
|
||||
};
|
||||
|
||||
// Parse recurrence with BYDAY support for weekly recurrence
|
||||
let rrule = match request.recurrence.to_uppercase().as_str() {
|
||||
"DAILY" => Some("FREQ=DAILY".to_string()),
|
||||
"WEEKLY" => {
|
||||
// Handle weekly recurrence with optional BYDAY parameter
|
||||
let mut rrule = "FREQ=WEEKLY".to_string();
|
||||
|
||||
// Check if specific days are selected (recurrence_days has 7 elements: [Sun, Mon, Tue, Wed, Thu, Fri, Sat])
|
||||
if request.recurrence_days.len() == 7 {
|
||||
let selected_days: Vec<&str> = request.recurrence_days
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter_map(|(i, &selected)| {
|
||||
if selected {
|
||||
Some(match i {
|
||||
0 => "SU", // Sunday
|
||||
1 => "MO", // Monday
|
||||
2 => "TU", // Tuesday
|
||||
3 => "WE", // Wednesday
|
||||
4 => "TH", // Thursday
|
||||
5 => "FR", // Friday
|
||||
6 => "SA", // Saturday
|
||||
_ => return None,
|
||||
})
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
if !selected_days.is_empty() {
|
||||
rrule = format!("{};BYDAY={}", rrule, selected_days.join(","));
|
||||
}
|
||||
}
|
||||
|
||||
Some(rrule)
|
||||
},
|
||||
"MONTHLY" => Some("FREQ=MONTHLY".to_string()),
|
||||
"YEARLY" => Some("FREQ=YEARLY".to_string()),
|
||||
_ => None,
|
||||
};
|
||||
|
||||
// Create the VEvent struct (RFC 5545 compliant)
|
||||
let mut event = VEvent::new(uid, start_datetime);
|
||||
event.dtend = Some(end_datetime);
|
||||
event.summary = if request.title.trim().is_empty() { None } else { Some(request.title.clone()) };
|
||||
event.description = if request.description.trim().is_empty() { None } else { Some(request.description) };
|
||||
event.location = if request.location.trim().is_empty() { None } else { Some(request.location) };
|
||||
event.status = Some(status);
|
||||
event.class = Some(class);
|
||||
event.priority = request.priority;
|
||||
event.organizer = if request.organizer.trim().is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(CalendarUser {
|
||||
cal_address: request.organizer,
|
||||
common_name: None,
|
||||
dir_entry_ref: None,
|
||||
sent_by: None,
|
||||
language: None,
|
||||
})
|
||||
};
|
||||
event.attendees = attendees.into_iter().map(|email| Attendee {
|
||||
cal_address: email,
|
||||
common_name: None,
|
||||
role: None,
|
||||
part_stat: None,
|
||||
rsvp: None,
|
||||
cu_type: None,
|
||||
member: Vec::new(),
|
||||
delegated_to: Vec::new(),
|
||||
delegated_from: Vec::new(),
|
||||
sent_by: None,
|
||||
dir_entry_ref: None,
|
||||
language: None,
|
||||
}).collect();
|
||||
event.categories = categories;
|
||||
event.rrule = rrule;
|
||||
event.all_day = request.all_day;
|
||||
event.alarms = alarms.into_iter().map(|reminder| VAlarm {
|
||||
action: AlarmAction::Display,
|
||||
trigger: AlarmTrigger::Duration(chrono::Duration::minutes(-reminder.minutes_before as i64)),
|
||||
duration: None,
|
||||
repeat: None,
|
||||
description: reminder.description,
|
||||
summary: None,
|
||||
attendees: Vec::new(),
|
||||
attach: Vec::new(),
|
||||
}).collect();
|
||||
event.calendar_path = Some(calendar_path.clone());
|
||||
|
||||
// Create the event on the CalDAV server
|
||||
let event_href = client.create_event(&calendar_path, &event)
|
||||
.await
|
||||
.map_err(|e| ApiError::Internal(format!("Failed to create event: {}", e)))?;
|
||||
|
||||
println!("✅ Event created successfully with UID: {} at href: {}", event.uid, event_href);
|
||||
|
||||
Ok(Json(CreateEventResponse {
|
||||
success: true,
|
||||
message: "Event created successfully".to_string(),
|
||||
event_href: Some(event_href),
|
||||
}))
|
||||
}
|
||||
|
||||
pub async fn update_event(
|
||||
State(state): State<Arc<AppState>>,
|
||||
headers: HeaderMap,
|
||||
Json(request): Json<UpdateEventRequest>,
|
||||
) -> Result<Json<UpdateEventResponse>, ApiError> {
|
||||
// Handle update request
|
||||
|
||||
// Extract and verify token
|
||||
let token = extract_bearer_token(&headers)?;
|
||||
let password = extract_password_header(&headers)?;
|
||||
|
||||
// Validate request
|
||||
if request.uid.trim().is_empty() {
|
||||
return Err(ApiError::BadRequest("Event UID is required".to_string()));
|
||||
}
|
||||
|
||||
if request.title.trim().is_empty() {
|
||||
return Err(ApiError::BadRequest("Event title is required".to_string()));
|
||||
}
|
||||
|
||||
if request.title.len() > 200 {
|
||||
return Err(ApiError::BadRequest("Event title too long (max 200 characters)".to_string()));
|
||||
}
|
||||
|
||||
// Create CalDAV config from token and password
|
||||
let config = state.auth_service.caldav_config_from_token(&token, &password)?;
|
||||
let client = CalDAVClient::new(config);
|
||||
|
||||
// Find the event across all calendars (or in the specified calendar)
|
||||
let calendar_paths = if let Some(path) = &request.calendar_path {
|
||||
vec![path.clone()]
|
||||
} else {
|
||||
client.discover_calendars()
|
||||
.await
|
||||
.map_err(|e| ApiError::Internal(format!("Failed to discover calendars: {}", e)))?
|
||||
};
|
||||
|
||||
let mut found_event: Option<(CalendarEvent, String, String)> = None; // (event, calendar_path, event_href)
|
||||
|
||||
for calendar_path in &calendar_paths {
|
||||
match client.fetch_events(calendar_path).await {
|
||||
Ok(events) => {
|
||||
for event in events {
|
||||
if event.uid == request.uid {
|
||||
// Generate event href from UID
|
||||
let event_href = format!("{}.ics", event.uid);
|
||||
found_event = Some((event, calendar_path.clone(), event_href));
|
||||
break;
|
||||
}
|
||||
}
|
||||
if found_event.is_some() {
|
||||
break;
|
||||
}
|
||||
},
|
||||
Err(e) => {
|
||||
eprintln!("Failed to fetch events from calendar {}: {}", calendar_path, e);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let (mut event, calendar_path, event_href) = found_event
|
||||
.ok_or_else(|| ApiError::NotFound(format!("Event with UID '{}' not found", request.uid)))?;
|
||||
|
||||
// Parse dates and times
|
||||
let start_datetime = parse_event_datetime(&request.start_date, &request.start_time, request.all_day)
|
||||
.map_err(|e| ApiError::BadRequest(format!("Invalid start date/time: {}", e)))?;
|
||||
|
||||
let end_datetime = parse_event_datetime(&request.end_date, &request.end_time, request.all_day)
|
||||
.map_err(|e| ApiError::BadRequest(format!("Invalid end date/time: {}", e)))?;
|
||||
|
||||
// Validate that end is after start
|
||||
if end_datetime <= start_datetime {
|
||||
return Err(ApiError::BadRequest("End date/time must be after start date/time".to_string()));
|
||||
}
|
||||
|
||||
// Update event properties
|
||||
event.dtstart = start_datetime;
|
||||
event.dtend = Some(end_datetime);
|
||||
event.summary = if request.title.trim().is_empty() { None } else { Some(request.title) };
|
||||
event.description = if request.description.trim().is_empty() { None } else { Some(request.description) };
|
||||
event.location = if request.location.trim().is_empty() { None } else { Some(request.location) };
|
||||
event.all_day = request.all_day;
|
||||
|
||||
// Parse and update status
|
||||
event.status = Some(match request.status.to_lowercase().as_str() {
|
||||
"tentative" => EventStatus::Tentative,
|
||||
"cancelled" => EventStatus::Cancelled,
|
||||
_ => EventStatus::Confirmed,
|
||||
});
|
||||
|
||||
// Parse and update class
|
||||
event.class = Some(match request.class.to_lowercase().as_str() {
|
||||
"private" => EventClass::Private,
|
||||
"confidential" => EventClass::Confidential,
|
||||
_ => EventClass::Public,
|
||||
});
|
||||
|
||||
event.priority = request.priority;
|
||||
|
||||
// Update the event on the CalDAV server
|
||||
client.update_event(&calendar_path, &event, &event_href)
|
||||
.await
|
||||
.map_err(|e| ApiError::Internal(format!("Failed to update event: {}", e)))?;
|
||||
|
||||
Ok(Json(UpdateEventResponse {
|
||||
success: true,
|
||||
message: "Event updated successfully".to_string(),
|
||||
}))
|
||||
}
|
||||
|
||||
fn parse_event_datetime(date_str: &str, time_str: &str, all_day: bool) -> Result<chrono::DateTime<chrono::Utc>, String> {
|
||||
use chrono::{NaiveDate, NaiveTime, NaiveDateTime, Utc, TimeZone};
|
||||
|
||||
// Parse the date
|
||||
let date = NaiveDate::parse_from_str(date_str, "%Y-%m-%d")
|
||||
.map_err(|_| format!("Invalid date format: {}. Expected YYYY-MM-DD", date_str))?;
|
||||
|
||||
if all_day {
|
||||
// For all-day events, use midnight UTC
|
||||
let datetime = date.and_hms_opt(0, 0, 0)
|
||||
.ok_or_else(|| "Failed to create midnight datetime".to_string())?;
|
||||
Ok(Utc.from_utc_datetime(&datetime))
|
||||
} else {
|
||||
// Parse the time
|
||||
let time = NaiveTime::parse_from_str(time_str, "%H:%M")
|
||||
.map_err(|_| format!("Invalid time format: {}. Expected HH:MM", time_str))?;
|
||||
|
||||
// Combine date and time
|
||||
let datetime = NaiveDateTime::new(date, time);
|
||||
|
||||
// Assume local time and convert to UTC (in a real app, you'd want timezone support)
|
||||
Ok(Utc.from_utc_datetime(&datetime))
|
||||
}
|
||||
}
|
||||
694
backend/src/handlers/series.rs
Normal file
694
backend/src/handlers/series.rs
Normal file
@@ -0,0 +1,694 @@
|
||||
use axum::{
|
||||
extract::State,
|
||||
http::HeaderMap,
|
||||
response::Json,
|
||||
};
|
||||
use std::sync::Arc;
|
||||
use chrono::TimeZone;
|
||||
|
||||
use crate::{AppState, models::{ApiError, CreateEventSeriesRequest, CreateEventSeriesResponse, UpdateEventSeriesRequest, UpdateEventSeriesResponse, DeleteEventSeriesRequest, DeleteEventSeriesResponse}};
|
||||
use crate::calendar::CalDAVClient;
|
||||
use calendar_models::{VEvent, EventStatus, EventClass};
|
||||
|
||||
use super::auth::{extract_bearer_token, extract_password_header};
|
||||
|
||||
/// Create a new recurring event series
|
||||
pub async fn create_event_series(
|
||||
State(state): State<Arc<AppState>>,
|
||||
headers: HeaderMap,
|
||||
Json(request): Json<CreateEventSeriesRequest>,
|
||||
) -> Result<Json<CreateEventSeriesResponse>, ApiError> {
|
||||
println!("📝 Create event series request received: title='{}', recurrence='{}', all_day={}",
|
||||
request.title, request.recurrence, request.all_day);
|
||||
|
||||
// Extract and verify token
|
||||
let token = extract_bearer_token(&headers)?;
|
||||
let password = extract_password_header(&headers)?;
|
||||
|
||||
// Validate request
|
||||
if request.title.trim().is_empty() {
|
||||
return Err(ApiError::BadRequest("Event title is required".to_string()));
|
||||
}
|
||||
|
||||
if request.title.len() > 200 {
|
||||
return Err(ApiError::BadRequest("Event title too long (max 200 characters)".to_string()));
|
||||
}
|
||||
|
||||
if request.recurrence == "none" {
|
||||
return Err(ApiError::BadRequest("Use regular create endpoint for non-recurring events".to_string()));
|
||||
}
|
||||
|
||||
// Validate recurrence type
|
||||
match request.recurrence.to_lowercase().as_str() {
|
||||
"daily" | "weekly" | "monthly" | "yearly" => {},
|
||||
_ => return Err(ApiError::BadRequest("Invalid recurrence type. Must be daily, weekly, monthly, or yearly".to_string())),
|
||||
}
|
||||
|
||||
// Create CalDAV config from token and password
|
||||
let config = state.auth_service.caldav_config_from_token(&token, &password)?;
|
||||
let client = CalDAVClient::new(config);
|
||||
|
||||
// Determine which calendar to use
|
||||
let calendar_path = if let Some(ref path) = request.calendar_path {
|
||||
path.clone()
|
||||
} else {
|
||||
// Use the first available calendar
|
||||
let calendar_paths = client.discover_calendars()
|
||||
.await
|
||||
.map_err(|e| ApiError::Internal(format!("Failed to discover calendars: {}", e)))?;
|
||||
|
||||
if calendar_paths.is_empty() {
|
||||
return Err(ApiError::BadRequest("No calendars available for event creation".to_string()));
|
||||
}
|
||||
|
||||
calendar_paths[0].clone()
|
||||
};
|
||||
|
||||
println!("📅 Using calendar path: {}", calendar_path);
|
||||
|
||||
// Parse datetime components
|
||||
let start_date = chrono::NaiveDate::parse_from_str(&request.start_date, "%Y-%m-%d")
|
||||
.map_err(|_| ApiError::BadRequest("Invalid start_date format. Expected YYYY-MM-DD".to_string()))?;
|
||||
|
||||
let (start_datetime, end_datetime) = if request.all_day {
|
||||
// For all-day events, use the dates as-is
|
||||
let start_dt = start_date.and_hms_opt(0, 0, 0)
|
||||
.ok_or_else(|| ApiError::BadRequest("Invalid start date".to_string()))?;
|
||||
|
||||
let end_date = if !request.end_date.is_empty() {
|
||||
chrono::NaiveDate::parse_from_str(&request.end_date, "%Y-%m-%d")
|
||||
.map_err(|_| ApiError::BadRequest("Invalid end_date format. Expected YYYY-MM-DD".to_string()))?
|
||||
} else {
|
||||
start_date
|
||||
};
|
||||
|
||||
let end_dt = end_date.and_hms_opt(23, 59, 59)
|
||||
.ok_or_else(|| ApiError::BadRequest("Invalid end date".to_string()))?;
|
||||
|
||||
(chrono::Utc.from_utc_datetime(&start_dt), chrono::Utc.from_utc_datetime(&end_dt))
|
||||
} else {
|
||||
// Parse times for timed events
|
||||
let start_time = if !request.start_time.is_empty() {
|
||||
chrono::NaiveTime::parse_from_str(&request.start_time, "%H:%M")
|
||||
.map_err(|_| ApiError::BadRequest("Invalid start_time format. Expected HH:MM".to_string()))?
|
||||
} else {
|
||||
chrono::NaiveTime::from_hms_opt(9, 0, 0).unwrap() // Default to 9 AM
|
||||
};
|
||||
|
||||
let end_time = if !request.end_time.is_empty() {
|
||||
chrono::NaiveTime::parse_from_str(&request.end_time, "%H:%M")
|
||||
.map_err(|_| ApiError::BadRequest("Invalid end_time format. Expected HH:MM".to_string()))?
|
||||
} else {
|
||||
chrono::NaiveTime::from_hms_opt(10, 0, 0).unwrap() // Default to 1 hour duration
|
||||
};
|
||||
|
||||
let start_dt = start_date.and_time(start_time);
|
||||
let end_dt = if !request.end_date.is_empty() {
|
||||
let end_date = chrono::NaiveDate::parse_from_str(&request.end_date, "%Y-%m-%d")
|
||||
.map_err(|_| ApiError::BadRequest("Invalid end_date format. Expected YYYY-MM-DD".to_string()))?;
|
||||
end_date.and_time(end_time)
|
||||
} else {
|
||||
start_date.and_time(end_time)
|
||||
};
|
||||
|
||||
(chrono::Utc.from_utc_datetime(&start_dt), chrono::Utc.from_utc_datetime(&end_dt))
|
||||
};
|
||||
|
||||
// Generate a unique UID for the series
|
||||
let uid = format!("series-{}", uuid::Uuid::new_v4().to_string());
|
||||
|
||||
// Create the VEvent for the series
|
||||
let mut event = VEvent::new(uid.clone(), start_datetime);
|
||||
event.dtend = Some(end_datetime);
|
||||
event.summary = if request.title.trim().is_empty() { None } else { Some(request.title.clone()) };
|
||||
event.description = if request.description.trim().is_empty() { None } else { Some(request.description.clone()) };
|
||||
event.location = if request.location.trim().is_empty() { None } else { Some(request.location.clone()) };
|
||||
|
||||
// Set event status
|
||||
event.status = Some(match request.status.to_lowercase().as_str() {
|
||||
"tentative" => EventStatus::Tentative,
|
||||
"cancelled" => EventStatus::Cancelled,
|
||||
_ => EventStatus::Confirmed,
|
||||
});
|
||||
|
||||
// Set event class
|
||||
event.class = Some(match request.class.to_lowercase().as_str() {
|
||||
"private" => EventClass::Private,
|
||||
"confidential" => EventClass::Confidential,
|
||||
_ => EventClass::Public,
|
||||
});
|
||||
|
||||
// Set priority
|
||||
event.priority = request.priority;
|
||||
|
||||
// Generate the RRULE for recurrence
|
||||
let rrule = build_series_rrule(&request)?;
|
||||
event.rrule = Some(rrule);
|
||||
|
||||
println!("🔁 Generated RRULE: {:?}", event.rrule);
|
||||
|
||||
// Create the event on the CalDAV server
|
||||
let event_href = client.create_event(&calendar_path, &event)
|
||||
.await
|
||||
.map_err(|e| ApiError::Internal(format!("Failed to create event series: {}", e)))?;
|
||||
|
||||
println!("✅ Event series created successfully with UID: {}, href: {}", uid, event_href);
|
||||
|
||||
Ok(Json(CreateEventSeriesResponse {
|
||||
success: true,
|
||||
message: "Event series created successfully".to_string(),
|
||||
series_uid: Some(uid),
|
||||
occurrences_created: Some(1), // Series created as a single repeating event
|
||||
event_href: Some(event_href),
|
||||
}))
|
||||
}
|
||||
|
||||
/// Update a recurring event series with different scope options
|
||||
pub async fn update_event_series(
|
||||
State(state): State<Arc<AppState>>,
|
||||
headers: HeaderMap,
|
||||
Json(request): Json<UpdateEventSeriesRequest>,
|
||||
) -> Result<Json<UpdateEventSeriesResponse>, ApiError> {
|
||||
println!("🔄 Update event series request received: series_uid='{}', update_scope='{}'",
|
||||
request.series_uid, request.update_scope);
|
||||
|
||||
// Extract and verify token
|
||||
let token = extract_bearer_token(&headers)?;
|
||||
let password = extract_password_header(&headers)?;
|
||||
|
||||
// Validate request
|
||||
if request.series_uid.trim().is_empty() {
|
||||
return Err(ApiError::BadRequest("Series UID is required".to_string()));
|
||||
}
|
||||
|
||||
if request.title.trim().is_empty() {
|
||||
return Err(ApiError::BadRequest("Event title is required".to_string()));
|
||||
}
|
||||
|
||||
if request.title.len() > 200 {
|
||||
return Err(ApiError::BadRequest("Event title too long (max 200 characters)".to_string()));
|
||||
}
|
||||
|
||||
// Validate update scope
|
||||
match request.update_scope.as_str() {
|
||||
"this_only" | "this_and_future" | "all_in_series" => {},
|
||||
_ => return Err(ApiError::BadRequest("Invalid update_scope. Must be: this_only, this_and_future, or all_in_series".to_string())),
|
||||
}
|
||||
|
||||
// Validate recurrence type
|
||||
match request.recurrence.to_lowercase().as_str() {
|
||||
"daily" | "weekly" | "monthly" | "yearly" => {},
|
||||
_ => return Err(ApiError::BadRequest("Invalid recurrence type. Must be daily, weekly, monthly, or yearly".to_string())),
|
||||
}
|
||||
|
||||
// Create CalDAV config from token and password
|
||||
let config = state.auth_service.caldav_config_from_token(&token, &password)?;
|
||||
let client = CalDAVClient::new(config);
|
||||
|
||||
// Determine which calendar to search (or search all calendars)
|
||||
let calendar_paths = if let Some(ref path) = request.calendar_path {
|
||||
vec![path.clone()]
|
||||
} else {
|
||||
client.discover_calendars()
|
||||
.await
|
||||
.map_err(|e| ApiError::Internal(format!("Failed to discover calendars: {}", e)))?
|
||||
};
|
||||
|
||||
if calendar_paths.is_empty() {
|
||||
return Err(ApiError::BadRequest("No calendars available for event update".to_string()));
|
||||
}
|
||||
|
||||
// Find the series event across all specified calendars
|
||||
let mut existing_event = None;
|
||||
let mut calendar_path = String::new();
|
||||
|
||||
for path in &calendar_paths {
|
||||
if let Ok(Some(event)) = client.fetch_event_by_uid(path, &request.series_uid).await {
|
||||
existing_event = Some(event);
|
||||
calendar_path = path.clone();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
let mut existing_event = existing_event
|
||||
.ok_or_else(|| ApiError::NotFound(format!("Event series with UID '{}' not found", request.series_uid)))?;
|
||||
|
||||
println!("📅 Found series event in calendar: {}", calendar_path);
|
||||
|
||||
// Parse datetime components for the update
|
||||
let start_date = chrono::NaiveDate::parse_from_str(&request.start_date, "%Y-%m-%d")
|
||||
.map_err(|_| ApiError::BadRequest("Invalid start_date format. Expected YYYY-MM-DD".to_string()))?;
|
||||
|
||||
let (start_datetime, end_datetime) = if request.all_day {
|
||||
let start_dt = start_date.and_hms_opt(0, 0, 0)
|
||||
.ok_or_else(|| ApiError::BadRequest("Invalid start date".to_string()))?;
|
||||
|
||||
let end_date = if !request.end_date.is_empty() {
|
||||
chrono::NaiveDate::parse_from_str(&request.end_date, "%Y-%m-%d")
|
||||
.map_err(|_| ApiError::BadRequest("Invalid end_date format. Expected YYYY-MM-DD".to_string()))?
|
||||
} else {
|
||||
start_date
|
||||
};
|
||||
|
||||
let end_dt = end_date.and_hms_opt(23, 59, 59)
|
||||
.ok_or_else(|| ApiError::BadRequest("Invalid end date".to_string()))?;
|
||||
|
||||
(chrono::Utc.from_utc_datetime(&start_dt), chrono::Utc.from_utc_datetime(&end_dt))
|
||||
} else {
|
||||
let start_time = if !request.start_time.is_empty() {
|
||||
chrono::NaiveTime::parse_from_str(&request.start_time, "%H:%M")
|
||||
.map_err(|_| ApiError::BadRequest("Invalid start_time format. Expected HH:MM".to_string()))?
|
||||
} else {
|
||||
existing_event.dtstart.time()
|
||||
};
|
||||
|
||||
let end_time = if !request.end_time.is_empty() {
|
||||
chrono::NaiveTime::parse_from_str(&request.end_time, "%H:%M")
|
||||
.map_err(|_| ApiError::BadRequest("Invalid end_time format. Expected HH:MM".to_string()))?
|
||||
} else {
|
||||
existing_event.dtend.map(|dt| dt.time()).unwrap_or_else(|| {
|
||||
existing_event.dtstart.time() + chrono::Duration::hours(1)
|
||||
})
|
||||
};
|
||||
|
||||
let start_dt = start_date.and_time(start_time);
|
||||
let end_dt = if !request.end_date.is_empty() {
|
||||
let end_date = chrono::NaiveDate::parse_from_str(&request.end_date, "%Y-%m-%d")
|
||||
.map_err(|_| ApiError::BadRequest("Invalid end_date format. Expected YYYY-MM-DD".to_string()))?;
|
||||
end_date.and_time(end_time)
|
||||
} else {
|
||||
start_date.and_time(end_time)
|
||||
};
|
||||
|
||||
(chrono::Utc.from_utc_datetime(&start_dt), chrono::Utc.from_utc_datetime(&end_dt))
|
||||
};
|
||||
|
||||
// Handle different update scopes
|
||||
let (updated_event, occurrences_affected) = match request.update_scope.as_str() {
|
||||
"all_in_series" => {
|
||||
// Update the entire series - modify the master event
|
||||
update_entire_series(&mut existing_event, &request, start_datetime, end_datetime)?
|
||||
},
|
||||
"this_and_future" => {
|
||||
// Split the series: keep past occurrences, create new series from occurrence date
|
||||
update_this_and_future(&mut existing_event, &request, start_datetime, end_datetime)?
|
||||
},
|
||||
"this_only" => {
|
||||
// Create exception for single occurrence, keep original series
|
||||
update_single_occurrence(&mut existing_event, &request, start_datetime, end_datetime)?
|
||||
},
|
||||
_ => unreachable!(), // Already validated above
|
||||
};
|
||||
|
||||
// Update the event on the CalDAV server
|
||||
// Generate event href from UID
|
||||
let event_href = format!("{}.ics", request.series_uid);
|
||||
client.update_event(&calendar_path, &updated_event, &event_href)
|
||||
.await
|
||||
.map_err(|e| ApiError::Internal(format!("Failed to update event series: {}", e)))?;
|
||||
|
||||
println!("✅ Event series updated successfully with UID: {}", request.series_uid);
|
||||
|
||||
Ok(Json(UpdateEventSeriesResponse {
|
||||
success: true,
|
||||
message: "Event series updated successfully".to_string(),
|
||||
series_uid: Some(request.series_uid),
|
||||
occurrences_affected: Some(occurrences_affected),
|
||||
}))
|
||||
}
|
||||
|
||||
/// Delete a recurring event series or specific occurrences
|
||||
pub async fn delete_event_series(
|
||||
State(state): State<Arc<AppState>>,
|
||||
headers: HeaderMap,
|
||||
Json(request): Json<DeleteEventSeriesRequest>,
|
||||
) -> Result<Json<DeleteEventSeriesResponse>, ApiError> {
|
||||
println!("🗑️ Delete event series request received: series_uid='{}', delete_scope='{}'",
|
||||
request.series_uid, request.delete_scope);
|
||||
|
||||
// Extract and verify token
|
||||
let token = extract_bearer_token(&headers)?;
|
||||
let password = extract_password_header(&headers)?;
|
||||
|
||||
// Validate request
|
||||
if request.series_uid.trim().is_empty() {
|
||||
return Err(ApiError::BadRequest("Series UID is required".to_string()));
|
||||
}
|
||||
|
||||
if request.calendar_path.trim().is_empty() {
|
||||
return Err(ApiError::BadRequest("Calendar path is required".to_string()));
|
||||
}
|
||||
|
||||
if request.event_href.trim().is_empty() {
|
||||
return Err(ApiError::BadRequest("Event href is required".to_string()));
|
||||
}
|
||||
|
||||
// Validate delete scope
|
||||
match request.delete_scope.as_str() {
|
||||
"this_only" | "this_and_future" | "all_in_series" => {},
|
||||
_ => return Err(ApiError::BadRequest("Invalid delete_scope. Must be: this_only, this_and_future, or all_in_series".to_string())),
|
||||
}
|
||||
|
||||
// Create CalDAV config from token and password
|
||||
let config = state.auth_service.caldav_config_from_token(&token, &password)?;
|
||||
let client = CalDAVClient::new(config);
|
||||
|
||||
// Handle different deletion scopes
|
||||
let occurrences_affected = match request.delete_scope.as_str() {
|
||||
"all_in_series" => {
|
||||
// Delete the entire series - simply delete the event
|
||||
delete_entire_series(&client, &request).await?
|
||||
},
|
||||
"this_and_future" => {
|
||||
// Modify RRULE to end before this occurrence
|
||||
delete_this_and_future(&client, &request).await?
|
||||
},
|
||||
"this_only" => {
|
||||
// Add EXDATE for single occurrence
|
||||
delete_single_occurrence(&client, &request).await?
|
||||
},
|
||||
_ => unreachable!(), // Already validated above
|
||||
};
|
||||
|
||||
println!("✅ Event series deletion completed with {} occurrences affected", occurrences_affected);
|
||||
|
||||
Ok(Json(DeleteEventSeriesResponse {
|
||||
success: true,
|
||||
message: "Event series deletion completed successfully".to_string(),
|
||||
occurrences_affected: Some(occurrences_affected),
|
||||
}))
|
||||
}
|
||||
|
||||
// Helper functions
|
||||
|
||||
fn build_series_rrule(request: &CreateEventSeriesRequest) -> Result<String, ApiError> {
|
||||
let mut rrule_parts = Vec::new();
|
||||
|
||||
// Add frequency
|
||||
match request.recurrence.to_lowercase().as_str() {
|
||||
"daily" => rrule_parts.push("FREQ=DAILY".to_string()),
|
||||
"weekly" => rrule_parts.push("FREQ=WEEKLY".to_string()),
|
||||
"monthly" => rrule_parts.push("FREQ=MONTHLY".to_string()),
|
||||
"yearly" => rrule_parts.push("FREQ=YEARLY".to_string()),
|
||||
_ => return Err(ApiError::BadRequest("Invalid recurrence type".to_string())),
|
||||
}
|
||||
|
||||
// Add interval if specified and greater than 1
|
||||
if let Some(interval) = request.recurrence_interval {
|
||||
if interval > 1 {
|
||||
rrule_parts.push(format!("INTERVAL={}", interval));
|
||||
}
|
||||
}
|
||||
|
||||
// Handle weekly recurrence with specific days (BYDAY)
|
||||
if request.recurrence.to_lowercase() == "weekly" && request.recurrence_days.len() == 7 {
|
||||
let selected_days: Vec<&str> = request.recurrence_days
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter_map(|(i, &selected)| {
|
||||
if selected {
|
||||
Some(match i {
|
||||
0 => "SU", // Sunday
|
||||
1 => "MO", // Monday
|
||||
2 => "TU", // Tuesday
|
||||
3 => "WE", // Wednesday
|
||||
4 => "TH", // Thursday
|
||||
5 => "FR", // Friday
|
||||
6 => "SA", // Saturday
|
||||
_ => return None,
|
||||
})
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
if !selected_days.is_empty() {
|
||||
rrule_parts.push(format!("BYDAY={}", selected_days.join(",")));
|
||||
}
|
||||
}
|
||||
|
||||
// Add end date if specified (UNTIL takes precedence over COUNT)
|
||||
if let Some(end_date) = &request.recurrence_end_date {
|
||||
// Parse the end date and convert to RRULE format (YYYYMMDDTHHMMSSZ)
|
||||
match chrono::NaiveDate::parse_from_str(end_date, "%Y-%m-%d") {
|
||||
Ok(date) => {
|
||||
let end_datetime = date.and_hms_opt(23, 59, 59)
|
||||
.ok_or_else(|| ApiError::BadRequest("Invalid end date".to_string()))?;
|
||||
let utc_end = chrono::Utc.from_utc_datetime(&end_datetime);
|
||||
rrule_parts.push(format!("UNTIL={}", utc_end.format("%Y%m%dT%H%M%SZ")));
|
||||
},
|
||||
Err(_) => return Err(ApiError::BadRequest("Invalid recurrence_end_date format. Expected YYYY-MM-DD".to_string())),
|
||||
}
|
||||
} else if let Some(count) = request.recurrence_count {
|
||||
if count > 0 {
|
||||
rrule_parts.push(format!("COUNT={}", count));
|
||||
}
|
||||
}
|
||||
|
||||
Ok(rrule_parts.join(";"))
|
||||
}
|
||||
|
||||
/// Update the entire series - modify the master event
|
||||
fn update_entire_series(
|
||||
existing_event: &mut VEvent,
|
||||
request: &UpdateEventSeriesRequest,
|
||||
start_datetime: chrono::DateTime<chrono::Utc>,
|
||||
end_datetime: chrono::DateTime<chrono::Utc>,
|
||||
) -> Result<(VEvent, u32), ApiError> {
|
||||
// Create a new series request for RRULE generation
|
||||
let series_request = CreateEventSeriesRequest {
|
||||
title: request.title.clone(),
|
||||
description: request.description.clone(),
|
||||
start_date: request.start_date.clone(),
|
||||
start_time: request.start_time.clone(),
|
||||
end_date: request.end_date.clone(),
|
||||
end_time: request.end_time.clone(),
|
||||
location: request.location.clone(),
|
||||
all_day: request.all_day,
|
||||
status: request.status.clone(),
|
||||
class: request.class.clone(),
|
||||
priority: request.priority,
|
||||
organizer: request.organizer.clone(),
|
||||
attendees: request.attendees.clone(),
|
||||
categories: request.categories.clone(),
|
||||
reminder: request.reminder.clone(),
|
||||
recurrence: request.recurrence.clone(),
|
||||
recurrence_days: request.recurrence_days.clone(),
|
||||
recurrence_interval: request.recurrence_interval,
|
||||
recurrence_end_date: request.recurrence_end_date.clone(),
|
||||
recurrence_count: request.recurrence_count,
|
||||
calendar_path: None, // Not needed for RRULE generation
|
||||
};
|
||||
|
||||
// Update all fields of the existing event
|
||||
existing_event.dtstart = start_datetime;
|
||||
existing_event.dtend = Some(end_datetime);
|
||||
existing_event.summary = if request.title.trim().is_empty() { None } else { Some(request.title.clone()) };
|
||||
existing_event.description = if request.description.trim().is_empty() { None } else { Some(request.description.clone()) };
|
||||
existing_event.location = if request.location.trim().is_empty() { None } else { Some(request.location.clone()) };
|
||||
|
||||
existing_event.status = Some(match request.status.to_lowercase().as_str() {
|
||||
"tentative" => EventStatus::Tentative,
|
||||
"cancelled" => EventStatus::Cancelled,
|
||||
_ => EventStatus::Confirmed,
|
||||
});
|
||||
|
||||
existing_event.class = Some(match request.class.to_lowercase().as_str() {
|
||||
"private" => EventClass::Private,
|
||||
"confidential" => EventClass::Confidential,
|
||||
_ => EventClass::Public,
|
||||
});
|
||||
|
||||
existing_event.priority = request.priority;
|
||||
|
||||
// Update the RRULE
|
||||
existing_event.rrule = Some(build_series_rrule(&series_request)?);
|
||||
|
||||
Ok((existing_event.clone(), 1)) // 1 series updated (affects all occurrences)
|
||||
}
|
||||
|
||||
/// Update this occurrence and all future occurrences
|
||||
fn update_this_and_future(
|
||||
existing_event: &mut VEvent,
|
||||
request: &UpdateEventSeriesRequest,
|
||||
start_datetime: chrono::DateTime<chrono::Utc>,
|
||||
end_datetime: chrono::DateTime<chrono::Utc>,
|
||||
) -> Result<(VEvent, u32), ApiError> {
|
||||
// For now, treat this the same as update_entire_series
|
||||
// In a full implementation, this would:
|
||||
// 1. Add UNTIL to the original series to stop at the occurrence date
|
||||
// 2. Create a new series starting from the occurrence date with updated properties
|
||||
|
||||
// For simplicity, we'll modify the original series with an UNTIL date if occurrence_date is provided
|
||||
if let Some(occurrence_date) = &request.occurrence_date {
|
||||
// Parse occurrence date and set as UNTIL for the original series
|
||||
match chrono::NaiveDate::parse_from_str(occurrence_date, "%Y-%m-%d") {
|
||||
Ok(date) => {
|
||||
let until_datetime = date.and_hms_opt(0, 0, 0)
|
||||
.ok_or_else(|| ApiError::BadRequest("Invalid occurrence date".to_string()))?;
|
||||
let utc_until = chrono::Utc.from_utc_datetime(&until_datetime);
|
||||
|
||||
// Create modified RRULE with UNTIL clause
|
||||
let mut rrule = existing_event.rrule.clone().unwrap_or_else(|| "FREQ=WEEKLY".to_string());
|
||||
|
||||
// Remove existing UNTIL or COUNT if present
|
||||
let parts: Vec<&str> = rrule.split(';').filter(|part| {
|
||||
!part.starts_with("UNTIL=") && !part.starts_with("COUNT=")
|
||||
}).collect();
|
||||
|
||||
rrule = format!("{};UNTIL={}", parts.join(";"), utc_until.format("%Y%m%dT%H%M%SZ"));
|
||||
existing_event.rrule = Some(rrule);
|
||||
},
|
||||
Err(_) => return Err(ApiError::BadRequest("Invalid occurrence date format".to_string())),
|
||||
}
|
||||
}
|
||||
|
||||
// Then apply the same updates as all_in_series for the rest of the properties
|
||||
update_entire_series(existing_event, request, start_datetime, end_datetime)
|
||||
}
|
||||
|
||||
/// Update only a single occurrence (create an exception)
|
||||
fn update_single_occurrence(
|
||||
existing_event: &mut VEvent,
|
||||
request: &UpdateEventSeriesRequest,
|
||||
start_datetime: chrono::DateTime<chrono::Utc>,
|
||||
end_datetime: chrono::DateTime<chrono::Utc>,
|
||||
) -> Result<(VEvent, u32), ApiError> {
|
||||
// For single occurrence updates, we need to:
|
||||
// 1. Keep the original series unchanged
|
||||
// 2. Create a new single event (exception) with the same UID but different RECURRENCE-ID
|
||||
|
||||
// Create a new event for the single occurrence
|
||||
let occurrence_uid = if let Some(occurrence_date) = &request.occurrence_date {
|
||||
format!("{}-exception-{}", existing_event.uid, occurrence_date)
|
||||
} else {
|
||||
format!("{}-exception", existing_event.uid)
|
||||
};
|
||||
|
||||
let mut exception_event = VEvent::new(occurrence_uid, start_datetime);
|
||||
exception_event.dtend = Some(end_datetime);
|
||||
exception_event.summary = if request.title.trim().is_empty() { None } else { Some(request.title.clone()) };
|
||||
exception_event.description = if request.description.trim().is_empty() { None } else { Some(request.description.clone()) };
|
||||
exception_event.location = if request.location.trim().is_empty() { None } else { Some(request.location.clone()) };
|
||||
|
||||
exception_event.status = Some(match request.status.to_lowercase().as_str() {
|
||||
"tentative" => EventStatus::Tentative,
|
||||
"cancelled" => EventStatus::Cancelled,
|
||||
_ => EventStatus::Confirmed,
|
||||
});
|
||||
|
||||
exception_event.class = Some(match request.class.to_lowercase().as_str() {
|
||||
"private" => EventClass::Private,
|
||||
"confidential" => EventClass::Confidential,
|
||||
_ => EventClass::Public,
|
||||
});
|
||||
|
||||
exception_event.priority = request.priority;
|
||||
|
||||
// Note: This function returns the exception event, but in a full implementation,
|
||||
// we would need to create this as a separate event and add an EXDATE to the original series
|
||||
Ok((exception_event, 1)) // 1 occurrence updated
|
||||
}
|
||||
|
||||
/// Delete the entire series
|
||||
async fn delete_entire_series(
|
||||
client: &CalDAVClient,
|
||||
request: &DeleteEventSeriesRequest,
|
||||
) -> Result<u32, ApiError> {
|
||||
// Simply delete the entire event from the CalDAV server
|
||||
client.delete_event(&request.calendar_path, &request.event_href)
|
||||
.await
|
||||
.map_err(|e| ApiError::Internal(format!("Failed to delete event series: {}", e)))?;
|
||||
|
||||
println!("🗑️ Entire series deleted: {}", request.series_uid);
|
||||
Ok(1) // 1 series deleted (affects all occurrences)
|
||||
}
|
||||
|
||||
/// Delete this occurrence and all future occurrences (modify RRULE with UNTIL)
|
||||
async fn delete_this_and_future(
|
||||
client: &CalDAVClient,
|
||||
request: &DeleteEventSeriesRequest,
|
||||
) -> Result<u32, ApiError> {
|
||||
// Fetch the existing event to modify its RRULE
|
||||
let event_uid = request.series_uid.clone();
|
||||
let existing_event = client.fetch_event_by_uid(&request.calendar_path, &event_uid)
|
||||
.await
|
||||
.map_err(|e| ApiError::Internal(format!("Failed to fetch event for modification: {}", e)))?
|
||||
.ok_or_else(|| ApiError::NotFound(format!("Event series with UID '{}' not found", event_uid)))?;
|
||||
|
||||
// If no occurrence_date is provided, delete the entire series
|
||||
let Some(occurrence_date) = &request.occurrence_date else {
|
||||
return delete_entire_series(client, request).await;
|
||||
};
|
||||
|
||||
// Parse occurrence date to set as UNTIL for the RRULE
|
||||
let until_date = chrono::NaiveDate::parse_from_str(occurrence_date, "%Y-%m-%d")
|
||||
.map_err(|_| ApiError::BadRequest("Invalid occurrence date format. Expected YYYY-MM-DD".to_string()))?;
|
||||
|
||||
// Set UNTIL to the day before the occurrence to exclude it and all future occurrences
|
||||
let until_datetime = until_date.pred_opt()
|
||||
.ok_or_else(|| ApiError::BadRequest("Cannot delete from the first possible date".to_string()))?
|
||||
.and_hms_opt(23, 59, 59)
|
||||
.ok_or_else(|| ApiError::BadRequest("Invalid date calculation".to_string()))?;
|
||||
let utc_until = chrono::Utc.from_utc_datetime(&until_datetime);
|
||||
|
||||
// Modify the existing event's RRULE
|
||||
let mut updated_event = existing_event;
|
||||
if let Some(rrule) = &updated_event.rrule {
|
||||
// Remove existing UNTIL or COUNT if present and add new UNTIL
|
||||
let parts: Vec<&str> = rrule.split(';').filter(|part| {
|
||||
!part.starts_with("UNTIL=") && !part.starts_with("COUNT=")
|
||||
}).collect();
|
||||
|
||||
updated_event.rrule = Some(format!("{};UNTIL={}", parts.join(";"), utc_until.format("%Y%m%dT%H%M%SZ")));
|
||||
}
|
||||
|
||||
// Update the event on the CalDAV server
|
||||
client.update_event(&request.calendar_path, &updated_event, &request.event_href)
|
||||
.await
|
||||
.map_err(|e| ApiError::Internal(format!("Failed to update event series for deletion: {}", e)))?;
|
||||
|
||||
println!("🗑️ Series modified with UNTIL for this_and_future deletion: {}", utc_until.format("%Y-%m-%d"));
|
||||
Ok(1) // 1 series modified
|
||||
}
|
||||
|
||||
/// Delete only a single occurrence (add EXDATE)
|
||||
async fn delete_single_occurrence(
|
||||
client: &CalDAVClient,
|
||||
request: &DeleteEventSeriesRequest,
|
||||
) -> Result<u32, ApiError> {
|
||||
// Fetch the existing event to add EXDATE
|
||||
let event_uid = request.series_uid.clone();
|
||||
let existing_event = client.fetch_event_by_uid(&request.calendar_path, &event_uid)
|
||||
.await
|
||||
.map_err(|e| ApiError::Internal(format!("Failed to fetch event for modification: {}", e)))?
|
||||
.ok_or_else(|| ApiError::NotFound(format!("Event series with UID '{}' not found", event_uid)))?;
|
||||
|
||||
// If no occurrence_date is provided, cannot delete single occurrence
|
||||
let Some(occurrence_date) = &request.occurrence_date else {
|
||||
return Err(ApiError::BadRequest("occurrence_date is required for single occurrence deletion".to_string()));
|
||||
};
|
||||
|
||||
// Parse occurrence date
|
||||
let exception_date = chrono::NaiveDate::parse_from_str(occurrence_date, "%Y-%m-%d")
|
||||
.map_err(|_| ApiError::BadRequest("Invalid occurrence date format. Expected YYYY-MM-DD".to_string()))?;
|
||||
|
||||
// Create the EXDATE datetime (use the same time as the original event)
|
||||
let original_time = existing_event.dtstart.time();
|
||||
let exception_datetime = exception_date.and_time(original_time);
|
||||
let exception_utc = chrono::Utc.from_utc_datetime(&exception_datetime);
|
||||
|
||||
// Add the exception date to the event's EXDATE list
|
||||
let mut updated_event = existing_event;
|
||||
updated_event.exdate.push(exception_utc);
|
||||
|
||||
println!("🗑️ Added EXDATE for single occurrence deletion: {}", exception_utc.format("%Y%m%dT%H%M%SZ"));
|
||||
|
||||
// Update the event on the CalDAV server
|
||||
client.update_event(&request.calendar_path, &updated_event, &request.event_href)
|
||||
.await
|
||||
.map_err(|e| ApiError::Internal(format!("Failed to update event series for single deletion: {}", e)))?;
|
||||
|
||||
Ok(1) // 1 occurrence excluded
|
||||
}
|
||||
Reference in New Issue
Block a user