kanban-domain 0.7.0

Domain models and business logic for the kanban project management tool
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
//! Cascade primitives.
//!
//! These commands are deliberately atomic and bypass the per-entity validation
//! that the standalone delete commands (e.g. [`DeleteColumn`](super::DeleteColumn))
//! enforce. They are intended to be composed by the cascade helpers in
//! [`super::cascade`] and executed as a single `KanbanContext::execute(...)`
//! batch so the whole cascade is one undo unit with snapshot/rollback.
//!
//! **Do not construct these commands directly outside the cascade module.** The
//! canonical entry points are the helpers in [`super::cascade`] which encode the
//! ordering invariants (graph edges → cards → archived → columns → sprints →
//! board) that make the bypassed validations safe.

use super::dependency_commands::edges_to_undo_commands;
use super::{BoardCommand, Command, CommandContext, ImportEntities};
use crate::data_store::DataStore;
use crate::{KanbanError, KanbanResult};
use serde::{Deserialize, Serialize};
use uuid::Uuid;

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "action", rename_all = "snake_case")]
pub enum CascadeCommand {
    DeleteCardEdges(DeleteCardEdges),
    DeleteCardsByColumns(DeleteCardsByColumns),
    DeleteArchivedCardsByColumns(DeleteArchivedCardsByColumns),
    DeleteColumnsByBoard(DeleteColumnsByBoard),
    DeleteSprintsByBoard(DeleteSprintsByBoard),
    /// Internal: set `sprint_id` on a list of archived cards. Used by
    /// `DeleteSprint`'s inverse to restore the binding that
    /// `clear_sprint_from_archived_cards` cleared. Not a user-facing
    /// command — accessed only via the inverse-capture path.
    SetArchivedCardsSprint(SetArchivedCardsSprint),
}

impl CascadeCommand {
    pub fn execute(&self, context: &CommandContext) -> KanbanResult<()> {
        match self {
            CascadeCommand::DeleteCardEdges(c) => c.execute(context),
            CascadeCommand::DeleteCardsByColumns(c) => c.execute(context),
            CascadeCommand::DeleteArchivedCardsByColumns(c) => c.execute(context),
            CascadeCommand::DeleteColumnsByBoard(c) => c.execute(context),
            CascadeCommand::DeleteSprintsByBoard(c) => c.execute(context),
            CascadeCommand::SetArchivedCardsSprint(c) => c.execute(context),
        }
    }

    pub fn description(&self) -> String {
        match self {
            CascadeCommand::DeleteCardEdges(c) => c.description(),
            CascadeCommand::DeleteCardsByColumns(c) => c.description(),
            CascadeCommand::DeleteArchivedCardsByColumns(c) => c.description(),
            CascadeCommand::DeleteColumnsByBoard(c) => c.description(),
            CascadeCommand::DeleteSprintsByBoard(c) => c.description(),
            CascadeCommand::SetArchivedCardsSprint(c) => c.description(),
        }
    }

    pub fn capture_inverse(&self, store: &dyn DataStore) -> KanbanResult<Vec<Command>> {
        match self {
            CascadeCommand::DeleteCardEdges(c) => c.capture_inverse(store),
            CascadeCommand::DeleteCardsByColumns(c) => c.capture_inverse(store),
            CascadeCommand::DeleteArchivedCardsByColumns(c) => c.capture_inverse(store),
            CascadeCommand::DeleteColumnsByBoard(c) => c.capture_inverse(store),
            CascadeCommand::DeleteSprintsByBoard(c) => c.capture_inverse(store),
            CascadeCommand::SetArchivedCardsSprint(c) => c.capture_inverse(store),
        }
    }
}

/// Remove all dependency-graph edges for a batch of card IDs.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DeleteCardEdges {
    pub ids: Vec<Uuid>,
}

impl DeleteCardEdges {
    pub fn execute(&self, context: &CommandContext) -> KanbanResult<()> {
        let ids = self.ids.clone();
        context.store.modify_graph(Box::new(move |graph| {
            for id in &ids {
                graph.remove_node(*id);
            }
            Ok(())
        }))
    }

    pub fn description(&self) -> String {
        format!("Remove {} card(s) from dependency graph", self.ids.len())
    }

