drapcode-utility 1.0.0

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.
Files changed (39) hide show
  1. package/build/encryption/index.d.ts +15 -0
  2. package/build/encryption/index.js +255 -0
  3. package/build/errors/app-error.d.ts +6 -0
  4. package/build/errors/app-error.js +30 -0
  5. package/build/errors/axios-error.d.ts +13 -0
  6. package/build/errors/axios-error.js +78 -0
  7. package/build/errors/bad-request-error.d.ts +9 -0
  8. package/build/errors/bad-request-error.js +32 -0
  9. package/build/errors/custom-error.d.ts +8 -0
  10. package/build/errors/custom-error.js +26 -0
  11. package/build/errors/not-found.d.ts +8 -0
  12. package/build/errors/not-found.js +31 -0
  13. package/build/index.d.ts +17 -0
  14. package/build/index.js +29 -0
  15. package/build/middlewares/api-limiter.d.ts +1 -0
  16. package/build/middlewares/api-limiter.js +21 -0
  17. package/build/middlewares/error-logger.d.ts +3 -0
  18. package/build/middlewares/error-logger.js +74 -0
  19. package/build/middlewares/interceptor-logger-new.d.ts +2 -0
  20. package/build/middlewares/interceptor-logger-new.js +54 -0
  21. package/build/middlewares/interceptor-logger.d.ts +2 -0
  22. package/build/middlewares/interceptor-logger.js +42 -0
  23. package/build/middlewares/redis/request-log.d.ts +1 -0
  24. package/build/middlewares/redis/request-log.js +79 -0
  25. package/build/utils/check-error.d.ts +9 -0
  26. package/build/utils/check-error.js +188 -0
  27. package/build/utils/date-util.d.ts +8 -0
  28. package/build/utils/date-util.js +104 -0
  29. package/build/utils/prepare-query.d.ts +132 -0
  30. package/build/utils/prepare-query.js +94 -0
  31. package/build/utils/query-parser.d.ts +11 -0
  32. package/build/utils/query-parser.js +778 -0
  33. package/build/utils/query-paser-new.d.ts +1 -0
  34. package/build/utils/query-paser-new.js +682 -0
  35. package/build/utils/util.d.ts +27 -0
  36. package/build/utils/util.js +373 -0
  37. package/build/utils/uuid-generator.d.ts +1 -0
  38. package/build/utils/uuid-generator.js +65 -0
  39. package/package.json +47 -0
