@konplit-services/common 1.16.0 → 1.16.1

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.
@@ -45,6 +45,16 @@ export declare const generateQueryObject: (filters: FilterOptions, searchesField
45
45
  select: string | Record<string, any>;
46
46
  populate: (string | PopulateOptions)[];
47
47
  };
48
+ export declare const generateQueryObjectV2: (filters: FilterOptions, searchesFieldNames?: string[], select?: string | Record<string, any>, populate?: (string | PopulateOptions)[]) => {
49
+ page: number;
50
+ limit: number;
51
+ query: any;
52
+ sort: {
53
+ [key: string]: "asc" | "desc";
54
+ };
55
+ select: string | Record<string, any>;
56
+ populate: (string | PopulateOptions)[];
57
+ };
48
58
  export interface DefaultFilterOptions {
49
59
  search?: string;
50
60
  page?: string;
@@ -9,7 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  });
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.generateQueryObject = void 0;
12
+ exports.generateQueryObjectV2 = exports.generateQueryObject = void 0;
13
13
  exports.paginate = paginate;
14
14
  const date_processing_1 = require("./date-processing");
15
15
  function paginate(model_1) {
@@ -133,3 +133,85 @@ const generateQueryObject = (filters, searchesFieldNames = [], select = "", popu
133
133
  };
134
134
  };
135
135
  exports.generateQueryObject = generateQueryObject;
136
+ // this method use direct deterministic approach to search instead of using th text search
137
+ const generateQueryObjectV2 = (filters, searchesFieldNames = [], select = "", populate = []) => {
138
+ const { status, limit, page, sort_by = "createdAt", order = "desc", merchantId, accountId, dateField, amountField, } = filters;
139
+ const excludes = [
140
+ "status",
141
+ "from",
142
+ "page",
143
+ "to",
144
+ "limit",
145
+ "sort_by",
146
+ "search",
147
+ "accountId",
148
+ "merchantId",
149
+ "order",
150
+ "dateField",
151
+ "amountField",
152
+ "min_amount",
153
+ "max_amount",
154
+ ];
155
+ const query = {};
156
+ let sort = {};
157
+ const keys = Object.keys(filters);
158
+ for (const key of keys) {
159
+ if (!excludes.includes(key)) {
160
+ query[key] = filters[key];
161
+ }
162
+ }
163
+ if (filters.merchantId) {
164
+ query.merchantId = merchantId;
165
+ }
166
+ if (filters.accountId) {
167
+ query.accountId = accountId;
168
+ }
169
+ // ---- Optimized Search Block ----
170
+ if (filters.search && searchesFieldNames.length) {
171
+ const s = filters.search.trim();
172
+ query.$or = searchesFieldNames.map((field) => ({ [field]: s }));
173
+ }
174
+ if (filters.status) {
175
+ query.status = status;
176
+ }
177
+ // for dynamic date creation
178
+ const fieldToFilter = dateField || "createdAt";
179
+ if (filters.from && filters.to) {
180
+ const date = (0, date_processing_1.convertDateToUTCRange)(filters.from, filters.to);
181
+ query[fieldToFilter] = {
182
+ $gte: date.startUTC,
183
+ $lte: date.endUTC,
184
+ };
185
+ }
186
+ else if (filters.from) {
187
+ query[fieldToFilter] = {
188
+ $gte: (0, date_processing_1.convertDateToUTC)(filters.from),
189
+ };
190
+ }
191
+ // for dynamic amount range filter
192
+ const fieldAmountFilter = amountField || "amount";
193
+ const min = Number(filters.min_amount);
194
+ const max = Number(filters.max_amount);
195
+ if (!isNaN(min) || !isNaN(max)) {
196
+ query[fieldAmountFilter] = {};
197
+ if (!isNaN(min))
198
+ query[fieldAmountFilter]["$gte"] = min;
199
+ if (!isNaN(max))
200
+ query[fieldAmountFilter]["$lte"] = max;
201
+ }
202
+ if (filters.sort_by && filters.order) {
203
+ const newSory = {
204
+ [sort_by]: order === "asc" ? "asc" : "desc",
205
+ };
206
+ sort = Object.assign({}, newSory);
207
+ }
208
+ return {
209
+ page: parseInt(page, 10),
210
+ limit: parseInt(limit, 10),
211
+ query: query,
212
+ sort: sort,
213
+ select: select,
214
+ populate,
215
+ };
216
+ };
217
+ exports.generateQueryObjectV2 = generateQueryObjectV2;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@konplit-services/common",
3
- "version": "1.16.0",
3
+ "version": "1.16.1",
4
4
  "description": "",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",