    /// Inverse: capture every active edge involving any id in self.ids and
    /// emit the matching Add* / SetParent command for each.
    pub fn capture_inverse(&self, store: &dyn DataStore) -> KanbanResult<Vec<Command>> {
        let id_set: std::collections::HashSet<_> = self.ids.iter().copied().collect();
        let graph = store.get_graph()?;
        Ok(edges_to_undo_commands(&graph, |s, t| {
            id_set.contains(&s) || id_set.contains(&t)
        }))
    }
}

/// Delete all active cards belonging to the given columns.
///
/// Bypasses per-card validation. The dependency graph must be cleaned up
/// separately (see [`DeleteCardEdges`]).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DeleteCardsByColumns {
    pub column_ids: Vec<Uuid>,
}

impl DeleteCardsByColumns {
    pub fn execute(&self, context: &CommandContext) -> KanbanResult<()> {
        context.store.delete_cards_by_columns(&self.column_ids)
    }

    pub fn description(&self) -> String {
        format!("Delete all cards in {} column(s)", self.column_ids.len())
    }

    /// Inverse: capture every live card in the target columns and emit an
    /// `ImportEntities` that re-inserts them (the cascade's outer
    /// transaction already removed them by the time undo runs).
    pub fn capture_inverse(&self, store: &dyn DataStore) -> KanbanResult<Vec<Command>> {
        let cards = store.list_cards_by_columns(&self.column_ids)?;
        if cards.is_empty() {
            return Ok(Vec::new());
        }
        Ok(vec![Command::Board(BoardCommand::Import(ImportEntities {
            cards,
            ..Default::default()
        }))])
    }
}

/// Delete all archived cards belonging to the given columns.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DeleteArchivedCardsByColumns {
    pub column_ids: Vec<Uuid>,
}

impl DeleteArchivedCardsByColumns {
    pub fn execute(&self, context: &CommandContext) -> KanbanResult<()> {
        let archived = context
            .store
            .list_archived_cards_by_columns(&self.column_ids)?;
        for ac in archived {
            context.store.delete_archived_card(ac.card.id)?;
        }
        Ok(())
    }

    pub fn description(&self) -> String {
        format!(
            "Delete archived cards in {} column(s)",
            self.column_ids.len()
        )
    }

    pub fn capture_inverse(&self, store: &dyn DataStore) -> KanbanResult<Vec<Command>> {
        let archived_cards = store.list_archived_cards_by_columns(&self.column_ids)?;
        if archived_cards.is_empty() {
            return Ok(Vec::new());
        }
        Ok(vec![Command::Board(BoardCommand::Import(ImportEntities {
            archived_cards,
            ..Default::default()
        }))])
    }
}

/// Delete all columns belonging to the given board.
///
/// Bypasses the emptiness checks in [`super::DeleteColumn`]. The caller is
/// responsible for removing cards beforehand.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DeleteColumnsByBoard {
    pub board_id: Uuid,
}

impl DeleteColumnsByBoard {
    pub fn execute(&self, context: &CommandContext) -> KanbanResult<()> {
        context.store.delete_columns_by_board(self.board_id)
    }

    pub fn description(&self) -> String {
        format!("Delete all columns in board {}", self.board_id)
    }

    pub fn capture_inverse(&self, store: &dyn DataStore) -> KanbanResult<Vec<Command>> {
        let columns = store.list_columns_by_board(self.board_id)?;
        if columns.is_empty() {
            return Ok(Vec::new());
        }
        Ok(vec![Command::Board(BoardCommand::Import(ImportEntities {
            columns,
            ..Default::default()
        }))])
    }
}

/// Delete all sprints belonging to the given board.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DeleteSprintsByBoard {
    pub board_id: Uuid,
}

impl DeleteSprintsByBoard {
    pub fn execute(&self, context: &CommandContext) -> KanbanResult<()> {
        context.store.delete_sprints_by_board(self.board_id)
    }

    pub fn description(&self) -> String {
        format!("Delete all sprints in board {}", self.board_id)
    }

    pub fn capture_inverse(&self, store: &dyn DataStore) -> KanbanResult<Vec<Command>> {
        let sprints = store.list_sprints_by_board(self.board_id)?;
        if sprints.is_empty() {
            return Ok(Vec::new());
        }
        Ok(vec![Command::Board(BoardCommand::Import(ImportEntities {
            sprints,
            ..Default::default()
        }))])
    }
}

