|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * Task Action Handlers |
| 5 | + * |
| 6 | + * Example handler implementations for actions defined in task.actions.ts. |
| 7 | + * Each handler is registered via `engine.registerAction()` and referenced |
| 8 | + * by name through the action's `target` field. |
| 9 | + * |
| 10 | + * @example Registration (in a plugin or config bootstrap): |
| 11 | + * ```ts |
| 12 | + * engine.registerAction('task', 'completeTask', completeTask); |
| 13 | + * engine.registerAction('task', 'startTask', startTask); |
| 14 | + * ``` |
| 15 | + */ |
| 16 | + |
| 17 | +// ─── Handler Context (simplified for example purposes) ────────────── |
| 18 | +interface ActionContext { |
| 19 | + /** The record being acted upon */ |
| 20 | + record: Record<string, unknown>; |
| 21 | + /** Current authenticated user */ |
| 22 | + user: { id: string; name: string }; |
| 23 | + /** Data engine for CRUD operations */ |
| 24 | + engine: { |
| 25 | + update(object: string, id: string, data: Record<string, unknown>): Promise<void>; |
| 26 | + insert(object: string, data: Record<string, unknown>): Promise<{ _id: string }>; |
| 27 | + find(object: string, query: Record<string, unknown>): Promise<Array<Record<string, unknown>>>; |
| 28 | + delete(object: string, ids: string[]): Promise<void>; |
| 29 | + }; |
| 30 | + /** Action parameters (from user input / params) */ |
| 31 | + params?: Record<string, unknown>; |
| 32 | +} |
| 33 | + |
| 34 | +/** Mark a single task as complete */ |
| 35 | +export async function completeTask(ctx: ActionContext): Promise<void> { |
| 36 | + const { record, engine, user } = ctx; |
| 37 | + await engine.update('task', record._id as string, { |
| 38 | + status: 'completed', |
| 39 | + completed_at: new Date().toISOString(), |
| 40 | + completed_by: user.id, |
| 41 | + }); |
| 42 | +} |
| 43 | + |
| 44 | +/** Mark a task as in-progress */ |
| 45 | +export async function startTask(ctx: ActionContext): Promise<void> { |
| 46 | + const { record, engine } = ctx; |
| 47 | + await engine.update('task', record._id as string, { |
| 48 | + status: 'in_progress', |
| 49 | + started_at: new Date().toISOString(), |
| 50 | + }); |
| 51 | +} |
| 52 | + |
| 53 | +/** Clone a task (duplicate with reset status) */ |
| 54 | +export async function cloneTask(ctx: ActionContext): Promise<{ _id: string }> { |
| 55 | + const { record, engine } = ctx; |
| 56 | + const { _id, created_at, updated_at, completed_at, completed_by, ...fields } = record as Record<string, unknown>; |
| 57 | + return engine.insert('task', { |
| 58 | + ...fields, |
| 59 | + status: 'not_started', |
| 60 | + subject: `Copy of ${fields.subject ?? 'Untitled'}`, |
| 61 | + }); |
| 62 | +} |
| 63 | + |
| 64 | +/** Mark all selected tasks as complete (bulk) */ |
| 65 | +export async function massCompleteTasks(ctx: ActionContext): Promise<void> { |
| 66 | + const { params, engine, user } = ctx; |
| 67 | + const ids = (params?.selectedIds ?? []) as string[]; |
| 68 | + const now = new Date().toISOString(); |
| 69 | + for (const id of ids) { |
| 70 | + await engine.update('task', id, { |
| 71 | + status: 'completed', |
| 72 | + completed_at: now, |
| 73 | + completed_by: user.id, |
| 74 | + }); |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +/** Delete all completed tasks */ |
| 79 | +export async function deleteCompletedTasks(ctx: ActionContext): Promise<void> { |
| 80 | + const { engine } = ctx; |
| 81 | + const completed = await engine.find('task', { status: 'completed' }); |
| 82 | + const ids = completed.map((r) => r._id as string); |
| 83 | + if (ids.length > 0) { |
| 84 | + await engine.delete('task', ids); |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +/** Export tasks to CSV format */ |
| 89 | +export async function exportTasksToCSV(ctx: ActionContext): Promise<string> { |
| 90 | + const { engine } = ctx; |
| 91 | + const tasks = await engine.find('task', {}); |
| 92 | + const header = 'subject,status,priority,category,due_date'; |
| 93 | + const rows = tasks.map((t) => |
| 94 | + [t.subject, t.status, t.priority, t.category, t.due_date ?? ''].join(','), |
| 95 | + ); |
| 96 | + return [header, ...rows].join('\n'); |
| 97 | +} |
0 commit comments