c2-mongoose 2.1.11 → 2.1.13

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.
@@ -46,7 +46,11 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
46
46
  if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
47
  }
48
48
  };
49
+ var __importDefault = (this && this.__importDefault) || function (mod) {
50
+ return (mod && mod.__esModule) ? mod : { "default": mod };
51
+ };
49
52
  Object.defineProperty(exports, "__esModule", { value: true });
53
+ var moment_1 = __importDefault(require("moment"));
50
54
  var Environment_1 = require("../configuration/Environment");
51
55
  var Utils_1 = require("../utils/Utils");
52
56
  var SearchFlow = /** @class */ (function () {
@@ -237,27 +241,29 @@ var SearchFlow = /** @class */ (function () {
237
241
  };
238
242
  SearchFlow.prototype.buildDefaultFilters = function (objectSearch) {
239
243
  var filters = { $and: [] };
240
- Object.entries(objectSearch).forEach(function (_a) {
244
+ Object.entries(objectSearch.model).forEach(function (_a) {
241
245
  var key = _a[0], value = _a[1];
242
246
  if ((0, Utils_1.isNotEmpty)(value)) {
243
247
  var condition = {};
244
248
  if (['order', 'orderBy', 'properties', 'populate', 'page', 'limit', 'model', 'select', 'searchText', 'isPageable', 'searchPageable'].includes(key)) {
245
249
  return;
246
250
  }
247
- if (key.endsWith('DateRange')) {
248
- var keyAux = key.replace('Range', '');
251
+ if (key.endsWith('DateRange') || key.endsWith('DateTimeRange')) {
252
+ var fieldName = key.replace('Range', '');
249
253
  var values = value;
250
254
  if ((0, Utils_1.isNotEmpty)(values[0])) {
251
- condition[keyAux] = __assign(__assign({}, condition[keyAux]), { $gte: new Date(values[0]) });
255
+ var momentValue = (0, moment_1.default)(values[0]);
256
+ condition[fieldName] = __assign(__assign({}, condition[fieldName]), { $gte: momentValue.toDate() });
252
257
  }
253
258
  if ((0, Utils_1.isNotEmpty)(values[1])) {
254
- condition[keyAux] = __assign(__assign({}, condition[keyAux]), { $lte: new Date(values[1]) });
259
+ var momentValue = (0, moment_1.default)(values[1]);
260
+ condition[fieldName] = __assign(__assign({}, condition[fieldName]), { $lte: momentValue.toDate() });
255
261
  }
256
262
  filters.$and.push(condition);
257
263
  }
258
264
  else if (key.endsWith('Like')) {
259
- var keyAux = key.replace('Like', '');
260
- condition[keyAux] = { $regex: value, $options: 'i' };
265
+ var fieldName = key.replace('Like', '');
266
+ condition[fieldName] = { $regex: value, $options: 'i' };
261
267
  filters.$and.push(condition);
262
268
  }
263
269
  else {
@@ -271,9 +277,9 @@ var SearchFlow = /** @class */ (function () {
271
277
  }
272
278
  }
273
279
  });
274
- if ((0, Utils_1.isNotEmpty)(objectSearch.model)) {
275
- this.addFilterModel(objectSearch.model, filters);
276
- }
280
+ // if (isNotEmpty(objectSearch.model)) {
281
+ // this.addFilterModel(objectSearch.model, filters)
282
+ // }
277
283
  if (filters.$and.length === 0)
278
284
  delete filters['$and'];
279
285
  return filters;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "c2-mongoose",
3
- "version": "2.1.11",
3
+ "version": "2.1.13",
4
4
  "description": "Lib to make any search in database mongoose and use as basic crud",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -29,6 +29,7 @@
29
29
  "homepage": "https://gitlab.com/cabral_consultoria/c2-mongoose#readme",
30
30
  "dependencies": {
31
31
  "dotenv": "^16.0.3",
32
+ "moment": "^2.29.4",
32
33
  "mongoose": "^7.0.1"
33
34
  },
34
35
  "devDependencies": {
@@ -1,3 +1,4 @@
1
+ import moment from "moment"
1
2
  import mongoose from "mongoose"
2
3
  import { dbCollation } from "../configuration/Environment"
3
4
  import { Pagination, SearchResponse } from "../types/SearchResponse"
@@ -162,33 +163,35 @@ abstract class SearchFlow {
162
163
 
163
164
  public buildDefaultFilters(objectSearch: any) {
164
165
  let filters = { $and: [] } as any
165
- Object.entries(objectSearch).forEach(([key, value]) => {
166
+ Object.entries(objectSearch.model).forEach(([key, value]) => {
166
167
  if (isNotEmpty(value)) {
167
168
  let condition = {} as any
168
169
  if (['order', 'orderBy', 'properties', 'populate', 'page', 'limit', 'model', 'select', 'searchText', 'isPageable', 'searchPageable'].includes(key)) {
169
170
  return
170
171
  }
171
172
 
172
- if (key.endsWith('DateRange')) {
173
- var keyAux = key.replace('Range', '')
173
+ if (key.endsWith('DateRange') || key.endsWith('DateTimeRange') ) {
174
+ var fieldName = key.replace('Range', '')
174
175
  var values = value as any
175
176
  if (isNotEmpty(values[0])) {
176
- condition[keyAux] = {
177
- ...condition[keyAux],
178
- $gte: new Date(values[0])
177
+ var momentValue = moment(values[0])
178
+ condition[fieldName] = {
179
+ ...condition[fieldName],
180
+ $gte: momentValue.toDate()
179
181
  }
180
182
  }
181
183
 
182
184
  if (isNotEmpty(values[1])) {
183
- condition[keyAux] = {
184
- ...condition[keyAux],
185
- $lte: new Date(values[1])
185
+ var momentValue = moment(values[1])
186
+ condition[fieldName] = {
187
+ ...condition[fieldName],
188
+ $lte: momentValue.toDate()
186
189
  }
187
190
  }
188
191
  filters.$and.push(condition)
189
192
  } else if (key.endsWith('Like')) {
190
- var keyAux = key.replace('Like', '')
191
- condition[keyAux] = { $regex: value as any, $options: 'i' }
193
+ var fieldName = key.replace('Like', '')
194
+ condition[fieldName] = { $regex: value as any, $options: 'i' }
192
195
  filters.$and.push(condition)
193
196
  } else {
194
197
  if (!Array.isArray(value)) {
@@ -202,9 +205,9 @@ abstract class SearchFlow {
202
205
  })
203
206
 
204
207
 
205
- if (isNotEmpty(objectSearch.model)) {
206
- this.addFilterModel(objectSearch.model, filters)
207
- }
208
+ // if (isNotEmpty(objectSearch.model)) {
209
+ // this.addFilterModel(objectSearch.model, filters)
210
+ // }
208
211
 
209
212
  if (filters.$and.length === 0)
210
213
  delete filters['$and']