-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask-status.enum.ts
More file actions
47 lines (45 loc) · 1.47 KB
/
Copy pathtask-status.enum.ts
File metadata and controls
47 lines (45 loc) · 1.47 KB
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
/**
* The lifecycle states a task can occupy.
*
* String values are used so they serialise cleanly over JSON / WebSocket
* and read well in logs and the frontend.
*/
export enum TaskStatus {
Todo = 'todo',
InProgress = 'in_progress',
Done = 'done',
Archived = 'archived',
}
/**
* The status transition state machine.
*
* Each key maps to the set of statuses it is legal to move *to*. Keeping the
* rules in a single declarative table makes the policy trivial to audit and
* extend — adding a new state or edge is a one-line change here, with no
* branching logic scattered across services.
*
* todo → in_progress | archived
* in_progress → todo | done | archived
* done → archived (cannot reopen to todo)
* archived → (terminal)
*/
export const TASK_STATUS_TRANSITIONS: Readonly<
Record<TaskStatus, readonly TaskStatus[]>
> = {
[TaskStatus.Todo]: [TaskStatus.InProgress, TaskStatus.Archived],
[TaskStatus.InProgress]: [
TaskStatus.Todo,
TaskStatus.Done,
TaskStatus.Archived,
],
[TaskStatus.Done]: [TaskStatus.Archived],
[TaskStatus.Archived]: [],
};
/**
* Returns true when moving `from` → `to` is permitted by the state machine.
* A no-op transition (from === to) is treated as illegal so callers get a
* descriptive error rather than silently re-writing `updatedAt`.
*/
export function canTransition(from: TaskStatus, to: TaskStatus): boolean {
return TASK_STATUS_TRANSITIONS[from].includes(to);
}