denwa-nest-shared 1.0.4 → 1.0.6

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/dist/src/lib.js CHANGED
@@ -9,12 +9,21 @@ function _export(target, all) {
9
9
  });
10
10
  }
11
11
  _export(exports, {
12
+ checkPageAndLimit: function() {
13
+ return checkPageAndLimit;
14
+ },
12
15
  concatenateTwo32BitNumbers: function() {
13
16
  return concatenateTwo32BitNumbers;
14
17
  },
15
18
  createPaginate: function() {
16
19
  return createPaginate;
17
20
  },
21
+ createQueryData: function() {
22
+ return createQueryData;
23
+ },
24
+ fileStreamToBuffer: function() {
25
+ return fileStreamToBuffer;
26
+ },
18
27
  formatPhone: function() {
19
28
  return formatPhone;
20
29
  },
@@ -26,6 +35,7 @@ _export(exports, {
26
35
  }
27
36
  });
28
37
  const _libphonenumberjs = /*#__PURE__*/ _interop_require_default(require("libphonenumber-js"));
38
+ const _typeorm = require("typeorm");
29
39
  function _interop_require_default(obj) {
30
40
  return obj && obj.__esModule ? obj : {
31
41
  default: obj
@@ -58,3 +68,284 @@ const formatPhone = (phone)=>{
58
68
  const concatenateTwo32BitNumbers = (low, high)=>{
59
69
  return (high >>> 0) * Math.pow(2, 32) + (low >>> 0);
60
70
  };
71
+ const checkPageAndLimit = (page, limit, maxLimit = 500)=>{
72
+ const paginationData = {
73
+ checkedPage: page,
74
+ checkedLimit: limit
75
+ };
76
+ if (page < 1) {
77
+ paginationData.checkedPage = 1;
78
+ }
79
+ if (limit < 1) {
80
+ paginationData.checkedLimit = 1;
81
+ }
82
+ if (limit > maxLimit) {
83
+ paginationData.checkedLimit = maxLimit;
84
+ }
85
+ return paginationData;
86
+ };
87
+ const fileStreamToBuffer = async (fileStream)=>{
88
+ const chunks = [];
89
+ for await (const chunk of fileStream){
90
+ chunks.push(chunk);
91
+ }
92
+ return Buffer.concat(chunks);
93
+ };
94
+ const createQueryData = (page, limit, sortField, sortOrder, filterType, filter, search)=>{
95
+ const queryData = {
96
+ skip: getPaginateSkip(page, limit),
97
+ take: limit
98
+ };
99
+ if (sortField && sortOrder) {
100
+ queryData.order = {
101
+ [sortField]: sortOrder
102
+ };
103
+ }
104
+ if (filter) {
105
+ let whereAnd = {};
106
+ let whereOr = [];
107
+ let priceFrom;
108
+ let priceTo;
109
+ let priorityFrom;
110
+ let priorityTo;
111
+ let createdAtFrom;
112
+ let createdAtTo;
113
+ let updatedAtFrom;
114
+ let updatedAtTo;
115
+ for (const [key, value] of Object.entries(filter)){
116
+ let currentKey = key;
117
+ let currentValue;
118
+ if (typeof value === 'object' && !Array.isArray(value) && typeof value?.getTime !== 'function') {
119
+ const obj = JSON.parse(JSON.stringify(value));
120
+ const valueObject = {};
121
+ for (const [key, value] of Object.entries(obj)){
122
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
123
+ valueObject[key] = typeof value === 'boolean' ? value : (0, _typeorm.In)(value);
124
+ }
125
+ currentValue = valueObject;
126
+ } else if (typeof value === 'boolean') {
127
+ currentValue = value;
128
+ } else {
129
+ if (key === 'priceFrom') {
130
+ priceFrom = value;
131
+ continue;
132
+ }
133
+ if (key === 'priceTo') {
134
+ priceTo = value;
135
+ continue;
136
+ }
137
+ if (key === 'priorityFrom') {
138
+ priorityFrom = value;
139
+ continue;
140
+ }
141
+ if (key === 'priorityTo') {
142
+ priorityTo = value;
143
+ continue;
144
+ }
145
+ if (key === 'createdAtFrom') {
146
+ createdAtFrom = value;
147
+ continue;
148
+ }
149
+ if (key === 'createdAtTo') {
150
+ createdAtTo = value;
151
+ continue;
152
+ }
153
+ if (key === 'updatedAtFrom') {
154
+ updatedAtFrom = value;
155
+ continue;
156
+ }
157
+ if (key === 'updatedAtTo') {
158
+ updatedAtTo = value;
159
+ continue;
160
+ }
161
+ if (Array.isArray(value) && value.length) {
162
+ // @NOTE тут перечисляются поля, которые массивы в базе данных и надо найти хотя бы одно вхождение.
163
+ const arraysFields = [
164
+ {
165
+ key: 'sectionsOR',
166
+ originalKey: 'sections'
167
+ }
168
+ ];
169
+ const finded = arraysFields.find((item)=>item.key === key);
170
+ if (finded) {
171
+ currentValue = (0, _typeorm.ArrayOverlap)(value);
172
+ currentKey = finded.originalKey;
173
+ } else {
174
+ currentValue = (0, _typeorm.In)(value);
175
+ }
176
+ } else if (Array.isArray(value)) {
177
+ currentValue = (0, _typeorm.In)(value);
178
+ }
179
+ }
180
+ if (currentValue) {
181
+ whereAnd = {
182
+ ...whereAnd,
183
+ [currentKey]: currentValue
184
+ };
185
+ whereOr = [
186
+ ...whereOr,
187
+ {
188
+ [currentKey]: currentValue
189
+ }
190
+ ];
191
+ }
192
+ }
193
+ if (priceFrom && priceTo) {
194
+ whereAnd = {
195
+ ...whereAnd,
196
+ price: (0, _typeorm.And)((0, _typeorm.MoreThanOrEqual)(priceFrom), (0, _typeorm.LessThanOrEqual)(priceTo))
197
+ };
198
+ whereOr = [
199
+ ...whereOr,
200
+ {
201
+ price: (0, _typeorm.And)((0, _typeorm.MoreThanOrEqual)(priceFrom), (0, _typeorm.LessThanOrEqual)(priceTo))
202
+ }
203
+ ];
204
+ } else if (priceFrom) {
205
+ whereAnd = {
206
+ ...whereAnd,
207
+ price: (0, _typeorm.MoreThanOrEqual)(priceFrom)
208
+ };
209
+ whereOr = [
210
+ ...whereOr,
211
+ {
212
+ price: (0, _typeorm.MoreThanOrEqual)(priceFrom)
213
+ }
214
+ ];
215
+ } else if (priceTo) {
216
+ whereAnd = {
217
+ ...whereAnd,
218
+ price: (0, _typeorm.LessThanOrEqual)(priceTo)
219
+ };
220
+ whereOr = [
221
+ ...whereOr,
222
+ {
223
+ price: (0, _typeorm.LessThanOrEqual)(priceTo)
224
+ }
225
+ ];
226
+ }
227
+ if (priorityFrom && priorityTo) {
228
+ whereAnd = {
229
+ ...whereAnd,
230
+ priority: (0, _typeorm.And)((0, _typeorm.MoreThanOrEqual)(priorityFrom), (0, _typeorm.LessThanOrEqual)(priorityTo))
231
+ };
232
+ whereOr = [
233
+ ...whereOr,
234
+ {
235
+ priority: (0, _typeorm.And)((0, _typeorm.MoreThanOrEqual)(priorityFrom), (0, _typeorm.LessThanOrEqual)(priorityTo))
236
+ }
237
+ ];
238
+ } else if (priorityFrom) {
239
+ whereAnd = {
240
+ ...whereAnd,
241
+ priority: (0, _typeorm.MoreThanOrEqual)(priorityFrom)
242
+ };
243
+ whereOr = [
244
+ ...whereOr,
245
+ {
246
+ priority: (0, _typeorm.MoreThanOrEqual)(priorityFrom)
247
+ }
248
+ ];
249
+ } else if (priorityTo) {
250
+ whereAnd = {
251
+ ...whereAnd,
252
+ priority: (0, _typeorm.LessThanOrEqual)(priorityTo)
253
+ };
254
+ whereOr = [
255
+ ...whereOr,
256
+ {
257
+ priority: (0, _typeorm.LessThanOrEqual)(priorityTo)
258
+ }
259
+ ];
260
+ }
261
+ if (createdAtFrom && createdAtTo) {
262
+ whereAnd = {
263
+ ...whereAnd,
264
+ createdAt: (0, _typeorm.And)((0, _typeorm.MoreThanOrEqual)(createdAtFrom), (0, _typeorm.LessThanOrEqual)(createdAtTo))
265
+ };
266
+ whereOr = [
267
+ ...whereOr,
268
+ {
269
+ createdAt: (0, _typeorm.And)((0, _typeorm.MoreThanOrEqual)(createdAtFrom), (0, _typeorm.LessThanOrEqual)(createdAtTo))
270
+ }
271
+ ];
272
+ } else if (createdAtFrom) {
273
+ whereAnd = {
274
+ ...whereAnd,
275
+ createdAt: (0, _typeorm.MoreThanOrEqual)(createdAtFrom)
276
+ };
277
+ whereOr = [
278
+ ...whereOr,
279
+ {
280
+ createdAt: (0, _typeorm.MoreThanOrEqual)(createdAtFrom)
281
+ }
282
+ ];
283
+ } else if (createdAtTo) {
284
+ whereAnd = {
285
+ ...whereAnd,
286
+ createdAt: (0, _typeorm.LessThanOrEqual)(createdAtTo)
287
+ };
288
+ whereOr = [
289
+ ...whereOr,
290
+ {
291
+ createdAt: (0, _typeorm.LessThanOrEqual)(createdAtTo)
292
+ }
293
+ ];
294
+ }
295
+ if (updatedAtFrom && updatedAtTo) {
296
+ whereAnd = {
297
+ ...whereAnd,
298
+ updatedAt: (0, _typeorm.And)((0, _typeorm.MoreThanOrEqual)(updatedAtFrom), (0, _typeorm.LessThanOrEqual)(updatedAtTo))
299
+ };
300
+ whereOr = [
301
+ ...whereOr,
302
+ {
303
+ updatedAt: (0, _typeorm.And)((0, _typeorm.MoreThanOrEqual)(updatedAtFrom), (0, _typeorm.LessThanOrEqual)(updatedAtTo))
304
+ }
305
+ ];
306
+ } else if (updatedAtFrom) {
307
+ whereAnd = {
308
+ ...whereAnd,
309
+ updatedAt: (0, _typeorm.MoreThanOrEqual)(updatedAtFrom)
310
+ };
311
+ whereOr = [
312
+ ...whereOr,
313
+ {
314
+ updatedAt: (0, _typeorm.MoreThanOrEqual)(updatedAtFrom)
315
+ }
316
+ ];
317
+ } else if (updatedAtTo) {
318
+ whereAnd = {
319
+ ...whereAnd,
320
+ updatedAt: (0, _typeorm.LessThanOrEqual)(updatedAtTo)
321
+ };
322
+ whereOr = [
323
+ ...whereOr,
324
+ {
325
+ updatedAt: (0, _typeorm.LessThanOrEqual)(updatedAtTo)
326
+ }
327
+ ];
328
+ }
329
+ if (filterType === 'and') queryData.where = whereAnd;
330
+ else if (filterType === 'or') queryData.where = whereOr;
331
+ }
332
+ if (search) {
333
+ let searchWhere = {};
334
+ for (const [key, value] of Object.entries(search)){
335
+ searchWhere = {
336
+ ...searchWhere,
337
+ [key]: (0, _typeorm.ILike)(`%${value}%`)
338
+ };
339
+ }
340
+ if (!queryData.where) queryData.where = searchWhere;
341
+ if (Array.isArray(queryData.where)) queryData.where = [
342
+ ...queryData.where,
343
+ searchWhere
344
+ ];
345
+ else queryData.where = {
346
+ ...searchWhere,
347
+ ...queryData.where
348
+ };
349
+ }
350
+ return queryData;
351
+ };
package/dist/src/main.js CHANGED
@@ -5,6 +5,8 @@ Object.defineProperty(exports, "__esModule", {
5
5
  _export_star(require("./dto"), exports);
6
6
  _export_star(require("./lib"), exports);
7
7
  _export_star(require("./types"), exports);
8
+ _export_star(require("./const"), exports);
9
+ _export_star(require("./validation"), exports);
8
10
  function _export_star(from, to) {
9
11
  Object.keys(from).forEach(function(k) {
10
12
  if (k !== "default" && !Object.prototype.hasOwnProperty.call(to, k)) {
package/dist/src/types.js CHANGED
@@ -2,3 +2,16 @@
2
2
  Object.defineProperty(exports, "__esModule", {
3
3
  value: true
4
4
  });
5
+ Object.defineProperty(exports, "ImageContentTypeEnum", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return ImageContentTypeEnum;
9
+ }
10
+ });
11
+ var ImageContentTypeEnum = /*#__PURE__*/ function(ImageContentTypeEnum) {
12
+ ImageContentTypeEnum["image/webp"] = "image/webp";
13
+ ImageContentTypeEnum["image/jpeg"] = "image/jpeg";
14
+ ImageContentTypeEnum["image/png"] = "image/png";
15
+ ImageContentTypeEnum["application/octet-stream"] = "application/octet-stream";
16
+ return ImageContentTypeEnum;
17
+ }({});
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ _export_star(require("./validation.exception"), exports);
6
+ _export_star(require("./validation.pipe"), exports);
7
+ function _export_star(from, to) {
8
+ Object.keys(from).forEach(function(k) {
9
+ if (k !== "default" && !Object.prototype.hasOwnProperty.call(to, k)) {
10
+ Object.defineProperty(to, k, {
11
+ enumerable: true,
12
+ get: function() {
13
+ return from[k];
14
+ }
15
+ });
16
+ }
17
+ });
18
+ return from;
19
+ }
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "ValidationException", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return ValidationException;
9
+ }
10
+ });
11
+ const _common = require("@nestjs/common");
12
+ function _ts_decorate(decorators, target, key, desc) {
13
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
14
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
15
+ else for(var i = decorators.length - 1; i >= 0; i--)if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
16
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
17
+ }
18
+ function _ts_metadata(k, v) {
19
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
20
+ }
21
+ class ValidationException extends _common.HttpException {
22
+ messages;
23
+ constructor(response){
24
+ super(response, _common.HttpStatus.BAD_REQUEST);
25
+ this.messages = response;
26
+ }
27
+ }
28
+ ValidationException = _ts_decorate([
29
+ (0, _common.Injectable)(),
30
+ _ts_metadata("design:type", Function),
31
+ _ts_metadata("design:paramtypes", [
32
+ String
33
+ ])
34
+ ], ValidationException);
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "ValidationPipe", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return ValidationPipe;
9
+ }
10
+ });
11
+ const _common = require("@nestjs/common");
12
+ const _classtransformer = require("class-transformer");
13
+ const _classvalidator = require("class-validator");
14
+ const _validationexception = require("./validation.exception");
15
+ function _ts_decorate(decorators, target, key, desc) {
16
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
17
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
18
+ else for(var i = decorators.length - 1; i >= 0; i--)if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
19
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
20
+ }
21
+ class ValidationPipe {
22
+ async transform(value, metadata) {
23
+ if (!metadata.metatype) return value;
24
+ const obj = (0, _classtransformer.plainToClass)(metadata.metatype, value);
25
+ if (typeof obj !== 'object') return value;
26
+ const errors = await (0, _classvalidator.validate)(obj);
27
+ if (errors.length) {
28
+ const messages = errors.map((err)=>{
29
+ if (!err.constraints) return `${err.property}`;
30
+ return `${err.property} - ${Object.values(err.constraints).join(', ')}`;
31
+ });
32
+ const response = {
33
+ statusCode: _common.HttpStatus.BAD_REQUEST,
34
+ error: 'Bad request',
35
+ messages
36
+ };
37
+ throw new _validationexception.ValidationException(JSON.stringify(response));
38
+ }
39
+ return value;
40
+ }
41
+ }
42
+ ValidationPipe = _ts_decorate([
43
+ (0, _common.Injectable)()
44
+ ], ValidationPipe);