Skip to main content

max / goingson

3.9 KB · 84 lines History Blame Raw
1 use super::*;
2
3 /// Repository for calendar event operations.
4 ///
5 /// Events can be standalone or linked to tasks (for time-blocking).
6 ///
7 /// # Ordering Contract
8 ///
9 /// All methods returning `Vec<Event>` **MUST** return results sorted by
10 /// `start_time ASC`. This is enforced at the SQL level (`ORDER BY e.start_time ASC`)
11 /// and callers rely on this guarantee — no post-fetch sorting is needed.
12 #[async_trait]
13 pub trait EventRepository: Send + Sync {
14 /// Lists all events for a user, ordered by `start_time ASC`.
15 async fn list_all(&self, user_id: UserId) -> Result<Vec<Event>>;
16
17 /// Lists events belonging to a specific project, ordered by `start_time ASC`.
18 async fn list_by_project(&self, user_id: UserId, project_id: ProjectId) -> Result<Vec<Event>>;
19
20 /// Lists events linked to a specific contact, ordered by `start_time DESC`.
21 async fn list_by_contact(&self, user_id: UserId, contact_id: ContactId) -> Result<Vec<Event>>;
22
23 /// Retrieves an event by ID.
24 async fn get_by_id(&self, id: EventId, user_id: UserId) -> Result<Option<Event>>;
25
26 /// Creates a new event.
27 async fn create(&self, user_id: UserId, event: NewEvent) -> Result<Event>;
28
29 /// Restores an event verbatim from a backup, preserving its original ID and
30 /// all metadata the normal create path never sets — `block_type`,
31 /// `external_source`/`external_id`, `recurrence_parent_id`, `is_read_only`,
32 /// `snoozed_until`, and reminder offsets. Idempotent (`INSERT OR IGNORE`).
33 async fn restore(&self, user_id: UserId, event: &Event) -> Result<()>;
34
35 /// Updates an existing event.
36 async fn update(&self, id: EventId, user_id: UserId, event: crate::models::UpdateEvent) -> Result<Option<Event>>;
37
38 /// Deletes an event.
39 async fn delete(&self, id: EventId, user_id: UserId) -> Result<bool>;
40
41 /// Records the external source/id for an event (e.g. after an iCal import),
42 /// used to dedup on re-import.
43 async fn set_external_ref(
44 &self,
45 id: EventId,
46 user_id: UserId,
47 source: &str,
48 external_id: &str,
49 ) -> Result<()>;
50
51 /// Deletes multiple events by ID, returning the number deleted.
52 async fn delete_many(&self, ids: &[EventId], user_id: UserId) -> Result<u64>;
53
54 /// Gets events starting within the next N days, ordered by `start_time ASC`.
55 async fn get_upcoming(&self, user_id: UserId, days: i64) -> Result<Vec<Event>>;
56
57 /// Finds the event linked to a specific task (for time-blocking).
58 async fn get_by_linked_task(&self, user_id: UserId, task_id: TaskId) -> Result<Option<Event>>;
59
60 /// Deletes the event linked to a task.
61 async fn delete_by_linked_task(&self, user_id: UserId, task_id: TaskId) -> Result<bool>;
62
63 /// Lists events occurring on a specific date, ordered by `start_time ASC`.
64 async fn list_for_date(&self, user_id: UserId, date: NaiveDate) -> Result<Vec<Event>>;
65
66 /// Lists events within a date range (for weekly review), ordered by `start_time ASC`.
67 async fn list_between(&self, user_id: UserId, start: DateTime<Utc>, end: DateTime<Utc>) -> Result<Vec<Event>>;
68
69 /// Lists all recurring events (recurrence != 'None' or recurrence_rule is set).
70 async fn list_recurring(&self, user_id: UserId) -> Result<Vec<Event>>;
71
72 /// Finds an event by external source and ID (for dedup during import).
73 async fn find_by_external_id(&self, source: &str, ext_id: &str, user_id: UserId) -> Result<Option<Event>>;
74
75 /// Snoozes an event until `until`. Returns the updated event, or `None` if not found.
76 async fn snooze(&self, id: EventId, user_id: UserId, until: DateTime<Utc>) -> Result<Option<Event>>;
77
78 /// Clears any snooze on an event. Returns the updated event, or `None` if not found.
79 async fn unsnooze(&self, id: EventId, user_id: UserId) -> Result<Option<Event>>;
80
81 /// Lists currently snoozed events (snoozed_until is in the future).
82 async fn list_snoozed(&self, user_id: UserId) -> Result<Vec<Event>>;
83 }
84