/// Set `sprint_id` on every archived card in `archived_card_ids`.
/// Internal — only used by KAN-191 inverse-command capture (DeleteSprint
/// undo) to restore the binding that `clear_sprint_from_archived_cards`
/// cleared during forward execution.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SetArchivedCardsSprint {
    pub archived_card_ids: Vec<Uuid>,
    pub sprint_id: Uuid,
}

impl SetArchivedCardsSprint {
    pub fn execute(&self, context: &CommandContext) -> KanbanResult<()> {
        for id in &self.archived_card_ids {
            if let Some(mut ac) = context.store.get_archived_card(*id)? {
                ac.card.sprint_id = Some(self.sprint_id);
                context.store.delete_archived_card(ac.card.id)?;
                context.store.insert_archived_card(ac)?;
            }
        }
        Ok(())
    }

    pub fn description(&self) -> String {
        format!(
            "Re-attach sprint {} to {} archived card(s)",
            self.sprint_id,
            self.archived_card_ids.len()
        )
    }

    /// Synthetic-only. Rejects top-level execute so misuse fails
    /// loudly instead of producing a silently-broken undo entry.
    pub fn capture_inverse(&self, _store: &dyn DataStore) -> KanbanResult<Vec<Command>> {
        Err(KanbanError::Internal(format!(
            "SetArchivedCardsSprint is a synthetic command — it must only \
             appear inside an inverse batch (DeleteSprint undo), never as a \
             top-level forward command. Got {} card id(s) bound to sprint {}.",
            self.archived_card_ids.len(),
            self.sprint_id
        )))
    }
}

#[cfg(test)]
mod tests {
    use super::super::test_helpers::TestContext;
    use super::*;
    use crate::DataStore;

    #[test]
    fn test_delete_card_edges_removes_all_edges_for_given_ids() {
        let tc = TestContext::new();
        let card_a = Uuid::new_v4();
        let card_b = Uuid::new_v4();
        let card_c = Uuid::new_v4();

        {
            let mut graph = tc.store.get_graph().unwrap();
            graph.set_block(card_a, card_b).unwrap();
            graph.set_block(card_b, card_c).unwrap();
            tc.store.set_graph(graph).unwrap();
        }
        assert_eq!(tc.store.get_graph().unwrap().len(), 2);

        let context = tc.as_command_context();
        let cmd = DeleteCardEdges {
            ids: vec![card_a, card_b],
        };
        cmd.execute(&context).unwrap();

        let graph = tc.store.get_graph().unwrap();
        assert_eq!(
            graph.len(),
            0,
            "edges incident to card_a or card_b should be removed"
        );
    }

    #[test]
    fn test_delete_card_edges_with_empty_input_is_noop() {
        let tc = TestContext::new();
        let card_a = Uuid::new_v4();
        let card_b = Uuid::new_v4();
        {
            let mut graph = tc.store.get_graph().unwrap();
            graph.set_block(card_a, card_b).unwrap();
            tc.store.set_graph(graph).unwrap();
        }

        let context = tc.as_command_context();
        let cmd = DeleteCardEdges { ids: vec![] };
        cmd.execute(&context).unwrap();

        assert_eq!(tc.store.get_graph().unwrap().len(), 1);
    }

    #[test]
    fn test_delete_cards_by_columns_removes_only_cards_in_given_columns() {
        let tc = TestContext::new();
        let mut board = crate::Board::new("B", Some("TST"));
        let col1 = crate::Column::new(board.id, "C1", 0);
        let col2 = crate::Column::new(board.id, "C2", 1);
        let col3 = crate::Column::new(board.id, "C3", 2);
        let card1 = crate::Card::new(&mut board, col1.id, "1", 0);
        let card2 = crate::Card::new(&mut board, col2.id, "2", 0);
        let card3 = crate::Card::new(&mut board, col3.id, "3", 0);
        let card3_id = card3.id;
        let col3_id = col3.id;
        tc.store.upsert_board(board).unwrap();
        tc.store.upsert_column(col1.clone()).unwrap();
        tc.store.upsert_column(col2.clone()).unwrap();
        tc.store.upsert_column(col3).unwrap();
        tc.store.upsert_card(card1).unwrap();
        tc.store.upsert_card(card2).unwrap();
        tc.store.upsert_card(card3).unwrap();

        let context = tc.as_command_context();
        let cmd = DeleteCardsByColumns {
            column_ids: vec![col1.id, col2.id],
        };
        cmd.execute(&context).unwrap();

        let remaining = tc.store.list_all_cards().unwrap();
        assert_eq!(remaining.len(), 1);
        assert_eq!(remaining[0].id, card3_id);
        assert_eq!(remaining[0].column_id, col3_id);
    }

