-
Notifications
You must be signed in to change notification settings - Fork 146
Expand file tree
/
Copy pathlist-workflow-query.ts
More file actions
103 lines (85 loc) · 2.4 KB
/
list-workflow-query.ts
File metadata and controls
103 lines (85 loc) · 2.4 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
import { get } from 'svelte/store';
import { supportsAdvancedVisibility } from '$lib/stores/advanced-visibility';
import type {
ArchiveFilterParameters,
FilterParameters,
} from '$lib/types/workflows';
import { isDuration, isDurationString, toDate, tomorrow } from '../to-duration';
export type QueryKey =
| 'WorkflowId'
| 'WorkflowType'
| 'StartTime'
| 'CloseTime'
| 'ExecutionTime'
| 'ExecutionStatus'
| 'ActivityId';
export type FilterKey =
| 'workflowId'
| 'workflowType'
| 'timeRange'
| 'executionStatus'
| 'closeTime'
| 'activityId';
type FilterValue = string | Duration;
const queryKeys: Readonly<Record<string, QueryKey>> = {
workflowId: 'WorkflowId',
workflowType: 'WorkflowType',
timeRange: 'StartTime',
executionStatus: 'ExecutionStatus',
closeTime: 'CloseTime',
activityId: 'ActivityId',
} as const;
const filterKeys: readonly FilterKey[] = [
'workflowId',
'workflowType',
'timeRange',
'executionStatus',
'closeTime',
'activityId',
] as const;
const isValid = (value: unknown): boolean => {
if (value === null) return false;
if (value === undefined) return false;
if (value === '') return false;
if (typeof value === 'string' && value === 'undefined') return false;
return true;
};
export const isFilterKey = (key: unknown): key is FilterKey => {
if (typeof key !== 'string') return false;
for (const filterKey of filterKeys) {
if (filterKey === key) return true;
}
return false;
};
const toQueryStatement = (
key: FilterKey,
value: FilterValue,
archived: boolean,
): string => {
const queryKey = queryKeys[key];
if (value === 'All') return '';
if (isDuration(value) || isDurationString(value)) {
if (archived || get(supportsAdvancedVisibility)) {
return `${queryKey} > "${toDate(value)}"`;
}
return `${queryKey} BETWEEN "${toDate(value)}" AND "${tomorrow()}"`;
}
return `${queryKey}="${value}"`;
};
const toQueryStatements = (
parameters: FilterParameters | ArchiveFilterParameters,
archived: boolean,
): string[] => {
return Object.entries(parameters)
.map(([key, value]) => {
if (isFilterKey(key) && isValid(value))
return toQueryStatement(key, value, archived);
})
.filter(Boolean);
};
export const toListWorkflowQuery = (
parameters: FilterParameters | ArchiveFilterParameters,
archived = false,
): string => {
return toQueryStatements(parameters, archived).join(' and ');
};