@@ -0,0 +1,682 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (_) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ var __spreadArrays = (this && this.__spreadArrays) || function () {
39
+ for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
40
+ for (var r = Array(s), k = 0, i = 0; i < il; i++)
41
+ for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
42
+ r[k] = a[j];
43
+ return r;
44
+ };
45
+ Object.defineProperty(exports, "__esModule", { value: true });
46
+ exports.queryParserNew = void 0;
47
+ var drapcode_constant_1 = require("drapcode-constant");
48
+ var date_util_1 = require("./date-util");
49
+ var util_1 = require("./util");
50
+ var queryParserNew = function (collectionName, query, constants, externalParams, currentUser, timezone, searchObj, refCollectionFieldsInItems, searchQueryTypeObj) {
51
+ if (currentUser === void 0) { currentUser = {}; }
52
+ if (searchObj === void 0) { searchObj = null; }
53
+ if (refCollectionFieldsInItems === void 0) { refCollectionFieldsInItems = null; }
54
+ if (searchQueryTypeObj === void 0) { searchQueryTypeObj = {}; }
55
+ return __awaiter(void 0, void 0, void 0, function () {
56
+ var filterQuery, aggregateQuery, queryStr, searchQueryObj_1, finalQuery, likeQuery_1, projection, finder, sortBy, orderBy, aggregateFunctionField, offset, limit, direction, str;
57
+ var _a;
58
+ return __generator(this, function (_b) {
59
+ switch (_b.label) {
60
+ case 0:
61
+ if (!collectionName)
62
+ return [2 /*return*/, ""];
63
+ if (!query)
64
+ return [2 /*return*/, ""];
65
+ console.log("***1*** queryParserNew ***");
66
+ filterQuery = mongoFilterQuery(query, externalParams, constants, currentUser);
67
+ console.log("**************************");
68
+ console.log("**************************");
69
+ console.log("filterQuery", filterQuery);
70
+ console.log("==> searchQueryTypeObj :>>", searchQueryTypeObj);
71
+ aggregateQuery = [];
72
+ queryStr = ".aggregate([])";
73
+ if (filterQuery.length &&
74
+ ((filterQuery[0].$or && filterQuery[0].$or.length) ||
75
+ (filterQuery[0].$and && filterQuery[0].$and.length))) {
76
+ searchQueryObj_1 = null;
77
+ if (searchObj) {
78
+ searchQueryObj_1 = [];
79
+ Object.entries(searchObj).forEach(function (_a) {
80
+ var _b, _c, _d;
81
+ var key = _a[0], value = _a[1];
82
+ if (value) {
83
+ if (searchQueryTypeObj[key]) {
84
+ aggregateQuery.push({
85
+ $addFields: (_b = {},
86
+ _b["_" + key] = { $toString: "$" + key },
87
+ _b),
88
+ });
89
+ searchQueryObj_1.push((_c = {},
90
+ _c["_" + key] = {
91
+ $regex: value,
92
+ $options: "i",
93
+ },
94
+ _c));
95
+ }
96
+ else {
97
+ searchQueryObj_1.push((_d = {},
98
+ _d[key] = {
99
+ $regex: value,
100
+ $options: "i",
101
+ },
102
+ _d));
103
+ }
104
+ }
105
+ });
106
+ }
107
+ finalQuery = { $and: filterQuery };
108
+ if (searchQueryObj_1.length) {
109
+ finalQuery.$and.push([searchQueryObj_1]);
110
+ }
111
+ aggregateQuery.push({ $match: finalQuery });
112
+ }
113
+ else if (searchObj) {
114
+ likeQuery_1 = [];
115
+ Object.entries(searchObj).forEach(function (_a) {
116
+ var _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
117
+ var key = _a[0], value = _a[1];
118
+ if (value) {
119
+ if (searchQueryTypeObj[key]) {
120
+ if (searchQueryTypeObj[key] === "boolean") {
121
+ // if input is string and db field type is boolean
122
+ aggregateQuery.push({
123
+ $addFields: (_b = {},
124
+ _b["_" + key] = "$" + key,
125
+ _b),
126
+ });
127
+ likeQuery_1.push((_c = {},
128
+ _c["_" + key] = toBoolean(value),
129
+ _c));
130
+ }
131
+ else if (["static_option", "dynamic_option", "reference", "belongsTo"].includes(searchQueryTypeObj[key])) {
132
+ // if input is string and db field type is array
133
+ aggregateQuery.push({
134
+ $addFields: (_d = {},
135
+ _d["_" + key] = "$" + key,
136
+ _d),
137
+ });
138
+ likeQuery_1.push((_e = {},
139
+ _e["_" + key] = { $all: [value] },
140
+ _e));
141
+ }
142
+ else if (searchQueryTypeObj[key] === "tel") {
143
+ // handling value when db field type is tel
144
+ aggregateQuery.push({
145
+ $addFields: (_f = {},
146
+ _f["_" + key] = { $toString: "$" + key },
147
+ _f),
148
+ });
149
+ likeQuery_1.push((_g = {},
150
+ _g["_" + key] = {
151
+ $regex: value.startsWith("+") ? value.replace("+", "") : value,
152
+ $options: "i",
153
+ },
154
+ _g));
155
+ }
156
+ else if (searchQueryTypeObj[key] === "number") {
157
+ // handling value when db field type is Double/Int
158
+ aggregateQuery.push({
159
+ $addFields: (_h = {},
160
+ _h["_" + key] = { $toString: { $toLong: "$" + key } },
161
+ _h),
162
+ });
163
+ likeQuery_1.push((_j = {},
164
+ _j["_" + key] = {
165
+ $regex: value,
166
+ $options: "i",
167
+ },
168
+ _j));
169
+ }
170
+ else {
171
+ // if input is string and db field type is number
172
+ // converting db field to string first
173
+ aggregateQuery.push({
174
+ $addFields: (_k = {},
175
+ _k["_" + key] = { $toString: "$" + key },
176
+ _k),
177
+ });
178
+ likeQuery_1.push((_l = {},
179
+ _l["_" + key] = {
180
+ $regex: value,
181
+ $options: "i",
182
+ },
183
+ _l));
184
+ }
185
+ }
186
+ else {
187
+ likeQuery_1.push((_m = {},
188
+ _m[key] = {
189
+ $regex: value,
190
+ $options: "i",
191
+ },
192
+ _m));
193
+ }
194
+ }
195
+ });
196
+ console.log("==> aggregateQuery", aggregateQuery);
197
+ console.log("==> likeQuery", likeQuery_1);
198
+ if (likeQuery_1.length) {
199
+ // aggregateQuery.push({ $match: { $or: likeQuery } });
200
+ aggregateQuery.push({ $match: { $and: likeQuery_1 } });
201
+ }
202
+ }
203
+ console.log("==> query", query);
204
+ projection = mongoSelectQuery(query);
205
+ finder = query.finder, sortBy = query.sortBy, orderBy = query.orderBy, aggregateFunctionField = query.aggregateFunctionField;
206
+ if (!(finder != "COUNT" && refCollectionFieldsInItems)) return [3 /*break*/, 2];
207
+ return [4 /*yield*/, Promise.all(refCollectionFieldsInItems.map(function (field) {
208
+ var _a, _b, _c;
209
+ if (["reference", "multi_reference", "belongsTo"].includes(field.type)) {
210
+ var collectionName_1 = field.refCollection
211
+ ? field.refCollection["collectionName"]
212
+ : null;
213
+ if (collectionName_1) {
214
+ if (field.type === "belongsTo") {
215
+ aggregateQuery.push({
216
+ $lookup: {
217
+ from: "" + collectionName_1,
218
+ localField: field.fieldName,
219
+ foreignField: "uuid",
220
+ as: field.fieldName,
221
+ },
222
+ });
223
+ aggregateQuery.push({
224
+ $addFields: (_a = {},
225
+ _a["_$belongsToMetaData"] = field,
226
+ _a),
227
+ });
228
+ }
229
+ else {
230
+ aggregateQuery.push({
231
+ $lookup: {
232
+ from: "" + collectionName_1,
233
+ let: (_b = {}, _b["" + field.fieldName] = "$" + field.fieldName, _b),
234
+ pipeline: [
235
+ {
236
+ $match: {
237
+ $expr: {
238
+ $in: [
239
+ "$uuid",
240
+ {
241
+ $cond: {
242
+ if: {
243
+ $in: ["$$" + field.fieldName, ["", null]],
244
+ },
245
+ then: [],
246
+ else: { $ifNull: ["$$" + field.fieldName, []] },
247
+ },
248
+ },
249
+ ],
250
+ },
251
+ },
252
+ },
253
+ {
254
+ $lookup: {
255
+ from: "user",
256
+ let: { createdBy: "$createdBy" },
257
+ pipeline: [
258
+ {
259
+ $match: {
260
+ $expr: { $eq: ["$uuid", "$$createdBy"] },
261
+ },
262
+ },
263
+ { $project: { _id: 0, password: 0 } },
264
+ ],
265
+ as: "createdBy",
266
+ },
267
+ },
268
+ ],
269
+ as: field.fieldName,
270
+ },
271
+ });
272
+ }
273
+ }
274
+ }
275
+ if (field.type === "createdBy")
276
+ aggregateQuery.push({
277
+ $lookup: {
278
+ from: "user",
279
+ let: (_c = {}, _c["" + field.fieldName] = "$" + field.fieldName, _c),
280
+ pipeline: [
281
+ {
282
+ $match: { $expr: { $eq: ["$uuid", "$$" + field.fieldName] } },
283
+ },
284
+ { $project: { _id: 0, password: 0 } },
285
+ ],
286
+ as: field.fieldName,
287
+ },
288
+ });
289
+ }))];
290
+ case 1:
291
+ _b.sent();
292
+ _b.label = 2;
293
+ case 2:
294
+ if (!["COUNT", "SUM", "AVG", "MIN", "MAX"].includes(finder) &&
295
+ !util_1.isEmpty(projection)) {
296
+ aggregateQuery.push({ $project: projection });
297
+ }
298
+ if (finder === "COUNT") {
299
+ aggregateQuery.push({ $group: { _id: null, count: { $sum: 1 } } });
300
+ }
301
+ else if (finder === "SUM") {
302
+ aggregateQuery.push({
303
+ $group: { _id: null, total: { $sum: "$" + aggregateFunctionField } },
304
+ });
305
+ }
306
+ else if (finder === "AVG") {
307
+ aggregateQuery.push({
308
+ $group: { _id: null, average: { $avg: "$" + aggregateFunctionField } },
309
+ });
310
+ }
311
+ else if (finder === "MIN") {
312
+ aggregateQuery.push({
313
+ $group: { _id: null, minimum: { $min: "$" + aggregateFunctionField } },
314
+ });
315
+ }
316
+ else if (finder === "MAX") {
317
+ aggregateQuery.push({
318
+ $group: { _id: null, maximum: { $max: "$" + aggregateFunctionField } },
319
+ });
320
+ }
321
+ offset = externalParams.offset || 0;
322
+ limit = externalParams.limit || 10;
323
+ if (externalParams.pagination == "false") {
324
+ limit = 0;
325
+ sortBy = 0;
326
+ }
327
+ if (finder != "COUNT" && sortBy) {
328
+ direction = orderBy && orderBy == "asc" ? 1 : -1;
329
+ aggregateQuery.push({ $sort: (_a = {}, _a[sortBy] = direction, _a) });
330
+ }
331
+ if (finder != "COUNT" && limit) {
332
+ aggregateQuery.push({ $skip: +offset }, { $limit: +limit });
333
+ }
334
+ console.log("==> aggregateQuery", JSON.stringify(aggregateQuery, null, 2));
335
+ queryStr = ".aggregate(" + JSON.stringify(aggregateQuery) + ", { collation: { locale: \"en\" } })";
336
+ str = "req.db.collection('" + collectionName + "')" + queryStr;
337
+ str += ".toArray()";
338
+ return [2 /*return*/, str];
339
+ }
340
+ });
341
+ });
342
+ };
343
+ exports.queryParserNew = queryParserNew;
344
+ var toBoolean = function (value) {
345
+ if (!value) {
346
+ return false;
347
+ }
348
+ switch (value.toLocaleLowerCase()) {
349
+ case "true":
350
+ case "1":
351
+ case "on":
352
+ case "yes":
353
+ return true;
354
+ default:
355
+ return false;
356
+ }
357
+ };
358
+ var mongoFilterQuery = function (query, externalParams, constants, currentUser) {
359
+ if (currentUser === void 0) { currentUser = {}; }
360
+ var filterQuery = { $or0: [] };
361
+ var conditions = query.conditions;
362
+ var queryConjunctions = [];
363
+ conditions.forEach(function (queryObj, index) {
364
+ var query = queryObj.query, requiredExternal = queryObj.requiredExternal;
365
+ var mongoQuery = queryToMongo(query, requiredExternal, externalParams, constants, currentUser);
366
+ queryConjunctions.push(queryObj.conjunctionType);
367
+ if (queryObj.conjunctionType == "OR") {
368
+ var lastKey = Object.keys(filterQuery)[Object.keys(filterQuery).length - 1];
369
+ if (filterQuery[lastKey] && Array.isArray(filterQuery[lastKey])) {
370
+ filterQuery[lastKey].push(mongoQuery);
371
+ }
372
+ else {
373
+ filterQuery["$or" + index] = [mongoQuery];
374
+ }
375
+ }
376
+ else if (queryObj.conjunctionType == "AND") {
377
+ var lastKey = Object.keys(filterQuery)[Object.keys(filterQuery).length - 1];
378
+ if (filterQuery[lastKey] && Array.isArray(filterQuery[lastKey])) {
379
+ filterQuery[lastKey].push(mongoQuery);
380
+ }
381
+ else {
382
+ filterQuery["$and" + index] = [mongoQuery];
383
+ }
384
+ }
385
+ else {
386
+ filterQuery["$or" + index] = [mongoQuery];
387
+ }
388
+ });
389
+ console.log("==> *** filterQuery #2 :>> ", filterQuery);
390
+ var hasAndQueryConjunction = queryConjunctions.includes("AND");
391
+ return modifyQuery(filterQuery, hasAndQueryConjunction);
392
+ };
393
+ var mongoSelectQuery = function (query) {
394
+ if (query === void 0) { query = {}; }
395
+ var projection = {};
396
+ var fieldsInclude = query.fieldsInclude, fieldsExclude = query.fieldsExclude;
397
+ if (!fieldsExclude)
398
+ fieldsExclude = [];
399
+ if (fieldsInclude[0] === "*") {
400
+ fieldsExclude.map(function (field) {
401
+ if (field)
402
+ return (projection["" + field] = 0);
403
+ });
404
+ }
405
+ else {
406
+ fieldsInclude.map(function (field) {
407
+ if (field)
408
+ return (projection["" + field] = 1);
409
+ });
410
+ }
411
+ return projection;
412
+ };
413
+ var modifyQuery = function (obj, hasAndQueryConjunction) {
414
+ if (obj === void 0) { obj = {}; }
415
+ if (hasAndQueryConjunction === void 0) { hasAndQueryConjunction = false; }
416
+ var arr = [];
417
+ console.log("🚀 ~ file: query-parser-new.ts:294 ~ modifyQuery ~ hasAndQueryConjunction:", hasAndQueryConjunction);
418
+ Object.entries(obj).forEach(function (_a) {
419
+ var key = _a[0], value = _a[1];
420
+ if (hasAndQueryConjunction) {
421
+ arr.push({ $and: value });
422
+ }
423
+ else {
424
+ arr.push({ $or: value });
425
+ }
426
+ });
427
+ console.log("🚀 ~ file: query-parser-new.ts:303 ~ Object.entries ~ arr:", arr);
428
+ return arr;
429
+ };
430
+ var queryToMongo = function (query, requiredExternal, externalParams, constants, currentUser) {
431
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z;
432
+ if (currentUser === void 0) { currentUser = {}; }
433
+ var fieldValue = replaceExternalParams(query, requiredExternal, externalParams, constants, currentUser);
434
+ var isDate = checkDate(fieldValue);
435
+ var key = query.key, field = query.field, value = query.value, fieldType = query.fieldType;
436
+ if (isDate)
437
+ fieldValue = new Date(fieldValue);
438
+ var condition = ["dynamic_option", "reference", "static_option"].includes(fieldType);
439
+ if (condition && key === drapcode_constant_1.EQUALS && !isDate) {
440
+ if (!Array.isArray(fieldValue)) {
441
+ fieldValue = [fieldValue];
442
+ }
443
+ return _a = {}, _a[field] = { $in: fieldValue || [] }, _a;
444
+ }
445
+ if (condition && key === drapcode_constant_1.NOT_EQUAL && !isDate) {
446
+ if (!Array.isArray(fieldValue)) {
447
+ fieldValue = [fieldValue];
448
+ }
449
+ return _b = {}, _b[field] = { $nin: fieldValue || [] }, _b;
450
+ }
451
+ if (condition && key === drapcode_constant_1.IS_NULL) {
452
+ return _c = {}, _c[field] = { $type: "array", $eq: [] }, _c;
453
+ }
454
+ if (condition && key === drapcode_constant_1.IS_NOT_NULL) {
455
+ return _d = {}, _d[field] = { $exists: true, $type: "array", $ne: [] }, _d;
456
+ }
457
+ if (key === drapcode_constant_1.IS_NOT_NULL)
458
+ return _e = {}, _e[field] = { $nin: [null, ""] }, _e;
459
+ if (key === drapcode_constant_1.IS_NULL)
460
+ return _f = {}, _f[field] = { $in: [null, ""] }, _f;
461
+ if (key === drapcode_constant_1.LIKE)
462
+ return _g = {}, _g[field] = { $regex: fieldValue, $options: "i" }, _g;
463
+ if (key === drapcode_constant_1.GREATER_THAN_EQUALS_TO)
464
+ return _h = {}, _h[field] = { $gte: isDate ? fieldValue : +fieldValue }, _h;
465
+ if (key === drapcode_constant_1.LESS_THAN_EQUALS_TO)
466
+ return _j = {}, _j[field] = { $lte: isDate ? fieldValue : +fieldValue }, _j;
467
+ if (key === drapcode_constant_1.LESS_THAN)
468
+ return _k = {}, _k[field] = { $lt: isDate ? fieldValue : +fieldValue }, _k;
469
+ if (key === drapcode_constant_1.GREATER_THAN)
470
+ return _l = {}, _l[field] = { $gt: isDate ? fieldValue : +fieldValue }, _l;
471
+ if (key === drapcode_constant_1.IN_LIST) {
472
+ if (!Array.isArray(fieldValue)) {
473
+ fieldValue = [fieldValue];
474
+ }
475
+ return _m = {}, _m[field] = { $in: fieldValue || [] }, _m;
476
+ }
477
+ if (key === drapcode_constant_1.NOT_IN_LIST) {
478
+ if (!Array.isArray(fieldValue)) {
479
+ fieldValue = [fieldValue];
480
+ }
481
+ return _o = {}, _o[field] = { $nin: fieldValue || [] }, _o;
482
+ }
483
+ if (key === drapcode_constant_1.EQUALS && isDate) {
484
+ if (drapcode_constant_1.DateConstant.some(function (cnst) { return value.includes(cnst); }))
485
+ return _p = {},
486
+ _p[field] = {
487
+ $gte: fieldValue,
488
+ },
489
+ _p;
490
+ else
491
+ return _q = {},
492
+ _q[field] = {
493
+ $gte: fieldValue,
494
+ $lt: new Date(date_util_1.nextDayDate(fieldValue)),
495
+ },
496
+ _q;
497
+ }
498
+ if (key == drapcode_constant_1.EQUALS && field === "createdBy")
499
+ return {
500
+ $and: [
501
+ (_r = {}, _r[field] = { $ne: null }, _r),
502
+ (_s = {}, _s[field] = { $ne: "" }, _s),
503
+ (_t = {}, _t[field] = { $eq: fieldValue }, _t),
504
+ ],
505
+ };
506
+ if (key == drapcode_constant_1.EQUALS && fieldType === "boolean" && fieldValue)
507
+ return JSON.parse(fieldValue)
508
+ ? (_u = {},
509
+ _u[field] = JSON.parse(fieldValue),
510
+ _u) : {
511
+ $or: [
512
+ (_v = {}, _v[field] = JSON.parse(fieldValue), _v),
513
+ (_w = {}, _w[field] = { $exists: false }, _w),
514
+ ],
515
+ };
516
+ if (key == drapcode_constant_1.EQUALS)
517
+ return _x = {},
518
+ _x[field] = fieldType === "boolean" && fieldValue
519
+ ? JSON.parse(fieldValue)
520
+ : !isNaN(fieldValue)
521
+ ? +fieldValue
522
+ : fieldValue,
523
+ _x;
524
+ if (key == drapcode_constant_1.NOT_EQUAL)
525
+ return _y = {},
526
+ _y[field] = {
527
+ $ne: fieldType === "boolean" && fieldValue
528
+ ? JSON.parse(fieldValue)
529
+ : fieldValue,
530
+ },
531
+ _y;
532
+ if (key == drapcode_constant_1.BETWEEN) {
533
+ var _0 = getMinMaxValue(fieldValue), startValue = _0.startValue, endValue = _0.endValue, isInteger = _0.isInteger;
534
+ return _z = {},
535
+ _z[field] = {
536
+ $gte: isInteger ? +startValue : startValue,
537
+ $lte: isInteger ? +endValue : endValue,
538
+ },
539
+ _z;
540
+ }
541
+ };
542
+ var replaceExternalParams = function (query, requiredExternal, externalParams, constants, currentUser) {
543
+ var key = query.key, value = query.value, fieldType = query.fieldType;
544
+ if (fieldType &&
545
+ !requiredExternal &&
546
+ ["dynamic_option", "static_option", "reference"].includes(fieldType)) {
547
+ return value;
548
+ }
549
+ if (fieldType &&
550
+ fieldType === "boolean" &&
551
+ ["true", "false", "", true, false].includes(value)) {
552
+ return "" + value;
553
+ }
554
+ if (key === drapcode_constant_1.IN_LIST || key === drapcode_constant_1.NOT_IN_LIST) {
555
+ var valueList_1 = [];
556
+ constants.forEach(function (constant) {
557
+ if (value && Array.isArray(value)) {
558
+ value.forEach(function (val) {
559
+ if (val === constant.name) {
560
+ valueList_1.push(constant.value);
561
+ }
562
+ else
563
+ valueList_1.push(val);
564
+ });
565
+ }
566
+ });
567
+ return valueList_1;
568
+ }
569
+ if (key === drapcode_constant_1.BETWEEN) {
570
+ var _a = getMinMaxValue(value), startValue_1 = _a.startValue, endValue_1 = _a.endValue, isInteger = _a.isInteger;
571
+ if (requiredExternal) {
572
+ startValue_1 = "" + externalParams[startValue_1];
573
+ endValue_1 = "" + externalParams[endValue_1];
574
+ }
575
+ else {
576
+ if (__spreadArrays([drapcode_constant_1.CURRENT_USER], drapcode_constant_1.DateConstant).some(function (cnst) {
577
+ return startValue_1.includes(cnst);
578
+ })) {
579
+ startValue_1 = getValueOfProjectConstant(startValue_1, currentUser);
580
+ }
581
+ else {
582
+ startValue_1 = constants.filter(function (constant) { return constant.name == startValue_1; });
583
+ }
584
+ if (__spreadArrays([drapcode_constant_1.CURRENT_USER], drapcode_constant_1.DateConstant).some(function (cnst) { return endValue_1.includes(cnst); })) {
585
+ endValue_1 = getValueOfProjectConstant(endValue_1, currentUser);
586
+ }
587
+ else {
588
+ endValue_1 = constants.filter(function (constant) { return constant.name == endValue_1; });
589
+ }
590
+ }
591
+ //TODO: Need to test constant from collection and when this below if will run
592
+ if (Array.isArray(startValue_1)) {
593
+ startValue_1 = startValue_1[0].value;
594
+ }
595
+ if (Array.isArray(endValue_1)) {
596
+ endValue_1 = endValue_1[0].value;
597
+ }
598
+ //TODO: Need to figure out the params should be treated as number/string
599
+ if (!isNaN(startValue_1)) {
600
+ console.log("I am Number");
601
+ return startValue_1 + "---" + endValue_1 + "^";
602
+ }
603
+ else
604
+ return startValue_1 + "---" + endValue_1;
605
+ }
606
+ console.log("==> value", value);
607
+ console.log("==> currentUser", currentUser);
608
+ if (value.includes(drapcode_constant_1.CURRENT_USER)) {
609
+ if (value === drapcode_constant_1.CURRENT_USER) {
610
+ return currentUser["uuid"];
611
+ }
612
+ var splitValues = value.split("::");
613
+ console.log("splitValues", splitValues);
614
+ var refCollection = splitValues[1];
615
+ var refCollectionField = splitValues[2];
616
+ if (refCollection && refCollectionField) {
617
+ console.log("I have refCollection and Field");
618
+ var refCollectionData = currentUser[refCollection];
619
+ console.log("refCollectionData", refCollectionData);
620
+ if (!refCollectionData) {
621
+ return "";
622
+ }
623
+ if (refCollectionData.length > 1) {
624
+ //TODO: Need to handle multi ref
625
+ return "";
626
+ }
627
+ refCollectionData = refCollectionData[0];
628
+ console.log("refCollectionData[refCollectionField]", refCollectionData[refCollectionField]);
629
+ return refCollectionData[refCollectionField];
630
+ }
631
+ //this means that data has to be match with currnet user field that is not ref type
632
+ if (refCollection) {
633
+ return currentUser[refCollection];
634
+ }
635
+ return currentUser["uuid"];
636
+ }
637
+ if (value === drapcode_constant_1.CURRENT_USER) {
638
+ return currentUser["uuid"];
639
+ }
640
+ if (drapcode_constant_1.DateConstant.some(function (cnst) { return value.includes(cnst); })) {
641
+ return getValueOfProjectConstant(value, {});
642
+ }
643
+ //TODO: This is just to confirm if external then it should be get from externalParams
644
+ if (requiredExternal) {
645
+ //TODO: Value returns from here undefined.
646
+ var externalParamValue = externalParams["" + value];
647
+ if (!isNaN(externalParamValue)) {
648
+ return +externalParamValue;
649
+ }
650
+ return externalParams["" + value];
651
+ }
652
+ var constantArr = constants.filter(function (constant) { return constant.name == value; });
653
+ return constantArr.length ? "" + constantArr[0].value : "";
654
+ };
655
+ var checkDate = function (queryValue) {
656
+ return ("" + queryValue).match(date_util_1.DATE_REGEX);
657
+ };
658
+ var getMinMaxValue = function (value) {
659
+ var isValid = value && value.includes("---");
660
+ if (isValid) {
661
+ var isInteger = value.includes("^");
662
+ var result = value.split("---");
663
+ var endValue = result[1];
664
+ if (isInteger && result.length)
665
+ endValue = endValue.slice(0, -1);
666
+ return { startValue: result[0], endValue: endValue, isInteger: isInteger };
667
+ }
668
+ else {
669
+ return { startValue: "", endValue: "" };
670
+ }
671
+ };
672
+ //TODO: need to fix for all app constant with these
673
+ var getValueOfProjectConstant = function (value, currentUser) {
674
+ if (value === drapcode_constant_1.CURRENT_USER)
675
+ return currentUser["uuid"];
676
+ if (value === drapcode_constant_1.CURRENT_DATE)
677
+ return date_util_1.createLoggerDateFormat();
678
+ if ([drapcode_constant_1.CURRENT_TIME, drapcode_constant_1.CURRENT_DATE_TIME].includes(value))
679
+ return date_util_1.createLogsDateFormat();
680
+ if (drapcode_constant_1.DateTimeUnit.some(function (cnst) { return value.includes(cnst); }))
681
+ return date_util_1.getDateValue(value);
682
+ };