task-list.ts
TypeScript · 141 рядків
1const ACTIVE_STATUSES = new Set(['open', 'in_progress'])
2const SORT_FIELDS = new Set([
3 'id',
4 'entityId',
5 'orderId',
6 'type',
7 'title',
8 'status',
9 'state',
10 'createdAt',
11 'updatedAt'
12])
13
14function text(value) {
15 return String(value ?? '').toLocaleLowerCase()
16}
17
18export function summarizeTask(task) {
19 return {
20 id: task.id,
21 executionId: task.executionId || null,
22 entityType: task.entityType || null,
23 entityId: task.entityId || null,
24 parentTaskId: task.parentTaskId || null,
25 orderId: task.orderId || null,
26 type: task.type || null,
27 handlerWorkflowId: task.handlerWorkflowId || null,
28 handlerWorkflowVersion: task.handlerWorkflowVersion || null,
29 title: task.title || null,
30 description: task.description || null,
31 priority: task.priority || null,
32 status: task.status,
33 state: task.state || null,
34 progress: task.progress || null,
35 assignee: task.assignee || null,
36 version: task.version || 1,
37 createdAt: task.createdAt || null,
38 updatedAt: task.updatedAt || task.createdAt || null,
39 completedAt: task.completedAt || null
40 }
41}
42
43export function queryExternalTasks(tasks = [], options: any = {}) {
44 const exact = (value) => String(value || '').trim()
45 const taskId = exact(options.taskId)
46 const entityType = exact(options.entityType)
47 const entityId = exact(options.entityId)
48 const userId = exact(options.userId)
49 const type = exact(options.type)
50 const statuses = exact(options.status).split(',').map(value => value.trim()).filter(Boolean)
51 const limit = Math.min(Math.max(Number.parseInt(options.limit, 10) || 50, 1), 200)
52 const offset = Math.max(Number.parseInt(options.offset, 10) || 0, 0)
53
54 const filtered = tasks
55 .filter(task => !taskId || String(task.id) === taskId)
56 .filter(task => !entityType || String(task.entityType) === entityType)
57 .filter(task => !entityId || String(task.entityId) === entityId)
58 .filter(task => !userId || (
59 task.assignee?.type === 'person' && String(task.assignee?.id) === userId
60 ))
61 .filter(task => !type || String(task.type) === type)
62 .filter(task => !statuses.length || statuses.includes(String(task.status)))
63 .sort((left, right) => compareValues(right, left, 'updatedAt'))
64
65 return {
66 tasks: filtered.slice(offset, offset + limit).map(summarizeTask),
67 total: filtered.length,
68 limit,
69 offset
70 }
71}
72
73function compareValues(left, right, field) {
74 if (['createdAt', 'updatedAt'].includes(field)) {
75 return Date.parse(left?.[field] || 0) - Date.parse(right?.[field] || 0)
76 }
77 return String(left?.[field] ?? '').localeCompare(
78 String(right?.[field] ?? ''),
79 undefined,
80 {
81 numeric: true,
82 sensitivity: 'base'
83 }
84 )
85}
86
87export function queryTasks(tasks = [], options: any = {}) {
88 const scope = options.scope === 'completed' ? 'completed' : 'active'
89 const query = text(options.query).trim()
90 const type = String(options.type || '').trim()
91 const sort = SORT_FIELDS.has(options.sort) ? options.sort : 'updatedAt'
92 const direction = options.direction === 'asc' ? 'asc' : 'desc'
93
94 const counts = {
95 active: tasks.filter(task => ACTIVE_STATUSES.has(task.status)).length,
96 completed: tasks.filter(task => task.status === 'completed').length
97 }
98 const scopedTasks = tasks.filter(task =>
99 scope === 'active'
100 ? ACTIVE_STATUSES.has(task.status)
101 : task.status === 'completed'
102 )
103 const types = [...new Set(
104 scopedTasks
105 .map(task => String(task.type || '').trim())
106 .filter(Boolean)
107 )].sort((left, right) => left.localeCompare(right, undefined, {
108 numeric: true,
109 sensitivity: 'base'
110 }))
111
112 const filtered = scopedTasks
113 .filter(task => !type || task.type === type)
114 .filter(task => {
115 if (!query) return true
116 return [
117 task.id,
118 task.entityId,
119 task.orderId,
120 task.parentTaskId,
121 task.type,
122 task.title
123 ].some(value => text(value).includes(query))
124 })
125 .sort((left, right) => {
126 const result = compareValues(left, right, sort)
127 if (result !== 0) return direction === 'asc' ? result : -result
128 return String(left.id).localeCompare(String(right.id))
129 })
130
131 return {
132 tasks: filtered.map(summarizeTask),
133 total: filtered.length,
134 counts,
135 types,
136 scope,
137 sort,
138 direction
139 }
140}
141