Skip to main content

aven_core/
change_log.rs

1use anyhow::Result;
2use serde_json::Value;
3use sqlx::SqliteConnection;
4
5use crate::db::insert_change;
6use crate::workspaces::Workspace;
7
8pub mod op_type {
9    macro_rules! operations {
10        ($($name:ident = $value:literal;)*) => {
11            $(pub const $name: &str = $value;)*
12
13            /// Every registered operation, in released vocabulary order.
14            pub const ALL: &[&str] = &[$($name),*];
15        };
16    }
17
18    operations! {
19        CREATE_TASK = "create_task";
20        MOVE_TASKS = "move_tasks";
21        SET_FIELD = "set_field";
22        RESOLVE_FIELD = "resolve_field";
23        LABEL_ADD = "label_add";
24        LABEL_REMOVE = "label_remove";
25        NOTE_ADD = "note_add";
26        NOTE_EDIT = "note_edit";
27        NOTE_DELETE = "note_delete";
28        DEPENDENCY_ADD = "dependency_add";
29        DEPENDENCY_REMOVE = "dependency_remove";
30        RELATED_ADD = "related_add";
31        RELATED_REMOVE = "related_remove";
32        EPIC_LINK_ADD = "epic_link_add";
33        EPIC_LINK_REMOVE = "epic_link_remove";
34        ATTACHMENT_ADD = "attachment_add";
35        ATTACHMENT_DELETE = "attachment_delete";
36        CREATE_PROJECT = "create_project";
37        SET_PROJECT_METADATA = "set_project_metadata";
38        PROJECT_DELETE = "project_delete";
39        CREATE_LABEL = "create_label";
40        SET_LABEL_NAME = "set_label_name";
41        LABEL_DELETE = "label_delete";
42        LABEL_RESTORE = "label_restore";
43        CREATE_WORKSPACE = "create_workspace";
44        SET_WORKSPACE_FIELD = "set_workspace_field";
45        CREATE_RECURRENCE_SERIES = "create_recurrence_series";
46        UPDATE_RECURRENCE_TEMPLATE = "update_recurrence_template";
47        PROJECT_RECURRENCE_OCCURRENCE = "project_recurrence_occurrence";
48        RESOLVE_RECURRENCE_OCCURRENCE = "resolve_recurrence_occurrence";
49        SET_RECURRENCE_STATE = "set_recurrence_state";
50        CREATE_METADATA_FIELD = "create_metadata_field";
51        SET_METADATA_FIELD = "set_metadata_field";
52        SET_TASK_METADATA = "set_task_metadata";
53        REMOVE_TASK_METADATA = "remove_task_metadata";
54        SET_RECURRENCE_METADATA = "set_recurrence_metadata";
55        REMOVE_RECURRENCE_METADATA = "remove_recurrence_metadata";
56        OPEN_RECURRENCE_PAUSE = "open_recurrence_pause";
57        CLOSE_RECURRENCE_PAUSE = "close_recurrence_pause";
58        STOP_RECURRENCE_SERIES = "stop_recurrence_series";
59        PUBLISH_DEVICE_LABEL = "publish_device_label";
60    }
61}
62
63pub enum ChangeEntity {
64    Task,
65    TaskMove,
66    Project,
67    Label,
68    MetadataField,
69    #[allow(dead_code)]
70    Workspace,
71    RecurrenceSeries,
72}
73
74impl ChangeEntity {
75    pub const fn as_str(&self) -> &'static str {
76        match self {
77            Self::Task => "task",
78            Self::TaskMove => "task_move",
79            Self::Project => "project",
80            Self::Label => "label",
81            Self::MetadataField => "metadata_field",
82            Self::Workspace => "workspace",
83            Self::RecurrenceSeries => "recurrence_series",
84        }
85    }
86}
87
88/// Builder for change payload JSON produced by operations.
89///
90/// Use `.workspace(&workspace)` to seed workspace_id and workspace_key,
91/// then chain `.set(key, value)` for each payload field.
92///
93/// Example:
94/// ```ignore
95/// ChangePayload::workspace(&workspace)
96///     .set("title", draft.title)
97///     .set("project_id", project.id)
98///     .into_value()
99/// ```
100pub struct ChangePayload {
101    map: serde_json::Map<String, serde_json::Value>,
102}
103
104impl ChangePayload {
105    pub fn workspace(workspace: &Workspace) -> Self {
106        let mut map = serde_json::Map::new();
107        map.insert(
108            "workspace_id".to_string(),
109            Value::String(workspace.id.to_string()),
110        );
111        map.insert(
112            "workspace_key".to_string(),
113            Value::String(workspace.key.clone()),
114        );
115        Self { map }
116    }
117
118    pub fn set(mut self, key: &str, value: impl serde::Serialize) -> Self {
119        self.map.insert(
120            key.to_string(),
121            serde_json::to_value(value).expect("change payload value serialization"),
122        );
123        self
124    }
125
126    pub fn into_value(self) -> Value {
127        Value::Object(self.map)
128    }
129}
130
131/// Insert a change-log row using a `ChangeEntity` and pre-built `ChangePayload`.
132///
133/// This wrapper always passes `None` for `base_version`. Task field-level
134/// operations that need version tracking for conflict detection use
135/// `insert_change` directly through `mutation.rs`.
136pub(crate) async fn append_change(
137    conn: &mut SqliteConnection,
138    entity: ChangeEntity,
139    entity_id: &str,
140    field: Option<&str>,
141    op_type: &'static str,
142    payload: ChangePayload,
143) -> Result<String> {
144    insert_change(
145        conn,
146        entity.as_str(),
147        entity_id,
148        field,
149        op_type,
150        payload.into_value(),
151        None,
152    )
153    .await
154}