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
437
438
439
440
441
442
use crate::query::filter_sort::{filter_and_sort_cards, ArchivedCardListFilter, CardListFilter};
use crate::KanbanResult;
use crate::{
    AmbiguousMatch, ArchivedCard, BatchResolutionCause, BatchResolutionFailure, Board, BoardUpdate,
    Card, CardSummary, CardUpdate, Column, ColumnUpdate, CreateCardOptions, KanbanError, Sprint,
    SprintUpdate,
};
use uuid::Uuid;

/// Trait ensuring TUI and CLI implement the same operations.
/// Adding a method here forces both implementations to add it.
pub trait KanbanOperations {
    // Board operations
    fn create_board(&mut self, name: String, card_prefix: Option<String>) -> KanbanResult<Board>;
    fn list_boards(&self) -> KanbanResult<Vec<Board>>;
    fn get_board(&self, id: Uuid) -> KanbanResult<Option<Board>>;
    fn update_board(&mut self, id: Uuid, updates: BoardUpdate) -> KanbanResult<Board>;
    fn delete_board(&mut self, id: Uuid) -> KanbanResult<()>;

    // Column operations
    fn create_column(
        &mut self,
        board_id: Uuid,
        name: String,
        position: Option<i32>,
    ) -> KanbanResult<Column>;
    fn list_columns(&self, board_id: Uuid) -> KanbanResult<Vec<Column>>;
    fn get_column(&self, id: Uuid) -> KanbanResult<Option<Column>>;
    fn update_column(&mut self, id: Uuid, updates: ColumnUpdate) -> KanbanResult<Column>;
    fn delete_column(&mut self, id: Uuid) -> KanbanResult<()>;
    fn reorder_column(&mut self, id: Uuid, new_position: i32) -> KanbanResult<Column>;

    // Card operations
    fn create_card(
        &mut self,
        board_id: Uuid,
        column_id: Uuid,
        title: String,
        options: CreateCardOptions,
    ) -> KanbanResult<Card>;
    fn list_cards(&self, filter: CardListFilter) -> KanbanResult<Vec<CardSummary>>;
    fn get_card(&self, id: Uuid) -> KanbanResult<Option<Card>>;
    fn find_cards_by_identifier(&self, identifier: &str) -> KanbanResult<Vec<Card>>;
    /// Single-query snapshot of every card across all boards. Used by
    /// resolvers and batch operations that need to scan once and reason
    /// in memory. Implementors must back this with a single backend
    /// query — do not compose from `list_cards_by_column`.
    fn list_all_cards(&self) -> KanbanResult<Vec<Card>>;
    /// Single-query snapshot of every column across all boards.
    fn list_all_columns(&self) -> KanbanResult<Vec<Column>>;
    /// Single-query snapshot of every sprint across all boards.
    fn list_all_sprints(&self) -> KanbanResult<Vec<Sprint>>;
    fn update_card(&mut self, id: Uuid, updates: CardUpdate) -> KanbanResult<Card>;
    fn move_card(&mut self, id: Uuid, column_id: Uuid, position: Option<i32>)
        -> KanbanResult<Card>;
    fn archive_card(&mut self, id: Uuid) -> KanbanResult<()>;
    fn restore_card(&mut self, id: Uuid, column_id: Option<Uuid>) -> KanbanResult<Card>;
    fn delete_card(&mut self, id: Uuid) -> KanbanResult<()>;
    fn list_archived_cards(&self) -> KanbanResult<Vec<ArchivedCard>>;

    fn list_archived_cards_sorted(
        &self,
        filter: ArchivedCardListFilter,
    ) -> KanbanResult<Vec<ArchivedCard>> {
        let cards = self.list_archived_cards()?;
        let board = match filter.board_id {
            Some(bid) => self.get_board(bid)?,
            None => None,
        };
        let columns = match filter.board_id {
            Some(bid) => self.list_columns(bid)?,
            None => Vec::new(),
        };
        let card_filter = CardListFilter {
            board_id: filter.board_id,
            sort: filter.sort,
            sort_order: filter.sort_order,
            ..Default::default()
        };
        Ok(filter_and_sort_cards(
            &cards,
            &columns,
            &[],
            board.as_ref(),
            &card_filter,
        ))
    }

    // Card sprint operations
    fn assign_card_to_sprint(&mut self, card_id: Uuid, sprint_id: Uuid) -> KanbanResult<Card>;
    fn unassign_card_from_sprint(&mut self, card_id: Uuid) -> KanbanResult<Card>;

