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