| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
use chrono::{DateTime, Utc}; |
| 9 |
use serde::{Deserialize, Serialize}; |
| 10 |
use crate::constants::DAYS_THRESHOLD_SHORT_FORMAT; |
| 11 |
use crate::id_types::{EventId, UserId, ProjectId, ContactId, TaskId}; |
| 12 |
use super::shared::{BlockType, Recurrence, RecurrenceRule}; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#[derive(Debug, Clone, Serialize, Deserialize)] |
| 21 |
#[serde(rename_all = "camelCase")] |
| 22 |
pub struct Event { |
| 23 |
|
| 24 |
pub id: EventId, |
| 25 |
|
| 26 |
#[serde(skip_serializing)] |
| 27 |
pub user_id: Option<UserId>, |
| 28 |
|
| 29 |
pub project_id: Option<ProjectId>, |
| 30 |
|
| 31 |
pub project_name: Option<String>, |
| 32 |
|
| 33 |
pub contact_id: Option<ContactId>, |
| 34 |
|
| 35 |
pub contact_name: Option<String>, |
| 36 |
|
| 37 |
pub title: String, |
| 38 |
|
| 39 |
pub description: String, |
| 40 |
|
| 41 |
pub start_time: DateTime<Utc>, |
| 42 |
|
| 43 |
pub end_time: Option<DateTime<Utc>>, |
| 44 |
|
| 45 |
pub location: Option<String>, |
| 46 |
|
| 47 |
pub linked_task_id: Option<TaskId>, |
| 48 |
|
| 49 |
pub recurrence: Recurrence, |
| 50 |
|
| 51 |
pub recurrence_rule: Option<RecurrenceRule>, |
| 52 |
|
| 53 |
pub recurrence_parent_id: Option<EventId>, |
| 54 |
|
| 55 |
#[serde(default)] |
| 56 |
pub is_recurring_instance: bool, |
| 57 |
|
| 58 |
pub block_type: Option<BlockType>, |
| 59 |
|
| 60 |
pub external_source: Option<String>, |
| 61 |
|
| 62 |
pub external_id: Option<String>, |
| 63 |
|
| 64 |
pub is_read_only: bool, |
| 65 |
|
| 66 |
pub snoozed_until: Option<DateTime<Utc>>, |
| 67 |
|
| 68 |
|
| 69 |
#[serde(default)] |
| 70 |
pub reminder_offsets_seconds: Vec<i64>, |
| 71 |
} |
| 72 |
|
| 73 |
impl Event { |
| 74 |
|
| 75 |
pub fn time_formatted(&self) -> String { |
| 76 |
let start = self.start_time.format("%b %d, %H:%M").to_string(); |
| 77 |
match &self.end_time { |
| 78 |
Some(end) => format!("{} - {}", start, end.format("%H:%M")), |
| 79 |
None => start, |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
pub fn date_formatted(&self) -> String { |
| 85 |
let now = Utc::now(); |
| 86 |
let days = (self.start_time.date_naive() - now.date_naive()).num_days(); |
| 87 |
|
| 88 |
if days < 0 { |
| 89 |
"Past".to_string() |
| 90 |
} else if days == 0 { |
| 91 |
"Today".to_string() |
| 92 |
} else if days == 1 { |
| 93 |
"Tomorrow".to_string() |
| 94 |
} else if days < DAYS_THRESHOLD_SHORT_FORMAT { |
| 95 |
self.start_time.format("%A").to_string() |
| 96 |
} else { |
| 97 |
self.start_time.format("%b %d").to_string() |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
pub fn day_number(&self) -> String { |
| 103 |
self.start_time.format("%d").to_string() |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
pub fn timestamp(&self) -> i64 { |
| 108 |
self.start_time.timestamp() |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
pub fn has_location(&self) -> bool { |
| 113 |
self.location.is_some() |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
pub fn location_or_empty(&self) -> &str { |
| 118 |
self.location.as_deref().unwrap_or("") |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
pub fn has_project(&self) -> bool { |
| 123 |
self.project_name.is_some() |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
pub fn project_name_or_empty(&self) -> &str { |
| 128 |
self.project_name.as_deref().unwrap_or("") |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
pub fn has_description(&self) -> bool { |
| 133 |
!self.description.is_empty() |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
pub fn has_recurrence(&self) -> bool { |
| 138 |
self.recurrence_rule.is_some() || self.recurrence != Recurrence::None |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
pub fn effective_recurrence_rule(&self) -> Option<RecurrenceRule> { |
| 144 |
RecurrenceRule::effective(self.recurrence_rule.as_ref(), &self.recurrence) |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
pub fn is_linked_to_task(&self) -> bool { |
| 149 |
self.linked_task_id.is_some() |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
pub fn is_snoozed(&self) -> bool { |
| 154 |
self.snoozed_until.is_some_and(|t| t > Utc::now()) |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
#[derive(Debug, Clone, Serialize, Deserialize)] |
| 162 |
pub struct NewEvent { |
| 163 |
|
| 164 |
pub user_id: Option<UserId>, |
| 165 |
|
| 166 |
pub project_id: Option<ProjectId>, |
| 167 |
|
| 168 |
pub contact_id: Option<ContactId>, |
| 169 |
|
| 170 |
pub title: String, |
| 171 |
|
| 172 |
pub description: String, |
| 173 |
|
| 174 |
pub start_time: DateTime<Utc>, |
| 175 |
|
| 176 |
pub end_time: Option<DateTime<Utc>>, |
| 177 |
|
| 178 |
pub location: Option<String>, |
| 179 |
|
| 180 |
pub linked_task_id: Option<TaskId>, |
| 181 |
|
| 182 |
pub recurrence: Recurrence, |
| 183 |
|
| 184 |
pub recurrence_rule: Option<RecurrenceRule>, |
| 185 |
|
| 186 |
pub block_type: Option<BlockType>, |
| 187 |
|
| 188 |
#[serde(default)] |
| 189 |
pub reminder_offsets_seconds: Vec<i64>, |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
#[derive(Debug, Clone, Serialize, Deserialize)] |
| 194 |
pub struct UpdateEvent { |
| 195 |
|
| 196 |
pub project_id: Option<ProjectId>, |
| 197 |
|
| 198 |
pub contact_id: Option<ContactId>, |
| 199 |
|
| 200 |
pub title: String, |
| 201 |
|
| 202 |
pub description: String, |
| 203 |
|
| 204 |
pub start_time: DateTime<Utc>, |
| 205 |
|
| 206 |
pub end_time: Option<DateTime<Utc>>, |
| 207 |
|
| 208 |
pub location: Option<String>, |
| 209 |
|
| 210 |
pub linked_task_id: Option<TaskId>, |
| 211 |
|
| 212 |
pub recurrence: Recurrence, |
| 213 |
|
| 214 |
pub recurrence_rule: Option<RecurrenceRule>, |
| 215 |
|
| 216 |
pub block_type: Option<BlockType>, |
| 217 |
|
| 218 |
#[serde(default)] |
| 219 |
pub reminder_offsets_seconds: Vec<i64>, |
| 220 |
} |
| 221 |
|
| 222 |
impl NewEvent { |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
pub fn builder(title: impl Into<String>, start_time: DateTime<Utc>) -> NewEventBuilder { |
| 238 |
NewEventBuilder::new(title, start_time) |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
#[derive(Debug, Clone)] |
| 244 |
pub struct NewEventBuilder { |
| 245 |
title: String, |
| 246 |
start_time: DateTime<Utc>, |
| 247 |
user_id: Option<UserId>, |
| 248 |
project_id: Option<ProjectId>, |
| 249 |
contact_id: Option<ContactId>, |
| 250 |
description: String, |
| 251 |
end_time: Option<DateTime<Utc>>, |
| 252 |
location: Option<String>, |
| 253 |
linked_task_id: Option<TaskId>, |
| 254 |
recurrence: Recurrence, |
| 255 |
recurrence_rule: Option<RecurrenceRule>, |
| 256 |
block_type: Option<BlockType>, |
| 257 |
} |
| 258 |
|
| 259 |
impl NewEventBuilder { |
| 260 |
|
| 261 |
pub fn new(title: impl Into<String>, start_time: DateTime<Utc>) -> Self { |
| 262 |
Self { |
| 263 |
title: title.into(), |
| 264 |
start_time, |
| 265 |
user_id: None, |
| 266 |
project_id: None, |
| 267 |
contact_id: None, |
| 268 |
description: String::new(), |
| 269 |
end_time: None, |
| 270 |
location: None, |
| 271 |
linked_task_id: None, |
| 272 |
recurrence: Recurrence::default(), |
| 273 |
recurrence_rule: None, |
| 274 |
block_type: None, |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
pub fn user_id(mut self, user_id: UserId) -> Self { |
| 280 |
self.user_id = Some(user_id); |
| 281 |
self |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
pub fn project_id(mut self, project_id: ProjectId) -> Self { |
| 286 |
self.project_id = Some(project_id); |
| 287 |
self |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
pub fn contact_id(mut self, contact_id: ContactId) -> Self { |
| 292 |
self.contact_id = Some(contact_id); |
| 293 |
self |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
pub fn description(mut self, description: impl Into<String>) -> Self { |
| 298 |
self.description = description.into(); |
| 299 |
self |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
pub fn end_time(mut self, end_time: DateTime<Utc>) -> Self { |
| 304 |
self.end_time = Some(end_time); |
| 305 |
self |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
pub fn location(mut self, location: impl Into<String>) -> Self { |
| 310 |
self.location = Some(location.into()); |
| 311 |
self |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
pub fn linked_task_id(mut self, task_id: TaskId) -> Self { |
| 316 |
self.linked_task_id = Some(task_id); |
| 317 |
self |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
pub fn recurrence(mut self, recurrence: Recurrence) -> Self { |
| 322 |
self.recurrence = recurrence; |
| 323 |
self |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
pub fn recurrence_rule(mut self, rule: RecurrenceRule) -> Self { |
| 328 |
self.recurrence_rule = Some(rule); |
| 329 |
self |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
pub fn block_type(mut self, block_type: BlockType) -> Self { |
| 334 |
self.block_type = Some(block_type); |
| 335 |
self |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
pub fn build(self) -> NewEvent { |
| 340 |
NewEvent { |
| 341 |
user_id: self.user_id, |
| 342 |
project_id: self.project_id, |
| 343 |
contact_id: self.contact_id, |
| 344 |
title: self.title, |
| 345 |
description: self.description, |
| 346 |
start_time: self.start_time, |
| 347 |
end_time: self.end_time, |
| 348 |
location: self.location, |
| 349 |
linked_task_id: self.linked_task_id, |
| 350 |
recurrence: self.recurrence, |
| 351 |
recurrence_rule: self.recurrence_rule, |
| 352 |
block_type: self.block_type, |
| 353 |
reminder_offsets_seconds: Vec::new(), |
| 354 |
} |
| 355 |
} |
| 356 |
} |
| 357 |
|