    // Card utilities
    fn get_card_branch_name(&self, id: Uuid) -> KanbanResult<String>;
    fn get_card_git_checkout(&self, id: Uuid) -> KanbanResult<String>;

    // Multi-card operations
    fn archive_cards(&mut self, ids: Vec<Uuid>) -> KanbanResult<usize>;
    fn move_cards(&mut self, ids: Vec<Uuid>, column_id: Uuid) -> KanbanResult<usize>;
    /// Apply per-card updates as a single undo unit. For each entry, the service
    /// layer auto-syncs the status ↔ completion-column invariant:
    /// - `status` set + `column_id` unset → chain a move to the completion column
    /// - `column_id` set + `status` unset → chain a status flip when the move crosses
    ///   the completion-column boundary
    /// - both set → caller intent wins, no chaining
    fn update_cards(&mut self, updates: Vec<(Uuid, CardUpdate)>) -> KanbanResult<usize>;
    fn assign_cards_to_sprint(&mut self, ids: Vec<Uuid>, sprint_id: Uuid) -> KanbanResult<usize>;
    fn carry_over_sprint_cards(
        &mut self,
        from_sprint_id: Uuid,
        to_sprint_id: Uuid,
    ) -> KanbanResult<usize>;

    // Sprint operations
    fn create_sprint(
        &mut self,
        board_id: Uuid,
        prefix: Option<String>,
        name: Option<String>,
    ) -> KanbanResult<Sprint>;
    fn list_sprints(&self, board_id: Uuid) -> KanbanResult<Vec<Sprint>>;
    fn get_sprint(&self, id: Uuid) -> KanbanResult<Option<Sprint>>;
    fn update_sprint(&mut self, id: Uuid, updates: SprintUpdate) -> KanbanResult<Sprint>;
    fn activate_sprint(&mut self, id: Uuid, duration_days: Option<i32>) -> KanbanResult<Sprint>;
    fn complete_sprint(&mut self, id: Uuid) -> KanbanResult<Sprint>;
    fn cancel_sprint(&mut self, id: Uuid) -> KanbanResult<Sprint>;
    fn delete_sprint(&mut self, id: Uuid) -> KanbanResult<()>;

    // Import/Export
    fn export_board(&self, board_id: Option<Uuid>) -> KanbanResult<String>;
    fn import_board(&mut self, data: &str) -> KanbanResult<Board>;

    // ---------- Name/UUID resolvers (shared by CLI, MCP, anything else) ----------
    //
    // Each resolver accepts a raw string that may be a UUID, an entity name, or
    // (for sprints) a sprint number. The UUID fast path returns immediately;
    // otherwise the resolver returns `DomainError::NotFoundByName` (carrying the
    // available alternatives) or `DomainError::Ambiguous` (carrying the matches).
    // CLI/MCP layers just stringify the error; the human-friendly message lives
    // in `DomainError`'s `Display` impl.
    //
    // Each resolver call takes one fresh snapshot of the relevant slice. No
    // caching across calls; the snapshot lives only for the duration of the
    // call. Batch resolvers (`resolve_card_ids`, `require_same_board`) take a
    // single snapshot for the whole batch — internally consistent by definition.

    fn resolve_board_id(&self, raw: &str) -> KanbanResult<Uuid> {
        if let Ok(uuid) = Uuid::parse_str(raw) {
            return Ok(uuid);
        }
        let boards = self.list_boards()?;
        let matches = crate::search::find_boards_by_name(raw, &boards);
        match matches.as_slice() {
            [] => Err(KanbanError::not_found_by_name(
                "Board",
                raw,
                boards.iter().map(|b| b.name.clone()).collect(),
            )),
            [b] => Ok(b.id),
            many => Err(KanbanError::ambiguous(
                "Board",
                raw,
                many.iter()
                    .map(|b| AmbiguousMatch {
                        label: format!("'{}'", b.name),
                        id: b.id,
                    })
                    .collect(),
            )),
        }
    }

    fn resolve_column_id(&self, raw: &str, board_id: Uuid) -> KanbanResult<Uuid> {
        if let Ok(uuid) = Uuid::parse_str(raw) {
            return Ok(uuid);
        }
        let columns = self.list_columns(board_id)?;
        let matches = crate::search::find_columns_by_name(raw, &columns);
        match matches.as_slice() {
            [] => Err(KanbanError::not_found_by_name(
                "Column",
                raw,
                columns.iter().map(|c| c.name.clone()).collect(),
            )),
            [c] => Ok(c.id),
            many => Err(KanbanError::ambiguous(
                "Column",
                raw,
                many.iter()
                    .map(|c| AmbiguousMatch {
                        label: format!("'{}'", c.name),
                        id: c.id,
                    })
                    .collect(),
            )),
        }
    }

