| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
use chrono::{DateTime, Duration, Local, TimeZone, Utc}; |
| 7 |
use serde::{Deserialize, Serialize}; |
| 8 |
use std::sync::Arc; |
| 9 |
use tauri::State; |
| 10 |
use tracing::instrument; |
| 11 |
|
| 12 |
use goingson_core::{BlockType, ContactId, DbValue, Event, EventId, NewEvent, ParseableEnum, ProjectId, Recurrence, RecurrenceRule, TaskId, UpdateEvent, Validate, expand_recurrence_in_tz}; |
| 13 |
|
| 14 |
use crate::state::{AppState, DESKTOP_USER_ID}; |
| 15 |
use super::{ApiError, OptionNotFound}; |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
#[derive(Debug, Deserialize)] |
| 24 |
#[serde(rename_all = "camelCase")] |
| 25 |
pub struct EventInput { |
| 26 |
|
| 27 |
pub project_id: Option<ProjectId>, |
| 28 |
|
| 29 |
pub title: String, |
| 30 |
|
| 31 |
pub description: Option<String>, |
| 32 |
|
| 33 |
pub start_time: DateTime<Utc>, |
| 34 |
|
| 35 |
pub end_time: Option<DateTime<Utc>>, |
| 36 |
|
| 37 |
pub location: Option<String>, |
| 38 |
|
| 39 |
pub recurrence: Option<String>, |
| 40 |
|
| 41 |
pub contact_id: Option<ContactId>, |
| 42 |
|
| 43 |
pub block_type: Option<String>, |
| 44 |
|
| 45 |
pub recurrence_rule: Option<RecurrenceRule>, |
| 46 |
|
| 47 |
#[serde(default)] |
| 48 |
pub reminder_offsets_seconds: Vec<i64>, |
| 49 |
|
| 50 |
|
| 51 |
#[serde(default)] |
| 52 |
pub is_all_day: bool, |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
fn resolve_event_span(input: &EventInput) -> (DateTime<Utc>, Option<DateTime<Utc>>) { |
| 61 |
if input.is_all_day { |
| 62 |
let (start, end) = goingson_core::snap_all_day_span(input.start_time, input.end_time, &Local); |
| 63 |
(start, Some(end)) |
| 64 |
} else { |
| 65 |
(input.start_time, input.end_time) |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
#[derive(Debug, Serialize)] |
| 70 |
#[serde(rename_all = "camelCase")] |
| 71 |
pub struct EventResponse { |
| 72 |
pub id: EventId, |
| 73 |
pub project_id: Option<ProjectId>, |
| 74 |
pub project_name: Option<String>, |
| 75 |
pub title: String, |
| 76 |
pub description: String, |
| 77 |
pub description_html: String, |
| 78 |
pub start_time: DateTime<Utc>, |
| 79 |
pub end_time: Option<DateTime<Utc>>, |
| 80 |
pub location: Option<String>, |
| 81 |
pub linked_task_id: Option<TaskId>, |
| 82 |
pub recurrence: String, |
| 83 |
pub recurrence_rule: Option<RecurrenceRule>, |
| 84 |
pub recurrence_display: String, |
| 85 |
pub is_recurring_instance: bool, |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
pub is_template: bool, |
| 90 |
pub contact_id: Option<ContactId>, |
| 91 |
pub contact_name: Option<String>, |
| 92 |
pub block_type: Option<String>, |
| 93 |
|
| 94 |
|
| 95 |
pub is_past: bool, |
| 96 |
|
| 97 |
pub proximity_class: String, |
| 98 |
|
| 99 |
pub proximity_label: String, |
| 100 |
|
| 101 |
pub date_formatted: String, |
| 102 |
|
| 103 |
pub time_formatted: String, |
| 104 |
|
| 105 |
pub start_time_epoch: i64, |
| 106 |
|
| 107 |
pub end_time_epoch: i64, |
| 108 |
|
| 109 |
|
| 110 |
pub status: String, |
| 111 |
|
| 112 |
pub status_label: String, |
| 113 |
|
| 114 |
pub is_all_day: bool, |
| 115 |
|
| 116 |
pub is_snoozed: bool, |
| 117 |
|
| 118 |
pub snoozed_until: Option<DateTime<Utc>>, |
| 119 |
|
| 120 |
pub reminder_offsets_seconds: Vec<i64>, |
| 121 |
} |
| 122 |
|
| 123 |
impl From<Event> for EventResponse { |
| 124 |
fn from(e: Event) -> Self { |
| 125 |
let now = Local::now(); |
| 126 |
let now_utc = Utc::now(); |
| 127 |
let event_local = e.start_time.with_timezone(&Local); |
| 128 |
|
| 129 |
let today = now.date_naive(); |
| 130 |
let event_day = event_local.date_naive(); |
| 131 |
let diff_days = (event_day - today).num_days(); |
| 132 |
|
| 133 |
let start_time_epoch = e.start_time.timestamp_millis(); |
| 134 |
let end_time_epoch = e.end_time |
| 135 |
.map(|et| et.timestamp_millis()) |
| 136 |
.unwrap_or(start_time_epoch + 3_600_000); |
| 137 |
|
| 138 |
|
| 139 |
let effective_end = e.end_time.unwrap_or(e.start_time + chrono::Duration::hours(1)); |
| 140 |
let (status, status_label, is_past) = if effective_end <= now_utc { |
| 141 |
("past".to_string(), "Past".to_string(), true) |
| 142 |
} else if e.start_time <= now_utc && effective_end > now_utc { |
| 143 |
("happening_now".to_string(), "Happening now".to_string(), false) |
| 144 |
} else if diff_days == 0 { |
| 145 |
("upcoming_today".to_string(), "Today".to_string(), false) |
| 146 |
} else { |
| 147 |
("upcoming".to_string(), "Upcoming".to_string(), false) |
| 148 |
}; |
| 149 |
|
| 150 |
|
| 151 |
let proximity_class = if is_past { |
| 152 |
"past" |
| 153 |
} else if diff_days == 0 { |
| 154 |
"today" |
| 155 |
} else if diff_days == 1 { |
| 156 |
"tomorrow" |
| 157 |
} else if diff_days <= 7 { |
| 158 |
"week" |
| 159 |
} else { |
| 160 |
"future" |
| 161 |
}.to_string(); |
| 162 |
|
| 163 |
let proximity_label = if is_past { |
| 164 |
"Past".to_string() |
| 165 |
} else if diff_days == 0 { |
| 166 |
"Today".to_string() |
| 167 |
} else if diff_days == 1 { |
| 168 |
"Tomorrow".to_string() |
| 169 |
} else if diff_days <= 7 { |
| 170 |
event_local.format("%a").to_string() |
| 171 |
} else { |
| 172 |
event_local.format("%b %d").to_string() |
| 173 |
}; |
| 174 |
|
| 175 |
let date_formatted = proximity_label.clone(); |
| 176 |
let time_formatted = if let Some(end) = e.end_time { |
| 177 |
let end_local = end.with_timezone(&Local); |
| 178 |
format!("{} – {}", event_local.format("%-I:%M %p"), end_local.format("%-I:%M %p")) |
| 179 |
} else { |
| 180 |
event_local.format("%-I:%M %p").to_string() |
| 181 |
}; |
| 182 |
|
| 183 |
let recurrence_display = e.effective_recurrence_rule() |
| 184 |
.map(|r| r.display()) |
| 185 |
.unwrap_or_default(); |
| 186 |
let recurrence_str = e.recurrence.as_str().to_string(); |
| 187 |
let is_all_day = e.is_all_day_in(&Local); |
| 188 |
let is_snoozed = e.is_snoozed(); |
| 189 |
let snoozed_until = e.snoozed_until; |
| 190 |
let reminder_offsets_seconds = e.reminder_offsets_seconds.clone(); |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
let is_template = e.recurrence != Recurrence::None && !e.is_recurring_instance; |
| 195 |
|
| 196 |
EventResponse { |
| 197 |
id: e.id, |
| 198 |
project_id: e.project_id, |
| 199 |
project_name: e.project_name, |
| 200 |
title: e.title, |
| 201 |
description_html: docengine::render_standard(&e.description), |
| 202 |
description: e.description, |
| 203 |
start_time: e.start_time, |
| 204 |
end_time: e.end_time, |
| 205 |
location: e.location, |
| 206 |
linked_task_id: e.linked_task_id, |
| 207 |
recurrence: recurrence_str, |
| 208 |
recurrence_display, |
| 209 |
recurrence_rule: e.recurrence_rule, |
| 210 |
is_recurring_instance: e.is_recurring_instance, |
| 211 |
is_template, |
| 212 |
contact_id: e.contact_id, |
| 213 |
contact_name: e.contact_name, |
| 214 |
block_type: e.block_type.as_ref().map(|b| b.db_value().to_string()), |
| 215 |
is_all_day, |
| 216 |
is_past, |
| 217 |
proximity_class, |
| 218 |
proximity_label, |
| 219 |
date_formatted, |
| 220 |
time_formatted, |
| 221 |
start_time_epoch, |
| 222 |
end_time_epoch, |
| 223 |
status, |
| 224 |
status_label, |
| 225 |
is_snoozed, |
| 226 |
snoozed_until, |
| 227 |
reminder_offsets_seconds, |
| 228 |
} |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
fn sanitize_reminder_offsets(input: &[i64]) -> Vec<i64> { |
| 235 |
let mut offsets: Vec<i64> = input.iter().copied().filter(|s| *s >= 0).collect(); |
| 236 |
offsets.sort_unstable(); |
| 237 |
offsets.dedup(); |
| 238 |
offsets.truncate(8); |
| 239 |
offsets |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
fn expand_and_merge(events: Vec<Event>, range_start: DateTime<Utc>, range_end: DateTime<Utc>) -> Vec<Event> { |
| 247 |
let mut result: Vec<Event> = Vec::new(); |
| 248 |
|
| 249 |
for event in events { |
| 250 |
if event.has_recurrence() && !event.is_recurring_instance { |
| 251 |
|
| 252 |
let expanded = expand_recurrence_in_tz(&event, range_start, range_end, crate::tz::system_tz()); |
| 253 |
result.extend(expanded); |
| 254 |
|
| 255 |
let effective_end = event.end_time.unwrap_or(event.start_time + Duration::hours(1)); |
| 256 |
if effective_end >= range_start && event.start_time <= range_end { |
| 257 |
result.push(event); |
| 258 |
} |
| 259 |
} else { |
| 260 |
result.push(event); |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
result.sort_by_key(|e| e.start_time); |
| 265 |
result |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
#[tauri::command] |
| 276 |
#[instrument(skip_all)] |
| 277 |
pub async fn list_events(state: State<'_, Arc<AppState>>) -> Result<Vec<EventResponse>, ApiError> { |
| 278 |
let now = Utc::now(); |
| 279 |
let range_start = now - Duration::days(30); |
| 280 |
let range_end = now + Duration::days(90); |
| 281 |
|
| 282 |
|
| 283 |
let (all_events, recurring) = tokio::join!( |
| 284 |
state.events.list_all(DESKTOP_USER_ID), |
| 285 |
state.events.list_recurring(DESKTOP_USER_ID), |
| 286 |
); |
| 287 |
let mut events = all_events?; |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
let recurring = recurring?; |
| 292 |
let existing_ids: std::collections::HashSet<_> = events.iter().map(|e| e.id).collect(); |
| 293 |
for r in recurring { |
| 294 |
if !existing_ids.contains(&r.id) { |
| 295 |
events.push(r); |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
let expanded = expand_and_merge(events, range_start, range_end); |
| 300 |
Ok(expanded.into_iter().map(EventResponse::from).collect()) |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
#[tauri::command] |
| 305 |
#[instrument(skip_all)] |
| 306 |
pub async fn list_events_between( |
| 307 |
state: State<'_, Arc<AppState>>, |
| 308 |
start: DateTime<Utc>, |
| 309 |
end: DateTime<Utc>, |
| 310 |
) -> Result<Vec<EventResponse>, ApiError> { |
| 311 |
let (range_events, recurring) = tokio::join!( |
| 312 |
state.events.list_between(DESKTOP_USER_ID, start, end), |
| 313 |
state.events.list_recurring(DESKTOP_USER_ID), |
| 314 |
); |
| 315 |
let mut events = range_events?; |
| 316 |
let recurring = recurring?; |
| 317 |
let existing_ids: std::collections::HashSet<_> = events.iter().map(|e| e.id).collect(); |
| 318 |
for r in recurring { |
| 319 |
if !existing_ids.contains(&r.id) { |
| 320 |
events.push(r); |
| 321 |
} |
| 322 |
} |
| 323 |
let expanded = expand_and_merge(events, start, end); |
| 324 |
Ok(expanded.into_iter().map(EventResponse::from).collect()) |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
#[tauri::command] |
| 334 |
#[instrument(skip_all)] |
| 335 |
pub async fn get_event(state: State<'_, Arc<AppState>>, id: EventId) -> Result<Option<EventResponse>, ApiError> { |
| 336 |
let event = state.events.get_by_id(id, DESKTOP_USER_ID).await?; |
| 337 |
Ok(event.map(EventResponse::from)) |
| 338 |
} |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
#[tauri::command] |
| 358 |
#[instrument(skip_all)] |
| 359 |
pub async fn create_event(state: State<'_, Arc<AppState>>, input: EventInput) -> Result<EventResponse, ApiError> { |
| 360 |
if input.title.trim().is_empty() { |
| 361 |
return Err(ApiError::validation("title", "Title is required")); |
| 362 |
} |
| 363 |
|
| 364 |
let (start_time, end_time) = resolve_event_span(&input); |
| 365 |
|
| 366 |
|
| 367 |
if let Some(end_time) = end_time |
| 368 |
&& end_time <= start_time { |
| 369 |
return Err(ApiError::validation("endTime", "End time must be after start time")); |
| 370 |
} |
| 371 |
|
| 372 |
let recurrence = input.recurrence.as_deref().map(Recurrence::from_str_or_default).unwrap_or(Recurrence::None); |
| 373 |
let block_type = input.block_type.as_deref().and_then(BlockType::from_str_opt); |
| 374 |
|
| 375 |
let new_event = NewEvent { |
| 376 |
user_id: Some(DESKTOP_USER_ID), |
| 377 |
project_id: input.project_id, |
| 378 |
title: input.title, |
| 379 |
description: input.description.unwrap_or_default(), |
| 380 |
start_time, |
| 381 |
end_time, |
| 382 |
location: input.location, |
| 383 |
linked_task_id: None, |
| 384 |
recurrence, |
| 385 |
recurrence_rule: input.recurrence_rule.clone(), |
| 386 |
contact_id: input.contact_id, |
| 387 |
block_type, |
| 388 |
reminder_offsets_seconds: sanitize_reminder_offsets(&input.reminder_offsets_seconds), |
| 389 |
}; |
| 390 |
|
| 391 |
new_event.validate()?; |
| 392 |
|
| 393 |
let event = state.events.create(DESKTOP_USER_ID, new_event).await?; |
| 394 |
Ok(EventResponse::from(event)) |
| 395 |
} |
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
#[tauri::command] |
| 407 |
#[instrument(skip_all)] |
| 408 |
pub async fn update_event(state: State<'_, Arc<AppState>>, id: EventId, input: EventInput) -> Result<EventResponse, ApiError> { |
| 409 |
|
| 410 |
let existing = state.events |
| 411 |
.get_by_id(id, DESKTOP_USER_ID) |
| 412 |
.await? |
| 413 |
.or_not_found("event", id)?; |
| 414 |
|
| 415 |
let recurrence = input.recurrence.as_deref().map(Recurrence::from_str_or_default).unwrap_or(Recurrence::None); |
| 416 |
let block_type = match &input.block_type { |
| 417 |
Some(s) if s.is_empty() => None, |
| 418 |
Some(s) => BlockType::from_str_opt(s), |
| 419 |
None => existing.block_type, |
| 420 |
}; |
| 421 |
|
| 422 |
let (start_time, end_time) = resolve_event_span(&input); |
| 423 |
|
| 424 |
let update_event = UpdateEvent { |
| 425 |
project_id: input.project_id, |
| 426 |
title: input.title, |
| 427 |
description: input.description.unwrap_or_default(), |
| 428 |
start_time, |
| 429 |
end_time, |
| 430 |
location: input.location, |
| 431 |
linked_task_id: existing.linked_task_id, |
| 432 |
recurrence, |
| 433 |
recurrence_rule: input.recurrence_rule.clone(), |
| 434 |
contact_id: input.contact_id, |
| 435 |
block_type, |
| 436 |
reminder_offsets_seconds: sanitize_reminder_offsets(&input.reminder_offsets_seconds), |
| 437 |
}; |
| 438 |
|
| 439 |
update_event.validate()?; |
| 440 |
|
| 441 |
let event = state.events |
| 442 |
.update(id, DESKTOP_USER_ID, update_event) |
| 443 |
.await? |
| 444 |
.or_not_found("event", id)?; |
| 445 |
|
| 446 |
Ok(EventResponse::from(event)) |
| 447 |
} |
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
#[tauri::command] |
| 455 |
#[instrument(skip_all)] |
| 456 |
pub async fn delete_event(state: State<'_, Arc<AppState>>, id: EventId) -> Result<bool, ApiError> { |
| 457 |
Ok(state.events.delete(id, DESKTOP_USER_ID).await?) |
| 458 |
} |
| 459 |
|
| 460 |
|
| 461 |
#[tauri::command] |
| 462 |
#[instrument(skip_all)] |
| 463 |
pub async fn bulk_delete_events( |
| 464 |
state: State<'_, Arc<AppState>>, |
| 465 |
ids: Vec<EventId>, |
| 466 |
) -> Result<u64, ApiError> { |
| 467 |
Ok(state.events.delete_many(&ids, DESKTOP_USER_ID).await?) |
| 468 |
} |
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
#[tauri::command] |
| 476 |
#[instrument(skip_all)] |
| 477 |
pub async fn list_upcoming_events(state: State<'_, Arc<AppState>>) -> Result<Vec<EventResponse>, ApiError> { |
| 478 |
let now = Utc::now(); |
| 479 |
let range_end = now + Duration::days(7); |
| 480 |
|
| 481 |
let (upcoming, recurring) = tokio::join!( |
| 482 |
state.events.get_upcoming(DESKTOP_USER_ID, 7), |
| 483 |
state.events.list_recurring(DESKTOP_USER_ID), |
| 484 |
); |
| 485 |
let mut events = upcoming?; |
| 486 |
let recurring = recurring?; |
| 487 |
let existing_ids: std::collections::HashSet<_> = events.iter().map(|e| e.id).collect(); |
| 488 |
for r in recurring { |
| 489 |
if !existing_ids.contains(&r.id) { |
| 490 |
events.push(r); |
| 491 |
} |
| 492 |
} |
| 493 |
|
| 494 |
let expanded = expand_and_merge(events, now, range_end); |
| 495 |
Ok(expanded.into_iter().map(EventResponse::from).collect()) |
| 496 |
} |
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
#[tauri::command] |
| 506 |
#[instrument(skip_all)] |
| 507 |
pub async fn list_events_for_project(state: State<'_, Arc<AppState>>, project_id: ProjectId) -> Result<Vec<EventResponse>, ApiError> { |
| 508 |
let events = state.events.list_by_project(DESKTOP_USER_ID, project_id).await?; |
| 509 |
Ok(events.into_iter().map(EventResponse::from).collect()) |
| 510 |
} |
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
#[derive(Debug, Serialize)] |
| 519 |
#[serde(rename_all = "camelCase")] |
| 520 |
pub struct EventStatusIndicator { |
| 521 |
|
| 522 |
pub status: String, |
| 523 |
|
| 524 |
pub label: String, |
| 525 |
} |
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
#[tauri::command] |
| 543 |
#[instrument(skip_all)] |
| 544 |
pub async fn get_event_status_indicator( |
| 545 |
state: State<'_, Arc<AppState>>, |
| 546 |
lead_minutes: i64, |
| 547 |
) -> Result<EventStatusIndicator, ApiError> { |
| 548 |
let events = state.events.list_all(DESKTOP_USER_ID).await?; |
| 549 |
let now = Utc::now(); |
| 550 |
let now_millis = now.timestamp_millis(); |
| 551 |
|
| 552 |
|
| 553 |
let local_now = Local::now(); |
| 554 |
let end_of_day = local_now |
| 555 |
.date_naive() |
| 556 |
.and_hms_opt(23, 59, 59) |
| 557 |
.and_then(|ndt| Local.from_local_datetime(&ndt).earliest()) |
| 558 |
.map(|dt| dt.with_timezone(&Utc)); |
| 559 |
|
| 560 |
let eod_millis = end_of_day |
| 561 |
.map(|dt| dt.timestamp_millis()) |
| 562 |
.unwrap_or(now_millis); |
| 563 |
|
| 564 |
let mut has_remaining_today = false; |
| 565 |
|
| 566 |
for e in &events { |
| 567 |
let start_millis = e.start_time.timestamp_millis(); |
| 568 |
let end_millis = e.end_time |
| 569 |
.map(|et| et.timestamp_millis()) |
| 570 |
.unwrap_or(start_millis + 3_600_000); |
| 571 |
|
| 572 |
|
| 573 |
if start_millis <= now_millis && end_millis > now_millis { |
| 574 |
return Ok(EventStatusIndicator { |
| 575 |
status: "red".to_string(), |
| 576 |
label: "Event happening now".to_string(), |
| 577 |
}); |
| 578 |
} |
| 579 |
|
| 580 |
|
| 581 |
let minutes_until = (start_millis - now_millis) as f64 / 60_000.0; |
| 582 |
if minutes_until > 0.0 && minutes_until <= lead_minutes as f64 { |
| 583 |
let rounded = minutes_until.round() as i64; |
| 584 |
let plural = if rounded != 1 { "s" } else { "" }; |
| 585 |
return Ok(EventStatusIndicator { |
| 586 |
status: "yellow".to_string(), |
| 587 |
label: format!("Event in {rounded} minute{plural}"), |
| 588 |
}); |
| 589 |
} |
| 590 |
|
| 591 |
|
| 592 |
if start_millis > now_millis && start_millis <= eod_millis { |
| 593 |
has_remaining_today = true; |
| 594 |
} |
| 595 |
} |
| 596 |
|
| 597 |
if has_remaining_today { |
| 598 |
Ok(EventStatusIndicator { |
| 599 |
status: "green".to_string(), |
| 600 |
label: "No imminent events".to_string(), |
| 601 |
}) |
| 602 |
} else { |
| 603 |
Ok(EventStatusIndicator { |
| 604 |
status: "none".to_string(), |
| 605 |
label: "No more events today".to_string(), |
| 606 |
}) |
| 607 |
} |
| 608 |
} |
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
use super::SnoozeInput; |
| 613 |
|
| 614 |
|
| 615 |
#[tauri::command] |
| 616 |
#[instrument(skip_all)] |
| 617 |
pub async fn list_snoozed_events(state: State<'_, Arc<AppState>>) -> Result<Vec<EventResponse>, ApiError> { |
| 618 |
let events = state.events.list_snoozed(DESKTOP_USER_ID).await?; |
| 619 |
Ok(events.into_iter().map(EventResponse::from).collect()) |
| 620 |
} |
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
#[tauri::command] |
| 627 |
#[instrument(skip_all)] |
| 628 |
pub async fn snooze_event( |
| 629 |
state: State<'_, Arc<AppState>>, |
| 630 |
id: EventId, |
| 631 |
input: SnoozeInput, |
| 632 |
) -> Result<EventResponse, ApiError> { |
| 633 |
let event = state.events |
| 634 |
.snooze(id, DESKTOP_USER_ID, input.until) |
| 635 |
.await? |
| 636 |
.or_not_found("event", id)?; |
| 637 |
Ok(EventResponse::from(event)) |
| 638 |
} |
| 639 |
|
| 640 |
|
| 641 |
#[tauri::command] |
| 642 |
#[instrument(skip_all)] |
| 643 |
pub async fn unsnooze_event( |
| 644 |
state: State<'_, Arc<AppState>>, |
| 645 |
id: EventId, |
| 646 |
) -> Result<EventResponse, ApiError> { |
| 647 |
let event = state.events |
| 648 |
.unsnooze(id, DESKTOP_USER_ID) |
| 649 |
.await? |
| 650 |
.or_not_found("event", id)?; |
| 651 |
Ok(EventResponse::from(event)) |
| 652 |
} |
| 653 |
|
| 654 |
#[cfg(test)] |
| 655 |
mod sanitize_tests { |
| 656 |
use super::sanitize_reminder_offsets; |
| 657 |
|
| 658 |
#[test] |
| 659 |
fn drops_negative() { |
| 660 |
assert_eq!(sanitize_reminder_offsets(&[-1, 0, 60, -100]), vec![0, 60]); |
| 661 |
} |
| 662 |
|
| 663 |
#[test] |
| 664 |
fn dedupes_and_sorts() { |
| 665 |
assert_eq!(sanitize_reminder_offsets(&[60, 0, 60, 300]), vec![0, 60, 300]); |
| 666 |
} |
| 667 |
|
| 668 |
#[test] |
| 669 |
fn caps_to_eight() { |
| 670 |
let many: Vec<i64> = (0..20).map(|i| i * 60).collect(); |
| 671 |
let out = sanitize_reminder_offsets(&many); |
| 672 |
assert_eq!(out.len(), 8); |
| 673 |
assert_eq!(out[0], 0); |
| 674 |
assert_eq!(out[7], 7 * 60); |
| 675 |
} |
| 676 |
|
| 677 |
#[test] |
| 678 |
fn empty_stays_empty() { |
| 679 |
assert!(sanitize_reminder_offsets(&[]).is_empty()); |
| 680 |
} |
| 681 |
} |
| 682 |
|
| 683 |
#[cfg(test)] |
| 684 |
mod is_template_tests { |
| 685 |
use super::EventResponse; |
| 686 |
use chrono::Utc; |
| 687 |
use goingson_core::{Event, EventId, Recurrence}; |
| 688 |
|
| 689 |
fn event(recurrence: Recurrence, is_recurring_instance: bool) -> Event { |
| 690 |
Event { |
| 691 |
id: EventId::new(), |
| 692 |
user_id: None, |
| 693 |
project_id: None, |
| 694 |
project_name: None, |
| 695 |
contact_id: None, |
| 696 |
contact_name: None, |
| 697 |
title: "E".to_string(), |
| 698 |
description: String::new(), |
| 699 |
start_time: Utc::now(), |
| 700 |
end_time: None, |
| 701 |
location: None, |
| 702 |
linked_task_id: None, |
| 703 |
recurrence, |
| 704 |
recurrence_rule: None, |
| 705 |
recurrence_parent_id: None, |
| 706 |
is_recurring_instance, |
| 707 |
block_type: None, |
| 708 |
external_source: None, |
| 709 |
external_id: None, |
| 710 |
is_read_only: false, |
| 711 |
snoozed_until: None, |
| 712 |
reminder_offsets_seconds: Vec::new(), |
| 713 |
} |
| 714 |
} |
| 715 |
|
| 716 |
#[test] |
| 717 |
fn recurring_parent_rule_is_a_template() { |
| 718 |
let r = EventResponse::from(event(Recurrence::Weekly, false)); |
| 719 |
assert!(r.is_template); |
| 720 |
} |
| 721 |
|
| 722 |
#[test] |
| 723 |
fn generated_instance_is_not_a_template() { |
| 724 |
|
| 725 |
let r = EventResponse::from(event(Recurrence::Weekly, true)); |
| 726 |
assert!(!r.is_template); |
| 727 |
} |
| 728 |
|
| 729 |
#[test] |
| 730 |
fn non_recurring_event_is_not_a_template() { |
| 731 |
let r = EventResponse::from(event(Recurrence::None, false)); |
| 732 |
assert!(!r.is_template); |
| 733 |
} |
| 734 |
} |
| 735 |
|