dtable-utils 4.3.2-beta.1 → 4.3.2-beta.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,682 @@
1
+ import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
2
+ import dayjs from 'dayjs';
3
+ import utc from 'dayjs/plugin/utc';
4
+ import { ValidateFilter } from '../../validate/filter.js';
5
+ import { otherDate } from '../../filter/core.js';
6
+ import { getTableColumnByKey } from '../../table/column.js';
7
+ import { FILTER_ERR_MSG } from '../../constants/filter/index.js';
8
+ import { CellType } from '../../constants/cell-type.js';
9
+ import { DEPARTMENT_SELECT_RANGE_MAP } from '../../constants/column.js';
10
+ import { FORMULA_RESULT_TYPE } from '../../constants/formula.js';
11
+ import { FILTER_PREDICATE_TYPE } from '../../constants/filter/filter-predicate.js';
12
+ import { FILTER_TERM_MODIFIER_TYPE } from '../../constants/filter/filter-modifier.js';
13
+
14
+ dayjs.extend(utc);
15
+
16
+ // TEXT | GEOLOCATION | EMAIL | AUTO_NUMBER
17
+ var textSqlCondition = function textSqlCondition(column, filterItem, userId) {
18
+ var name = column.name;
19
+ var filter_predicate = filterItem.filter_predicate,
20
+ filter_term = filterItem.filter_term;
21
+
22
+ // 'TEXT' type special treatment
23
+ if (column.type === CellType.TEXT) {
24
+ if (filter_predicate === FILTER_PREDICATE_TYPE.EMPTY) {
25
+ return "`".concat(name, "` = '' or `").concat(name, "` is null");
26
+ }
27
+ if (filter_predicate === FILTER_PREDICATE_TYPE.NOT_EMPTY) {
28
+ return "`".concat(name, "` <> '' and `").concat(name, "` is not null");
29
+ }
30
+ }
31
+ if (filter_predicate === FILTER_PREDICATE_TYPE.EMPTY) {
32
+ return "`".concat(name, "` is null");
33
+ }
34
+ if (filter_predicate === FILTER_PREDICATE_TYPE.NOT_EMPTY) {
35
+ return "`".concat(name, "` is not null");
36
+ }
37
+ if (filter_predicate === FILTER_PREDICATE_TYPE.IS_CURRENT_USER_ID) {
38
+ if (!userId) {
39
+ return "(`".concat(name, "` IS NULL AND `").concat(name, "` IS NOT NULL)");
40
+ }
41
+ return "`".concat(name, "` = '").concat(userId, "'");
42
+ }
43
+ if (!filter_term) return '';
44
+ if (filter_predicate === FILTER_PREDICATE_TYPE.CONTAINS) {
45
+ return "`".concat(name, "` ilike '%").concat(filter_term, "%'");
46
+ }
47
+ if (filter_predicate === FILTER_PREDICATE_TYPE.NOT_CONTAIN) {
48
+ return "`".concat(name, "` not ilike '%").concat(filter_term, "%'");
49
+ }
50
+ if (filter_predicate === FILTER_PREDICATE_TYPE.IS) {
51
+ return "`".concat(name, "` = '").concat(filter_term, "'");
52
+ }
53
+ if (filter_predicate === FILTER_PREDICATE_TYPE.IS_NOT) {
54
+ return "`".concat(name, "` <> '").concat(filter_term, "'");
55
+ }
56
+ return '';
57
+ };
58
+
59
+ // NUMBER | DURATION | RATE
60
+ var numberSqlCondition = function numberSqlCondition(column, filterItem) {
61
+ var name = column.name;
62
+ var filter_predicate = filterItem.filter_predicate,
63
+ filter_term = filterItem.filter_term;
64
+
65
+ // recalculate duration date
66
+ var value = filter_term;
67
+ if (filter_predicate === FILTER_PREDICATE_TYPE.EMPTY) {
68
+ return "`".concat(name, "` is null");
69
+ }
70
+ if (filter_predicate === FILTER_PREDICATE_TYPE.NOT_EMPTY) {
71
+ return "`".concat(name, "` is not null");
72
+ }
73
+ // filter_term is invalid
74
+ if (!value && value !== 0) {
75
+ return '';
76
+ }
77
+ value = parseFloat(value);
78
+ if (filter_predicate === FILTER_PREDICATE_TYPE.EQUAL) {
79
+ return "`".concat(name, "` = ").concat(value);
80
+ }
81
+ if (filter_predicate === FILTER_PREDICATE_TYPE.NOT_EQUAL) {
82
+ return "`".concat(name, "` <> ").concat(value);
83
+ }
84
+ if (filter_predicate === FILTER_PREDICATE_TYPE.LESS) {
85
+ return "`".concat(name, "` < ").concat(value);
86
+ }
87
+ if (filter_predicate === FILTER_PREDICATE_TYPE.GREATER) {
88
+ return "`".concat(name, "` > ").concat(value);
89
+ }
90
+ if (filter_predicate === FILTER_PREDICATE_TYPE.LESS_OR_EQUAL) {
91
+ return "`".concat(name, "` <= ").concat(value);
92
+ }
93
+ if (filter_predicate === FILTER_PREDICATE_TYPE.GREATER_OR_EQUAL) {
94
+ return "`".concat(name, "` >= ").concat(value);
95
+ }
96
+ return '';
97
+ };
98
+
99
+ // CHECK_BOX
100
+ var checkboxSqlCondition = function checkboxSqlCondition(column, filterItem) {
101
+ var name = column.name;
102
+ var filter_term = filterItem.filter_term;
103
+ if (!filter_term) {
104
+ return "`".concat(name, "` = ").concat(filter_term, " or `").concat(name, "` is null");
105
+ }
106
+ return "`".concat(name, "` = ").concat(filter_term);
107
+ };
108
+
109
+ // DATE
110
+ var dateSqlCondition = function dateSqlCondition(column, filterItem) {
111
+ var name = column.name;
112
+ var filter_predicate = filterItem.filter_predicate,
113
+ filter_term = filterItem.filter_term,
114
+ filter_term_modifier = filterItem.filter_term_modifier;
115
+ var filterLabel = [FILTER_PREDICATE_TYPE.EMPTY, FILTER_PREDICATE_TYPE.NOT_EMPTY];
116
+ if (filter_term.length === 0 && filterLabel.indexOf(filter_predicate) < 0 && filter_term_modifier === FILTER_TERM_MODIFIER_TYPE.EXACT_DATE) {
117
+ return '';
118
+ }
119
+ if (filter_predicate === FILTER_PREDICATE_TYPE.IS) {
120
+ var date = otherDate(filter_term_modifier, filter_term);
121
+ var nextDate = dayjs(date).add(1, 'days').format('YYYY-MM-DD');
122
+ var targetDate = dayjs(date).format('YYYY-MM-DD');
123
+ return "`".concat(name, "` >= '").concat(targetDate, "' and `").concat(name, "` < '").concat(nextDate, "'");
124
+ }
125
+ if (filter_predicate === FILTER_PREDICATE_TYPE.IS_WITHIN) {
126
+ var _otherDate = otherDate(filter_term_modifier, filter_term),
127
+ startDate = _otherDate.startDate,
128
+ endDate = _otherDate.endDate;
129
+ endDate = dayjs(endDate).format('YYYY-MM-DD');
130
+ startDate = dayjs(startDate).format('YYYY-MM-DD');
131
+ return "`".concat(name, "` >= '").concat(startDate, "' and `").concat(name, "` <= '").concat(endDate, "'");
132
+ }
133
+ if (filter_predicate === FILTER_PREDICATE_TYPE.IS_BEFORE) {
134
+ var _targetDate = otherDate(filter_term_modifier, filter_term);
135
+ _targetDate = dayjs(_targetDate).format('YYYY-MM-DD');
136
+ return "`".concat(name, "` < '").concat(_targetDate, "' and `").concat(name, "` is not null");
137
+ }
138
+ if (filter_predicate === FILTER_PREDICATE_TYPE.IS_AFTER) {
139
+ var _targetDate2 = otherDate(filter_term_modifier, filter_term);
140
+ _targetDate2 = dayjs(_targetDate2).format('YYYY-MM-DD');
141
+ return "`".concat(name, "` > '").concat(_targetDate2, "'");
142
+ }
143
+ if (filter_predicate === FILTER_PREDICATE_TYPE.IS_ON_OR_BEFORE) {
144
+ var _targetDate3 = otherDate(filter_term_modifier, filter_term);
145
+ _targetDate3 = dayjs(_targetDate3).format('YYYY-MM-DD');
146
+ return "`".concat(name, "` <= '").concat(_targetDate3, "' and `").concat(name, "` is not null");
147
+ }
148
+ if (filter_predicate === FILTER_PREDICATE_TYPE.IS_ON_OR_AFTER) {
149
+ var _targetDate4 = otherDate(filter_term_modifier, filter_term);
150
+ _targetDate4 = dayjs(_targetDate4).format('YYYY-MM-DD');
151
+ return "`".concat(name, "` >= '").concat(_targetDate4, "' and `").concat(name, "` is not null");
152
+ }
153
+ if (filter_predicate === FILTER_PREDICATE_TYPE.IS_NOT) {
154
+ var _targetDate5 = otherDate(filter_term_modifier, filter_term);
155
+ var _startDate = dayjs(_targetDate5).subtract(1, 'days').format('YYYY-MM-DD');
156
+ var _endDate = dayjs(_targetDate5).add(1, 'days').format('YYYY-MM-DD');
157
+ return "(`".concat(name, "` is null or `").concat(name, "` >= '").concat(_endDate, "' or `").concat(name, "` <= '").concat(_startDate, "')");
158
+ }
159
+ if (filter_predicate === FILTER_PREDICATE_TYPE.EMPTY) {
160
+ return "`".concat(name, "` is null");
161
+ }
162
+ if (filter_predicate === FILTER_PREDICATE_TYPE.NOT_EMPTY) {
163
+ return "`".concat(name, "` is not null");
164
+ }
165
+ return '';
166
+ };
167
+
168
+ // CTIME | MTIME
169
+ var ctimeSqlCondition = function ctimeSqlCondition(column, filterItem) {
170
+ var name = column.name;
171
+ var filter_predicate = filterItem.filter_predicate,
172
+ filter_term = filterItem.filter_term,
173
+ filter_term_modifier = filterItem.filter_term_modifier;
174
+ var filterLabel = [FILTER_PREDICATE_TYPE.EMPTY, FILTER_PREDICATE_TYPE.NOT_EMPTY];
175
+ if (filter_term.length === 0 && filterLabel.indexOf(filter_predicate) < 0 && filter_term_modifier === FILTER_TERM_MODIFIER_TYPE.EXACT_DATE) {
176
+ return '';
177
+ }
178
+
179
+ // in the following cases, filter_term is a number and no special treatment is required
180
+ var specialTermModifier = [FILTER_TERM_MODIFIER_TYPE.NUMBER_OF_DAYS_AGO, FILTER_TERM_MODIFIER_TYPE.NUMBER_OF_DAYS_FROM_NOW, FILTER_TERM_MODIFIER_TYPE.THE_PAST_NUMBERS_OF_DAYS, FILTER_TERM_MODIFIER_TYPE.THE_NEXT_NUMBERS_OF_DAYS];
181
+ if (!specialTermModifier.includes(filter_term_modifier)) {
182
+ // treat date as the start of the day
183
+ filter_term = filter_term ? "".concat(filter_term, " 00:00:00") : '';
184
+ }
185
+ if (filter_predicate === FILTER_PREDICATE_TYPE.IS) {
186
+ var date = otherDate(filter_term_modifier, filter_term);
187
+ var targetDate = dayjs(date).utc().format();
188
+ var nextDate = dayjs(date).add(1, 'day').utc().format();
189
+ return "`".concat(name, "` >= '").concat(targetDate, "' and `").concat(name, "` < '").concat(nextDate, "'");
190
+ }
191
+ if (filter_predicate === FILTER_PREDICATE_TYPE.IS_WITHIN) {
192
+ var _otherDate2 = otherDate(filter_term_modifier, filter_term),
193
+ startDate = _otherDate2.startDate,
194
+ endDate = _otherDate2.endDate;
195
+ startDate = dayjs(startDate).utc().format();
196
+ endDate = dayjs(endDate).utc().format();
197
+ if (filter_term_modifier !== FILTER_TERM_MODIFIER_TYPE.THE_PAST_NUMBERS_OF_DAYS) {
198
+ // when the interval value is taken, the end time is 24:00 of the day
199
+ endDate = dayjs(endDate).add(24, 'hour').utc().format();
200
+ }
201
+ return "`".concat(name, "` >= '").concat(startDate, "' and `").concat(name, "` <= '").concat(endDate, "'");
202
+ }
203
+ if (filter_predicate === FILTER_PREDICATE_TYPE.IS_BEFORE) {
204
+ var _targetDate6 = otherDate(filter_term_modifier, filter_term);
205
+ _targetDate6 = dayjs(_targetDate6).utc().format();
206
+ return "`".concat(name, "` < '").concat(_targetDate6, "' and `").concat(name, "` is not null");
207
+ }
208
+
209
+ // after a certain day, it should start at 24:00 a certain day
210
+ if (filter_predicate === FILTER_PREDICATE_TYPE.IS_AFTER) {
211
+ var _targetDate7 = otherDate(filter_term_modifier, filter_term);
212
+ _targetDate7 = dayjs(_targetDate7).add(24, 'hour').utc().format();
213
+ return "`".concat(name, "` > '").concat(_targetDate7, "'");
214
+ }
215
+
216
+ // on or before a certain day, it should end at 24:00 on a certain day
217
+ if (filter_predicate === FILTER_PREDICATE_TYPE.IS_ON_OR_BEFORE) {
218
+ var _targetDate8 = otherDate(filter_term_modifier, filter_term);
219
+ _targetDate8 = dayjs(_targetDate8).add(24, 'hour').utc().format();
220
+ return "`".concat(name, "` <= '").concat(_targetDate8, "' and `").concat(name, "` is not null");
221
+ }
222
+ if (filter_predicate === FILTER_PREDICATE_TYPE.IS_ON_OR_AFTER) {
223
+ var _targetDate9 = otherDate(filter_term_modifier, filter_term);
224
+ _targetDate9 = dayjs(_targetDate9).utc().format();
225
+ return "`".concat(name, "` >= '").concat(_targetDate9, "' and `").concat(name, "` is not null");
226
+ }
227
+ if (filter_predicate === FILTER_PREDICATE_TYPE.IS_NOT) {
228
+ var _targetDate10 = otherDate(filter_term_modifier, filter_term);
229
+ var _startDate2 = dayjs(_targetDate10).utc().format();
230
+ var _endDate2 = dayjs(_targetDate10).add(1, 'day').utc().format();
231
+ return "(`".concat(name, "` is null or `").concat(name, "` >= '").concat(_endDate2, "' or `").concat(name, "` <= '").concat(_startDate2, "')");
232
+ }
233
+ if (filter_predicate === FILTER_PREDICATE_TYPE.EMPTY) {
234
+ return "`".concat(name, "` is null");
235
+ }
236
+ if (filter_predicate === FILTER_PREDICATE_TYPE.NOT_EMPTY) {
237
+ return "`".concat(name, "` is not null");
238
+ }
239
+ return '';
240
+ };
241
+
242
+ // SINGLE-SELECT
243
+ var singleSelectSqlCondition = function singleSelectSqlCondition(column, filterItem) {
244
+ var filter_predicate = filterItem.filter_predicate,
245
+ filter_term = filterItem.filter_term;
246
+ var name = column.name,
247
+ data = column.data;
248
+ if (filter_predicate === FILTER_PREDICATE_TYPE.NOT_EMPTY) {
249
+ return "`".concat(name, "` is not null");
250
+ }
251
+ if (filter_predicate === FILTER_PREDICATE_TYPE.EMPTY) {
252
+ return "`".concat(name, "` is null");
253
+ }
254
+ if (!filter_term) return '';
255
+ var options = data && data.options;
256
+ if (!Array.isArray(options) || options.length === 0) return '';
257
+ if (filter_predicate === FILTER_PREDICATE_TYPE.IS) {
258
+ if (typeof filter_term !== 'string') return '';
259
+ var option = options.find(function (item) {
260
+ return item.id === filter_term;
261
+ });
262
+ if (!option) return '';
263
+ return "`".concat(name, "` = '").concat(option.name, "'");
264
+ }
265
+ if (filter_predicate === FILTER_PREDICATE_TYPE.IS_NOT) {
266
+ if (typeof filter_term !== 'string') return '';
267
+ var _option = options.find(function (item) {
268
+ return item.id === filter_term;
269
+ });
270
+ if (!_option) return '';
271
+ return "`".concat(name, "` <> '").concat(_option.name, "'");
272
+ }
273
+ if (filter_predicate === FILTER_PREDICATE_TYPE.IS_ANY_OF) {
274
+ if (!Array.isArray(filter_term) || filter_term.length === 0) return '';
275
+ var selectedOptionsNames = filter_term.map(function (id) {
276
+ var option = options.find(function (item) {
277
+ return item.id === id;
278
+ });
279
+ if (!option) return null;
280
+ return "'".concat(option.name, "'");
281
+ }).filter(Boolean);
282
+ if (selectedOptionsNames.length !== filter_term.length) return ''; // contains deleted option(s)
283
+
284
+ return "`".concat(name, "` in (").concat(selectedOptionsNames.join(', '), ")");
285
+ }
286
+ if (filter_predicate === FILTER_PREDICATE_TYPE.IS_NONE_OF) {
287
+ if (!Array.isArray(filter_term) || filter_term.length === 0) return '';
288
+ var _selectedOptionsNames = filter_term.map(function (id) {
289
+ var option = options.find(function (item) {
290
+ return item.id === id;
291
+ });
292
+ if (!option) return null;
293
+ return "'".concat(option.name, "'");
294
+ }).filter(Boolean);
295
+ if (_selectedOptionsNames.length !== filter_term.length) return ''; // contains deleted option(s)
296
+
297
+ return "`".concat(name, "` not in (").concat(_selectedOptionsNames.join(', '), ")");
298
+ }
299
+ return '';
300
+ };
301
+ var getSpecificDepartmentIds = function getSpecificDepartmentIds(filter_term, currentUserDepartmentIdsOrSubIds) {
302
+ var numberIds = filter_term.filter(function (id) {
303
+ return typeof id === 'number';
304
+ });
305
+ var specificDepartmentIds = [].concat(_toConsumableArray(numberIds), _toConsumableArray(currentUserDepartmentIdsOrSubIds));
306
+ return specificDepartmentIds;
307
+ };
308
+
309
+ // DEPARTMENT-SINGLE-SELECT
310
+ var departmentSingleSelectSqlCondition = function departmentSingleSelectSqlCondition(column, filterItem, userDepartmentIdsMap) {
311
+ var filter_predicate = filterItem.filter_predicate,
312
+ filter_term = filterItem.filter_term;
313
+ var name = column.name;
314
+ if (filter_predicate === FILTER_PREDICATE_TYPE.NOT_EMPTY) {
315
+ return "`".concat(name, "` is not null");
316
+ }
317
+ if (filter_predicate === FILTER_PREDICATE_TYPE.EMPTY) {
318
+ return "`".concat(name, "` is null");
319
+ }
320
+
321
+ // filter_term is invalid
322
+ if (!filter_term || filter_term === 0) {
323
+ return '';
324
+ }
325
+ var currentUserDepartmentIds = userDepartmentIdsMap && userDepartmentIdsMap.current_user_department_ids;
326
+ var currentUserDepartmentAndSubIds = userDepartmentIdsMap && userDepartmentIdsMap.current_user_department_and_sub_ids;
327
+ var isValidCurrentUserDepartmentIds = Array.isArray(currentUserDepartmentIds);
328
+ var isValidCurrentUserDepartmentAndSubIds = Array.isArray(currentUserDepartmentAndSubIds);
329
+ if (filter_predicate === FILTER_PREDICATE_TYPE.IS) {
330
+ if (filter_term === DEPARTMENT_SELECT_RANGE_MAP.CURRENT_USER_DEPARTMENT) {
331
+ if (!isValidCurrentUserDepartmentIds) return '';
332
+ return "`".concat(name, "` in (").concat(currentUserDepartmentIds.join(', '), ")");
333
+ }
334
+ if (filter_term === DEPARTMENT_SELECT_RANGE_MAP.CURRENT_USER_DEPARTMENT_AND_SUB) {
335
+ if (!isValidCurrentUserDepartmentAndSubIds) return '';
336
+ return "`".concat(name, "` in (").concat(currentUserDepartmentAndSubIds.join(', '), ")");
337
+ }
338
+ return "`".concat(name, "` = ").concat(filter_term);
339
+ }
340
+ if (filter_predicate === FILTER_PREDICATE_TYPE.IS_NOT) {
341
+ if (filter_term === DEPARTMENT_SELECT_RANGE_MAP.CURRENT_USER_DEPARTMENT) {
342
+ if (!isValidCurrentUserDepartmentIds) return '';
343
+ return "`".concat(name, "` not in (").concat(currentUserDepartmentIds.join(', '), ")");
344
+ }
345
+ if (filter_term === DEPARTMENT_SELECT_RANGE_MAP.CURRENT_USER_DEPARTMENT_AND_SUB) {
346
+ if (!isValidCurrentUserDepartmentAndSubIds) return '';
347
+ return "`".concat(name, "` not in (").concat(currentUserDepartmentAndSubIds.join(', '), ")");
348
+ }
349
+ return "`".concat(name, "` <> ").concat(filter_term);
350
+ }
351
+ if (filter_predicate === FILTER_PREDICATE_TYPE.IS_ANY_OF) {
352
+ if (!Array.isArray(filter_term) || filter_term.length === 0) return '';
353
+ if (filter_term.includes(DEPARTMENT_SELECT_RANGE_MAP.CURRENT_USER_DEPARTMENT_AND_SUB) && isValidCurrentUserDepartmentAndSubIds) {
354
+ var _specificDepartmentIds = getSpecificDepartmentIds(filter_term, currentUserDepartmentAndSubIds);
355
+ return "`".concat(name, "` in (").concat(_specificDepartmentIds.join(', '), ")");
356
+ }
357
+ if (filter_term.includes(DEPARTMENT_SELECT_RANGE_MAP.CURRENT_USER_DEPARTMENT) && isValidCurrentUserDepartmentIds) {
358
+ var _specificDepartmentIds2 = getSpecificDepartmentIds(filter_term, currentUserDepartmentIds);
359
+ return "`".concat(name, "` in (").concat(_specificDepartmentIds2.join(', '), ")");
360
+ }
361
+ var specificDepartmentIds = getSpecificDepartmentIds(filter_term, []);
362
+ return "`".concat(name, "` in (").concat(specificDepartmentIds.join(', '), ")");
363
+ }
364
+ if (filter_predicate === FILTER_PREDICATE_TYPE.IS_NONE_OF) {
365
+ if (!Array.isArray(filter_term) || filter_term.length === 0) return '';
366
+ if (filter_term.includes(DEPARTMENT_SELECT_RANGE_MAP.CURRENT_USER_DEPARTMENT_AND_SUB) && isValidCurrentUserDepartmentAndSubIds) {
367
+ var _specificDepartmentIds3 = getSpecificDepartmentIds(filter_term, currentUserDepartmentAndSubIds);
368
+ return "`".concat(name, "` not in (").concat(_specificDepartmentIds3.join(', '), ")");
369
+ }
370
+ if (filter_term.includes(DEPARTMENT_SELECT_RANGE_MAP.CURRENT_USER_DEPARTMENT) && isValidCurrentUserDepartmentIds) {
371
+ var _specificDepartmentIds4 = getSpecificDepartmentIds(filter_term, currentUserDepartmentIds);
372
+ return "`".concat(name, "` not in (").concat(_specificDepartmentIds4.join(', '), ")");
373
+ }
374
+ var _specificDepartmentIds5 = getSpecificDepartmentIds(filter_term, []);
375
+ return "`".concat(name, "` not in (").concat(_specificDepartmentIds5.join(', '), ")");
376
+ }
377
+ return '';
378
+ };
379
+
380
+ // MULTIPLE_SELECT
381
+ var multipleSelectSqlCondition = function multipleSelectSqlCondition(column, filterItem) {
382
+ var name = column.name,
383
+ data = column.data;
384
+ var filterPredicate = filterItem.filter_predicate,
385
+ filterTerm = filterItem.filter_term;
386
+ if (filterPredicate === FILTER_PREDICATE_TYPE.NOT_EMPTY) {
387
+ return "`".concat(name, "` is not null");
388
+ }
389
+ if (filterPredicate === FILTER_PREDICATE_TYPE.EMPTY) {
390
+ return "`".concat(name, "` is null");
391
+ }
392
+ var options = data && data.options;
393
+ if (!Array.isArray(options) || options.length === 0) return '';
394
+ var selectedOptionIds = Array.isArray(filterTerm) ? filterTerm : [];
395
+ var optionNames = selectedOptionIds.map(function (itemId) {
396
+ var option = options.find(function (item) {
397
+ return item.id === itemId;
398
+ });
399
+ return option ? "'".concat(option.name, "'") : null;
400
+ }).filter(Boolean);
401
+
402
+ // empty/contains deleted option(s)
403
+ if (optionNames.length === 0 || optionNames.length !== selectedOptionIds.length) {
404
+ return '';
405
+ }
406
+ if (filterPredicate === FILTER_PREDICATE_TYPE.HAS_ANY_OF) {
407
+ return "`".concat(name, "` in (").concat(optionNames.join(', '), ")");
408
+ }
409
+ if (filterPredicate === FILTER_PREDICATE_TYPE.HAS_ALL_OF) {
410
+ return "`".concat(name, "` has all of (").concat(optionNames.join(', '), ")");
411
+ }
412
+ if (filterPredicate === FILTER_PREDICATE_TYPE.HAS_NONE_OF) {
413
+ return "`".concat(name, "` has none of (").concat(optionNames.join(', '), ")");
414
+ }
415
+ if (filterPredicate === FILTER_PREDICATE_TYPE.IS_EXACTLY) {
416
+ return "`".concat(name, "` is exactly (").concat(optionNames.join(', '), ")");
417
+ }
418
+ return '';
419
+ };
420
+
421
+ // CREATOR | MODIFIER
422
+ var creatorSqlCondition = function creatorSqlCondition(column, filterItem, username) {
423
+ var filterPredicate = filterItem.filter_predicate,
424
+ filterTerm = filterItem.filter_term;
425
+ var name = column.name;
426
+ if (filterPredicate === FILTER_PREDICATE_TYPE.INCLUDE_ME) {
427
+ if (!username) {
428
+ return "(`".concat(name, "` IS NULL AND `").concat(name, "` IS NOT NULL)");
429
+ }
430
+ return "`".concat(name, "` = '").concat(username, "'");
431
+ }
432
+ if (filterTerm && filterTerm.length === 0) {
433
+ return '';
434
+ }
435
+ if (filterPredicate === FILTER_PREDICATE_TYPE.IS) {
436
+ return "`".concat(name, "` = '").concat(filterTerm, "'");
437
+ }
438
+ if (filterPredicate === FILTER_PREDICATE_TYPE.IS_NOT) {
439
+ return "`".concat(name, "` != '").concat(filterTerm, "'");
440
+ }
441
+ var selectedCollaborators = filterTerm.map(function (item) {
442
+ return "'".concat(item, "'");
443
+ });
444
+ if (filterPredicate === FILTER_PREDICATE_TYPE.CONTAINS) {
445
+ return "`".concat(name, "` in (").concat(selectedCollaborators.join(','), ")");
446
+ }
447
+ if (filterPredicate === FILTER_PREDICATE_TYPE.NOT_CONTAIN) {
448
+ var items = selectedCollaborators.map(function (item) {
449
+ return "`".concat(name, "` != ").concat(item);
450
+ });
451
+ return "".concat(items.join(' and '));
452
+ }
453
+ return '';
454
+ };
455
+
456
+ // COLLABORATOR
457
+ var collaboratorSqlCondition = function collaboratorSqlCondition(column, filterItem, username) {
458
+ var name = column.name;
459
+ var filterPredicate = filterItem.filter_predicate,
460
+ filterTerm = filterItem.filter_term;
461
+ var selectedCollaborators = filterTerm.map(function (item) {
462
+ return "'".concat(item, "'");
463
+ });
464
+ if (selectedCollaborators.length === 0) {
465
+ if (filterPredicate === FILTER_PREDICATE_TYPE.INCLUDE_ME) {
466
+ if (!username) {
467
+ return "(`".concat(name, "` IS NULL AND `").concat(name, "` IS NOT NULL)");
468
+ }
469
+ return "`".concat(name, "` in ('").concat(username, "')");
470
+ }
471
+ if (filterPredicate === FILTER_PREDICATE_TYPE.NOT_EMPTY) {
472
+ return "`".concat(name, "` is not null");
473
+ }
474
+ if (filterPredicate === FILTER_PREDICATE_TYPE.EMPTY) {
475
+ return "`".concat(name, "` is null");
476
+ }
477
+ return '';
478
+ }
479
+ if (filterPredicate === FILTER_PREDICATE_TYPE.HAS_ANY_OF) {
480
+ return "`".concat(name, "` in (").concat(selectedCollaborators.join(', '), ")");
481
+ }
482
+ if (filterPredicate === FILTER_PREDICATE_TYPE.HAS_ALL_OF) {
483
+ return "`".concat(name, "` has all of (").concat(selectedCollaborators.join(', '), ")");
484
+ }
485
+ if (filterPredicate === FILTER_PREDICATE_TYPE.HAS_NONE_OF) {
486
+ return "`".concat(name, "` has none of (").concat(selectedCollaborators.join(', '), ")");
487
+ }
488
+ if (filterPredicate === FILTER_PREDICATE_TYPE.IS_EXACTLY) {
489
+ return "`".concat(name, "` is exactly (").concat(selectedCollaborators.join(', '), ")");
490
+ }
491
+ return '';
492
+ };
493
+ var fileSqlCondition = function fileSqlCondition(column, filterItem) {
494
+ var filter_predicate = filterItem.filter_predicate;
495
+ var name = column.name;
496
+ if (filter_predicate === FILTER_PREDICATE_TYPE.NOT_EMPTY) {
497
+ return "`".concat(name, "` is not null");
498
+ }
499
+ if (filter_predicate === FILTER_PREDICATE_TYPE.EMPTY) {
500
+ return "`".concat(name, "` is null");
501
+ }
502
+ return '';
503
+ };
504
+ var longtextSqlCondition = function longtextSqlCondition(column, filterItem) {
505
+ var filter_predicate = filterItem.filter_predicate;
506
+ var name = column.name;
507
+ if (filter_predicate === FILTER_PREDICATE_TYPE.NOT_EMPTY) {
508
+ return "`".concat(name, "` is not null");
509
+ }
510
+ if (filter_predicate === FILTER_PREDICATE_TYPE.EMPTY) {
511
+ return "`".concat(name, "` is null");
512
+ }
513
+ return '';
514
+ };
515
+ var arrayColumnCondition = function arrayColumnCondition(column, filterItem) {
516
+ var name = column.name;
517
+ var filter_predicate = filterItem.filter_predicate;
518
+ if (filter_predicate === FILTER_PREDICATE_TYPE.EMPTY) {
519
+ return "`".concat(name, "` is null");
520
+ }
521
+ if (filter_predicate === FILTER_PREDICATE_TYPE.NOT_EMPTY) {
522
+ return "`".concat(name, "` is not null");
523
+ }
524
+ var _ref = column.data || {},
525
+ array_type = _ref.array_type,
526
+ array_data = _ref.array_data;
527
+ if (!array_type) return '';
528
+
529
+ // encapsulate a new column according to the calculation result
530
+ var linkedColumn = {
531
+ name: column.name,
532
+ type: array_type,
533
+ data: array_data
534
+ };
535
+
536
+ // link with formula(string) | lookup with formula(string)
537
+ if (array_type === FORMULA_RESULT_TYPE.STRING) {
538
+ var newColumn = {
539
+ name: column.name,
540
+ type: CellType.TEXT
541
+ };
542
+ return textSqlCondition(newColumn, filterItem);
543
+ }
544
+
545
+ // link with formula(bool) | lookup with formula(bool)
546
+ if (array_type === FORMULA_RESULT_TYPE.BOOL) {
547
+ var _newColumn = {
548
+ name: column.name,
549
+ type: CellType.CHECKBOX
550
+ };
551
+ return checkboxSqlCondition(_newColumn, filterItem);
552
+ }
553
+
554
+ // using multiple value sql generator
555
+ if (array_type === CellType.SINGLE_SELECT) {
556
+ return multipleSelectSqlCondition(linkedColumn, filterItem);
557
+ }
558
+ if ([CellType.CREATOR, CellType.LAST_MODIFIER].includes(array_type)) {
559
+ return collaboratorSqlCondition(linkedColumn, filterItem);
560
+ }
561
+
562
+ // generate the corresponding sql statement according to the type of the calculation result
563
+ return getSqlConditionByFilter(linkedColumn, filterItem);
564
+ };
565
+
566
+ // LINK
567
+ var linkSqlCondition = function linkSqlCondition(column, filterItem) {
568
+ return arrayColumnCondition(column, filterItem);
569
+ };
570
+
571
+ // FORMULA | LINK-FORMULA
572
+ var formulaSqlCondition = function formulaSqlCondition(column, filterItem) {
573
+ var _ref2 = column.data || {},
574
+ result_type = _ref2.result_type;
575
+ if (result_type === FORMULA_RESULT_TYPE.STRING) {
576
+ // need to change the type of column to text
577
+ var newColumn = {
578
+ name: column.name,
579
+ type: CellType.TEXT
580
+ };
581
+ return textSqlCondition(newColumn, filterItem);
582
+ }
583
+ if (result_type === FORMULA_RESULT_TYPE.BOOL) {
584
+ return checkboxSqlCondition(column, filterItem);
585
+ }
586
+ if (result_type === FORMULA_RESULT_TYPE.DATE) {
587
+ return dateSqlCondition(column, filterItem);
588
+ }
589
+ if (result_type === FORMULA_RESULT_TYPE.NUMBER) {
590
+ return numberSqlCondition(column, filterItem);
591
+ }
592
+ if (result_type === FORMULA_RESULT_TYPE.ARRAY) {
593
+ return arrayColumnCondition(column, filterItem);
594
+ }
595
+ return '';
596
+ };
597
+ var getSqlConditionByFilter = function getSqlConditionByFilter(column, filterItem, username, userId, userDepartmentIdsMap) {
598
+ var type = column.type;
599
+ if (type === CellType.TEXT || type === CellType.AUTO_NUMBER || type === CellType.EMAIL || type === CellType.GEOLOCATION || type === CellType.URL) {
600
+ return textSqlCondition(column, filterItem, userId);
601
+ }
602
+ if (type === CellType.DURATION || type === CellType.NUMBER || type === CellType.RATE) {
603
+ return numberSqlCondition(column, filterItem);
604
+ }
605
+ if (type === CellType.CHECKBOX) {
606
+ return checkboxSqlCondition(column, filterItem);
607
+ }
608
+ if (type === CellType.DATE) {
609
+ return dateSqlCondition(column, filterItem);
610
+ }
611
+ if (type === CellType.CTIME || type === CellType.MTIME) {
612
+ return ctimeSqlCondition(column, filterItem);
613
+ }
614
+ if (type === CellType.SINGLE_SELECT) {
615
+ return singleSelectSqlCondition(column, filterItem);
616
+ }
617
+ if (type === CellType.DEPARTMENT_SINGLE_SELECT) {
618
+ return departmentSingleSelectSqlCondition(column, filterItem, userDepartmentIdsMap);
619
+ }
620
+ if (type === CellType.MULTIPLE_SELECT) {
621
+ return multipleSelectSqlCondition(column, filterItem);
622
+ }
623
+ if (type === CellType.LAST_MODIFIER || type === CellType.CREATOR) {
624
+ return creatorSqlCondition(column, filterItem, username);
625
+ }
626
+ if (type === CellType.COLLABORATOR) {
627
+ return collaboratorSqlCondition(column, filterItem, username);
628
+ }
629
+ if (type === CellType.IMAGE || type === CellType.FILE || type === CellType.DIGITAL_SIGN) {
630
+ return fileSqlCondition(column, filterItem);
631
+ }
632
+ if (type === CellType.LONG_TEXT) {
633
+ return longtextSqlCondition(column, filterItem);
634
+ }
635
+ if (type === CellType.LINK) {
636
+ return linkSqlCondition(column, filterItem);
637
+ }
638
+ if (type === CellType.FORMULA || type === CellType.LINK_FORMULA) {
639
+ return formulaSqlCondition(column, filterItem);
640
+ }
641
+ return '';
642
+ };
643
+ var filter2SqlCondition = function filter2SqlCondition(table, view, username, userId, userDepartmentIdsMap) {
644
+ var columns = table.columns;
645
+ var filter_conjunction = view.filter_conjunction,
646
+ filters = view.filters;
647
+ if (!filters || filters.length === 0) {
648
+ return '';
649
+ }
650
+
651
+ // 1. add WHERE condition
652
+ var filterHeader = 'WHERE ';
653
+ var stringList = filters.map(function (filterItem) {
654
+ var _ValidateFilter$valid = ValidateFilter.validate(filterItem, columns),
655
+ error_message = _ValidateFilter$valid.error_message;
656
+ if (error_message) {
657
+ if (error_message === FILTER_ERR_MSG.INCOMPLETE_FILTER) {
658
+ return '';
659
+ }
660
+ throw new Error(error_message);
661
+ }
662
+ var column_key = filterItem.column_key;
663
+ var column = getTableColumnByKey(table, column_key);
664
+ return getSqlConditionByFilter(column, filterItem, username, userId, userDepartmentIdsMap);
665
+ });
666
+
667
+ // 2. filtered '' sql clause
668
+ stringList = stringList.filter(function (item) {
669
+ return item && item.length > 0;
670
+ });
671
+ if (stringList.length === 0) return '';
672
+
673
+ // 3. combine all sql clause
674
+ // const filterContent = stringList.join(' ' + filter_conjunction + ' ');
675
+ var filterContent = stringList[0];
676
+ for (var i = 1; i < stringList.length; i++) {
677
+ filterContent += " ".concat(filter_conjunction, " (").concat(stringList[i], ")");
678
+ }
679
+ return filterHeader + filterContent;
680
+ };
681
+
682
+ export { checkboxSqlCondition, collaboratorSqlCondition, creatorSqlCondition, ctimeSqlCondition, dateSqlCondition, departmentSingleSelectSqlCondition, fileSqlCondition, filter2SqlCondition, formulaSqlCondition, getSqlConditionByFilter, linkSqlCondition, longtextSqlCondition, multipleSelectSqlCondition, numberSqlCondition, singleSelectSqlCondition, textSqlCondition };