    fn resolve_column_id_global(&self, raw: &str) -> KanbanResult<Uuid> {
        if let Ok(uuid) = Uuid::parse_str(raw) {
            return Ok(uuid);
        }
        // Single snapshot — no N+1.
        let all_columns = self.list_all_columns()?;
        let matches = crate::search::find_columns_by_name(raw, &all_columns);
        match matches.as_slice() {
            [] => Err(KanbanError::not_found_by_name(
                "Column",
                raw,
                all_columns.iter().map(|c| c.name.clone()).collect(),
            )),
            [c] => Ok(c.id),
            many => {
                // Only need board names for the ambiguity message — one extra query.
                let boards = self.list_boards()?;
                let matches: Vec<AmbiguousMatch> = many
                    .iter()
                    .map(|c| {
                        let board_name = boards
                            .iter()
                            .find(|b| b.id == c.board_id)
                            .map(|b| b.name.as_str())
                            .unwrap_or("(unknown)");
                        AmbiguousMatch {
                            label: format!("on board '{}'", board_name),
                            id: c.id,
                        }
                    })
                    .collect();
                Err(KanbanError::ambiguous("Column", raw, matches))
            }
        }
    }

    fn resolve_sprint_id(&self, raw: &str, board_id: Uuid) -> KanbanResult<Uuid> {
        if let Ok(uuid) = Uuid::parse_str(raw) {
            return Ok(uuid);
        }
        let sprints = self.list_sprints(board_id)?;
        let board = self
            .get_board(board_id)?
            .ok_or_else(|| KanbanError::not_found("Board", board_id))?;
        let matches = crate::search::find_sprints_by_query_on_board(raw, &sprints, &board);
        match matches.as_slice() {
            [] => {
                let available = sprints
                    .iter()
                    .map(|s| {
                        let label = s.get_name(&board).unwrap_or("(unnamed)");
                        format!("#{} {}", s.sprint_number, label)
                    })
                    .collect();
                Err(KanbanError::not_found_by_name("Sprint", raw, available))
            }
            [s] => Ok(s.id),
            many => Err(KanbanError::ambiguous(
                "Sprint",
                raw,
                many.iter()
                    .map(|s| {
                        let name = s.get_name(&board).unwrap_or("(unnamed)");
                        AmbiguousMatch {
                            label: format!("#{} '{}'", s.sprint_number, name),
                            id: s.id,
                        }
                    })
                    .collect(),
            )),
        }
    }

    fn resolve_sprint_id_global(&self, raw: &str) -> KanbanResult<Uuid> {
        if let Ok(uuid) = Uuid::parse_str(raw) {
            return Ok(uuid);
        }
        // Single snapshot — no N+1.
        let all_sprints = self.list_all_sprints()?;
        let boards = self.list_boards()?;
        let matches = crate::search::find_sprints_by_query_global(raw, &all_sprints, &boards);
        match matches.as_slice() {
            [] => {
                let available = all_sprints
                    .iter()
                    .map(|s| {
                        let label = boards
                            .iter()
                            .find(|b| b.id == s.board_id)
                            .and_then(|b| s.get_name(b))
                            .unwrap_or("(unnamed)");
                        format!("#{} {}", s.sprint_number, label)
                    })
                    .collect();
                Err(KanbanError::not_found_by_name("Sprint", raw, available))
            }
            [s] => Ok(s.id),
            many => {
                let matches: Vec<AmbiguousMatch> = many
                    .iter()
                    .map(|s| {
                        let board = boards.iter().find(|b| b.id == s.board_id);
                        let board_name = board.map(|b| b.name.as_str()).unwrap_or("(unknown)");
                        let sprint_name = board.and_then(|b| s.get_name(b)).unwrap_or("(unnamed)");
                        AmbiguousMatch {
                            label: format!(
                                "#{} '{}' on board '{}'",
                                s.sprint_number, sprint_name, board_name
                            ),
                            id: s.id,
                        }
                    })
                    .collect();
                Err(KanbanError::ambiguous("Sprint", raw, matches))
            }
        }
    }