    #[test]
    fn test_delete_archived_cards_by_columns_removes_only_archived_in_given_columns() {
        let tc = TestContext::new();
        let mut board = crate::Board::new("B", Some("TST"));
        let col1 = crate::Column::new(board.id, "C1", 0);
        let col2 = crate::Column::new(board.id, "C2", 1);
        let col1_id = col1.id;
        let col2_id = col2.id;
        let card1 = crate::Card::new(&mut board, col1_id, "1", 0);
        let card2 = crate::Card::new(&mut board, col2_id, "2", 0);
        let arch1 = crate::ArchivedCard::new(card1, col1_id, 0);
        let arch2 = crate::ArchivedCard::new(card2, col2_id, 0);
        let arch2_card_id = arch2.card.id;
        tc.store.upsert_board(board).unwrap();
        tc.store.upsert_column(col1).unwrap();
        tc.store.upsert_column(col2).unwrap();
        tc.store.insert_archived_card(arch1).unwrap();
        tc.store.insert_archived_card(arch2).unwrap();

        let context = tc.as_command_context();
        let cmd = DeleteArchivedCardsByColumns {
            column_ids: vec![col1_id],
        };
        cmd.execute(&context).unwrap();

        let remaining = tc.store.list_archived_cards().unwrap();
        assert_eq!(remaining.len(), 1);
        assert_eq!(remaining[0].card.id, arch2_card_id);
    }

    #[test]
    fn test_delete_columns_by_board_removes_all_columns_of_board() {
        let tc = TestContext::new();
        let board = crate::Board::new("B", None::<String>);
        let board_id = board.id;
        let other_board = crate::Board::new("Other", None::<String>);
        let other_board_id = other_board.id;
        let col1 = crate::Column::new(board_id, "C1", 0);
        let col2 = crate::Column::new(board_id, "C2", 1);
        let other_col = crate::Column::new(other_board_id, "OC", 0);
        tc.store.upsert_board(board).unwrap();
        tc.store.upsert_board(other_board).unwrap();
        tc.store.upsert_column(col1).unwrap();
        tc.store.upsert_column(col2).unwrap();
        tc.store.upsert_column(other_col).unwrap();

        let context = tc.as_command_context();
        let cmd = DeleteColumnsByBoard { board_id };
        cmd.execute(&context).unwrap();

        let remaining = tc.store.list_all_columns().unwrap();
        assert_eq!(remaining.len(), 1);
        assert_eq!(remaining[0].board_id, other_board_id);
    }

    #[test]
    fn test_delete_sprints_by_board_removes_all_sprints_of_board() {
        let tc = TestContext::new();
        let board = crate::Board::new("B", None::<String>);
        let board_id = board.id;
        let other_board = crate::Board::new("Other", None::<String>);
        let other_board_id = other_board.id;
        let sprint1 = crate::Sprint::new(board_id, 1, None, None::<String>);
        let sprint2 = crate::Sprint::new(board_id, 2, None, None::<String>);
        let other_sprint = crate::Sprint::new(other_board_id, 1, None, None::<String>);
        tc.store.upsert_board(board).unwrap();
        tc.store.upsert_board(other_board).unwrap();
        tc.store.upsert_sprint(sprint1).unwrap();
        tc.store.upsert_sprint(sprint2).unwrap();
        tc.store.upsert_sprint(other_sprint).unwrap();

        let context = tc.as_command_context();
        let cmd = DeleteSprintsByBoard { board_id };
        cmd.execute(&context).unwrap();

        let remaining = tc.store.list_all_sprints().unwrap();
        assert_eq!(remaining.len(), 1);
        assert_eq!(remaining[0].board_id, other_board_id);
    }
}