drapcode-utility 2.0.1 → 2.0.2
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.
- package/build/encryption/KMS.js +54 -102
- package/build/encryption/crypt.d.ts +4 -2
- package/build/encryption/crypt.js +76 -91
- package/build/encryption/file.d.ts +0 -2
- package/build/encryption/file.js +14 -130
- package/build/encryption/index.js +162 -334
- package/build/encryption/utility.js +7 -10
- package/build/errors/app-error.js +9 -27
- package/build/errors/axios-error.js +3 -3
- package/build/errors/bad-request-error.js +10 -28
- package/build/errors/custom-error.js +5 -23
- package/build/errors/not-found.js +9 -27
- package/build/format-fields/index.d.ts +0 -1
- package/build/format-fields/index.js +32 -65
- package/build/index.d.ts +1 -4
- package/build/index.js +1 -4
- package/build/middlewares/error-logger.d.ts +1 -1
- package/build/middlewares/error-logger.js +29 -29
- package/build/middlewares/redis/request-log.js +24 -74
- package/build/query/queryBuilder.d.ts +9 -0
- package/build/query/queryBuilder.js +567 -0
- package/build/utils/check-error.d.ts +15 -8
- package/build/utils/check-error.js +71 -160
- package/build/utils/common-util.d.ts +40 -39
- package/build/utils/common-util.js +60 -59
- package/build/utils/date-util.d.ts +28 -7
- package/build/utils/date-util.js +180 -127
- package/build/utils/file-util.d.ts +51 -6
- package/build/utils/file-util.js +36 -40
- package/build/utils/prepare-query.js +70 -43
- package/build/utils/project-util.d.ts +43 -5
- package/build/utils/project-util.js +176 -121
- package/build/utils/query-parser.d.ts +1 -1
- package/build/utils/query-parser.js +289 -342
- package/build/utils/query-utils.d.ts +2 -2
- package/build/utils/query-utils.js +103 -116
- package/build/utils/rest-client.js +236 -328
- package/build/utils/s3-util.js +238 -469
- package/build/utils/token.js +34 -81
- package/build/utils/util.d.ts +58 -13
- package/build/utils/util.js +424 -494
- package/build/utils/uuid-generator.d.ts +20 -1
- package/build/utils/uuid-generator.js +111 -47
- package/package.json +7 -5
- package/build/middlewares/interceptor-logger-new.d.ts +0 -2
- package/build/middlewares/interceptor-logger-new.js +0 -53
- package/build/middlewares/interceptor-logger.d.ts +0 -2
- package/build/middlewares/interceptor-logger.js +0 -52
- package/build/utils/query-parser-new.d.ts +0 -1
- package/build/utils/query-parser-new.js +0 -541
|
@@ -0,0 +1,567 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isEntityCondition = exports.queryBuilder = void 0;
|
|
4
|
+
const drapcode_constant_1 = require("drapcode-constant");
|
|
5
|
+
const query_utils_1 = require("../utils/query-utils");
|
|
6
|
+
const date_util_1 = require("../utils/date-util");
|
|
7
|
+
const prepare_query_1 = require("../utils/prepare-query");
|
|
8
|
+
const util_1 = require("../utils/util");
|
|
9
|
+
const noFieldString = "random_string_since_field_not_present_1318874398806";
|
|
10
|
+
const { boolean, number } = drapcode_constant_1.FieldTypes;
|
|
11
|
+
const isEqualOrIgnore = [drapcode_constant_1.EQUALS, drapcode_constant_1.EQUALS_OR_IGNORE];
|
|
12
|
+
const queryBuilder = async (collectionName, query, constants, externalParams, authDetail, timezone, searchObj = null, refCollectionFieldsInItems = null, searchQueryTypeObj = {}, lookupConfig = [], rowLevelSecurityFilter) => {
|
|
13
|
+
if (!collectionName || !query)
|
|
14
|
+
return "";
|
|
15
|
+
let finalQuery = { $and: [] };
|
|
16
|
+
// Apply both filters using the helper
|
|
17
|
+
applyFilterQuery(query, finalQuery, externalParams, constants, timezone, authDetail);
|
|
18
|
+
console.log("After main query check: ", JSON.stringify(finalQuery));
|
|
19
|
+
finalQuery = simplifyMongoQuery(finalQuery);
|
|
20
|
+
console.log("After main simplifyMongoQuery: ", JSON.stringify(finalQuery));
|
|
21
|
+
applyFilterQuery(rowLevelSecurityFilter, finalQuery, externalParams, constants, timezone, authDetail);
|
|
22
|
+
console.log("After RLS query check: ", JSON.stringify(finalQuery));
|
|
23
|
+
finalQuery = simplifyMongoQuery(finalQuery);
|
|
24
|
+
console.log("After RLS simplifyMongoQuery: ", JSON.stringify(finalQuery));
|
|
25
|
+
console.log(`==> searchQueryTypeObj :>>`, searchQueryTypeObj);
|
|
26
|
+
let aggregateQuery = [];
|
|
27
|
+
let searchQueryObj = null;
|
|
28
|
+
if (searchObj) {
|
|
29
|
+
const { searchAggregateQuery, likeQuery } = (0, query_utils_1.getSearchObjQuery)(searchObj, searchQueryTypeObj);
|
|
30
|
+
aggregateQuery = [...aggregateQuery, ...searchAggregateQuery];
|
|
31
|
+
if (likeQuery && likeQuery.length > 0) {
|
|
32
|
+
searchQueryObj = likeQuery;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
console.log("1");
|
|
36
|
+
if (searchQueryObj) {
|
|
37
|
+
finalQuery.$and.push(...searchQueryObj);
|
|
38
|
+
}
|
|
39
|
+
console.log("2");
|
|
40
|
+
const { tenant, user } = authDetail;
|
|
41
|
+
const { addTenantQuery, addSubTenantQuery } = query;
|
|
42
|
+
const additionalFilters = [];
|
|
43
|
+
if (addTenantQuery && tenant?.uuid) {
|
|
44
|
+
additionalFilters.push({ tenantId: tenant.uuid });
|
|
45
|
+
}
|
|
46
|
+
console.log("3");
|
|
47
|
+
if (addSubTenantQuery &&
|
|
48
|
+
Array.isArray(user.subTenantId) &&
|
|
49
|
+
user.subTenantId.length > 0) {
|
|
50
|
+
console.log("4");
|
|
51
|
+
const subTenantUuids = user.subTenantId
|
|
52
|
+
.map((item) => item?.uuid)
|
|
53
|
+
.filter((uuid) => uuid); // Filters out undefined/null
|
|
54
|
+
console.log("5");
|
|
55
|
+
if (subTenantUuids && subTenantUuids.length > 0) {
|
|
56
|
+
additionalFilters.push({ subTenantId: { $in: subTenantUuids } });
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
console.log("6");
|
|
60
|
+
if (additionalFilters.length > 0) {
|
|
61
|
+
finalQuery.$and.push(...additionalFilters);
|
|
62
|
+
}
|
|
63
|
+
console.log("7");
|
|
64
|
+
//Remove empty and in final query
|
|
65
|
+
if (!finalQuery?.$and?.length) {
|
|
66
|
+
delete finalQuery.$and;
|
|
67
|
+
}
|
|
68
|
+
console.log("8");
|
|
69
|
+
if (finalQuery && Object.keys(finalQuery).length) {
|
|
70
|
+
aggregateQuery.push({ $match: finalQuery });
|
|
71
|
+
}
|
|
72
|
+
let projection = (0, query_utils_1.mongoSelectQuery)(query);
|
|
73
|
+
let { finder, sortBy, orderBy, aggregateFunctionField } = query;
|
|
74
|
+
if (finder != "COUNT" && refCollectionFieldsInItems) {
|
|
75
|
+
await Promise.all(refCollectionFieldsInItems.map((field) => (0, prepare_query_1.commonLookupSetting)(field, lookupConfig, aggregateQuery)));
|
|
76
|
+
}
|
|
77
|
+
if (!util_1.nonFindQuery.includes(finder) && !(0, util_1.isEmpty)(projection)) {
|
|
78
|
+
aggregateQuery.push({ $project: projection });
|
|
79
|
+
}
|
|
80
|
+
if (finder === "COUNT") {
|
|
81
|
+
aggregateQuery.push({ $group: { _id: null, count: { $sum: 1 } } });
|
|
82
|
+
}
|
|
83
|
+
else if (finder === "SUM") {
|
|
84
|
+
aggregateQuery.push({
|
|
85
|
+
$group: { _id: null, total: { $sum: `$${aggregateFunctionField}` } },
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
else if (finder === "AVG") {
|
|
89
|
+
aggregateQuery.push({
|
|
90
|
+
$group: { _id: null, average: { $avg: `$${aggregateFunctionField}` } },
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
else if (finder === "MIN") {
|
|
94
|
+
aggregateQuery.push({
|
|
95
|
+
$group: { _id: null, minimum: { $min: `$${aggregateFunctionField}` } },
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
else if (finder === "MAX") {
|
|
99
|
+
aggregateQuery.push({
|
|
100
|
+
$group: { _id: null, maximum: { $max: `$${aggregateFunctionField}` } },
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
let offset = externalParams.offset || 0;
|
|
104
|
+
let limit = externalParams.limit || 20000;
|
|
105
|
+
let resetLimit = externalParams.resetLimit || false;
|
|
106
|
+
if (externalParams.pagination == "false") {
|
|
107
|
+
limit = 0;
|
|
108
|
+
sortBy = 0;
|
|
109
|
+
}
|
|
110
|
+
// Reset limit value to 1 in case filter is of type FIND or when it's a COUNT finder for FIND filter
|
|
111
|
+
if (finder === "FIND" || (util_1.nonFindQuery.includes(finder) && resetLimit)) {
|
|
112
|
+
offset = 0;
|
|
113
|
+
limit = 1;
|
|
114
|
+
}
|
|
115
|
+
if (!util_1.nonFindQuery.includes(finder) && sortBy) {
|
|
116
|
+
let direction = orderBy && orderBy == "asc" ? 1 : -1;
|
|
117
|
+
aggregateQuery.push({ $sort: { [sortBy]: direction, _id: 1 } });
|
|
118
|
+
}
|
|
119
|
+
if (!util_1.nonFindQuery.includes(finder) && limit) {
|
|
120
|
+
aggregateQuery.push({ $skip: +offset }, { $limit: +limit });
|
|
121
|
+
}
|
|
122
|
+
return aggregateQuery;
|
|
123
|
+
};
|
|
124
|
+
exports.queryBuilder = queryBuilder;
|
|
125
|
+
const simplifyMongoQuery = (query) => {
|
|
126
|
+
if (typeof query !== "object" || query === null)
|
|
127
|
+
return query;
|
|
128
|
+
const keys = Object.keys(query);
|
|
129
|
+
// Recursively simplify nested queries
|
|
130
|
+
for (const key of keys) {
|
|
131
|
+
query[key] = Array.isArray(query[key])
|
|
132
|
+
? query[key].map(simplifyMongoQuery)
|
|
133
|
+
: simplifyMongoQuery(query[key]);
|
|
134
|
+
}
|
|
135
|
+
if (keys.length === 1 && (keys[0] === "$and" || keys[0] === "$or")) {
|
|
136
|
+
const op = keys[0];
|
|
137
|
+
const conditions = query[op];
|
|
138
|
+
// Flatten if nested same operator inside
|
|
139
|
+
const flattened = conditions.flatMap((condition) => {
|
|
140
|
+
if (condition && condition[op]) {
|
|
141
|
+
return condition[op];
|
|
142
|
+
}
|
|
143
|
+
return [condition];
|
|
144
|
+
});
|
|
145
|
+
// If only one condition, replace with that condition
|
|
146
|
+
if (flattened.length === 1) {
|
|
147
|
+
return simplifyMongoQuery(flattened[0]);
|
|
148
|
+
}
|
|
149
|
+
return { [op]: flattened };
|
|
150
|
+
}
|
|
151
|
+
return query;
|
|
152
|
+
};
|
|
153
|
+
const applyFilterQuery = (filter, finalQuery, externalParams, constants, timezone, authDetail) => {
|
|
154
|
+
const generatedQuery = createFilterQuery(filter, externalParams, constants, timezone, authDetail);
|
|
155
|
+
console.log("**************************");
|
|
156
|
+
if (generatedQuery)
|
|
157
|
+
finalQuery.$and.push(generatedQuery);
|
|
158
|
+
// const first = generatedQuery;
|
|
159
|
+
// const hasOr = first?.$or?.length > 0;
|
|
160
|
+
// const hasAnd = first?.$and?.length > 0;
|
|
161
|
+
// if (generatedQuery.length && (hasOr || hasAnd)) {
|
|
162
|
+
// finalQuery.$and.push(...generatedQuery);
|
|
163
|
+
// }
|
|
164
|
+
};
|
|
165
|
+
const createFilterQuery = (query, externalParams, constants, timezone, authDetail) => {
|
|
166
|
+
if (!query || !query.conditions.length)
|
|
167
|
+
return null;
|
|
168
|
+
const { conditions } = query;
|
|
169
|
+
const queryConjunctions = [];
|
|
170
|
+
const filterQuery = { $or0: [] };
|
|
171
|
+
const finalConditions = [];
|
|
172
|
+
conditions.forEach((queryObj, index) => {
|
|
173
|
+
const { query, requiredExternal, conjunctionType } = queryObj;
|
|
174
|
+
let mongoQuery = conditionToQuery(query, requiredExternal, externalParams, constants, timezone, authDetail);
|
|
175
|
+
if (mongoQuery) {
|
|
176
|
+
finalConditions.push({ conjunctionType, query: mongoQuery });
|
|
177
|
+
queryConjunctions.push(conjunctionType || "OR");
|
|
178
|
+
const key = `$${conjunctionType?.toLowerCase() || "or"}${index}`;
|
|
179
|
+
if (["OR", "AND"].includes(conjunctionType || "")) {
|
|
180
|
+
const lastKey = Object.keys(filterQuery)[Object.keys(filterQuery).length - 1];
|
|
181
|
+
if (filterQuery[lastKey] && Array.isArray(filterQuery[lastKey])) {
|
|
182
|
+
filterQuery[lastKey].push(mongoQuery);
|
|
183
|
+
}
|
|
184
|
+
else {
|
|
185
|
+
filterQuery[key] = [mongoQuery];
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
else {
|
|
189
|
+
filterQuery[`$or${index}`] = [mongoQuery];
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
});
|
|
193
|
+
const newQuery = prepareFinalQueryString(finalConditions);
|
|
194
|
+
console.log("fQuery :>> ", JSON.stringify(newQuery));
|
|
195
|
+
const hasAndQueryConjunction = queryConjunctions.includes("AND");
|
|
196
|
+
const oldQuery = (0, query_utils_1.modifyQuery)(filterQuery, hasAndQueryConjunction);
|
|
197
|
+
console.log("oldQuery :>> ", JSON.stringify(oldQuery));
|
|
198
|
+
return newQuery;
|
|
199
|
+
};
|
|
200
|
+
const prepareFinalQueryString = (conditions) => {
|
|
201
|
+
if (!Array.isArray(conditions) || conditions.length === 0) {
|
|
202
|
+
return {};
|
|
203
|
+
}
|
|
204
|
+
let finalQuery = null;
|
|
205
|
+
for (const condition of conditions) {
|
|
206
|
+
const { conjunctionType, query } = condition;
|
|
207
|
+
if (!finalQuery) {
|
|
208
|
+
finalQuery = query;
|
|
209
|
+
}
|
|
210
|
+
else {
|
|
211
|
+
if (conjunctionType === "AND") {
|
|
212
|
+
finalQuery = { $and: [finalQuery, query] };
|
|
213
|
+
}
|
|
214
|
+
else if (conjunctionType === "OR") {
|
|
215
|
+
finalQuery = { $or: [finalQuery, query] };
|
|
216
|
+
}
|
|
217
|
+
else {
|
|
218
|
+
throw new Error(`Invalid conjunctionType: ${conjunctionType}`);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
return finalQuery ?? {};
|
|
223
|
+
};
|
|
224
|
+
const conditionToQuery = (query, requiredExternal, externalParams, constants, timezone, authDetail) => {
|
|
225
|
+
const { key, field, value, fieldType } = query;
|
|
226
|
+
if ([drapcode_constant_1.NOT_EQUAL, ...isEqualOrIgnore].includes(key) &&
|
|
227
|
+
typeof value === "string" &&
|
|
228
|
+
drapcode_constant_1.DateRangeConstant.some((cnst) => value.includes(cnst))) {
|
|
229
|
+
let final = (0, date_util_1.getDateRangeValue)(key, field, value);
|
|
230
|
+
return final;
|
|
231
|
+
}
|
|
232
|
+
console.log("No date range constants");
|
|
233
|
+
let fieldValue = changeExternalParams(query, requiredExternal, externalParams, constants, timezone, authDetail);
|
|
234
|
+
console.log("After External Params replacements: ", fieldValue);
|
|
235
|
+
if (["undefined", "null", null, undefined, ""].includes(fieldValue)) {
|
|
236
|
+
//Assign some random value if fieldValue is missing from old records
|
|
237
|
+
fieldValue = noFieldString;
|
|
238
|
+
}
|
|
239
|
+
console.log("fieldValue queryToMongo 1", fieldValue);
|
|
240
|
+
let isDate = (0, query_utils_1.isItDate)(fieldValue);
|
|
241
|
+
console.log("isDate queryToMongo 2", isDate);
|
|
242
|
+
// Field Type: reference, belongsTo, static option and dynamic option
|
|
243
|
+
let condition = drapcode_constant_1.AllSelectOptionFields.includes(fieldType);
|
|
244
|
+
console.log("If field is of type reference, belongs, static or dynamic option:", condition);
|
|
245
|
+
if (condition) {
|
|
246
|
+
if (key === drapcode_constant_1.IS_NULL) {
|
|
247
|
+
return {
|
|
248
|
+
$or: [
|
|
249
|
+
{ [field]: { $exists: false } },
|
|
250
|
+
{ [field]: { $type: "array", $eq: [] } },
|
|
251
|
+
],
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
if (key === drapcode_constant_1.IS_NOT_NULL) {
|
|
255
|
+
return { [field]: { $exists: true, $type: "array", $ne: [] } };
|
|
256
|
+
}
|
|
257
|
+
if (!isDate) {
|
|
258
|
+
if (key === drapcode_constant_1.EQUALS_OR_IGNORE &&
|
|
259
|
+
typeof fieldValue === "string" &&
|
|
260
|
+
fieldValue.startsWith("CURRENT_")) {
|
|
261
|
+
//When Current Logged In Tenant/Settings/User have no data in that specific field
|
|
262
|
+
//Then they return the fieldValue as CURRENT_SETTINGS:field not blank
|
|
263
|
+
return {};
|
|
264
|
+
}
|
|
265
|
+
fieldValue = prepNonArrayValue(fieldValue);
|
|
266
|
+
console.log("After Non Array check:", fieldValue);
|
|
267
|
+
if (isEqualOrIgnore.includes(key)) {
|
|
268
|
+
return { [field]: { $in: fieldValue || [] } };
|
|
269
|
+
}
|
|
270
|
+
if (key === drapcode_constant_1.NOT_EQUAL) {
|
|
271
|
+
return { [field]: { $nin: fieldValue || [] } };
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
if (key === drapcode_constant_1.IN_LIST) {
|
|
275
|
+
if (!Array.isArray(fieldValue)) {
|
|
276
|
+
fieldValue = [fieldValue];
|
|
277
|
+
}
|
|
278
|
+
return { [field]: { $in: fieldValue || [] } };
|
|
279
|
+
}
|
|
280
|
+
if (key === drapcode_constant_1.NOT_IN_LIST) {
|
|
281
|
+
if (!Array.isArray(fieldValue)) {
|
|
282
|
+
fieldValue = [fieldValue];
|
|
283
|
+
}
|
|
284
|
+
return { [field]: { $nin: fieldValue || [] } };
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
else {
|
|
288
|
+
//Since we already checked condition for array/dropdown field type
|
|
289
|
+
//static_option, dynamic_option, reference, belongsTo
|
|
290
|
+
//Now check for remaining field type
|
|
291
|
+
//Let's check for all other keys
|
|
292
|
+
if (key === drapcode_constant_1.IS_NOT_NULL)
|
|
293
|
+
return { [field]: { $nin: [null, ""] } };
|
|
294
|
+
if (key === drapcode_constant_1.IS_NULL)
|
|
295
|
+
return { [field]: { $in: [null, ""] } };
|
|
296
|
+
if (key === drapcode_constant_1.LIKE)
|
|
297
|
+
return { [field]: { $regex: fieldValue, $options: "i" } };
|
|
298
|
+
if ([drapcode_constant_1.IN_LIST, drapcode_constant_1.NOT_IN_LIST].includes(key)) {
|
|
299
|
+
console.log("In list or not in list check");
|
|
300
|
+
fieldValue = prepNonArrayValue(fieldValue);
|
|
301
|
+
if (key === drapcode_constant_1.IN_LIST) {
|
|
302
|
+
return { [field]: { $in: fieldValue || [] } };
|
|
303
|
+
}
|
|
304
|
+
if (key === drapcode_constant_1.NOT_IN_LIST) {
|
|
305
|
+
return { [field]: { $nin: fieldValue || [] } };
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
if (key === drapcode_constant_1.NOT_EQUAL) {
|
|
309
|
+
if (fieldType === number.id) {
|
|
310
|
+
return { [field]: { $ne: +fieldValue } };
|
|
311
|
+
}
|
|
312
|
+
if (fieldType === boolean.id) {
|
|
313
|
+
return {
|
|
314
|
+
[field]: { $ne: fieldValue ? JSON.parse(fieldValue) : fieldValue },
|
|
315
|
+
};
|
|
316
|
+
}
|
|
317
|
+
return { [field]: { $ne: fieldValue } };
|
|
318
|
+
}
|
|
319
|
+
if (key === drapcode_constant_1.BETWEEN) {
|
|
320
|
+
const { startValue, endValue, isInteger } = (0, query_utils_1.getMinMaxValue)(fieldValue);
|
|
321
|
+
return {
|
|
322
|
+
[field]: {
|
|
323
|
+
$gte: isInteger ? +startValue : startValue,
|
|
324
|
+
$lte: isInteger ? +endValue : endValue,
|
|
325
|
+
},
|
|
326
|
+
};
|
|
327
|
+
}
|
|
328
|
+
if (key === drapcode_constant_1.GREATER_THAN_EQUALS_TO)
|
|
329
|
+
return { [field]: { $gte: isDate ? fieldValue : +fieldValue } };
|
|
330
|
+
if (key === drapcode_constant_1.LESS_THAN_EQUALS_TO)
|
|
331
|
+
return { [field]: { $lte: isDate ? fieldValue : +fieldValue } };
|
|
332
|
+
if (key === drapcode_constant_1.LESS_THAN)
|
|
333
|
+
return { [field]: { $lt: isDate ? fieldValue : +fieldValue } };
|
|
334
|
+
if (key === drapcode_constant_1.GREATER_THAN)
|
|
335
|
+
return { [field]: { $gt: isDate ? fieldValue : +fieldValue } };
|
|
336
|
+
//Equal & Equal Ignore
|
|
337
|
+
//Created By
|
|
338
|
+
if (equalIgnoreCheck(fieldValue, key)) {
|
|
339
|
+
return {};
|
|
340
|
+
}
|
|
341
|
+
if (field === "createdBy" && isEqualOrIgnore.includes(key)) {
|
|
342
|
+
return {
|
|
343
|
+
$and: [
|
|
344
|
+
{ [field]: { $ne: null } },
|
|
345
|
+
{ [field]: { $ne: "" } },
|
|
346
|
+
{ [field]: { $eq: fieldValue } },
|
|
347
|
+
],
|
|
348
|
+
};
|
|
349
|
+
}
|
|
350
|
+
//Boolean
|
|
351
|
+
if (fieldType === boolean.id && isEqualOrIgnore.includes(key)) {
|
|
352
|
+
return prepareBooleanQuery(field, fieldValue);
|
|
353
|
+
}
|
|
354
|
+
//Number
|
|
355
|
+
if (fieldType === number.id && isEqualOrIgnore.includes(key)) {
|
|
356
|
+
return { [field]: +fieldValue };
|
|
357
|
+
}
|
|
358
|
+
//Date
|
|
359
|
+
if (isDate && isEqualOrIgnore.includes(key)) {
|
|
360
|
+
return {
|
|
361
|
+
[field]: {
|
|
362
|
+
$gte: fieldValue,
|
|
363
|
+
$lt: (0, date_util_1.nextDayDate)(fieldValue),
|
|
364
|
+
},
|
|
365
|
+
};
|
|
366
|
+
}
|
|
367
|
+
//Since we already check equal & equal ignore for boolean, number, date, createdBy
|
|
368
|
+
//Now if it still equal & equal ignore, but no field type match then
|
|
369
|
+
if (isEqualOrIgnore.includes(key)) {
|
|
370
|
+
return { [field]: fieldValue };
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
};
|
|
374
|
+
const changeExternalParams = (query, requiredExternal, externalParams, constants, timezone, authDetail) => {
|
|
375
|
+
const { key, value, fieldType } = query;
|
|
376
|
+
const { user, tenant, setting, subTenant } = authDetail;
|
|
377
|
+
//For select
|
|
378
|
+
if (fieldType &&
|
|
379
|
+
!requiredExternal &&
|
|
380
|
+
drapcode_constant_1.AllSelectOptionFields.includes(fieldType)) {
|
|
381
|
+
const { refCollection, refField } = query;
|
|
382
|
+
let finalValue = value;
|
|
383
|
+
if (drapcode_constant_1.OptionTypeFields.includes(fieldType) && !value) {
|
|
384
|
+
switch (refCollection) {
|
|
385
|
+
case drapcode_constant_1.CURRENT_USER:
|
|
386
|
+
finalValue = user?.[refField];
|
|
387
|
+
break;
|
|
388
|
+
case drapcode_constant_1.CURRENT_TENANT:
|
|
389
|
+
finalValue = tenant?.[refField];
|
|
390
|
+
break;
|
|
391
|
+
case drapcode_constant_1.CURRENT_SETTINGS:
|
|
392
|
+
finalValue = setting?.[refField];
|
|
393
|
+
break;
|
|
394
|
+
case drapcode_constant_1.CURRENT_SUB_TENANT:
|
|
395
|
+
finalValue = subTenant?.[refField];
|
|
396
|
+
break;
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
if (value === drapcode_constant_1.CURRENT_USER) {
|
|
400
|
+
finalValue = user?.uuid;
|
|
401
|
+
}
|
|
402
|
+
if (!finalValue)
|
|
403
|
+
finalValue = value;
|
|
404
|
+
return finalValue;
|
|
405
|
+
}
|
|
406
|
+
if (fieldType === boolean.id &&
|
|
407
|
+
["true", "false", "", true, false].includes(value)) {
|
|
408
|
+
return String(value);
|
|
409
|
+
}
|
|
410
|
+
const constantDataMap = {
|
|
411
|
+
[drapcode_constant_1.CURRENT_USER]: user,
|
|
412
|
+
[drapcode_constant_1.CURRENT_TENANT]: tenant,
|
|
413
|
+
[drapcode_constant_1.CURRENT_SETTINGS]: setting,
|
|
414
|
+
[drapcode_constant_1.CURRENT_SUB_TENANT]: subTenant,
|
|
415
|
+
};
|
|
416
|
+
if ([drapcode_constant_1.IN_LIST, drapcode_constant_1.NOT_IN_LIST].includes(key)) {
|
|
417
|
+
let valueList = [];
|
|
418
|
+
if (Array.isArray(value) && value.length) {
|
|
419
|
+
valueList.push(...value);
|
|
420
|
+
value.forEach((val) => {
|
|
421
|
+
constants.forEach((constant) => {
|
|
422
|
+
if (val === constant.name) {
|
|
423
|
+
valueList.push(constant.value);
|
|
424
|
+
}
|
|
425
|
+
});
|
|
426
|
+
});
|
|
427
|
+
}
|
|
428
|
+
else if (typeof value === "string") {
|
|
429
|
+
Object.entries(constantDataMap).forEach(([constantName, data]) => {
|
|
430
|
+
if (value.includes(constantName)) {
|
|
431
|
+
const currentDataValue = getCurrentDataValueFrom(value, constantName, data, true);
|
|
432
|
+
if (currentDataValue) {
|
|
433
|
+
if (Array.isArray(currentDataValue)) {
|
|
434
|
+
valueList.push(...currentDataValue);
|
|
435
|
+
}
|
|
436
|
+
else {
|
|
437
|
+
valueList.push(currentDataValue);
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
});
|
|
442
|
+
}
|
|
443
|
+
return valueList;
|
|
444
|
+
}
|
|
445
|
+
if (key === drapcode_constant_1.BETWEEN) {
|
|
446
|
+
let { startValue, endValue, isFixedValue } = (0, query_utils_1.getMinMaxValue)(value);
|
|
447
|
+
if (requiredExternal) {
|
|
448
|
+
startValue = `${externalParams[startValue]}`;
|
|
449
|
+
endValue = `${externalParams[endValue]}`;
|
|
450
|
+
}
|
|
451
|
+
else {
|
|
452
|
+
const resolveValue = (val) => {
|
|
453
|
+
if ([drapcode_constant_1.CURRENT_USER, ...drapcode_constant_1.DateConstant].some((cnst) => val.includes?.(cnst))) {
|
|
454
|
+
return (0, query_utils_1.getValueOfProjectConstant)(val, user, timezone);
|
|
455
|
+
}
|
|
456
|
+
else if (!isFixedValue) {
|
|
457
|
+
const found = constants.find((constant) => constant.name === val);
|
|
458
|
+
return found?.value ?? val;
|
|
459
|
+
}
|
|
460
|
+
return val;
|
|
461
|
+
};
|
|
462
|
+
startValue = resolveValue(startValue);
|
|
463
|
+
endValue = resolveValue(endValue);
|
|
464
|
+
}
|
|
465
|
+
if (Array.isArray(startValue)) {
|
|
466
|
+
startValue = startValue[0].value;
|
|
467
|
+
}
|
|
468
|
+
if (Array.isArray(endValue)) {
|
|
469
|
+
endValue = endValue[0].value;
|
|
470
|
+
}
|
|
471
|
+
const isNumeric = !isNaN(startValue) && !isNaN(endValue);
|
|
472
|
+
const result = `${startValue}---${endValue}${isNumeric ? "^" : ""}`;
|
|
473
|
+
return result;
|
|
474
|
+
}
|
|
475
|
+
for (const [constant, source] of Object.entries(constantDataMap)) {
|
|
476
|
+
const valueFromSource = getCurrentDataValueFrom(value, constant, source);
|
|
477
|
+
console.log(`\n ${constant} Value :>>`, valueFromSource);
|
|
478
|
+
if (valueFromSource)
|
|
479
|
+
return valueFromSource;
|
|
480
|
+
}
|
|
481
|
+
if (drapcode_constant_1.DateConstant.some((cnst) => value.includes(cnst))) {
|
|
482
|
+
return (0, query_utils_1.getValueOfProjectConstant)(value, {}, timezone);
|
|
483
|
+
}
|
|
484
|
+
if ((0, exports.isEntityCondition)(value)) {
|
|
485
|
+
const key = value.replace("ENTITY::", "");
|
|
486
|
+
return externalParams[key] ? externalParams[key] : "";
|
|
487
|
+
}
|
|
488
|
+
if ((0, query_utils_1.isFixedValueQuery)(value))
|
|
489
|
+
return value.replace("FIXED_VALUE::", "");
|
|
490
|
+
if (requiredExternal) {
|
|
491
|
+
let externalParamValue = externalParams[`${value}`];
|
|
492
|
+
if (!isNaN(externalParamValue)) {
|
|
493
|
+
return +externalParamValue;
|
|
494
|
+
}
|
|
495
|
+
return externalParams[`${value}`];
|
|
496
|
+
}
|
|
497
|
+
let constantArr = constants.filter((constant) => constant.name == value);
|
|
498
|
+
return constantArr.length ? "" + constantArr[0].value : "";
|
|
499
|
+
};
|
|
500
|
+
const equalIgnoreCheck = (fieldValue, key) => {
|
|
501
|
+
return (key === drapcode_constant_1.EQUALS_OR_IGNORE &&
|
|
502
|
+
Array.isArray(fieldValue) &&
|
|
503
|
+
fieldValue.includes(noFieldString));
|
|
504
|
+
};
|
|
505
|
+
const getCurrentDataValueFrom = (value, constantName, data, isMultiRef = false) => {
|
|
506
|
+
if (!value.includes(constantName))
|
|
507
|
+
return;
|
|
508
|
+
if (value === constantName)
|
|
509
|
+
return data[`uuid`];
|
|
510
|
+
const splitValues = value.split("::");
|
|
511
|
+
let refCollection = "", refCollectionField = "";
|
|
512
|
+
if (splitValues.length === 2) {
|
|
513
|
+
[refCollection, refCollectionField] = splitValues;
|
|
514
|
+
}
|
|
515
|
+
else if (splitValues.length >= 3) {
|
|
516
|
+
[, refCollection, refCollectionField] = splitValues;
|
|
517
|
+
}
|
|
518
|
+
else {
|
|
519
|
+
refCollection = splitValues[0];
|
|
520
|
+
}
|
|
521
|
+
if (refCollection && refCollectionField) {
|
|
522
|
+
console.log("I have refCollection and Field");
|
|
523
|
+
if (refCollection === constantName) {
|
|
524
|
+
return data[refCollectionField];
|
|
525
|
+
}
|
|
526
|
+
let refCollectionData = data?.[refCollection];
|
|
527
|
+
if (!refCollectionData)
|
|
528
|
+
return "";
|
|
529
|
+
if (Array.isArray(refCollectionData)) {
|
|
530
|
+
if (!isMultiRef)
|
|
531
|
+
return "";
|
|
532
|
+
return refCollectionData
|
|
533
|
+
.map((ref) => ref?.[refCollectionField])
|
|
534
|
+
.filter((v) => v !== undefined && v !== null);
|
|
535
|
+
}
|
|
536
|
+
refCollectionData = refCollectionData[0];
|
|
537
|
+
return refCollectionData?.[refCollectionField];
|
|
538
|
+
}
|
|
539
|
+
//this means that data has to be match with current user field that is not ref type
|
|
540
|
+
return data?.[refCollection] ?? data?.uuid;
|
|
541
|
+
};
|
|
542
|
+
const isEntityCondition = (value) => {
|
|
543
|
+
if (Array.isArray(value)) {
|
|
544
|
+
return value.some((val) => typeof val === "string" && val.startsWith("ENTITY::"));
|
|
545
|
+
}
|
|
546
|
+
return typeof value === "string" && value.startsWith("ENTITY::");
|
|
547
|
+
};
|
|
548
|
+
exports.isEntityCondition = isEntityCondition;
|
|
549
|
+
const prepNonArrayValue = (val) => Array.isArray(val) ? val : [val];
|
|
550
|
+
const parseBooleanSafe = (val) => {
|
|
551
|
+
try {
|
|
552
|
+
return JSON.parse(val) === true;
|
|
553
|
+
}
|
|
554
|
+
catch {
|
|
555
|
+
return false;
|
|
556
|
+
}
|
|
557
|
+
};
|
|
558
|
+
const prepareBooleanQuery = (field, fieldValue) => {
|
|
559
|
+
const boolVal = parseBooleanSafe(fieldValue);
|
|
560
|
+
return boolVal
|
|
561
|
+
? {
|
|
562
|
+
[field]: boolVal,
|
|
563
|
+
}
|
|
564
|
+
: {
|
|
565
|
+
$or: [{ [field]: boolVal }, { [field]: { $exists: false } }],
|
|
566
|
+
};
|
|
567
|
+
};
|
|
@@ -1,9 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
interface ErrorConfig {
|
|
2
|
+
message: string;
|
|
3
|
+
path?: string;
|
|
4
|
+
value?: string | number;
|
|
5
|
+
}
|
|
6
|
+
interface CheckErrorResult {
|
|
7
|
+
status?: number;
|
|
8
|
+
responseData?: string;
|
|
4
9
|
noError: boolean;
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
}
|
|
10
|
+
}
|
|
11
|
+
interface ErrorMapping {
|
|
12
|
+
errorMsgPath: string;
|
|
13
|
+
errorValue: string | number;
|
|
14
|
+
}
|
|
15
|
+
export declare const checkForError: (result: Record<string, any>, errors: ErrorConfig[], mapping: ErrorMapping, status: number) => Promise<CheckErrorResult>;
|
|
16
|
+
export {};
|