    fn resolve_card_id(&self, raw: &str) -> KanbanResult<Uuid> {
        if let Ok(uuid) = Uuid::parse_str(raw) {
            return Ok(uuid);
        }
        let matches = self.find_cards_by_identifier(raw)?;
        match matches.as_slice() {
            [] => Err(KanbanError::not_found_by_name(
                "Card",
                raw,
                Vec::new(), // cards aren't enumerated in the error — too many.
            )),
            [c] => Ok(c.id),
            many => Err(KanbanError::ambiguous(
                "Card",
                raw,
                many.iter()
                    .map(|c| AmbiguousMatch {
                        label: format!("'{}'", c.title),
                        id: c.id,
                    })
                    .collect(),
            )),
        }
    }

    /// Resolve a batch of card identifiers against a single snapshot. Pure
    /// in-memory matching against `find_cards_by_identifier`; one set of
    /// `list_all_*` calls regardless of batch size.
    ///
    /// On failure, returns `KanbanError::BatchResolutionFailed` with per-input
    /// typed causes so callers can introspect (which raw inputs failed, and
    /// for what reason).
    fn resolve_card_ids(&self, raws: &[String]) -> KanbanResult<Vec<Uuid>> {
        // Single snapshot for the whole batch — no per-element backend round-trips.
        let cards = self.list_all_cards()?;
        let columns = self.list_all_columns()?;
        let boards = self.list_boards()?;
        let sprints = self.list_all_sprints()?;

        let mut resolved = Vec::with_capacity(raws.len());
        let mut failures = Vec::new();
        for raw in raws {
            if let Ok(uuid) = Uuid::parse_str(raw) {
                resolved.push(uuid);
                continue;
            }
            let matches =
                crate::search::find_cards_by_identifier(raw, &cards, &columns, &boards, &sprints);
            match matches.as_slice() {
                [] => failures.push(BatchResolutionFailure {
                    raw_input: raw.clone(),
                    cause: BatchResolutionCause::NotFound,
                }),
                [c] => resolved.push(c.id),
                many => failures.push(BatchResolutionFailure {
                    raw_input: raw.clone(),
                    cause: BatchResolutionCause::Ambiguous(
                        many.iter()
                            .map(|c| AmbiguousMatch {
                                label: format!("'{}'", c.title),
                                id: c.id,
                            })
                            .collect(),
                    ),
                }),
            }
        }
        if !failures.is_empty() {
            return Err(KanbanError::batch_resolution_failed("Card", failures));
        }
        Ok(resolved)
    }

    /// For batch operations, verify all the given cards belong to the same board.
    /// Returns the shared `board_id` on success; otherwise a validation error
    /// listing the boards involved. Single snapshot — O(1) backend queries.
    fn require_same_board(&self, card_ids: &[Uuid]) -> KanbanResult<Uuid> {
        use std::collections::HashMap;
        if card_ids.is_empty() {
            return Err(KanbanError::validation(
                "No cards provided for batch operation.".to_string(),
            ));
        }
        let cards = self.list_all_cards()?;
        let columns = self.list_all_columns()?;
        // column_id -> board_id, one lookup per card afterward.
        let col_to_board: HashMap<Uuid, Uuid> =
            columns.iter().map(|c| (c.id, c.board_id)).collect();
        let card_index: HashMap<Uuid, &Card> = cards.iter().map(|c| (c.id, c)).collect();

        let mut seen = std::collections::BTreeSet::new();
        let mut found_boards = Vec::new();
        for cid in card_ids {
            let card = card_index
                .get(cid)
                .ok_or_else(|| KanbanError::not_found("Card", *cid))?;
            let board_id = col_to_board
                .get(&card.column_id)
                .copied()
                .ok_or_else(|| KanbanError::not_found("Column", card.column_id))?;
            if seen.insert(board_id) {
                found_boards.push(board_id);
            }
        }
        match found_boards.as_slice() {
            [b] => Ok(*b),
            many => {
                let boards = self.list_boards()?;
                let names: Vec<String> = many
                    .iter()
                    .map(|bid| {
                        boards
                            .iter()
                            .find(|b| b.id == *bid)
                            .map(|b| format!("'{}'", b.name))
                            .unwrap_or_else(|| bid.to_string())
                    })
                    .collect();
                Err(KanbanError::validation(format!(
                    "Batch operation requires all cards on the same board, but the selection spans boards: {}",
                    names.join(", ")
                )))
            }
        }
    }
}