-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtask.handlers.ts
More file actions
113 lines (103 loc) · 4.01 KB
/
Copy pathtask.handlers.ts
File metadata and controls
113 lines (103 loc) · 4.01 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
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
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
/**
* Task Action Handlers
*
* Example handler implementations for actions defined in task.actions.ts.
* Each handler is registered via `engine.registerAction()` and referenced
* by name through the action's `target` field.
*
* @example Registration (in a plugin or config bootstrap):
* ```ts
* engine.registerAction('todo_task', 'completeTask', completeTask);
* engine.registerAction('todo_task', 'startTask', startTask);
* ```
*/
// ─── Handler Context (simplified for example purposes) ──────────────
interface ActionContext {
/** The record being acted upon */
record: Record<string, unknown>;
/** Current authenticated user */
user: { id: string; name: string };
/** Data engine for CRUD operations */
engine: {
update(object: string, id: string, data: Record<string, unknown>): Promise<void>;
insert(object: string, data: Record<string, unknown>): Promise<{ id: string }>;
find(object: string, query: Record<string, unknown>): Promise<Array<Record<string, unknown>>>;
delete(object: string, ids: string[]): Promise<void>;
};
/** Action parameters (from user input / params) */
params?: Record<string, unknown>;
}
/** Mark a single task as complete */
export async function completeTask(ctx: ActionContext): Promise<void> {
const { record, engine } = ctx;
await engine.update('todo_task', record.id as string, {
status: 'completed',
completed_date: new Date().toISOString(),
});
}
/** Mark a task as in-progress */
export async function startTask(ctx: ActionContext): Promise<void> {
const { record, engine } = ctx;
await engine.update('todo_task', record.id as string, {
status: 'in_progress',
});
}
/** Clone a task (duplicate with reset status) */
export async function cloneTask(ctx: ActionContext): Promise<{ id: string }> {
const { record, engine } = ctx;
const { id, created_at, updated_at, completed_date, ...fields } = record as Record<string, unknown>;
return engine.insert('todo_task', {
...fields,
status: 'not_started',
subject: `Copy of ${fields.subject ?? 'Untitled'}`,
});
}
/** Mark all selected tasks as complete (bulk) */
export async function massCompleteTasks(ctx: ActionContext): Promise<void> {
const { params, engine } = ctx;
const ids = (params?.selectedIds ?? []) as string[];
const now = new Date().toISOString();
for (const id of ids) {
await engine.update('todo_task', id, {
status: 'completed',
completed_date: now,
});
}
}
/** Delete all completed tasks */
export async function deleteCompletedTasks(ctx: ActionContext): Promise<void> {
const { engine } = ctx;
const completed = await engine.find('todo_task', { status: 'completed' });
const ids = completed.map((r) => r.id as string);
if (ids.length > 0) {
await engine.delete('todo_task', ids);
}
}
/** Defer a task by updating its due date (modal form submission handler) */
export async function deferTask(ctx: ActionContext): Promise<void> {
const { record, engine, params } = ctx;
await engine.update('todo_task', record.id as string, {
due_date: params?.new_due_date ? String(params.new_due_date) : null,
defer_reason: params?.reason ? String(params.reason) : null,
status: 'waiting',
});
}
/** Set a reminder on a task (modal form submission handler) */
export async function setReminder(ctx: ActionContext): Promise<void> {
const { record, engine, params } = ctx;
await engine.update('todo_task', record.id as string, {
reminder_date: params?.reminder_date ? String(params.reminder_date) : null,
has_reminder: true,
});
}
/** Export tasks to CSV format */
export async function exportTasksToCSV(ctx: ActionContext): Promise<string> {
const { engine } = ctx;
const tasks = await engine.find('todo_task', {});
const header = 'subject,status,priority,category,due_date';
const rows = tasks.map((t) =>
[t.subject, t.status, t.priority, t.category, t.due_date ?? ''].join(','),
);
return [header, ...rows].join('\n');
}